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.
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "CoordCache.h"
24 #include "Dimension.h"
25 #include "DispatchResult.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "MetricsInfo.h"
30 #include "TextClass.h"
32 #include "frontends/Painter.h"
33 #include "frontends/Application.h"
35 #include "support/convert.h"
36 #include "support/debug.h"
37 #include "support/gettext.h"
47 InsetName(string const & n, InsetCode c) : name(n), code(c) {}
53 typedef map<string, InsetCode> TranslatorMap;
56 static TranslatorMap const build_translator()
58 InsetName const insetnames[] = {
59 InsetName("toc", TOC_CODE),
60 InsetName("quote", QUOTE_CODE),
61 InsetName("ref", REF_CODE),
62 InsetName("href", HYPERLINK_CODE),
63 InsetName("separator", SEPARATOR_CODE),
64 InsetName("ending", ENDING_CODE),
65 InsetName("label", LABEL_CODE),
66 InsetName("note", NOTE_CODE),
67 InsetName("accent", ACCENT_CODE),
68 InsetName("math", MATH_CODE),
69 InsetName("index", INDEX_CODE),
70 InsetName("nomenclature", NOMENCL_CODE),
71 InsetName("include", INCLUDE_CODE),
72 InsetName("graphics", GRAPHICS_CODE),
73 InsetName("bibitem", BIBITEM_CODE),
74 InsetName("bibtex", BIBTEX_CODE),
75 InsetName("text", TEXT_CODE),
76 InsetName("ert", ERT_CODE),
77 InsetName("foot", FOOT_CODE),
78 InsetName("margin", MARGIN_CODE),
79 InsetName("float", FLOAT_CODE),
80 InsetName("wrap", WRAP_CODE),
81 InsetName("specialchar", SPECIALCHAR_CODE),
82 InsetName("tabular", TABULAR_CODE),
83 InsetName("external", EXTERNAL_CODE),
84 InsetName("caption", CAPTION_CODE),
85 InsetName("mathmacro", MATHMACRO_CODE),
86 InsetName("citation", CITE_CODE),
87 InsetName("floatlist", FLOAT_LIST_CODE),
88 InsetName("index_print", INDEX_PRINT_CODE),
89 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
90 InsetName("optarg", OPTARG_CODE),
91 InsetName("environment", ENVIRONMENT_CODE),
92 InsetName("hfill", HFILL_CODE),
93 InsetName("newline", NEWLINE_CODE),
94 InsetName("line", LINE_CODE),
95 InsetName("branch", BRANCH_CODE),
96 InsetName("box", BOX_CODE),
97 InsetName("flex", FLEX_CODE),
98 InsetName("vspace", VSPACE_CODE),
99 InsetName("mathmacroarg", MATHMACROARG_CODE),
100 InsetName("listings", LISTINGS_CODE),
101 InsetName("info", INFO_CODE),
102 InsetName("collapsable", COLLAPSABLE_CODE),
103 InsetName("newpage", NEWPAGE_CODE),
106 size_t const insetnames_size =
107 sizeof(insetnames) / sizeof(insetnames[0]);
109 map<string, InsetCode> data;
110 for (size_t i = 0; i != insetnames_size; ++i) {
111 InsetName const & var = insetnames[i];
112 data[var.name] = var.code;
119 docstring Inset::name() const
121 return from_ascii("unknown");
125 docstring Inset::toolTip(BufferView const &, int, int) const
131 docstring Inset::contextMenu(BufferView const &, int, int) const
137 Dimension const Inset::dimension(BufferView const & bv) const
139 return bv.coordCache().getInsets().dim(this);
143 InsetCode insetCode(string const & name)
145 static TranslatorMap const translator = build_translator();
147 TranslatorMap::const_iterator it = translator.find(name);
148 return it == translator.end() ? NO_CODE : it->second;
152 string insetName(InsetCode c)
154 static TranslatorMap const translator = build_translator();
156 TranslatorMap::const_iterator it = translator.begin();
157 TranslatorMap::const_iterator end = translator.end();
158 for (; it != end; ++it) {
166 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
168 cur.updateFlags(Update::Force | Update::FitCursor);
170 doDispatch(cur, cmd);
174 void Inset::doDispatch(Cursor & cur, FuncRequest &)
181 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
182 FuncStatus & flag) const
184 // LFUN_INSET_APPLY is sent from the dialogs when the data should
185 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
186 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
187 // not belong to us, i. e. the dialog was open, and the user moved
188 // the cursor in our inset) in LyXFunc::getStatus().
189 // Dialogs::checkStatus() ensures that the dialog is deactivated if
190 // LFUN_INSET_APPLY is disabled.
192 switch (cmd.action) {
193 case LFUN_INSET_MODIFY:
194 // Allow modification of our data.
195 // This needs to be handled in the doDispatch method of our
196 // instantiatable children.
200 case LFUN_INSET_INSERT:
201 // Don't allow insertion of new insets.
202 // Every inset that wants to allow new insets from open
203 // dialogs needs to override this.
213 void Inset::edit(Cursor &, bool, EntryDirection)
215 LYXERR(Debug::INSETS, "edit left/right");
219 Inset * Inset::editXY(Cursor &, int x, int y)
221 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
226 Inset::idx_type Inset::index(row_type row, col_type col) const
229 LYXERR0("illegal row: " << row);
231 LYXERR0("illegal col: " << col);
236 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
238 return from <= idx && idx <= to;
242 bool Inset::idxUpDown(Cursor &, bool) const
248 int Inset::docbook(Buffer const &,
249 odocstream &, OutputParams const &) const
255 bool Inset::directWrite() const
261 Inset::EDITABLE Inset::editable() const
267 bool Inset::autoDelete() const
273 docstring const Inset::editMessage() const
275 return _("Opened inset");
279 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
280 bool, int & x, int & y) const
282 LYXERR0("Inset::cursorPos called directly");
288 void Inset::metricsMarkers(Dimension & dim, int framesize) const
290 dim.wid += 2 * framesize;
291 dim.des += framesize;
295 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
297 dim.wid += 2 * framesize;
298 dim.asc += framesize;
299 dim.des += framesize;
303 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
305 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
306 Color_mathframe : Color_mathcorners;
308 Dimension const dim = dimension(*pi.base.bv);
310 int const t = x + dim.width() - 1;
311 int const d = y + dim.descent();
312 pi.pain.line(x, d - 3, x, d, pen_color);
313 pi.pain.line(t, d - 3, t, d, pen_color);
314 pi.pain.line(x, d, x + 3, d, pen_color);
315 pi.pain.line(t - 3, d, t, d, pen_color);
316 setPosCache(pi, x, y);
320 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
322 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
323 Color_mathframe : Color_mathcorners;
325 drawMarkers(pi, x, y);
326 Dimension const dim = dimension(*pi.base.bv);
327 int const t = x + dim.width() - 1;
328 int const a = y - dim.ascent();
329 pi.pain.line(x, a + 3, x, a, pen_color);
330 pi.pain.line(t, a + 3, t, a, pen_color);
331 pi.pain.line(x, a, x + 3, a, pen_color);
332 pi.pain.line(t - 3, a, t, a, pen_color);
333 setPosCache(pi, x, y);
337 bool Inset::editing(BufferView const * bv) const
339 return bv->cursor().isInside(this);
343 int Inset::xo(BufferView const & bv) const
345 return bv.coordCache().getInsets().x(this);
349 int Inset::yo(BufferView const & bv) const
351 return bv.coordCache().getInsets().y(this);
355 bool Inset::covers(BufferView const & bv, int x, int y) const
357 return bv.coordCache().getInsets().covers(this, x, y);
361 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
363 return bp.textClass().insetLayout(name());
367 void Inset::dump() const
369 Buffer buf("foo", 1);
374 ColorCode Inset::backgroundColor() const
376 return Color_background;
380 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
382 //LYXERR("Inset: set position cache to " << x << " " << y);
383 pi.base.bv->coordCache().insets().add(this, x, y);
387 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
389 mi.base.bv->coordCache().insets().add(this, dim);
393 Buffer const * Inset::updateFrontend() const
395 return theApp()? theApp()->updateInset(this) : 0;