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