]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
dcd11e599dff9c285fdc68ed3f2c2bd4c8398611
[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_MACRO_CODE] = InsetName("mathmacro");
172
173         passed = true;
174 }
175
176
177 void Inset::setBuffer(Buffer & buffer)
178 {
179         buffer_ = &buffer;
180 }
181
182
183 Buffer & Inset::buffer()
184 {
185         if (!buffer_) {
186                 odocstringstream s;
187                 lyxerr << "Inset: " << this
188                                         << " LyX Code: " << lyxCode()
189                                         << " name: " << insetName(lyxCode())
190                                         << std::endl;
191                 s << "LyX Code: " << lyxCode() << " name: " << name();
192                 LASSERT(false, /**/);
193                 throw ExceptionMessage(BufferException, 
194                         from_ascii("Inset::buffer_ member not initialized!"), s.str());
195         }
196         return *buffer_;
197 }
198
199
200 Buffer const & Inset::buffer() const
201 {
202         return const_cast<Inset *>(this)->buffer();
203 }
204
205
206 bool Inset::isBufferLoaded() const
207 {
208         return buffer_ && theBufferList().isLoaded(buffer_);
209 }
210
211
212 bool Inset::isBufferValid() const
213 {
214         return buffer_ 
215                 && (isBufferLoaded() || buffer_->isClone());
216 }
217
218
219 docstring Inset::name() const
220 {
221         return from_ascii("unknown");
222 }
223
224
225 bool Inset::isFreeSpacing() const 
226 {
227         return getLayout().isFreeSpacing();
228 }
229
230
231 bool Inset::allowEmpty() const
232 {
233         return getLayout().isKeepEmpty();
234 }
235
236 bool Inset::forceLTR() const
237 {
238         return getLayout().forceLTR();
239 }
240
241 void Inset::initView()
242 {
243         if (isLabeled())
244                 buffer().updateBuffer();
245 }
246
247
248 docstring Inset::toolTip(BufferView const &, int, int) const
249 {
250         return docstring();
251 }
252
253
254 docstring Inset::contextMenu(BufferView const &, int, int) const
255 {
256         return docstring();
257 }
258
259
260 Dimension const Inset::dimension(BufferView const & bv) const
261 {
262         return bv.coordCache().getInsets().dim(this);
263 }
264
265
266 InsetCode insetCode(string const & name)
267 {
268         build_translator();
269         for (int i = 1; i != int(INSET_CODE_SIZE); ++i) {
270                 if (insetnames[i].name == name)
271                         return InsetCode(i);
272         }
273         return NO_CODE;
274 }
275
276
277 string insetName(InsetCode c) 
278 {
279         build_translator();
280         return insetnames[c].name;
281 }
282
283
284 docstring insetDisplayName(InsetCode c) 
285 {
286         build_translator();
287         return insetnames[c].display_name;
288 }
289
290
291 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
292 {
293         if (buffer_ == 0) {
294                 lyxerr << "Unassigned buffer_ member in Inset::dispatch()" << std::endl;
295                 lyxerr << "LyX Code: " << lyxCode() << " name: " 
296                        << insetName(lyxCode()) << std::endl;
297         } else if (cur.buffer() != buffer_)
298                 lyxerr << "cur.buffer() != buffer_ in Inset::dispatch()" << std::endl;
299         cur.updateFlags(Update::Force | Update::FitCursor);
300         cur.dispatched();
301         doDispatch(cur, cmd);
302 }
303
304
305 bool Inset::showInsetDialog(BufferView * bv) const
306 {
307         InsetCode const code = lyxCode();
308         switch (code){
309         case ERT_CODE:
310         case FLOAT_CODE:
311         case BOX_CODE:
312         case BIBITEM_CODE:
313         case BRANCH_CODE:
314         case INFO_CODE:
315         case MATH_SPACE_CODE:
316         case SPACE_CODE:
317         case TABULAR_CODE:
318         case VSPACE_CODE:
319                 bv->showDialog(insetName(code));
320                 break;
321         default:
322                 return false;
323         }
324         return true;
325 }
326
327
328 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
329 {
330         switch (cmd.action()) {
331         case LFUN_MOUSE_RELEASE:
332                 // if the derived inset did not explicitly handle mouse_release,
333                 // we assume we request the settings dialog
334                 if (!cur.selection() && cmd.button() == mouse_button::button1
335                           && hasSettings()) {
336                         FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
337                         dispatch(cur, tmpcmd);
338                 }
339                 break;
340
341         case LFUN_INSET_SETTINGS:
342                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
343                         showInsetDialog(&cur.bv());
344                         cur.dispatched();
345                 } else
346                         cur.undispatched();
347                 break;
348
349         default:
350                 cur.noUpdate();
351                 cur.undispatched();
352                 break;
353         }
354 }
355
356
357 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
358         FuncStatus & flag) const
359 {
360         // LFUN_INSET_APPLY is sent from the dialogs when the data should
361         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
362         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
363         // not belong to us, i. e. the dialog was open, and the user moved
364         // the cursor in our inset) in lyx::getStatus().
365         // Dialogs::checkStatus() ensures that the dialog is deactivated if
366         // LFUN_INSET_APPLY is disabled.
367
368         switch (cmd.action()) {
369         case LFUN_INSET_MODIFY:
370                 // Allow modification of our data.
371                 // This needs to be handled in the doDispatch method of our
372                 // instantiatable children.
373                 flag.setEnabled(true);
374                 return true;
375
376         case LFUN_INSET_INSERT:
377                 // Don't allow insertion of new insets.
378                 // Every inset that wants to allow new insets from open
379                 // dialogs needs to override this.
380                 flag.setEnabled(false);
381                 return true;
382
383         case LFUN_INSET_SETTINGS:
384                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
385                         bool const enable = hasSettings();
386                         flag.setEnabled(enable);
387                         return true;
388                 } else {
389                         return false;
390                 }
391         
392         case LFUN_IN_MATHMACROTEMPLATE:
393                 // By default we're not in a MathMacroTemplate inset
394                 flag.setEnabled(false);
395                 return true;
396
397         default:
398                 break;
399         }
400         return false;
401 }
402
403
404 void Inset::edit(Cursor &, bool, EntryDirection)
405 {
406         LYXERR(Debug::INSETS, "edit left/right");
407 }
408
409
410 Inset * Inset::editXY(Cursor &, int x, int y)
411 {
412         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
413         return this;
414 }
415
416
417 Inset::idx_type Inset::index(row_type row, col_type col) const
418 {
419         if (row != 0)
420                 LYXERR0("illegal row: " << row);
421         if (col != 0)
422                 LYXERR0("illegal col: " << col);
423         return 0;
424 }
425
426
427 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
428 {
429         return from <= idx && idx <= to;
430 }
431
432
433 bool Inset::idxUpDown(Cursor &, bool) const
434 {
435         return false;
436 }
437
438
439 int Inset::docbook(odocstream &, OutputParams const &) const
440 {
441         return 0;
442 }
443
444
445 docstring Inset::xhtml(XHTMLStream & xs, OutputParams const &) const
446 {
447         xs << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
448         return docstring();
449 }
450
451
452 bool Inset::directWrite() const
453 {
454         return false;
455 }
456
457
458 bool Inset::editable() const
459 {
460         return false;
461 }
462
463
464 bool Inset::hasSettings() const
465 {
466         return false;
467 }
468
469
470
471 bool Inset::autoDelete() const
472 {
473         return false;
474 }
475
476
477 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
478                 bool, int & x, int & y) const
479 {
480         LYXERR0("Inset::cursorPos called directly");
481         x = 100;
482         y = 100;
483 }
484
485
486 void Inset::metricsMarkers(Dimension & dim, int framesize) const
487 {
488         dim.wid += 2 * framesize;
489         dim.des += framesize;
490 }
491
492
493 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
494 {
495         dim.wid += 2 * framesize;
496         dim.asc += framesize;
497         dim.des += framesize;
498 }
499
500
501 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
502 {
503         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
504                 Color_mathframe : Color_mathcorners;
505
506         Dimension const dim = dimension(*pi.base.bv);
507
508         int const t = x + dim.width() - 1;
509         int const d = y + dim.descent();
510         pi.pain.line(x, d - 3, x, d, pen_color);
511         pi.pain.line(t, d - 3, t, d, pen_color);
512         pi.pain.line(x, d, x + 3, d, pen_color);
513         pi.pain.line(t - 3, d, t, d, pen_color);
514         setPosCache(pi, x, y);
515 }
516
517
518 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
519 {
520         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
521                 Color_mathframe : Color_mathcorners;
522
523         drawMarkers(pi, x, y);
524         Dimension const dim = dimension(*pi.base.bv);
525         int const t = x + dim.width() - 1;
526         int const a = y - dim.ascent();
527         pi.pain.line(x, a + 3, x, a, pen_color);
528         pi.pain.line(t, a + 3, t, a, pen_color);
529         pi.pain.line(x, a, x + 3, a, pen_color);
530         pi.pain.line(t - 3, a, t, a, pen_color);
531         setPosCache(pi, x, y);
532 }
533
534
535 bool Inset::editing(BufferView const * bv) const
536 {
537         return bv->cursor().isInside(this);
538 }
539
540
541 int Inset::xo(BufferView const & bv) const
542 {
543         return bv.coordCache().getInsets().x(this);
544 }
545
546
547 int Inset::yo(BufferView const & bv) const
548 {
549         return bv.coordCache().getInsets().y(this);
550 }
551
552
553 bool Inset::covers(BufferView const & bv, int x, int y) const
554 {
555         return bv.coordCache().getInsets().covers(this, x, y);
556 }
557
558
559 InsetLayout const & Inset::getLayout() const
560 {
561         if (!buffer_)
562                 return DocumentClass::plainInsetLayout();
563         return buffer().params().documentClass().insetLayout(name());
564 }
565
566
567 bool Inset::undefined() const
568 {
569         docstring const & n = getLayout().name();
570         return n.empty() || n == DocumentClass::plainInsetLayout().name();
571 }
572
573
574 void Inset::dump() const
575 {
576         write(lyxerr);
577 }
578
579
580 ColorCode Inset::backgroundColor(PainterInfo const & /*pi*/) const
581 {
582         return Color_none;
583 }
584
585
586 ColorCode Inset::labelColor() const
587 {
588         return Color_foreground;
589 }
590
591
592 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
593 {
594         //LYXERR("Inset: set position cache to " << x << " " << y);
595         pi.base.bv->coordCache().insets().add(this, x, y);
596 }
597
598
599 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
600 {
601         mi.base.bv->coordCache().insets().add(this, dim);
602 }
603
604
605 Buffer const * Inset::updateFrontend() const
606 {
607         return theApp() ? theApp()->updateInset(this) : 0;
608 }
609
610
611 docstring Inset::completionPrefix(Cursor const &) const 
612 {
613         return docstring();
614 }
615
616 } // namespace lyx