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