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