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/convert.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/ExceptionMessage.h"
40 #include "support/gettext.h"
45 using namespace lyx::support;
51 InsetName(string const & n, InsetCode c) : name(n), code(c) {}
57 typedef map<string, InsetCode> TranslatorMap;
60 static TranslatorMap const build_translator()
62 InsetName const insetnames[] = {
63 InsetName("toc", TOC_CODE),
64 InsetName("quote", QUOTE_CODE),
65 InsetName("ref", REF_CODE),
66 InsetName("href", HYPERLINK_CODE),
67 InsetName("separator", SEPARATOR_CODE),
68 InsetName("ending", ENDING_CODE),
69 InsetName("label", LABEL_CODE),
70 InsetName("note", NOTE_CODE),
71 InsetName("accent", ACCENT_CODE),
72 InsetName("math", MATH_CODE),
73 InsetName("index", INDEX_CODE),
74 InsetName("nomenclature", NOMENCL_CODE),
75 InsetName("include", INCLUDE_CODE),
76 InsetName("graphics", GRAPHICS_CODE),
77 InsetName("bibitem", BIBITEM_CODE),
78 InsetName("bibtex", BIBTEX_CODE),
79 InsetName("text", TEXT_CODE),
80 InsetName("ert", ERT_CODE),
81 InsetName("foot", FOOT_CODE),
82 InsetName("margin", MARGIN_CODE),
83 InsetName("float", FLOAT_CODE),
84 InsetName("wrap", WRAP_CODE),
85 InsetName("specialchar", SPECIALCHAR_CODE),
86 InsetName("tabular", TABULAR_CODE),
87 InsetName("external", EXTERNAL_CODE),
88 InsetName("caption", CAPTION_CODE),
89 InsetName("mathmacro", MATHMACRO_CODE),
90 InsetName("citation", CITE_CODE),
91 InsetName("floatlist", FLOAT_LIST_CODE),
92 InsetName("index_print", INDEX_PRINT_CODE),
93 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
94 InsetName("optarg", OPTARG_CODE),
95 InsetName("environment", ENVIRONMENT_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("space", SPACE_CODE),
102 InsetName("vspace", VSPACE_CODE),
103 InsetName("mathmacroarg", MATHMACROARG_CODE),
104 InsetName("listings", LISTINGS_CODE),
105 InsetName("info", INFO_CODE),
106 InsetName("collapsable", COLLAPSABLE_CODE),
107 InsetName("newpage", NEWPAGE_CODE),
108 InsetName("tablecell", CELL_CODE)
111 size_t const insetnames_size =
112 sizeof(insetnames) / sizeof(insetnames[0]);
114 map<string, InsetCode> data;
115 for (size_t i = 0; i != insetnames_size; ++i) {
116 InsetName const & var = insetnames[i];
117 data[var.name] = var.code;
124 void Inset::setBuffer(Buffer & buffer)
130 Buffer & Inset::buffer()
134 s << "LyX Code: " << lyxCode() << " name: " << name();
136 throw ExceptionMessage(BufferException,
137 from_ascii("Inset::buffer_ member not initialized!"), s.str());
143 Buffer const & Inset::buffer() const
145 return const_cast<Inset *>(this)->buffer();
149 docstring Inset::name() const
151 return from_ascii("unknown");
155 void Inset::initView()
158 lyx::updateLabels(buffer());
162 docstring Inset::toolTip(BufferView const &, int, int) const
168 docstring Inset::contextMenu(BufferView const &, int, int) const
174 Dimension const Inset::dimension(BufferView const & bv) const
176 return bv.coordCache().getInsets().dim(this);
180 InsetCode insetCode(string const & name)
182 static TranslatorMap const translator = build_translator();
184 TranslatorMap::const_iterator it = translator.find(name);
185 return it == translator.end() ? NO_CODE : it->second;
189 string insetName(InsetCode c)
191 static TranslatorMap const translator = build_translator();
193 TranslatorMap::const_iterator it = translator.begin();
194 TranslatorMap::const_iterator end = translator.end();
195 for (; it != end; ++it) {
203 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
205 cur.updateFlags(Update::Force | Update::FitCursor);
207 doDispatch(cur, cmd);
211 void Inset::doDispatch(Cursor & cur, FuncRequest &)
218 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
219 FuncStatus & flag) const
221 // LFUN_INSET_APPLY is sent from the dialogs when the data should
222 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
223 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
224 // not belong to us, i. e. the dialog was open, and the user moved
225 // the cursor in our inset) in LyXFunc::getStatus().
226 // Dialogs::checkStatus() ensures that the dialog is deactivated if
227 // LFUN_INSET_APPLY is disabled.
229 switch (cmd.action) {
230 case LFUN_INSET_MODIFY:
231 // Allow modification of our data.
232 // This needs to be handled in the doDispatch method of our
233 // instantiatable children.
237 case LFUN_INSET_INSERT:
238 // Don't allow insertion of new insets.
239 // Every inset that wants to allow new insets from open
240 // dialogs needs to override this.
250 void Inset::edit(Cursor &, bool, EntryDirection)
252 LYXERR(Debug::INSETS, "edit left/right");
256 Inset * Inset::editXY(Cursor &, int x, int y)
258 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
263 Inset::idx_type Inset::index(row_type row, col_type col) const
266 LYXERR0("illegal row: " << row);
268 LYXERR0("illegal col: " << col);
273 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
275 return from <= idx && idx <= to;
279 bool Inset::idxUpDown(Cursor &, bool) const
285 int Inset::docbook(odocstream &, OutputParams const &) const
291 bool Inset::directWrite() const
297 Inset::EDITABLE Inset::editable() const
303 bool Inset::autoDelete() const
309 docstring Inset::editMessage() const
311 return _("Opened inset");
315 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
316 bool, int & x, int & y) const
318 LYXERR0("Inset::cursorPos called directly");
324 void Inset::metricsMarkers(Dimension & dim, int framesize) const
326 dim.wid += 2 * framesize;
327 dim.des += framesize;
331 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
333 dim.wid += 2 * framesize;
334 dim.asc += framesize;
335 dim.des += framesize;
339 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
341 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
342 Color_mathframe : Color_mathcorners;
344 Dimension const dim = dimension(*pi.base.bv);
346 int const t = x + dim.width() - 1;
347 int const d = y + dim.descent();
348 pi.pain.line(x, d - 3, x, d, pen_color);
349 pi.pain.line(t, d - 3, t, d, pen_color);
350 pi.pain.line(x, d, x + 3, d, pen_color);
351 pi.pain.line(t - 3, d, t, d, pen_color);
352 setPosCache(pi, x, y);
356 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
358 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
359 Color_mathframe : Color_mathcorners;
361 drawMarkers(pi, x, y);
362 Dimension const dim = dimension(*pi.base.bv);
363 int const t = x + dim.width() - 1;
364 int const a = y - dim.ascent();
365 pi.pain.line(x, a + 3, x, a, pen_color);
366 pi.pain.line(t, a + 3, t, a, pen_color);
367 pi.pain.line(x, a, x + 3, a, pen_color);
368 pi.pain.line(t - 3, a, t, a, pen_color);
369 setPosCache(pi, x, y);
373 bool Inset::editing(BufferView const * bv) const
375 return bv->cursor().isInside(this);
379 int Inset::xo(BufferView const & bv) const
381 return bv.coordCache().getInsets().x(this);
385 int Inset::yo(BufferView const & bv) const
387 return bv.coordCache().getInsets().y(this);
391 bool Inset::covers(BufferView const & bv, int x, int y) const
393 return bv.coordCache().getInsets().covers(this, x, y);
397 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
399 return bp.documentClass().insetLayout(name());
403 void Inset::dump() const
409 ColorCode Inset::backgroundColor() const
411 return Color_background;
415 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
417 //LYXERR("Inset: set position cache to " << x << " " << y);
418 pi.base.bv->coordCache().insets().add(this, x, y);
422 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
424 mi.base.bv->coordCache().insets().add(this, dim);
428 Buffer const * Inset::updateFrontend() const
430 return theApp() ? theApp()->updateInset(this) : 0;
434 docstring Inset::completionPrefix(Cursor const &) const