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"
27 #include "Dimension.h"
28 #include "DispatchResult.h"
29 #include "FuncRequest.h"
30 #include "FuncStatus.h"
33 #include "TextClass.h"
34 #include "MetricsInfo.h"
35 #include "MetricsInfo.h"
37 #include "frontends/Painter.h"
39 #include <boost/current_function.hpp>
49 InsetName(std::string const & n, Inset::Code c)
56 typedef std::map<std::string, Inset::Code> TranslatorMap;
59 static TranslatorMap const build_translator()
61 InsetName const insetnames[] = {
62 InsetName("toc", Inset::TOC_CODE),
63 InsetName("quote", Inset::QUOTE_CODE),
64 InsetName("ref", Inset::REF_CODE),
65 InsetName("url", Inset::URL_CODE),
66 InsetName("htmlurl", Inset::HTMLURL_CODE),
67 InsetName("separator", Inset::SEPARATOR_CODE),
68 InsetName("ending", Inset::ENDING_CODE),
69 InsetName("label", Inset::LABEL_CODE),
70 InsetName("note", Inset::NOTE_CODE),
71 InsetName("accent", Inset::ACCENT_CODE),
72 InsetName("math", Inset::MATH_CODE),
73 InsetName("index", Inset::INDEX_CODE),
74 InsetName("nomenclature", Inset::NOMENCL_CODE),
75 InsetName("include", Inset::INCLUDE_CODE),
76 InsetName("graphics", Inset::GRAPHICS_CODE),
77 InsetName("bibitem", Inset::BIBITEM_CODE),
78 InsetName("bibtex", Inset::BIBTEX_CODE),
79 InsetName("text", Inset::TEXT_CODE),
80 InsetName("ert", Inset::ERT_CODE),
81 InsetName("foot", Inset::FOOT_CODE),
82 InsetName("margin", Inset::MARGIN_CODE),
83 InsetName("float", Inset::FLOAT_CODE),
84 InsetName("wrap", Inset::WRAP_CODE),
85 InsetName("specialchar", Inset::SPECIALCHAR_CODE),
86 InsetName("tabular", Inset::TABULAR_CODE),
87 InsetName("external", Inset::EXTERNAL_CODE),
88 InsetName("caption", Inset::CAPTION_CODE),
89 InsetName("mathmacro", Inset::MATHMACRO_CODE),
90 InsetName("cite", Inset::CITE_CODE),
91 InsetName("float_list", Inset::FLOAT_LIST_CODE),
92 InsetName("index_print", Inset::INDEX_PRINT_CODE),
93 InsetName("nomencl_print", Inset::NOMENCL_PRINT_CODE),
94 InsetName("optarg", Inset::OPTARG_CODE),
95 InsetName("environment", Inset::ENVIRONMENT_CODE),
96 InsetName("hfill", Inset::HFILL_CODE),
97 InsetName("newline", Inset::NEWLINE_CODE),
98 InsetName("line", Inset::LINE_CODE),
99 InsetName("branch", Inset::BRANCH_CODE),
100 InsetName("box", Inset::BOX_CODE),
101 InsetName("flex", Inset::FLEX_CODE),
102 InsetName("vspace", Inset::VSPACE_CODE),
103 InsetName("mathmacroarg", Inset::MATHMACROARG_CODE),
104 InsetName("listings", Inset::LISTINGS_CODE),
107 std::size_t const insetnames_size =
108 sizeof(insetnames) / sizeof(insetnames[0]);
110 std::map<std::string, Inset::Code> data;
111 for (std::size_t i = 0; i != insetnames_size; ++i) {
112 InsetName const & var = insetnames[i];
113 data[var.name] = var.code;
120 /// pretty arbitrary dimensions
126 Inset::Code Inset::translate(std::string const & name)
128 static TranslatorMap const translator = build_translator();
130 TranslatorMap::const_iterator it = translator.find(name);
131 return it == translator.end() ? NO_CODE : it->second;
135 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
137 cur.updateFlags(Update::Force | Update::FitCursor);
139 doDispatch(cur, cmd);
143 void Inset::doDispatch(Cursor & cur, FuncRequest &)
150 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
151 FuncStatus & flag) const
153 // LFUN_INSET_APPLY is sent from the dialogs when the data should
154 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
155 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
156 // not belong to us, i. e. the dialog was open, and the user moved
157 // the cursor in our inset) in LyXFunc::getStatus().
158 // Dialogs::checkStatus() ensures that the dialog is deactivated if
159 // LFUN_INSET_APPLY is disabled.
161 switch (cmd.action) {
162 case LFUN_INSET_MODIFY:
163 // Allow modification of our data.
164 // This needs to be handled in the doDispatch method of our
165 // instantiatable children.
169 case LFUN_INSET_INSERT:
170 // Don't allow insertion of new insets.
171 // Every inset that wants to allow new insets from open
172 // dialogs needs to override this.
182 void Inset::edit(Cursor &, bool)
184 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
185 << ": edit left/right" << std::endl;
189 Inset * Inset::editXY(Cursor &, int x, int y)
191 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
192 << ": x=" << x << " y= " << y
198 Inset::idx_type Inset::index(row_type row, col_type col) const
201 lyxerr << BOOST_CURRENT_FUNCTION
202 << ": illegal row: " << row << std::endl;
204 lyxerr << BOOST_CURRENT_FUNCTION
205 << ": illegal col: " << col << std::endl;
210 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
212 return from <= idx && idx <= to;
216 bool Inset::idxUpDown(Cursor &, bool) const
222 int Inset::docbook(Buffer const &,
223 odocstream &, OutputParams const &) const
229 bool Inset::directWrite() const
235 Inset::EDITABLE Inset::editable() const
241 bool Inset::autoDelete() const
247 docstring const Inset::editMessage() const
249 return _("Opened inset");
253 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
254 bool, int & x, int & y) const
256 lyxerr << "Inset::cursorPos called directly" << std::endl;
262 void Inset::metricsMarkers(Dimension & dim, int framesize) const
264 dim.wid += 2 * framesize;
265 dim.des += framesize;
269 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
271 dim.wid += 2 * framesize;
272 dim.asc += framesize;
273 dim.des += framesize;
277 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
279 Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
280 Color::mathframe : Color::mathcorners;
282 int const t = x + width() - 1;
283 int const d = y + descent();
284 pi.pain.line(x, d - 3, x, d, pen_color);
285 pi.pain.line(t, d - 3, t, d, pen_color);
286 pi.pain.line(x, d, x + 3, d, pen_color);
287 pi.pain.line(t - 3, d, t, d, pen_color);
288 setPosCache(pi, x, y);
292 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
294 Color::color pen_color = mouseHovered() || editing(pi.base.bv)?
295 Color::mathframe : Color::mathcorners;
297 drawMarkers(pi, x, y);
298 int const t = x + width() - 1;
299 int const a = y - ascent();
300 pi.pain.line(x, a + 3, x, a, pen_color);
301 pi.pain.line(t, a + 3, t, a, pen_color);
302 pi.pain.line(x, a, x + 3, a, pen_color);
303 pi.pain.line(t - 3, a, t, a, pen_color);
304 setPosCache(pi, x, y);
308 bool Inset::editing(BufferView * bv) const
310 return bv->cursor().isInside(this);
314 int Inset::xo(BufferView const & bv) const
316 return bv.coordCache().getInsets().x(this);
320 int Inset::yo(BufferView const & bv) const
322 return bv.coordCache().getInsets().y(this);
326 bool Inset::covers(BufferView const & bv, int x, int y) const
328 //lyxerr << "Inset::covers, x: " << x << " y: " << y
329 // << " xo: " << xo(bv) << " yo: " << yo()
330 // << " x1: " << xo(bv) << " x2: " << xo() + width()
331 // << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent()
333 return bv.coordCache().getInsets().has(this)
335 && x <= xo(bv) + width()
336 && y >= yo(bv) - ascent()
337 && y <= yo(bv) + descent();
341 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
343 return bp.getTextClass().insetlayout(name());
347 void Inset::dump() const
349 Buffer buf("foo", 1);
354 Color_color Inset::backgroundColor() const
356 return Color::background;
360 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
362 //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
363 pi.base.bv->coordCache().insets().add(this, x, y);
367 /////////////////////////////////////////
369 bool isEditableInset(Inset const * inset)
371 return inset && inset->editable();
375 bool isHighlyEditableInset(Inset const * inset)
377 return inset && inset->editable() == Inset::HIGHLY_EDITABLE;