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"
29 #include "TextClass.h"
30 #include "MetricsInfo.h"
31 #include "MetricsInfo.h"
33 #include "frontends/Painter.h"
34 #include "frontends/Application.h"
36 #include "support/convert.h"
37 #include "support/debug.h"
38 #include "support/gettext.h"
50 InsetName(string const & n, InsetCode c) : name(n), code(c) {}
56 typedef map<string, InsetCode> TranslatorMap;
59 static TranslatorMap const build_translator()
61 InsetName const insetnames[] = {
62 InsetName("toc", TOC_CODE),
63 InsetName("quote", QUOTE_CODE),
64 InsetName("ref", REF_CODE),
65 InsetName("href", HYPERLINK_CODE),
66 InsetName("separator", SEPARATOR_CODE),
67 InsetName("ending", ENDING_CODE),
68 InsetName("label", LABEL_CODE),
69 InsetName("note", NOTE_CODE),
70 InsetName("accent", ACCENT_CODE),
71 InsetName("math", MATH_CODE),
72 InsetName("index", INDEX_CODE),
73 InsetName("nomenclature", NOMENCL_CODE),
74 InsetName("include", INCLUDE_CODE),
75 InsetName("graphics", GRAPHICS_CODE),
76 InsetName("bibitem", BIBITEM_CODE),
77 InsetName("bibtex", BIBTEX_CODE),
78 InsetName("text", TEXT_CODE),
79 InsetName("ert", ERT_CODE),
80 InsetName("foot", FOOT_CODE),
81 InsetName("margin", MARGIN_CODE),
82 InsetName("float", FLOAT_CODE),
83 InsetName("wrap", WRAP_CODE),
84 InsetName("specialchar", SPECIALCHAR_CODE),
85 InsetName("tabular", TABULAR_CODE),
86 InsetName("external", EXTERNAL_CODE),
87 InsetName("caption", CAPTION_CODE),
88 InsetName("mathmacro", MATHMACRO_CODE),
89 InsetName("citation", CITE_CODE),
90 InsetName("floatlist", FLOAT_LIST_CODE),
91 InsetName("index_print", INDEX_PRINT_CODE),
92 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
93 InsetName("optarg", OPTARG_CODE),
94 InsetName("environment", ENVIRONMENT_CODE),
95 InsetName("hfill", HFILL_CODE),
96 InsetName("newline", NEWLINE_CODE),
97 InsetName("line", LINE_CODE),
98 InsetName("branch", BRANCH_CODE),
99 InsetName("box", BOX_CODE),
100 InsetName("flex", FLEX_CODE),
101 InsetName("vspace", VSPACE_CODE),
102 InsetName("mathmacroarg", MATHMACROARG_CODE),
103 InsetName("listings", LISTINGS_CODE),
104 InsetName("info", INFO_CODE),
105 InsetName("collapsable", COLLAPSABLE_CODE),
106 InsetName("newpage", NEWPAGE_CODE),
109 size_t const insetnames_size =
110 sizeof(insetnames) / sizeof(insetnames[0]);
112 map<string, InsetCode> data;
113 for (size_t i = 0; i != insetnames_size; ++i) {
114 InsetName const & var = insetnames[i];
115 data[var.name] = var.code;
122 docstring Inset::name() const
124 return from_ascii("unknown");
128 docstring Inset::toolTip(BufferView const &, int, int) const
134 docstring Inset::contextMenu(BufferView const &, int, int) const
140 Dimension const Inset::dimension(BufferView const & bv) const
142 return bv.coordCache().getInsets().dim(this);
146 InsetCode insetCode(string const & name)
148 static TranslatorMap const translator = build_translator();
150 TranslatorMap::const_iterator it = translator.find(name);
151 return it == translator.end() ? NO_CODE : it->second;
155 string insetName(InsetCode c)
157 static TranslatorMap const translator = build_translator();
159 TranslatorMap::const_iterator it = translator.begin();
160 TranslatorMap::const_iterator end = translator.end();
161 for (; it != end; ++it) {
169 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
171 cur.updateFlags(Update::Force | Update::FitCursor);
173 doDispatch(cur, cmd);
177 void Inset::doDispatch(Cursor & cur, FuncRequest &)
184 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
185 FuncStatus & flag) const
187 // LFUN_INSET_APPLY is sent from the dialogs when the data should
188 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
189 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
190 // not belong to us, i. e. the dialog was open, and the user moved
191 // the cursor in our inset) in LyXFunc::getStatus().
192 // Dialogs::checkStatus() ensures that the dialog is deactivated if
193 // LFUN_INSET_APPLY is disabled.
195 switch (cmd.action) {
196 case LFUN_INSET_MODIFY:
197 // Allow modification of our data.
198 // This needs to be handled in the doDispatch method of our
199 // instantiatable children.
203 case LFUN_INSET_INSERT:
204 // Don't allow insertion of new insets.
205 // Every inset that wants to allow new insets from open
206 // dialogs needs to override this.
216 void Inset::edit(Cursor &, bool, EntryDirectionType)
218 LYXERR(Debug::INSETS, "edit left/right");
222 Inset * Inset::editXY(Cursor &, int x, int y)
224 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
229 Inset::idx_type Inset::index(row_type row, col_type col) const
232 LYXERR0("illegal row: " << row);
234 LYXERR0("illegal col: " << col);
239 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
241 return from <= idx && idx <= to;
245 bool Inset::idxUpDown(Cursor &, bool) const
251 int Inset::docbook(Buffer const &,
252 odocstream &, OutputParams const &) const
258 bool Inset::directWrite() const
264 Inset::EDITABLE Inset::editable() const
270 bool Inset::autoDelete() const
276 docstring const Inset::editMessage() const
278 return _("Opened inset");
282 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
283 bool, int & x, int & y) const
285 LYXERR0("Inset::cursorPos called directly");
291 void Inset::metricsMarkers(Dimension & dim, int framesize) const
293 dim.wid += 2 * framesize;
294 dim.des += framesize;
298 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
300 dim.wid += 2 * framesize;
301 dim.asc += framesize;
302 dim.des += framesize;
306 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
308 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
309 Color_mathframe : Color_mathcorners;
311 Dimension const dim = dimension(*pi.base.bv);
313 int const t = x + dim.width() - 1;
314 int const d = y + dim.descent();
315 pi.pain.line(x, d - 3, x, d, pen_color);
316 pi.pain.line(t, d - 3, t, d, pen_color);
317 pi.pain.line(x, d, x + 3, d, pen_color);
318 pi.pain.line(t - 3, d, t, d, pen_color);
319 setPosCache(pi, x, y);
323 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
325 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
326 Color_mathframe : Color_mathcorners;
328 drawMarkers(pi, x, y);
329 Dimension const dim = dimension(*pi.base.bv);
330 int const t = x + dim.width() - 1;
331 int const a = y - dim.ascent();
332 pi.pain.line(x, a + 3, x, a, pen_color);
333 pi.pain.line(t, a + 3, t, a, pen_color);
334 pi.pain.line(x, a, x + 3, a, pen_color);
335 pi.pain.line(t - 3, a, t, a, pen_color);
336 setPosCache(pi, x, y);
340 bool Inset::editing(BufferView const * bv) const
342 return bv->cursor().isInside(this);
346 int Inset::xo(BufferView const & bv) const
348 return bv.coordCache().getInsets().x(this);
352 int Inset::yo(BufferView const & bv) const
354 return bv.coordCache().getInsets().y(this);
358 bool Inset::covers(BufferView const & bv, int x, int y) const
360 return bv.coordCache().getInsets().covers(this, x, y);
364 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
366 return bp.getTextClass().insetlayout(name());
370 void Inset::dump() const
372 Buffer buf("foo", 1);
377 ColorCode Inset::backgroundColor() const
379 return Color_background;
383 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
385 //LYXERR("Inset: set position cache to " << x << " " << y);
386 pi.base.bv->coordCache().insets().add(this, x, y);
390 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
392 mi.base.bv->coordCache().insets().add(this, dim);
396 Buffer const * Inset::updateFrontend() const
400 return theApp()->updateInset(this);