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