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