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"
23 #include "CoordCache.h"
26 #include "Dimension.h"
27 #include "DispatchResult.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
32 #include "TextClass.h"
33 #include "MetricsInfo.h"
34 #include "MetricsInfo.h"
36 #include "frontends/Painter.h"
38 #include "support/convert.h"
40 #include <boost/current_function.hpp>
50 InsetName(std::string const & n, InsetCode c)
57 typedef std::map<std::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("hfill", HFILL_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("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("pagebreak", PAGEBREAK_CODE),
110 std::size_t const insetnames_size =
111 sizeof(insetnames) / sizeof(insetnames[0]);
113 std::map<std::string, InsetCode> data;
114 for (std::size_t i = 0; i != insetnames_size; ++i) {
115 InsetName const & var = insetnames[i];
116 data[var.name] = var.code;
127 Dimension const Inset::dimension(BufferView const & bv) const
129 return bv.coordCache().getInsets().dim(this);
133 InsetCode insetCode(std::string const & name)
135 static TranslatorMap const translator = build_translator();
137 TranslatorMap::const_iterator it = translator.find(name);
138 return it == translator.end() ? NO_CODE : it->second;
142 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
144 cur.updateFlags(Update::Force | Update::FitCursor);
146 doDispatch(cur, cmd);
150 void Inset::doDispatch(Cursor & cur, FuncRequest &)
157 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
158 FuncStatus & flag) const
160 // LFUN_INSET_APPLY is sent from the dialogs when the data should
161 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
162 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
163 // not belong to us, i. e. the dialog was open, and the user moved
164 // the cursor in our inset) in LyXFunc::getStatus().
165 // Dialogs::checkStatus() ensures that the dialog is deactivated if
166 // LFUN_INSET_APPLY is disabled.
168 switch (cmd.action) {
169 case LFUN_INSET_MODIFY:
170 // Allow modification of our data.
171 // This needs to be handled in the doDispatch method of our
172 // instantiatable children.
176 case LFUN_INSET_INSERT:
177 // Don't allow insertion of new insets.
178 // Every inset that wants to allow new insets from open
179 // dialogs needs to override this.
189 void Inset::edit(Cursor &, bool)
191 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
192 << ": edit left/right" << std::endl;
196 Inset * Inset::editXY(Cursor &, int x, int y)
198 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
199 << ": x=" << x << " y= " << y
205 Inset::idx_type Inset::index(row_type row, col_type col) const
208 lyxerr << BOOST_CURRENT_FUNCTION
209 << ": illegal row: " << row << std::endl;
211 lyxerr << BOOST_CURRENT_FUNCTION
212 << ": illegal col: " << col << std::endl;
217 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
219 return from <= idx && idx <= to;
223 bool Inset::idxUpDown(Cursor &, bool) const
229 int Inset::docbook(Buffer const &,
230 odocstream &, OutputParams const &) const
236 bool Inset::directWrite() const
242 Inset::EDITABLE Inset::editable() const
248 bool Inset::autoDelete() const
254 docstring const Inset::editMessage() const
256 return _("Opened inset");
260 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
261 bool, int & x, int & y) const
263 lyxerr << "Inset::cursorPos called directly" << std::endl;
269 void Inset::metricsMarkers(Dimension & dim, int framesize) const
271 dim.wid += 2 * framesize;
272 dim.des += framesize;
276 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
278 dim.wid += 2 * framesize;
279 dim.asc += framesize;
280 dim.des += framesize;
284 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
286 Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
287 Color::mathframe : Color::mathcorners;
289 Dimension const dim = dimension(*pi.base.bv);
291 int const t = x + dim.width() - 1;
292 int const d = y + dim.descent();
293 pi.pain.line(x, d - 3, x, d, pen_color);
294 pi.pain.line(t, d - 3, t, d, pen_color);
295 pi.pain.line(x, d, x + 3, d, pen_color);
296 pi.pain.line(t - 3, d, t, d, pen_color);
297 setPosCache(pi, x, y);
301 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
303 Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
304 Color::mathframe : Color::mathcorners;
306 drawMarkers(pi, x, y);
307 Dimension const dim = dimension(*pi.base.bv);
308 int const t = x + dim.width() - 1;
309 int const a = y - dim.ascent();
310 pi.pain.line(x, a + 3, x, a, pen_color);
311 pi.pain.line(t, a + 3, t, a, pen_color);
312 pi.pain.line(x, a, x + 3, a, pen_color);
313 pi.pain.line(t - 3, a, t, a, pen_color);
314 setPosCache(pi, x, y);
318 bool Inset::editing(BufferView * bv) const
320 return bv->cursor().isInside(this);
324 int Inset::xo(BufferView const & bv) const
326 return bv.coordCache().getInsets().x(this);
330 int Inset::yo(BufferView const & bv) const
332 return bv.coordCache().getInsets().y(this);
336 bool Inset::covers(BufferView const & bv, int x, int y) const
338 return bv.coordCache().getInsets().covers(this, x, y);
342 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
344 return bp.getTextClass().insetlayout(name());
348 void Inset::dump() const
350 Buffer buf("foo", 1);
355 Color_color Inset::backgroundColor() const
357 return Color::background;
361 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
363 //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
364 pi.base.bv->coordCache().insets().add(this, x, y);
368 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
370 mi.base.bv->coordCache().insets().add(this, dim);