]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
Squash some warnings.
[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_XYMATRIX_CODE] = InsetName("mathxymatrix");
176         insetnames[MATH_DIAGRAM_CODE] = InsetName("mathdiagram");
177         insetnames[MATH_MACRO_CODE] = InsetName("mathmacro");
178
179         passed = true;
180 }
181
182
183 void Inset::setBuffer(Buffer & buffer)
184 {
185         buffer_ = &buffer;
186 }
187
188
189 Buffer & Inset::buffer()
190 {
191         if (!buffer_) {
192                 odocstringstream s;
193                 string const iname = insetName(lyxCode());
194                 LYXERR0("Inset: " << this << " LyX Code: " << lyxCode()
195                                         << " name: " << iname);
196                 s << "LyX Code: " << lyxCode() << " name: " << from_ascii(iname);
197                 LATTEST(false);
198                 throw ExceptionMessage(BufferException, 
199                         from_ascii("Inset::buffer_ member not initialized!"), s.str());
200         }
201         return *buffer_;
202 }
203
204
205 Buffer const & Inset::buffer() const
206 {
207         return const_cast<Inset *>(this)->buffer();
208 }
209
210
211 bool Inset::isBufferLoaded() const
212 {
213         return buffer_ && theBufferList().isLoaded(buffer_);
214 }
215
216
217 bool Inset::isBufferValid() const
218 {
219         return buffer_ 
220                 && (isBufferLoaded() || buffer_->isClone());
221 }
222
223
224 docstring Inset::layoutName() const
225 {
226         return from_ascii("unknown");
227 }
228
229
230 bool Inset::isFreeSpacing() const 
231 {
232         return getLayout().isFreeSpacing();
233 }
234
235
236 bool Inset::allowEmpty() const
237 {
238         return getLayout().isKeepEmpty();
239 }
240
241
242 bool Inset::forceLTR() const
243 {
244         return getLayout().forceLTR();
245 }
246
247
248 bool Inset::isInToc() const
249 {
250         return getLayout().isInToc();
251 }
252
253
254 docstring Inset::toolTip(BufferView const &, int, int) const
255 {
256         return docstring();
257 }
258
259
260 void Inset::forOutliner(docstring &, size_t const, bool const) const
261 {
262 }
263
264
265 string Inset::contextMenu(BufferView const &, int, int) const
266 {
267         return contextMenuName();
268 }
269
270
271 string Inset::contextMenuName() const
272 {
273         return string();
274 }
275
276
277 Dimension const Inset::dimension(BufferView const & bv) const
278 {
279         return bv.coordCache().getInsets().dim(this);
280 }
281
282
283 InsetCode insetCode(string const & name)
284 {
285         build_translator();
286         for (int i = 1; i != int(INSET_CODE_SIZE); ++i) {
287                 if (insetnames[i].name == name)
288                         return InsetCode(i);
289         }
290         return NO_CODE;
291 }
292
293
294 string insetName(InsetCode c) 
295 {
296         build_translator();
297         return insetnames[c].name;
298 }
299
300
301 docstring insetDisplayName(InsetCode c) 
302 {
303         build_translator();
304         return insetnames[c].display_name;
305 }
306
307
308 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
309 {
310         if (buffer_ == 0) {
311                 lyxerr << "Unassigned buffer_ member in Inset::dispatch()" << std::endl;
312                 lyxerr << "LyX Code: " << lyxCode() << " name: " 
313                        << insetName(lyxCode()) << std::endl;
314         } else if (cur.buffer() != buffer_)
315                 lyxerr << "cur.buffer() != buffer_ in Inset::dispatch()" << std::endl;
316         cur.screenUpdateFlags(Update::Force | Update::FitCursor);
317         cur.dispatched();
318         doDispatch(cur, cmd);
319 }
320
321
322 bool Inset::showInsetDialog(BufferView * bv) const
323 {
324         InsetCode const code = lyxCode();
325         switch (code){
326         case ERT_CODE:
327         case FLOAT_CODE:
328         case BOX_CODE:
329         case BIBITEM_CODE:
330         case BRANCH_CODE:
331         case INFO_CODE:
332         case MATH_SPACE_CODE:
333         case SPACE_CODE:
334         case TABULAR_CODE:
335         case VSPACE_CODE:
336                 bv->showDialog(insetName(code));
337                 break;
338         default:
339                 return false;
340         }
341         return true;
342 }
343
344
345 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
346 {
347         switch (cmd.action()) {
348         case LFUN_MOUSE_RELEASE:
349                 // if the derived inset did not explicitly handle mouse_release,
350                 // we assume we request the settings dialog
351                 if (!cur.selection() && cmd.button() == mouse_button::button1
352                     && clickable(cur.bv(), cmd.x(), cmd.y()) && hasSettings()) {
353                         FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
354                         dispatch(cur, tmpcmd);
355                 }
356                 break;
357
358         case LFUN_INSET_SETTINGS:
359                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
360                         showInsetDialog(&cur.bv());
361                         cur.dispatched();
362                 } else
363                         cur.undispatched();
364                 break;
365
366         default:
367                 cur.noScreenUpdate();
368                 cur.undispatched();
369                 break;
370         }
371 }
372
373
374 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
375         FuncStatus & flag) const
376 {
377         // LFUN_INSET_APPLY is sent from the dialogs when the data should
378         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
379         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
380         // not belong to us, i. e. the dialog was open, and the user moved
381         // the cursor in our inset) in lyx::getStatus().
382         // Dialogs::checkStatus() ensures that the dialog is deactivated if
383         // LFUN_INSET_APPLY is disabled.
384
385         switch (cmd.action()) {
386         case LFUN_INSET_MODIFY:
387                 // Allow modification of our data.
388                 // This needs to be handled in the doDispatch method of our
389                 // instantiatable children.
390                 flag.setEnabled(true);
391                 return true;
392
393         case LFUN_INSET_INSERT:
394                 // Don't allow insertion of new insets.
395                 // Every inset that wants to allow new insets from open
396                 // dialogs needs to override this.
397                 flag.setEnabled(false);
398                 return true;
399
400         case LFUN_INSET_SETTINGS:
401                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
402                         bool const enable = hasSettings();
403                         flag.setEnabled(enable);
404                         return true;
405                 } else {
406                         return false;
407                 }
408
409         case LFUN_IN_MATHMACROTEMPLATE:
410                 // By default we're not in a MathMacroTemplate inset
411                 flag.setEnabled(false);
412                 return true;
413
414         case LFUN_IN_IPA:
415                 // By default we're not in an IPA inset
416                 flag.setEnabled(false);
417                 return true;
418
419         default:
420                 break;
421         }
422         return false;
423 }
424
425
426 void Inset::edit(Cursor &, bool, EntryDirection)
427 {
428         LYXERR(Debug::INSETS, "edit left/right");
429 }
430
431
432 Inset * Inset::editXY(Cursor &, int x, int y)
433 {
434         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
435         return this;
436 }
437
438
439 Inset::idx_type Inset::index(row_type row, col_type col) const
440 {
441         if (row != 0)
442                 LYXERR0("illegal row: " << row);
443         if (col != 0)
444                 LYXERR0("illegal col: " << col);
445         return 0;
446 }
447
448
449 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
450 {
451         return from <= idx && idx <= to;
452 }
453
454
455 bool Inset::idxUpDown(Cursor &, bool) const
456 {
457         return false;
458 }
459
460
461 int Inset::docbook(odocstream &, OutputParams const &) const
462 {
463         return 0;
464 }
465
466
467 docstring Inset::xhtml(XHTMLStream & xs, OutputParams const &) const
468 {
469         xs << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
470         return docstring();
471 }
472
473
474 bool Inset::directWrite() const
475 {
476         return false;
477 }
478
479
480 bool Inset::editable() const
481 {
482         return false;
483 }
484
485
486 bool Inset::hasSettings() const
487 {
488         return false;
489 }
490
491
492
493 bool Inset::autoDelete() const
494 {
495         return false;
496 }
497
498
499 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
500                 bool, int & x, int & y) const
501 {
502         LYXERR0("Inset::cursorPos called directly");
503         x = 100;
504         y = 100;
505 }
506
507
508 void Inset::metricsMarkers(Dimension & dim, int framesize) const
509 {
510         dim.wid += 2 * framesize;
511         dim.des += framesize;
512 }
513
514
515 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
516 {
517         dim.wid += 2 * framesize;
518         dim.asc += framesize;
519         dim.des += framesize;
520 }
521
522
523 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
524 {
525         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
526                 Color_mathframe : Color_mathcorners;
527
528         Dimension const dim = dimension(*pi.base.bv);
529
530         int const t = x + dim.width() - 1;
531         int const d = y + dim.descent();
532         pi.pain.line(x, d - 3, x, d, pen_color);
533         pi.pain.line(t, d - 3, t, d, pen_color);
534         pi.pain.line(x, d, x + 3, d, pen_color);
535         pi.pain.line(t - 3, d, t, d, pen_color);
536         setPosCache(pi, x, y);
537 }
538
539
540 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
541 {
542         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
543                 Color_mathframe : Color_mathcorners;
544
545         drawMarkers(pi, x, y);
546         Dimension const dim = dimension(*pi.base.bv);
547         int const t = x + dim.width() - 1;
548         int const a = y - dim.ascent();
549         pi.pain.line(x, a + 3, x, a, pen_color);
550         pi.pain.line(t, a + 3, t, a, pen_color);
551         pi.pain.line(x, a, x + 3, a, pen_color);
552         pi.pain.line(t - 3, a, t, a, pen_color);
553         setPosCache(pi, x, y);
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 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
615 {
616         //LYXERR("Inset: set position cache to " << x << " " << y);
617         pi.base.bv->coordCache().insets().add(this, x, y);
618 }
619
620
621 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
622 {
623         mi.base.bv->coordCache().insets().add(this, dim);
624 }
625
626
627 Buffer const * Inset::updateFrontend() const
628 {
629         //FIXME (Abdel 03/12/10): see bugs #6814 and #6949
630         // If the Buffer is null and we end up here this is most probably because we
631         // are in the CutAndPaste stack. See InsetGraphics, RenderGraphics and
632         // RenderPreview.
633         if (!buffer_)
634                 return 0;
635         return theApp() ? theApp()->updateInset(this) : 0;
636 }
637
638
639 bool Inset::resetFontEdit() const
640 {
641         return getLayout().resetsFont() || !inheritFont();
642 }
643
644
645 docstring Inset::completionPrefix(Cursor const &) const 
646 {
647         return docstring();
648 }
649
650 } // namespace lyx