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>
51 InsetName(std::string const & n, InsetCode c) : name(n), code(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("newpage", NEWPAGE_CODE),
110 size_t const insetnames_size =
111 sizeof(insetnames) / sizeof(insetnames[0]);
113 std::map<std::string, InsetCode> data;
114 for (size_t i = 0; i != insetnames_size; ++i) {
115 InsetName const & var = insetnames[i];
116 data[var.name] = var.code;
123 docstring Inset::name() const
125 return from_ascii("unknown");
129 Dimension const Inset::dimension(BufferView const & bv) const
131 return bv.coordCache().getInsets().dim(this);
135 InsetCode insetCode(std::string const & name)
137 static TranslatorMap const translator = build_translator();
139 TranslatorMap::const_iterator it = translator.find(name);
140 return it == translator.end() ? NO_CODE : it->second;
144 std::string insetName(InsetCode c)
146 static TranslatorMap const translator = build_translator();
148 TranslatorMap::const_iterator it = translator.begin();
149 TranslatorMap::const_iterator end = translator.end();
150 for (; it != end; ++it) {
154 return std::string();
158 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
160 cur.updateFlags(Update::Force | Update::FitCursor);
162 doDispatch(cur, cmd);
166 void Inset::doDispatch(Cursor & cur, FuncRequest &)
173 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
174 FuncStatus & flag) const
176 // LFUN_INSET_APPLY is sent from the dialogs when the data should
177 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
178 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
179 // not belong to us, i. e. the dialog was open, and the user moved
180 // the cursor in our inset) in LyXFunc::getStatus().
181 // Dialogs::checkStatus() ensures that the dialog is deactivated if
182 // LFUN_INSET_APPLY is disabled.
184 switch (cmd.action) {
185 case LFUN_INSET_MODIFY:
186 // Allow modification of our data.
187 // This needs to be handled in the doDispatch method of our
188 // instantiatable children.
192 case LFUN_INSET_INSERT:
193 // Don't allow insertion of new insets.
194 // Every inset that wants to allow new insets from open
195 // dialogs needs to override this.
205 void Inset::edit(Cursor &, bool)
207 LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": edit left/right");
211 Inset * Inset::editXY(Cursor &, int x, int y)
213 LYXERR(Debug::INSETS, BOOST_CURRENT_FUNCTION << ": x=" << x << " y= " << y);
218 Inset::idx_type Inset::index(row_type row, col_type col) const
221 LYXERR0(BOOST_CURRENT_FUNCTION << ": illegal row: " << row);
223 LYXERR0(BOOST_CURRENT_FUNCTION << ": illegal col: " << col);
228 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
230 return from <= idx && idx <= to;
234 bool Inset::idxUpDown(Cursor &, bool) const
240 int Inset::docbook(Buffer const &,
241 odocstream &, OutputParams const &) const
247 bool Inset::directWrite() const
253 Inset::EDITABLE Inset::editable() const
259 bool Inset::autoDelete() const
265 docstring const Inset::editMessage() const
267 return _("Opened inset");
271 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
272 bool, int & x, int & y) const
274 LYXERR0("Inset::cursorPos called directly");
280 void Inset::metricsMarkers(Dimension & dim, int framesize) const
282 dim.wid += 2 * framesize;
283 dim.des += framesize;
287 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
289 dim.wid += 2 * framesize;
290 dim.asc += framesize;
291 dim.des += framesize;
295 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
297 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
298 Color_mathframe : Color_mathcorners;
300 Dimension const dim = dimension(*pi.base.bv);
302 int const t = x + dim.width() - 1;
303 int const d = y + dim.descent();
304 pi.pain.line(x, d - 3, x, d, pen_color);
305 pi.pain.line(t, d - 3, t, d, pen_color);
306 pi.pain.line(x, d, x + 3, d, pen_color);
307 pi.pain.line(t - 3, d, t, d, pen_color);
308 setPosCache(pi, x, y);
312 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
314 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
315 Color_mathframe : Color_mathcorners;
317 drawMarkers(pi, x, y);
318 Dimension const dim = dimension(*pi.base.bv);
319 int const t = x + dim.width() - 1;
320 int const a = y - dim.ascent();
321 pi.pain.line(x, a + 3, x, a, pen_color);
322 pi.pain.line(t, a + 3, t, a, pen_color);
323 pi.pain.line(x, a, x + 3, a, pen_color);
324 pi.pain.line(t - 3, a, t, a, pen_color);
325 setPosCache(pi, x, y);
329 bool Inset::editing(BufferView * bv) const
331 return bv->cursor().isInside(this);
335 int Inset::xo(BufferView const & bv) const
337 return bv.coordCache().getInsets().x(this);
341 int Inset::yo(BufferView const & bv) const
343 return bv.coordCache().getInsets().y(this);
347 bool Inset::covers(BufferView const & bv, int x, int y) const
349 return bv.coordCache().getInsets().covers(this, x, y);
353 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
355 return bp.getTextClass().insetlayout(name());
359 void Inset::dump() const
361 Buffer buf("foo", 1);
366 ColorCode Inset::backgroundColor() const
368 return Color_background;
372 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
374 //LYXERR("Inset: set position cache to " << x << " " << y);
375 pi.base.bv->coordCache().insets().add(this, x, y);
379 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
381 mi.base.bv->coordCache().insets().add(this, dim);
385 Buffer const * Inset::updateFrontend() const
389 return theApp()->updateInset(this);