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"
25 #include "Dimension.h"
26 #include "DispatchResult.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
31 #include "TextClass.h"
32 #include "MetricsInfo.h"
33 #include "MetricsInfo.h"
35 #include "frontends/Painter.h"
36 #include "frontends/Application.h"
38 #include "support/convert.h"
40 #include <boost/current_function.hpp>
52 InsetName(std::string const & n, InsetCode c) : name(n), code(c) {}
58 typedef std::map<std::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("hfill", HFILL_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("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("pagebreak", PAGEBREAK_CODE),
111 std::size_t const insetnames_size =
112 sizeof(insetnames) / sizeof(insetnames[0]);
114 std::map<std::string, InsetCode> data;
115 for (std::size_t i = 0; i != insetnames_size; ++i) {
116 InsetName const & var = insetnames[i];
117 data[var.name] = var.code;
124 docstring Inset::name() const
126 return from_ascii("unknown");
130 Dimension const Inset::dimension(BufferView const & bv) const
132 return bv.coordCache().getInsets().dim(this);
136 InsetCode insetCode(std::string const & name)
138 static TranslatorMap const translator = build_translator();
140 TranslatorMap::const_iterator it = translator.find(name);
141 return it == translator.end() ? NO_CODE : it->second;
145 std::string insetName(InsetCode c)
147 static TranslatorMap const translator = build_translator();
149 TranslatorMap::const_iterator it = translator.begin();
150 TranslatorMap::const_iterator end = translator.end();
151 for (; it != end; ++it) {
155 return std::string();
159 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
161 cur.updateFlags(Update::Force | Update::FitCursor);
163 doDispatch(cur, cmd);
167 void Inset::doDispatch(Cursor & cur, FuncRequest &)
174 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
175 FuncStatus & flag) const
177 // LFUN_INSET_APPLY is sent from the dialogs when the data should
178 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
179 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
180 // not belong to us, i. e. the dialog was open, and the user moved
181 // the cursor in our inset) in LyXFunc::getStatus().
182 // Dialogs::checkStatus() ensures that the dialog is deactivated if
183 // LFUN_INSET_APPLY is disabled.
185 switch (cmd.action) {
186 case LFUN_INSET_MODIFY:
187 // Allow modification of our data.
188 // This needs to be handled in the doDispatch method of our
189 // instantiatable children.
193 case LFUN_INSET_INSERT:
194 // Don't allow insertion of new insets.
195 // Every inset that wants to allow new insets from open
196 // dialogs needs to override this.
206 void Inset::edit(Cursor &, bool)
208 LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": edit left/right");
212 Inset * Inset::editXY(Cursor &, int x, int y)
214 LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": x=" << x << " y= " << y);
219 Inset::idx_type Inset::index(row_type row, col_type col) const
222 lyxerr << BOOST_CURRENT_FUNCTION
223 << ": illegal row: " << row << std::endl;
225 lyxerr << BOOST_CURRENT_FUNCTION
226 << ": illegal col: " << col << std::endl;
231 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
233 return from <= idx && idx <= to;
237 bool Inset::idxUpDown(Cursor &, bool) const
243 int Inset::docbook(Buffer const &,
244 odocstream &, OutputParams const &) const
250 bool Inset::directWrite() const
256 Inset::EDITABLE Inset::editable() const
262 bool Inset::autoDelete() const
268 docstring const Inset::editMessage() const
270 return _("Opened inset");
274 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
275 bool, int & x, int & y) const
277 lyxerr << "Inset::cursorPos called directly" << std::endl;
283 void Inset::metricsMarkers(Dimension & dim, int framesize) const
285 dim.wid += 2 * framesize;
286 dim.des += framesize;
290 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
292 dim.wid += 2 * framesize;
293 dim.asc += framesize;
294 dim.des += framesize;
298 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
300 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
301 Color_mathframe : Color_mathcorners;
303 Dimension const dim = dimension(*pi.base.bv);
305 int const t = x + dim.width() - 1;
306 int const d = y + dim.descent();
307 pi.pain.line(x, d - 3, x, d, pen_color);
308 pi.pain.line(t, d - 3, t, d, pen_color);
309 pi.pain.line(x, d, x + 3, d, pen_color);
310 pi.pain.line(t - 3, d, t, d, pen_color);
311 setPosCache(pi, x, y);
315 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
317 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
318 Color_mathframe : Color_mathcorners;
320 drawMarkers(pi, x, y);
321 Dimension const dim = dimension(*pi.base.bv);
322 int const t = x + dim.width() - 1;
323 int const a = y - dim.ascent();
324 pi.pain.line(x, a + 3, x, a, pen_color);
325 pi.pain.line(t, a + 3, t, a, pen_color);
326 pi.pain.line(x, a, x + 3, a, pen_color);
327 pi.pain.line(t - 3, a, t, a, pen_color);
328 setPosCache(pi, x, y);
332 bool Inset::editing(BufferView * bv) const
334 return bv->cursor().isInside(this);
338 int Inset::xo(BufferView const & bv) const
340 return bv.coordCache().getInsets().x(this);
344 int Inset::yo(BufferView const & bv) const
346 return bv.coordCache().getInsets().y(this);
350 bool Inset::covers(BufferView const & bv, int x, int y) const
352 return bv.coordCache().getInsets().covers(this, x, y);
356 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
358 return bp.getTextClass().insetlayout(name());
362 void Inset::dump() const
364 Buffer buf("foo", 1);
369 ColorCode Inset::backgroundColor() const
371 return Color_background;
375 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
377 //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
378 pi.base.bv->coordCache().insets().add(this, x, y);
382 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
384 mi.base.bv->coordCache().insets().add(this, dim);
388 Buffer const * Inset::updateFrontend() const
392 return theApp()->updateInset(this);