]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
Kornel's gcc compile fix.
[lyx.git] / src / insets / Inset.cpp
1 /**
2  * \file Inset.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  * \author Matthias Ettrich
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Inset.h"
18
19 #include "buffer_funcs.h"
20 #include "Buffer.h"
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "CoordCache.h"
25 #include "Cursor.h"
26 #include "Dimension.h"
27 #include "DispatchResult.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "MetricsInfo.h"
31 #include "Text.h"
32 #include "TextClass.h"
33
34 #include "frontends/Application.h"
35 #include "frontends/Painter.h"
36
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/ExceptionMessage.h"
40 #include "support/gettext.h"
41 #include "support/lassert.h"
42
43 #include <map>
44
45 using namespace std;
46 using namespace lyx::support;
47
48 namespace lyx {
49
50 class InsetName {
51 public:
52         InsetName(string const & n, InsetCode c) : name(n), code(c) {}
53         string name;
54         InsetCode code;
55 };
56
57
58 typedef map<string, InsetCode> TranslatorMap;
59
60
61 static TranslatorMap const build_translator()
62 {
63         InsetName const insetnames[] = {
64                 InsetName("toc", TOC_CODE),
65                 InsetName("quote", QUOTE_CODE),
66                 InsetName("ref", REF_CODE),
67                 InsetName("href", HYPERLINK_CODE),
68                 InsetName("separator", SEPARATOR_CODE),
69                 InsetName("ending", ENDING_CODE),
70                 InsetName("label", LABEL_CODE),
71                 InsetName("note", NOTE_CODE),
72                 InsetName("phantom", PHANTOM_CODE),
73                 InsetName("accent", ACCENT_CODE),
74                 InsetName("math", MATH_CODE),
75                 InsetName("index", INDEX_CODE),
76                 InsetName("nomenclature", NOMENCL_CODE),
77                 InsetName("include", INCLUDE_CODE),
78                 InsetName("graphics", GRAPHICS_CODE),
79                 InsetName("bibitem", BIBITEM_CODE),
80                 InsetName("bibtex", BIBTEX_CODE),
81                 InsetName("text", TEXT_CODE),
82                 InsetName("ert", ERT_CODE),
83                 InsetName("foot", FOOT_CODE),
84                 InsetName("margin", MARGIN_CODE),
85                 InsetName("float", FLOAT_CODE),
86                 InsetName("wrap", WRAP_CODE),
87                 InsetName("specialchar", SPECIALCHAR_CODE),
88                 InsetName("tabular", TABULAR_CODE),
89                 InsetName("external", EXTERNAL_CODE),
90                 InsetName("caption", CAPTION_CODE),
91                 InsetName("mathmacro", MATHMACRO_CODE),
92                 InsetName("citation", CITE_CODE),
93                 InsetName("floatlist", FLOAT_LIST_CODE),
94                 InsetName("index_print", INDEX_PRINT_CODE),
95                 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
96                 InsetName("optarg", OPTARG_CODE),
97                 InsetName("newline", NEWLINE_CODE),
98                 InsetName("line", LINE_CODE),
99                 InsetName("branch", BRANCH_CODE),
100                 InsetName("box", BOX_CODE),
101                 InsetName("flex", FLEX_CODE),
102                 InsetName("space", SPACE_CODE),
103                 InsetName("vspace", VSPACE_CODE),
104                 InsetName("mathmacroarg", MATH_MACROARG_CODE),
105                 InsetName("listings", LISTINGS_CODE),
106                 InsetName("info", INFO_CODE),
107                 InsetName("collapsable", COLLAPSABLE_CODE),
108                 InsetName("newpage", NEWPAGE_CODE),
109                 InsetName("tablecell", CELL_CODE),
110                 InsetName("mathamsarray", MATH_AMSARRAY_CODE),
111                 InsetName("matharray", MATH_ARRAY_CODE),
112                 InsetName("mathbig", MATH_BIG_CODE),
113                 InsetName("mathboldsymbol", MATH_BOLDSYMBOL_CODE),
114                 InsetName("mathbox", MATH_BOX_CODE),
115                 InsetName("mathbrace", MATH_BRACE_CODE),
116                 InsetName("mathcases", MATH_CASES_CODE),
117                 InsetName("mathchar", MATH_CHAR_CODE),
118                 InsetName("mathcolor", MATH_COLOR_CODE),
119                 InsetName("mathcomment", MATH_COMMENT_CODE),
120                 InsetName("mathdecoration", MATH_DECORATION_CODE),
121                 InsetName("mathdelim", MATH_DELIM_CODE),
122                 InsetName("mathdiff", MATH_DIFF_CODE),
123                 InsetName("mathdots", MATH_DOTS_CODE),
124                 InsetName("mathensuremath", MATH_ENSUREMATH_CODE),
125                 InsetName("mathenv", MATH_ENV_CODE),
126                 InsetName("mathexfunc", MATH_EXFUNC_CODE),
127                 InsetName("mathexint", MATH_EXINT_CODE),
128                 InsetName("mathfont", MATH_FONT_CODE),
129                 InsetName("mathfontold", MATH_FONTOLD_CODE),
130                 InsetName("mathfrac", MATH_FRAC_CODE),
131                 InsetName("mathgrid", MATH_GRID_CODE),
132                 InsetName("math", MATH_CODE),
133                 InsetName("mathhull", MATH_HULL_CODE),
134                 InsetName("mathkern", MATH_KERN_CODE),
135                 InsetName("mathlefteqn", MATH_LEFTEQN_CODE),
136                 InsetName("mathlim", MATH_LIM_CODE),
137                 InsetName("mathmatrix", MATH_MATRIX_CODE),
138                 InsetName("mathmbox", MATH_MBOX_CODE),
139                 InsetName("mathnest", MATH_NEST_CODE),
140                 InsetName("mathnumber", MATH_NUMBER_CODE),
141                 InsetName("mathoverset", MATH_OVERSET_CODE),
142                 InsetName("mathpar", MATH_PAR_CODE),
143                 InsetName("mathphantom", MATH_PHANTOM_CODE),
144                 InsetName("mathref", MATH_REF_CODE),
145                 InsetName("mathroot", MATH_ROOT_CODE),
146                 InsetName("mathscript", MATH_SCRIPT_CODE),
147                 InsetName("mathsize", MATH_SIZE_CODE),
148                 InsetName("mathspace", MATH_SPACE_CODE),
149                 InsetName("mathspecialchar", MATH_SPECIALCHAR_CODE),
150                 InsetName("mathsplit", MATH_SPLIT_CODE),
151                 InsetName("mathsqrt", MATH_SQRT_CODE),
152                 InsetName("mathstackrel", MATH_STACKREL_CODE),
153                 InsetName("mathstring", MATH_STRING_CODE),
154                 InsetName("mathsubstack", MATH_SUBSTACK_CODE),
155                 InsetName("mathsymbol", MATH_SYMBOL_CODE),
156                 InsetName("mathtabular", MATH_TABULAR_CODE),
157                 InsetName("mathunderset", MATH_UNDERSET_CODE),
158                 InsetName("mathunknown", MATH_UNKNOWN_CODE),
159                 InsetName("mathxarrow", MATH_XARROW_CODE),
160                 InsetName("mathxyarrow", MATH_XYARROW_CODE),
161                 InsetName("mathxymatrix", MATH_XYMATRIX_CODE),
162                 InsetName("mathmacro", MATH_MACRO_CODE),
163         };
164
165         size_t const insetnames_size =
166                 sizeof(insetnames) / sizeof(insetnames[0]);
167
168         map<string, InsetCode> data;
169         for (size_t i = 0; i != insetnames_size; ++i) {
170                 InsetName const & var = insetnames[i];
171                 data[var.name] = var.code;
172         }
173
174         return data;
175 }
176
177
178 void Inset::setBuffer(Buffer & buffer)
179 {
180         buffer_ = &buffer;
181 }
182
183
184 Buffer & Inset::buffer()
185 {
186         if (!buffer_) {
187                 odocstringstream s;
188                 lyxerr << "Inset: " << this
189                                         << " LyX Code: " << lyxCode()
190                                         << " name: " << insetName(lyxCode())
191                                         << std::endl;
192                 s << "LyX Code: " << lyxCode() << " name: " << name();
193                 LASSERT(false, /**/);
194                 throw ExceptionMessage(BufferException, 
195                         from_ascii("Inset::buffer_ member not initialized!"), s.str());
196         }
197         return *buffer_;
198 }
199
200
201 Buffer const & Inset::buffer() const
202 {
203         return const_cast<Inset *>(this)->buffer();
204 }
205
206
207 bool Inset::isBufferValid() const
208 {
209         return buffer_ && theBufferList().isLoaded(buffer_);
210 }
211
212
213 docstring Inset::name() const
214 {
215         return from_ascii("unknown");
216 }
217
218
219 bool Inset::isFreeSpacing() const 
220 {
221         return getLayout().isFreeSpacing();
222 }
223
224
225 bool Inset::allowEmpty() const
226 {
227         return getLayout().isKeepEmpty();
228 }
229
230 bool Inset::forceLTR() const
231 {
232         return getLayout().forceLTR();
233 }
234
235 void Inset::initView()
236 {
237         if (isLabeled())
238                 buffer().updateLabels();
239 }
240
241
242 docstring Inset::toolTip(BufferView const &, int, int) const
243 {
244         return docstring();
245 }
246
247
248 docstring Inset::contextMenu(BufferView const &, int, int) const
249 {
250         return docstring();
251 }
252
253
254 Dimension const Inset::dimension(BufferView const & bv) const
255 {
256         return bv.coordCache().getInsets().dim(this);
257 }
258
259
260 InsetCode insetCode(string const & name)
261 {
262         static TranslatorMap const translator = build_translator();
263
264         TranslatorMap::const_iterator it = translator.find(name);
265         return it == translator.end() ? NO_CODE : it->second;
266 }
267
268
269 string insetName(InsetCode c) 
270 {
271         static TranslatorMap const translator = build_translator();
272
273         TranslatorMap::const_iterator it =  translator.begin();
274         TranslatorMap::const_iterator end = translator.end();
275         for (; it != end; ++it) {
276                 if (it->second == c)
277                         return it->first;
278         }
279         return string();
280 }
281
282
283 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
284 {
285         if (buffer_ == 0) {
286                 lyxerr << "Unassigned buffer_ member in Inset::dispatch()" << std::endl;
287                 lyxerr << "LyX Code: " << lyxCode() << " name: " << insetName(lyxCode()) << std::endl;
288         } else if (cur.buffer() != buffer_)
289                 lyxerr << "cur.buffer() != buffer_ in Inset::dispatch()" << std::endl;
290         cur.updateFlags(Update::Force | Update::FitCursor);
291         cur.dispatched();
292         doDispatch(cur, cmd);
293 }
294
295
296 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
297 {
298         switch (cmd.action) {
299         case LFUN_MOUSE_RELEASE:
300                 // if the derived inset did not explicitly handle mouse_release,
301                 // we assume we request the settings dialog
302                 if (!cur.selection() && cmd.button() == mouse_button::button1
303                           && hasSettings()) {
304                         FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
305                         dispatch(cur, tmpcmd);
306                 }
307                 break;
308
309         case LFUN_INSET_SETTINGS:
310                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
311                         showInsetDialog(&cur.bv());
312                         cur.dispatched();
313                 } else
314                         cur.undispatched();
315                 break;
316
317         default:
318                 cur.noUpdate();
319                 cur.undispatched();
320                 break;
321         }
322 }
323
324
325 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
326         FuncStatus & flag) const
327 {
328         // LFUN_INSET_APPLY is sent from the dialogs when the data should
329         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
330         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
331         // not belong to us, i. e. the dialog was open, and the user moved
332         // the cursor in our inset) in LyXFunc::getStatus().
333         // Dialogs::checkStatus() ensures that the dialog is deactivated if
334         // LFUN_INSET_APPLY is disabled.
335
336         switch (cmd.action) {
337         case LFUN_INSET_MODIFY:
338                 // Allow modification of our data.
339                 // This needs to be handled in the doDispatch method of our
340                 // instantiatable children.
341                 flag.setEnabled(true);
342                 return true;
343
344         case LFUN_INSET_INSERT:
345                 // Don't allow insertion of new insets.
346                 // Every inset that wants to allow new insets from open
347                 // dialogs needs to override this.
348                 flag.setEnabled(false);
349                 return true;
350
351         case LFUN_INSET_SETTINGS:
352                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
353                         bool const enable = hasSettings();
354                         flag.setEnabled(enable);
355                         return true;
356                 } else {
357                         flag.setEnabled(false);
358                         return false;
359                 }
360
361         default:
362                 break;
363         }
364         return false;
365 }
366
367
368 void Inset::edit(Cursor &, bool, EntryDirection)
369 {
370         LYXERR(Debug::INSETS, "edit left/right");
371 }
372
373
374 Inset * Inset::editXY(Cursor &, int x, int y)
375 {
376         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
377         return this;
378 }
379
380
381 Inset::idx_type Inset::index(row_type row, col_type col) const
382 {
383         if (row != 0)
384                 LYXERR0("illegal row: " << row);
385         if (col != 0)
386                 LYXERR0("illegal col: " << col);
387         return 0;
388 }
389
390
391 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
392 {
393         return from <= idx && idx <= to;
394 }
395
396
397 bool Inset::idxUpDown(Cursor &, bool) const
398 {
399         return false;
400 }
401
402
403 int Inset::docbook(odocstream &, OutputParams const &) const
404 {
405         return 0;
406 }
407
408
409 docstring Inset::xhtml(odocstream & od, OutputParams const &) const
410 {
411         od << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
412         return docstring();
413 }
414
415 bool Inset::directWrite() const
416 {
417         return false;
418 }
419
420
421 bool Inset::editable() const
422 {
423         return false;
424 }
425
426
427 bool Inset::hasSettings() const
428 {
429         return false;
430 }
431
432
433
434 bool Inset::autoDelete() const
435 {
436         return false;
437 }
438
439
440 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
441                 bool, int & x, int & y) const
442 {
443         LYXERR0("Inset::cursorPos called directly");
444         x = 100;
445         y = 100;
446 }
447
448
449 void Inset::metricsMarkers(Dimension & dim, int framesize) const
450 {
451         dim.wid += 2 * framesize;
452         dim.des += framesize;
453 }
454
455
456 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
457 {
458         dim.wid += 2 * framesize;
459         dim.asc += framesize;
460         dim.des += framesize;
461 }
462
463
464 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
465 {
466         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
467                 Color_mathframe : Color_mathcorners;
468
469         Dimension const dim = dimension(*pi.base.bv);
470
471         int const t = x + dim.width() - 1;
472         int const d = y + dim.descent();
473         pi.pain.line(x, d - 3, x, d, pen_color);
474         pi.pain.line(t, d - 3, t, d, pen_color);
475         pi.pain.line(x, d, x + 3, d, pen_color);
476         pi.pain.line(t - 3, d, t, d, pen_color);
477         setPosCache(pi, x, y);
478 }
479
480
481 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
482 {
483         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
484                 Color_mathframe : Color_mathcorners;
485
486         drawMarkers(pi, x, y);
487         Dimension const dim = dimension(*pi.base.bv);
488         int const t = x + dim.width() - 1;
489         int const a = y - dim.ascent();
490         pi.pain.line(x, a + 3, x, a, pen_color);
491         pi.pain.line(t, a + 3, t, a, pen_color);
492         pi.pain.line(x, a, x + 3, a, pen_color);
493         pi.pain.line(t - 3, a, t, a, pen_color);
494         setPosCache(pi, x, y);
495 }
496
497
498 bool Inset::editing(BufferView const * bv) const
499 {
500         return bv->cursor().isInside(this);
501 }
502
503
504 int Inset::xo(BufferView const & bv) const
505 {
506         return bv.coordCache().getInsets().x(this);
507 }
508
509
510 int Inset::yo(BufferView const & bv) const
511 {
512         return bv.coordCache().getInsets().y(this);
513 }
514
515
516 bool Inset::covers(BufferView const & bv, int x, int y) const
517 {
518         return bv.coordCache().getInsets().covers(this, x, y);
519 }
520
521
522 InsetLayout const & Inset::getLayout() const
523 {
524         return buffer().params().documentClass().insetLayout(name());
525 }
526
527
528 bool Inset::undefined() const
529 {
530         docstring const & n = getLayout().name();
531         return n.empty() || n == DocumentClass::plainInsetLayout().name();
532 }
533
534
535 void Inset::dump() const
536 {
537         write(lyxerr);
538 }
539
540
541 ColorCode Inset::backgroundColor() const
542 {
543         return Color_none;
544 }
545
546
547 ColorCode Inset::labelColor() const
548 {
549         return Color_foreground;
550 }
551
552
553 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
554 {
555         //LYXERR("Inset: set position cache to " << x << " " << y);
556         pi.base.bv->coordCache().insets().add(this, x, y);
557 }
558
559
560 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
561 {
562         mi.base.bv->coordCache().insets().add(this, dim);
563 }
564
565
566 Buffer const * Inset::updateFrontend() const
567 {
568         return theApp() ? theApp()->updateInset(this) : 0;
569 }
570
571
572 docstring Inset::completionPrefix(Cursor const &) const 
573 {
574         return docstring();
575 }
576
577 } // namespace lyx