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 "BufferView.h"
22 #include "CoordCache.h"
26 #include "Dimension.h"
27 #include "DispatchResult.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
32 #include "MetricsInfo.h"
33 #include "MetricsInfo.h"
35 #include "frontends/Painter.h"
37 #include <boost/current_function.hpp>
47 InsetName(std::string const & n, Inset::Code c)
54 typedef std::map<std::string, Inset::Code> TranslatorMap;
57 static TranslatorMap const build_translator()
59 InsetName const insetnames[] = {
60 InsetName("toc", Inset::TOC_CODE),
61 InsetName("quote", Inset::QUOTE_CODE),
62 InsetName("ref", Inset::REF_CODE),
63 InsetName("url", Inset::URL_CODE),
64 InsetName("htmlurl", Inset::HTMLURL_CODE),
65 InsetName("separator", Inset::SEPARATOR_CODE),
66 InsetName("ending", Inset::ENDING_CODE),
67 InsetName("label", Inset::LABEL_CODE),
68 InsetName("note", Inset::NOTE_CODE),
69 InsetName("accent", Inset::ACCENT_CODE),
70 InsetName("math", Inset::MATH_CODE),
71 InsetName("index", Inset::INDEX_CODE),
72 InsetName("nomenclature", Inset::NOMENCL_CODE),
73 InsetName("include", Inset::INCLUDE_CODE),
74 InsetName("graphics", Inset::GRAPHICS_CODE),
75 InsetName("bibitem", Inset::BIBITEM_CODE),
76 InsetName("bibtex", Inset::BIBTEX_CODE),
77 InsetName("text", Inset::TEXT_CODE),
78 InsetName("ert", Inset::ERT_CODE),
79 InsetName("foot", Inset::FOOT_CODE),
80 InsetName("margin", Inset::MARGIN_CODE),
81 InsetName("float", Inset::FLOAT_CODE),
82 InsetName("wrap", Inset::WRAP_CODE),
83 InsetName("specialchar", Inset::SPECIALCHAR_CODE),
84 InsetName("tabular", Inset::TABULAR_CODE),
85 InsetName("external", Inset::EXTERNAL_CODE),
86 InsetName("caption", Inset::CAPTION_CODE),
87 InsetName("mathmacro", Inset::MATHMACRO_CODE),
88 InsetName("cite", Inset::CITE_CODE),
89 InsetName("float_list", Inset::FLOAT_LIST_CODE),
90 InsetName("index_print", Inset::INDEX_PRINT_CODE),
91 InsetName("nomencl_print", Inset::NOMENCL_PRINT_CODE),
92 InsetName("optarg", Inset::OPTARG_CODE),
93 InsetName("environment", Inset::ENVIRONMENT_CODE),
94 InsetName("hfill", Inset::HFILL_CODE),
95 InsetName("newline", Inset::NEWLINE_CODE),
96 InsetName("line", Inset::LINE_CODE),
97 InsetName("branch", Inset::BRANCH_CODE),
98 InsetName("box", Inset::BOX_CODE),
99 InsetName("charstyle", Inset::CHARSTYLE_CODE),
100 InsetName("vspace", Inset::VSPACE_CODE),
101 InsetName("mathmacroarg", Inset::MATHMACROARG_CODE),
104 std::size_t const insetnames_size =
105 sizeof(insetnames) / sizeof(insetnames[0]);
107 std::map<std::string, Inset::Code> data;
108 for (std::size_t i = 0; i != insetnames_size; ++i) {
109 InsetName const & var = insetnames[i];
110 data[var.name] = var.code;
117 /// pretty arbitrary dimensions
119 : dim_(10, 10, 10), background_color_(Color::background)
123 Inset::Inset(Inset const & inset)
124 : dim_(inset.dim_), background_color_(inset.background_color_)
128 std::auto_ptr<Inset> Inset::clone() const
130 std::auto_ptr<Inset> b = doClone();
131 BOOST_ASSERT(typeid(*b) == typeid(*this));
136 docstring Inset::insetName() const
138 return from_ascii("unknown");
142 Inset::Code Inset::translate(std::string const & name)
144 static TranslatorMap const translator = build_translator();
146 TranslatorMap::const_iterator it = translator.find(name);
147 return it == translator.end() ? NO_CODE : it->second;
151 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
153 cur.updateFlags(Update::Force | Update::FitCursor);
155 doDispatch(cur, cmd);
159 void Inset::doDispatch(Cursor & cur, FuncRequest &)
166 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
167 FuncStatus & flag) const
169 // LFUN_INSET_APPLY is sent from the dialogs when the data should
170 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
171 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
172 // not belong to us, i. e. the dialog was open, and the user moved
173 // the cursor in our inset) in LyXFunc::getStatus().
174 // Dialogs::checkStatus() ensures that the dialog is deactivated if
175 // LFUN_INSET_APPLY is disabled.
177 switch (cmd.action) {
178 case LFUN_INSET_MODIFY:
179 // Allow modification of our data.
180 // This needs to be handled in the doDispatch method of our
181 // instantiatable children.
185 case LFUN_INSET_INSERT:
186 // Don't allow insertion of new insets.
187 // Every inset that wants to allow new insets from open
188 // dialogs needs to override this.
198 void Inset::edit(Cursor &, bool)
200 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
201 << ": edit left/right" << std::endl;
205 Inset * Inset::editXY(Cursor &, int x, int y)
207 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
208 << ": x=" << x << " y= " << y
214 Inset::idx_type Inset::index(row_type row, col_type col) const
217 lyxerr << BOOST_CURRENT_FUNCTION
218 << ": illegal row: " << row << std::endl;
220 lyxerr << BOOST_CURRENT_FUNCTION
221 << ": illegal col: " << col << std::endl;
226 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
228 return from <= idx && idx <= to;
232 bool Inset::idxUpDown(Cursor &, bool) const
238 int Inset::docbook(Buffer const &,
239 odocstream &, OutputParams const &) const
245 bool Inset::directWrite() const
251 Inset::EDITABLE Inset::editable() const
257 bool Inset::autoDelete() const
263 docstring const Inset::editMessage() const
265 return _("Opened inset");
269 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
270 bool, int & x, int & y) const
272 lyxerr << "Inset::cursorPos called directly" << std::endl;
278 void Inset::metricsMarkers(Dimension & dim, int framesize) const
280 dim.wid += 2 * framesize;
281 dim.asc += framesize;
285 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
287 dim.wid += 2 * framesize;
288 dim.asc += framesize;
289 dim.des += framesize;
293 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
295 Color::color pen_color = editing(pi.base.bv)?
296 Color::mathframe : Color::background;
298 int const t = x + width() - 1;
299 int const d = y + descent();
300 pi.pain.line(x, d - 3, x, d, pen_color);
301 pi.pain.line(t, d - 3, t, d, pen_color);
302 pi.pain.line(x, d, x + 3, d, pen_color);
303 pi.pain.line(t - 3, d, t, d, pen_color);
304 setPosCache(pi, x, y);
308 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
310 Color::color pen_color = editing(pi.base.bv)?
311 Color::mathframe : Color::background;
313 drawMarkers(pi, x, y);
314 int const t = x + width() - 1;
315 int const a = y - ascent();
316 pi.pain.line(x, a + 3, x, a, pen_color);
317 pi.pain.line(t, a + 3, t, a, pen_color);
318 pi.pain.line(x, a, x + 3, a, pen_color);
319 pi.pain.line(t - 3, a, t, a, pen_color);
320 setPosCache(pi, x, y);
324 bool Inset::editing(BufferView * bv) const
326 return bv->cursor().isInside(this);
330 int Inset::xo(BufferView const & bv) const
332 return bv.coordCache().getInsets().x(this);
336 int Inset::yo(BufferView const & bv) const
338 return bv.coordCache().getInsets().y(this);
342 bool Inset::covers(BufferView const & bv, int x, int y) const
344 //lyxerr << "Inset::covers, x: " << x << " y: " << y
345 // << " xo: " << xo(bv) << " yo: " << yo()
346 // << " x1: " << xo(bv) << " x2: " << xo() + width()
347 // << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent()
349 return bv.coordCache().getInsets().has(this)
351 && x <= xo(bv) + width()
352 && y >= yo(bv) - ascent()
353 && y <= yo(bv) + descent();
357 void Inset::dump() const
359 Buffer buf("foo", 1);
364 void Inset::setBackgroundColor(Color_color color)
366 background_color_ = color;
370 Color_color Inset::backgroundColor() const
372 return Color::color(background_color_);
376 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
378 //lyxerr << "Inset:: position cache to " << x << " " << y << std::endl;
379 pi.base.bv->coordCache().insets().add(this, x, y);
383 /////////////////////////////////////////
385 bool isEditableInset(Inset const * inset)
387 return inset && inset->editable();
391 bool isHighlyEditableInset(Inset const * inset)
393 return inset && inset->editable() == Inset::HIGHLY_EDITABLE;