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"
35 #include "frontends/Application.h"
36 #include "frontends/Painter.h"
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"
47 using namespace lyx::support;
53 InsetName(string const & n, InsetCode c) : name(n), code(c) {}
59 typedef map<string, InsetCode> TranslatorMap;
62 static TranslatorMap const build_translator()
64 InsetName const insetnames[] = {
65 InsetName("toc", TOC_CODE),
66 InsetName("quote", QUOTE_CODE),
67 InsetName("ref", REF_CODE),
68 InsetName("href", HYPERLINK_CODE),
69 InsetName("separator", SEPARATOR_CODE),
70 InsetName("ending", ENDING_CODE),
71 InsetName("label", LABEL_CODE),
72 InsetName("note", NOTE_CODE),
73 InsetName("phantom", PHANTOM_CODE),
74 InsetName("accent", ACCENT_CODE),
75 InsetName("math", MATH_CODE),
76 InsetName("index", INDEX_CODE),
77 InsetName("nomenclature", NOMENCL_CODE),
78 InsetName("include", INCLUDE_CODE),
79 InsetName("graphics", GRAPHICS_CODE),
80 InsetName("bibitem", BIBITEM_CODE),
81 InsetName("bibtex", BIBTEX_CODE),
82 InsetName("text", TEXT_CODE),
83 InsetName("ert", ERT_CODE),
84 InsetName("foot", FOOT_CODE),
85 InsetName("margin", MARGIN_CODE),
86 InsetName("float", FLOAT_CODE),
87 InsetName("wrap", WRAP_CODE),
88 InsetName("specialchar", SPECIALCHAR_CODE),
89 InsetName("tabular", TABULAR_CODE),
90 InsetName("external", EXTERNAL_CODE),
91 InsetName("caption", CAPTION_CODE),
92 InsetName("mathmacro", MATHMACRO_CODE),
93 InsetName("citation", CITE_CODE),
94 InsetName("floatlist", FLOAT_LIST_CODE),
95 InsetName("index_print", INDEX_PRINT_CODE),
96 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
97 InsetName("optarg", OPTARG_CODE),
98 InsetName("newline", NEWLINE_CODE),
99 InsetName("line", LINE_CODE),
100 InsetName("branch", BRANCH_CODE),
101 InsetName("box", BOX_CODE),
102 InsetName("flex", FLEX_CODE),
103 InsetName("space", SPACE_CODE),
104 InsetName("vspace", VSPACE_CODE),
105 InsetName("mathmacroarg", MATH_MACROARG_CODE),
106 InsetName("listings", LISTINGS_CODE),
107 InsetName("info", INFO_CODE),
108 InsetName("collapsable", COLLAPSABLE_CODE),
109 InsetName("newpage", NEWPAGE_CODE),
110 InsetName("tablecell", CELL_CODE),
111 InsetName("mathamsarray", MATH_AMSARRAY_CODE),
112 InsetName("matharray", MATH_ARRAY_CODE),
113 InsetName("mathbig", MATH_BIG_CODE),
114 InsetName("mathboldsymbol", MATH_BOLDSYMBOL_CODE),
115 InsetName("mathbox", MATH_BOX_CODE),
116 InsetName("mathbrace", MATH_BRACE_CODE),
117 InsetName("mathcases", MATH_CASES_CODE),
118 InsetName("mathchar", MATH_CHAR_CODE),
119 InsetName("mathcolor", MATH_COLOR_CODE),
120 InsetName("mathcomment", MATH_COMMENT_CODE),
121 InsetName("mathdecoration", MATH_DECORATION_CODE),
122 InsetName("mathdelim", MATH_DELIM_CODE),
123 InsetName("mathdiff", MATH_DIFF_CODE),
124 InsetName("mathdots", MATH_DOTS_CODE),
125 InsetName("mathensuremath", MATH_ENSUREMATH_CODE),
126 InsetName("mathenv", MATH_ENV_CODE),
127 InsetName("mathexfunc", MATH_EXFUNC_CODE),
128 InsetName("mathexint", MATH_EXINT_CODE),
129 InsetName("mathfont", MATH_FONT_CODE),
130 InsetName("mathfontold", MATH_FONTOLD_CODE),
131 InsetName("mathfrac", MATH_FRAC_CODE),
132 InsetName("mathgrid", MATH_GRID_CODE),
133 InsetName("math", MATH_CODE),
134 InsetName("mathhull", MATH_HULL_CODE),
135 InsetName("mathkern", MATH_KERN_CODE),
136 InsetName("mathlefteqn", MATH_LEFTEQN_CODE),
137 InsetName("mathlim", MATH_LIM_CODE),
138 InsetName("mathmatrix", MATH_MATRIX_CODE),
139 InsetName("mathmbox", MATH_MBOX_CODE),
140 InsetName("mathnest", MATH_NEST_CODE),
141 InsetName("mathnumber", MATH_NUMBER_CODE),
142 InsetName("mathoverset", MATH_OVERSET_CODE),
143 InsetName("mathpar", MATH_PAR_CODE),
144 InsetName("mathphantom", MATH_PHANTOM_CODE),
145 InsetName("mathref", MATH_REF_CODE),
146 InsetName("mathroot", MATH_ROOT_CODE),
147 InsetName("mathscript", MATH_SCRIPT_CODE),
148 InsetName("mathsize", MATH_SIZE_CODE),
149 InsetName("mathspace", MATH_SPACE_CODE),
150 InsetName("mathspecialchar", MATH_SPECIALCHAR_CODE),
151 InsetName("mathsplit", MATH_SPLIT_CODE),
152 InsetName("mathsqrt", MATH_SQRT_CODE),
153 InsetName("mathstackrel", MATH_STACKREL_CODE),
154 InsetName("mathstring", MATH_STRING_CODE),
155 InsetName("mathsubstack", MATH_SUBSTACK_CODE),
156 InsetName("mathsymbol", MATH_SYMBOL_CODE),
157 InsetName("mathtabular", MATH_TABULAR_CODE),
158 InsetName("mathunderset", MATH_UNDERSET_CODE),
159 InsetName("mathunknown", MATH_UNKNOWN_CODE),
160 InsetName("mathxarrow", MATH_XARROW_CODE),
161 InsetName("mathxyarrow", MATH_XYARROW_CODE),
162 InsetName("mathxymatrix", MATH_XYMATRIX_CODE),
163 InsetName("mathmacro", MATH_MACRO_CODE),
166 size_t const insetnames_size =
167 sizeof(insetnames) / sizeof(insetnames[0]);
169 map<string, InsetCode> data;
170 for (size_t i = 0; i != insetnames_size; ++i) {
171 InsetName const & var = insetnames[i];
172 data[var.name] = var.code;
179 void Inset::setBuffer(Buffer & buffer)
185 Buffer & Inset::buffer()
189 lyxerr << "Inset: " << this
190 << " LyX Code: " << lyxCode()
191 << " name: " << insetName(lyxCode())
193 s << "LyX Code: " << lyxCode() << " name: " << name();
194 LASSERT(false, /**/);
195 throw ExceptionMessage(BufferException,
196 from_ascii("Inset::buffer_ member not initialized!"), s.str());
202 Buffer const & Inset::buffer() const
204 return const_cast<Inset *>(this)->buffer();
208 bool Inset::isBufferValid() const
210 return buffer_ && theBufferList().isLoaded(buffer_);
214 docstring Inset::name() const
216 return from_ascii("unknown");
220 bool Inset::isFreeSpacing() const
222 return getLayout().isFreeSpacing();
226 bool Inset::allowEmpty() const
228 return getLayout().isKeepEmpty();
231 bool Inset::forceLTR() const
233 return getLayout().forceLTR();
236 void Inset::initView()
239 buffer().updateLabels();
243 docstring Inset::toolTip(BufferView const &, int, int) const
249 docstring Inset::contextMenu(BufferView const &, int, int) const
255 Dimension const Inset::dimension(BufferView const & bv) const
257 return bv.coordCache().getInsets().dim(this);
261 InsetCode insetCode(string const & name)
263 static TranslatorMap const translator = build_translator();
265 TranslatorMap::const_iterator it = translator.find(name);
266 return it == translator.end() ? NO_CODE : it->second;
270 string insetName(InsetCode c)
272 static TranslatorMap const translator = build_translator();
274 TranslatorMap::const_iterator it = translator.begin();
275 TranslatorMap::const_iterator end = translator.end();
276 for (; it != end; ++it) {
284 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
287 lyxerr << "Unassigned buffer_ member in Inset::dispatch()" << std::endl;
288 lyxerr << "LyX Code: " << lyxCode() << " name: " << insetName(lyxCode()) << std::endl;
289 } else if (cur.buffer() != buffer_)
290 lyxerr << "cur.buffer() != buffer_ in Inset::dispatch()" << std::endl;
291 cur.updateFlags(Update::Force | Update::FitCursor);
293 doDispatch(cur, cmd);
297 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
299 switch (cmd.action) {
300 case LFUN_MOUSE_RELEASE:
301 // if the derived inset did not explicitly handle mouse_release,
302 // we assume we request the settings dialog
303 if (!cur.selection() && cmd.button() == mouse_button::button1
305 FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
306 dispatch(cur, tmpcmd);
310 case LFUN_INSET_SETTINGS:
311 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
312 showInsetDialog(&cur.bv());
326 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
327 FuncStatus & flag) const
329 // LFUN_INSET_APPLY is sent from the dialogs when the data should
330 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
331 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
332 // not belong to us, i. e. the dialog was open, and the user moved
333 // the cursor in our inset) in LyXFunc::getStatus().
334 // Dialogs::checkStatus() ensures that the dialog is deactivated if
335 // LFUN_INSET_APPLY is disabled.
337 switch (cmd.action) {
338 case LFUN_INSET_MODIFY:
339 // Allow modification of our data.
340 // This needs to be handled in the doDispatch method of our
341 // instantiatable children.
342 flag.setEnabled(true);
345 case LFUN_INSET_INSERT:
346 // Don't allow insertion of new insets.
347 // Every inset that wants to allow new insets from open
348 // dialogs needs to override this.
349 flag.setEnabled(false);
352 case LFUN_INSET_SETTINGS:
353 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
354 bool const enable = hasSettings();
355 flag.setEnabled(enable);
358 flag.setEnabled(false);
369 void Inset::edit(Cursor &, bool, EntryDirection)
371 LYXERR(Debug::INSETS, "edit left/right");
375 Inset * Inset::editXY(Cursor &, int x, int y)
377 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
382 Inset::idx_type Inset::index(row_type row, col_type col) const
385 LYXERR0("illegal row: " << row);
387 LYXERR0("illegal col: " << col);
392 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
394 return from <= idx && idx <= to;
398 bool Inset::idxUpDown(Cursor &, bool) const
404 int Inset::docbook(odocstream &, OutputParams const &) const
410 docstring Inset::xhtml(XHTMLStream & xs, OutputParams const &) const
412 xs << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
416 docstring Inset::xhtml(odocstream & od, OutputParams const &) const
418 od << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
422 bool Inset::directWrite() const
428 bool Inset::editable() const
434 bool Inset::hasSettings() const
441 bool Inset::autoDelete() const
447 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
448 bool, int & x, int & y) const
450 LYXERR0("Inset::cursorPos called directly");
456 void Inset::metricsMarkers(Dimension & dim, int framesize) const
458 dim.wid += 2 * framesize;
459 dim.des += framesize;
463 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
465 dim.wid += 2 * framesize;
466 dim.asc += framesize;
467 dim.des += framesize;
471 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
473 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
474 Color_mathframe : Color_mathcorners;
476 Dimension const dim = dimension(*pi.base.bv);
478 int const t = x + dim.width() - 1;
479 int const d = y + dim.descent();
480 pi.pain.line(x, d - 3, x, d, pen_color);
481 pi.pain.line(t, d - 3, t, d, pen_color);
482 pi.pain.line(x, d, x + 3, d, pen_color);
483 pi.pain.line(t - 3, d, t, d, pen_color);
484 setPosCache(pi, x, y);
488 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
490 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
491 Color_mathframe : Color_mathcorners;
493 drawMarkers(pi, x, y);
494 Dimension const dim = dimension(*pi.base.bv);
495 int const t = x + dim.width() - 1;
496 int const a = y - dim.ascent();
497 pi.pain.line(x, a + 3, x, a, pen_color);
498 pi.pain.line(t, a + 3, t, a, pen_color);
499 pi.pain.line(x, a, x + 3, a, pen_color);
500 pi.pain.line(t - 3, a, t, a, pen_color);
501 setPosCache(pi, x, y);
505 bool Inset::editing(BufferView const * bv) const
507 return bv->cursor().isInside(this);
511 int Inset::xo(BufferView const & bv) const
513 return bv.coordCache().getInsets().x(this);
517 int Inset::yo(BufferView const & bv) const
519 return bv.coordCache().getInsets().y(this);
523 bool Inset::covers(BufferView const & bv, int x, int y) const
525 return bv.coordCache().getInsets().covers(this, x, y);
529 InsetLayout const & Inset::getLayout() const
531 return buffer().params().documentClass().insetLayout(name());
535 bool Inset::undefined() const
537 docstring const & n = getLayout().name();
538 return n.empty() || n == DocumentClass::plainInsetLayout().name();
542 void Inset::dump() const
548 ColorCode Inset::backgroundColor() const
554 ColorCode Inset::labelColor() const
556 return Color_foreground;
560 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
562 //LYXERR("Inset: set position cache to " << x << " " << y);
563 pi.base.bv->coordCache().insets().add(this, x, y);
567 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
569 mi.base.bv->coordCache().insets().add(this, dim);
573 Buffer const * Inset::updateFrontend() const
575 return theApp() ? theApp()->updateInset(this) : 0;
579 docstring Inset::completionPrefix(Cursor const &) const