3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Lars Gullik Bjønnes
9 * \author Matthias Ettrich
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
19 #include "buffer_funcs.h"
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "CoordCache.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"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "frontends/Application.h"
37 #include "frontends/Painter.h"
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"
48 using namespace lyx::support;
54 InsetName(string const & n = string(), docstring const & dn = docstring())
55 : name(n), display_name(dn) {}
57 docstring display_name;
61 static InsetName insetnames[INSET_CODE_SIZE];
64 // This list should be kept in sync with the list of dialogs in
65 // src/frontends/qt/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.
69 static void build_translator()
71 static bool passed = false;
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");
184 void Inset::setBuffer(Buffer & buffer)
190 void Inset::resetBuffer()
196 Buffer & Inset::buffer()
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);
205 throw ExceptionMessage(BufferException,
206 from_ascii("Inset::buffer_ member not initialized!"), s.str());
212 Buffer const & Inset::buffer() const
214 return const_cast<Inset *>(this)->buffer();
218 bool Inset::isBufferLoaded() const
220 return buffer_ && theBufferList().isLoaded(buffer_);
224 bool Inset::isBufferValid() const
227 && (isBufferLoaded() || buffer_->isClone());
231 docstring Inset::layoutName() const
233 return from_ascii("unknown");
237 bool Inset::isFreeSpacing() const
239 return getLayout().isFreeSpacing();
243 bool Inset::allowEmpty() const
245 return getLayout().isKeepEmpty();
249 bool Inset::forceLTR(OutputParams const &) const
251 return getLayout().forceLTR();
255 bool Inset::isInToc() const
257 return getLayout().isInToc();
261 docstring Inset::toolTip(BufferView const &, int, int) const
267 void Inset::forOutliner(docstring &, size_t const, bool const) const
272 string Inset::contextMenu(BufferView const &, int, int) const
274 return contextMenuName();
278 string Inset::contextMenuName() const
284 Dimension const Inset::dimension(BufferView const & bv) const
286 return bv.coordCache().getInsets().dim(this);
290 InsetCode insetCode(string const & name)
293 for (int i = 1; i != int(INSET_CODE_SIZE); ++i) {
294 if (insetnames[i].name == name)
301 string insetName(InsetCode c)
304 return insetnames[c].name;
308 docstring insetDisplayName(InsetCode c)
311 return insetnames[c].display_name;
315 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
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);
325 doDispatch(cur, cmd);
329 bool Inset::showInsetDialog(BufferView * bv) const
331 InsetCode const code = lyxCode();
339 case MATH_SPACE_CODE:
343 bv->showDialog(insetName(code));
352 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
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);
365 case LFUN_INSET_SETTINGS:
366 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
367 showInsetDialog(&cur.bv());
374 cur.noScreenUpdate();
381 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
382 FuncStatus & flag) const
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.
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);
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);
407 case LFUN_INSET_SETTINGS:
408 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
409 bool const enable = hasSettings();
410 flag.setEnabled(enable);
416 case LFUN_IN_MATHMACROTEMPLATE:
417 // By default we're not in a InsetMathMacroTemplate inset
418 flag.setEnabled(false);
422 // By default we're not in an IPA inset
423 flag.setEnabled(false);
433 void Inset::edit(Cursor &, bool, EntryDirection)
435 LYXERR(Debug::INSETS, "edit left/right");
439 Inset * Inset::editXY(Cursor &, int x, int y)
441 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
446 Inset::idx_type Inset::index(row_type row, col_type col) const
449 LYXERR0("illegal row: " << row);
451 LYXERR0("illegal col: " << col);
456 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
458 return from <= idx && idx <= to;
462 bool Inset::idxUpDown(Cursor &, bool) const
468 int Inset::docbook(odocstream &, OutputParams const &) const
474 docstring Inset::xhtml(XHTMLStream & xs, OutputParams const &) const
476 xs << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
481 bool Inset::directWrite() const
487 bool Inset::editable() const
493 bool Inset::hasSettings() const
500 bool Inset::autoDelete() const
506 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
507 bool, int & x, int & y) const
509 LYXERR0("Inset::cursorPos called directly");
515 void Inset::metricsMarkers(Dimension & dim, int framesize) const
517 dim.wid += 2 * framesize;
518 dim.des += framesize;
522 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
524 dim.wid += 2 * framesize;
525 dim.asc += framesize;
526 dim.des += framesize;
530 void Inset::drawBackground(PainterInfo & pi, int x, int y) const
532 if (pi.full_repaint && backgroundColor(pi) == Color_none)
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));
540 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
542 ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
543 Color_mathframe : Color_mathcorners;
545 Dimension const dim = dimension(*pi.base.bv);
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);
556 bool Inset::editing(BufferView const * bv) const
558 return bv->cursor().isInside(this);
562 int Inset::xo(BufferView const & bv) const
564 return bv.coordCache().getInsets().x(this);
568 int Inset::yo(BufferView const & bv) const
570 return bv.coordCache().getInsets().y(this);
574 bool Inset::covers(BufferView const & bv, int x, int y) const
576 return bv.coordCache().getInsets().covers(this, x, y);
580 InsetLayout const & Inset::getLayout() const
583 return DocumentClass::plainInsetLayout();
584 return buffer().params().documentClass().insetLayout(layoutName());
588 bool Inset::undefined() const
590 docstring const & n = getLayout().name();
591 return n.empty() || n == DocumentClass::plainInsetLayout().name();
595 void Inset::dump() const
601 ColorCode Inset::backgroundColor(PainterInfo const & /*pi*/) const
607 ColorCode Inset::labelColor() const
609 return Color_foreground;
613 Buffer const * Inset::updateFrontend() const
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
621 return theApp() ? theApp()->updateInset(this) : 0;
625 bool Inset::resetFontEdit() const
627 return getLayout().resetsFont() || !inheritFont();
631 docstring Inset::completionPrefix(Cursor const &) const