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