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"
39 #include "support/ExceptionMessage.h"
41 #include <boost/current_function.hpp>
51 InsetName(std::string const & n, Inset::Code c)
58 typedef std::map<std::string, Inset::Code> TranslatorMap;
61 static TranslatorMap const build_translator()
63 InsetName const insetnames[] = {
64 InsetName("toc", Inset::TOC_CODE),
65 InsetName("quote", Inset::QUOTE_CODE),
66 InsetName("ref", Inset::REF_CODE),
67 InsetName("url", Inset::URL_CODE),
68 InsetName("htmlurl", Inset::HTMLURL_CODE),
69 InsetName("separator", Inset::SEPARATOR_CODE),
70 InsetName("ending", Inset::ENDING_CODE),
71 InsetName("label", Inset::LABEL_CODE),
72 InsetName("note", Inset::NOTE_CODE),
73 InsetName("accent", Inset::ACCENT_CODE),
74 InsetName("math", Inset::MATH_CODE),
75 InsetName("index", Inset::INDEX_CODE),
76 InsetName("nomenclature", Inset::NOMENCL_CODE),
77 InsetName("include", Inset::INCLUDE_CODE),
78 InsetName("graphics", Inset::GRAPHICS_CODE),
79 InsetName("bibitem", Inset::BIBITEM_CODE),
80 InsetName("bibtex", Inset::BIBTEX_CODE),
81 InsetName("text", Inset::TEXT_CODE),
82 InsetName("ert", Inset::ERT_CODE),
83 InsetName("foot", Inset::FOOT_CODE),
84 InsetName("margin", Inset::MARGIN_CODE),
85 InsetName("float", Inset::FLOAT_CODE),
86 InsetName("wrap", Inset::WRAP_CODE),
87 InsetName("specialchar", Inset::SPECIALCHAR_CODE),
88 InsetName("tabular", Inset::TABULAR_CODE),
89 InsetName("external", Inset::EXTERNAL_CODE),
90 InsetName("caption", Inset::CAPTION_CODE),
91 InsetName("mathmacro", Inset::MATHMACRO_CODE),
92 InsetName("cite", Inset::CITE_CODE),
93 InsetName("float_list", Inset::FLOAT_LIST_CODE),
94 InsetName("index_print", Inset::INDEX_PRINT_CODE),
95 InsetName("nomencl_print", Inset::NOMENCL_PRINT_CODE),
96 InsetName("optarg", Inset::OPTARG_CODE),
97 InsetName("environment", Inset::ENVIRONMENT_CODE),
98 InsetName("hfill", Inset::HFILL_CODE),
99 InsetName("newline", Inset::NEWLINE_CODE),
100 InsetName("line", Inset::LINE_CODE),
101 InsetName("branch", Inset::BRANCH_CODE),
102 InsetName("box", Inset::BOX_CODE),
103 InsetName("flex", Inset::FLEX_CODE),
104 InsetName("vspace", Inset::VSPACE_CODE),
105 InsetName("mathmacroarg", Inset::MATHMACROARG_CODE),
106 InsetName("listings", Inset::LISTINGS_CODE),
109 std::size_t const insetnames_size =
110 sizeof(insetnames) / sizeof(insetnames[0]);
112 std::map<std::string, Inset::Code> data;
113 for (std::size_t i = 0; i != insetnames_size; ++i) {
114 InsetName const & var = insetnames[i];
115 data[var.name] = var.code;
126 Dimension const Inset::dimension(BufferView const &) const
128 docstring const id = convert<docstring>(int(lyxCode())) + " " + name();
129 throw support::ExceptionMessage(support::ErrorException,
130 _("Inset::dimension(): unimplemented method"), id);
134 Inset::Code Inset::translate(std::string const & name)
136 static TranslatorMap const translator = build_translator();
138 TranslatorMap::const_iterator it = translator.find(name);
139 return it == translator.end() ? NO_CODE : it->second;
143 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
145 cur.updateFlags(Update::Force | Update::FitCursor);
147 doDispatch(cur, cmd);
151 void Inset::doDispatch(Cursor & cur, FuncRequest &)
158 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
159 FuncStatus & flag) const
161 // LFUN_INSET_APPLY is sent from the dialogs when the data should
162 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
163 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
164 // not belong to us, i. e. the dialog was open, and the user moved
165 // the cursor in our inset) in LyXFunc::getStatus().
166 // Dialogs::checkStatus() ensures that the dialog is deactivated if
167 // LFUN_INSET_APPLY is disabled.
169 switch (cmd.action) {
170 case LFUN_INSET_MODIFY:
171 // Allow modification of our data.
172 // This needs to be handled in the doDispatch method of our
173 // instantiatable children.
177 case LFUN_INSET_INSERT:
178 // Don't allow insertion of new insets.
179 // Every inset that wants to allow new insets from open
180 // dialogs needs to override this.
190 void Inset::edit(Cursor &, bool)
192 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
193 << ": edit left/right" << std::endl;
197 Inset * Inset::editXY(Cursor &, int x, int y)
199 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
200 << ": x=" << x << " y= " << y
206 Inset::idx_type Inset::index(row_type row, col_type col) const
209 lyxerr << BOOST_CURRENT_FUNCTION
210 << ": illegal row: " << row << std::endl;
212 lyxerr << BOOST_CURRENT_FUNCTION
213 << ": illegal col: " << col << std::endl;
218 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
220 return from <= idx && idx <= to;
224 bool Inset::idxUpDown(Cursor &, bool) const
230 int Inset::docbook(Buffer const &,
231 odocstream &, OutputParams const &) const
237 bool Inset::directWrite() const
243 Inset::EDITABLE Inset::editable() const
249 bool Inset::autoDelete() const
255 docstring const Inset::editMessage() const
257 return _("Opened inset");
261 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
262 bool, int & x, int & y) const
264 lyxerr << "Inset::cursorPos called directly" << std::endl;
270 void Inset::metricsMarkers(Dimension & dim, int framesize) const
272 dim.wid += 2 * framesize;
273 dim.des += framesize;
277 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
279 dim.wid += 2 * framesize;
280 dim.asc += framesize;
281 dim.des += framesize;
285 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
287 Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
288 Color::mathframe : Color::mathcorners;
290 Dimension const dim = dimension(*pi.base.bv);
292 int const t = x + dim.width() - 1;
293 int const d = y + dim.descent();
294 pi.pain.line(x, d - 3, x, d, pen_color);
295 pi.pain.line(t, d - 3, t, d, pen_color);
296 pi.pain.line(x, d, x + 3, d, pen_color);
297 pi.pain.line(t - 3, d, t, d, pen_color);
298 setPosCache(pi, x, y);
302 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
304 Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
305 Color::mathframe : Color::mathcorners;
307 drawMarkers(pi, x, y);
308 Dimension const dim = dimension(*pi.base.bv);
309 int const t = x + dim.width() - 1;
310 int const a = y - dim.ascent();
311 pi.pain.line(x, a + 3, x, a, pen_color);
312 pi.pain.line(t, a + 3, t, a, pen_color);
313 pi.pain.line(x, a, x + 3, a, pen_color);
314 pi.pain.line(t - 3, a, t, a, pen_color);
315 setPosCache(pi, x, y);
319 bool Inset::editing(BufferView * bv) const
321 return bv->cursor().isInside(this);
325 int Inset::xo(BufferView const & bv) const
327 return bv.coordCache().getInsets().x(this);
331 int Inset::yo(BufferView const & bv) const
333 return bv.coordCache().getInsets().y(this);
337 bool Inset::covers(BufferView const & bv, int x, int y) const
339 //lyxerr << "Inset::covers, x: " << x << " y: " << y
340 // << " xo: " << xo(bv) << " yo: " << yo()
341 // << " x1: " << xo(bv) << " x2: " << xo() + width()
342 // << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent()
345 Dimension const dim = dimension(bv);
347 return bv.coordCache().getInsets().has(this)
349 && x <= xo(bv) + dim.width()
350 && y >= yo(bv) - dim.ascent()
351 && y <= yo(bv) + dim.descent();
355 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
357 return bp.getTextClass().insetlayout(name());
361 void Inset::dump() const
363 Buffer buf("foo", 1);
368 Color_color Inset::backgroundColor() const
370 return Color::background;
374 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
376 //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
377 pi.base.bv->coordCache().insets().add(this, x, y);
381 /////////////////////////////////////////
383 bool isEditableInset(Inset const * inset)
385 return inset && inset->editable();
389 bool isHighlyEditableInset(Inset const * inset)
391 return inset && inset->editable() == Inset::HIGHLY_EDITABLE;