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