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 "BufferParams.h"
22 #include "BufferView.h"
23 #include "CoordCache.h"
25 #include "Dimension.h"
26 #include "DispatchResult.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "MetricsInfo.h"
31 #include "TextClass.h"
33 #include "frontends/Painter.h"
34 #include "frontends/Application.h"
36 #include "support/lassert.h"
37 #include "support/convert.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/gettext.h"
46 using namespace lyx::support;
52 InsetName(string const & n, InsetCode c) : name(n), code(c) {}
58 typedef map<string, InsetCode> TranslatorMap;
61 static TranslatorMap const build_translator()
63 InsetName const insetnames[] = {
64 InsetName("toc", TOC_CODE),
65 InsetName("quote", QUOTE_CODE),
66 InsetName("ref", REF_CODE),
67 InsetName("href", HYPERLINK_CODE),
68 InsetName("separator", SEPARATOR_CODE),
69 InsetName("ending", ENDING_CODE),
70 InsetName("label", LABEL_CODE),
71 InsetName("note", NOTE_CODE),
72 InsetName("accent", ACCENT_CODE),
73 InsetName("math", MATH_CODE),
74 InsetName("index", INDEX_CODE),
75 InsetName("nomenclature", NOMENCL_CODE),
76 InsetName("include", INCLUDE_CODE),
77 InsetName("graphics", GRAPHICS_CODE),
78 InsetName("bibitem", BIBITEM_CODE),
79 InsetName("bibtex", BIBTEX_CODE),
80 InsetName("text", TEXT_CODE),
81 InsetName("ert", ERT_CODE),
82 InsetName("foot", FOOT_CODE),
83 InsetName("margin", MARGIN_CODE),
84 InsetName("float", FLOAT_CODE),
85 InsetName("wrap", WRAP_CODE),
86 InsetName("specialchar", SPECIALCHAR_CODE),
87 InsetName("tabular", TABULAR_CODE),
88 InsetName("external", EXTERNAL_CODE),
89 InsetName("caption", CAPTION_CODE),
90 InsetName("mathmacro", MATHMACRO_CODE),
91 InsetName("citation", CITE_CODE),
92 InsetName("floatlist", FLOAT_LIST_CODE),
93 InsetName("index_print", INDEX_PRINT_CODE),
94 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
95 InsetName("optarg", OPTARG_CODE),
96 InsetName("environment", ENVIRONMENT_CODE),
97 InsetName("newline", NEWLINE_CODE),
98 InsetName("line", LINE_CODE),
99 InsetName("branch", BRANCH_CODE),
100 InsetName("box", BOX_CODE),
101 InsetName("flex", FLEX_CODE),
102 InsetName("space", SPACE_CODE),
103 InsetName("vspace", VSPACE_CODE),
104 InsetName("mathmacroarg", MATHMACROARG_CODE),
105 InsetName("listings", LISTINGS_CODE),
106 InsetName("info", INFO_CODE),
107 InsetName("collapsable", COLLAPSABLE_CODE),
108 InsetName("newpage", NEWPAGE_CODE),
109 InsetName("tablecell", CELL_CODE)
112 size_t const insetnames_size =
113 sizeof(insetnames) / sizeof(insetnames[0]);
115 map<string, InsetCode> data;
116 for (size_t i = 0; i != insetnames_size; ++i) {
117 InsetName const & var = insetnames[i];
118 data[var.name] = var.code;
125 void Inset::setBuffer(Buffer & buffer)
131 Buffer & Inset::buffer()
135 lyxerr << "LyX Code: " << lyxCode() << " name: " << name() << std::endl;
136 s << "LyX Code: " << lyxCode() << " name: " << name();
137 LASSERT(false, /**/);
138 throw ExceptionMessage(BufferException,
139 from_ascii("Inset::buffer_ member not initialized!"), s.str());
145 Buffer const & Inset::buffer() const
147 return const_cast<Inset *>(this)->buffer();
151 docstring Inset::name() const
153 return from_ascii("unknown");
157 void Inset::initView()
160 lyx::updateLabels(buffer());
164 docstring Inset::toolTip(BufferView const &, int, int) const
170 docstring Inset::contextMenu(BufferView const &, int, int) const
176 Dimension const Inset::dimension(BufferView const & bv) const
178 return bv.coordCache().getInsets().dim(this);
182 InsetCode insetCode(string const & name)
184 static TranslatorMap const translator = build_translator();
186 TranslatorMap::const_iterator it = translator.find(name);
187 return it == translator.end() ? NO_CODE : it->second;
191 string insetName(InsetCode c)
193 static TranslatorMap const translator = build_translator();
195 TranslatorMap::const_iterator it = translator.begin();
196 TranslatorMap::const_iterator end = translator.end();
197 for (; it != end; ++it) {
205 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
207 cur.updateFlags(Update::Force | Update::FitCursor);
209 doDispatch(cur, cmd);
213 void Inset::doDispatch(Cursor & cur, FuncRequest &)
220 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
221 FuncStatus & flag) const
223 // LFUN_INSET_APPLY is sent from the dialogs when the data should
224 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
225 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
226 // not belong to us, i. e. the dialog was open, and the user moved
227 // the cursor in our inset) in LyXFunc::getStatus().
228 // Dialogs::checkStatus() ensures that the dialog is deactivated if
229 // LFUN_INSET_APPLY is disabled.
231 switch (cmd.action) {
232 case LFUN_INSET_MODIFY:
233 // Allow modification of our data.
234 // This needs to be handled in the doDispatch method of our
235 // instantiatable children.
239 case LFUN_INSET_INSERT:
240 // Don't allow insertion of new insets.
241 // Every inset that wants to allow new insets from open
242 // dialogs needs to override this.
252 void Inset::edit(Cursor &, bool, EntryDirection)
254 LYXERR(Debug::INSETS, "edit left/right");
258 Inset * Inset::editXY(Cursor &, int x, int y)
260 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
265 Inset::idx_type Inset::index(row_type row, col_type col) const
268 LYXERR0("illegal row: " << row);
270 LYXERR0("illegal col: " << col);
275 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
277 return from <= idx && idx <= to;
281 bool Inset::idxUpDown(Cursor &, bool) const
287 int Inset::docbook(odocstream &, OutputParams const &) const
293 bool Inset::directWrite() const
299 Inset::EDITABLE Inset::editable() const
305 bool Inset::autoDelete() const
311 docstring Inset::editMessage() const
313 return _("Opened inset");
317 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
318 bool, int & x, int & y) const
320 LYXERR0("Inset::cursorPos called directly");
326 void Inset::metricsMarkers(Dimension & dim, int framesize) const
328 dim.wid += 2 * framesize;
329 dim.des += framesize;
333 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
335 dim.wid += 2 * framesize;
336 dim.asc += framesize;
337 dim.des += framesize;
341 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
343 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
344 Color_mathframe : Color_mathcorners;
346 Dimension const dim = dimension(*pi.base.bv);
348 int const t = x + dim.width() - 1;
349 int const d = y + dim.descent();
350 pi.pain.line(x, d - 3, x, d, pen_color);
351 pi.pain.line(t, d - 3, t, d, pen_color);
352 pi.pain.line(x, d, x + 3, d, pen_color);
353 pi.pain.line(t - 3, d, t, d, pen_color);
354 setPosCache(pi, x, y);
358 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
360 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
361 Color_mathframe : Color_mathcorners;
363 drawMarkers(pi, x, y);
364 Dimension const dim = dimension(*pi.base.bv);
365 int const t = x + dim.width() - 1;
366 int const a = y - dim.ascent();
367 pi.pain.line(x, a + 3, x, a, pen_color);
368 pi.pain.line(t, a + 3, t, a, pen_color);
369 pi.pain.line(x, a, x + 3, a, pen_color);
370 pi.pain.line(t - 3, a, t, a, pen_color);
371 setPosCache(pi, x, y);
375 bool Inset::editing(BufferView const * bv) const
377 return bv->cursor().isInside(this);
381 int Inset::xo(BufferView const & bv) const
383 return bv.coordCache().getInsets().x(this);
387 int Inset::yo(BufferView const & bv) const
389 return bv.coordCache().getInsets().y(this);
393 bool Inset::covers(BufferView const & bv, int x, int y) const
395 return bv.coordCache().getInsets().covers(this, x, y);
399 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
401 return bp.documentClass().insetLayout(name());
405 void Inset::dump() const
411 ColorCode Inset::backgroundColor() const
413 return Color_background;
417 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
419 //LYXERR("Inset: set position cache to " << x << " " << y);
420 pi.base.bv->coordCache().insets().add(this, x, y);
424 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
426 mi.base.bv->coordCache().insets().add(this, dim);
430 Buffer const * Inset::updateFrontend() const
432 return theApp() ? theApp()->updateInset(this) : 0;
436 docstring Inset::completionPrefix(Cursor const &) const