3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "insetbase.h"
16 #include "coordcache.h"
17 #include "BufferView.h"
21 #include "dimension.h"
22 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "FuncStatus.h"
27 #include "metricsinfo.h"
29 #include "frontends/Painter.h"
31 #include <boost/current_function.hpp>
41 InsetName(std::string const & n, InsetBase::Code c)
48 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
51 static TranslatorMap const build_translator()
53 InsetName const insetnames[] = {
54 InsetName("toc", InsetBase::TOC_CODE),
55 InsetName("quote", InsetBase::QUOTE_CODE),
56 InsetName("ref", InsetBase::REF_CODE),
57 InsetName("url", InsetBase::URL_CODE),
58 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
59 InsetName("separator", InsetBase::SEPARATOR_CODE),
60 InsetName("ending", InsetBase::ENDING_CODE),
61 InsetName("label", InsetBase::LABEL_CODE),
62 InsetName("note", InsetBase::NOTE_CODE),
63 InsetName("accent", InsetBase::ACCENT_CODE),
64 InsetName("math", InsetBase::MATH_CODE),
65 InsetName("index", InsetBase::INDEX_CODE),
66 InsetName("nomenclature", InsetBase::NOMENCL_CODE),
67 InsetName("include", InsetBase::INCLUDE_CODE),
68 InsetName("graphics", InsetBase::GRAPHICS_CODE),
69 InsetName("bibitem", InsetBase::BIBITEM_CODE),
70 InsetName("bibtex", InsetBase::BIBTEX_CODE),
71 InsetName("text", InsetBase::TEXT_CODE),
72 InsetName("ert", InsetBase::ERT_CODE),
73 InsetName("foot", InsetBase::FOOT_CODE),
74 InsetName("margin", InsetBase::MARGIN_CODE),
75 InsetName("float", InsetBase::FLOAT_CODE),
76 InsetName("wrap", InsetBase::WRAP_CODE),
77 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
78 InsetName("tabular", InsetBase::TABULAR_CODE),
79 InsetName("external", InsetBase::EXTERNAL_CODE),
80 InsetName("caption", InsetBase::CAPTION_CODE),
81 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
82 InsetName("cite", InsetBase::CITE_CODE),
83 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
84 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
85 InsetName("nomencl_print", InsetBase::NOMENCL_PRINT_CODE),
86 InsetName("optarg", InsetBase::OPTARG_CODE),
87 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
88 InsetName("hfill", InsetBase::HFILL_CODE),
89 InsetName("newline", InsetBase::NEWLINE_CODE),
90 InsetName("line", InsetBase::LINE_CODE),
91 InsetName("branch", InsetBase::BRANCH_CODE),
92 InsetName("box", InsetBase::BOX_CODE),
93 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
94 InsetName("vspace", InsetBase::VSPACE_CODE),
95 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
98 std::size_t const insetnames_size =
99 sizeof(insetnames) / sizeof(insetnames[0]);
101 std::map<std::string, InsetBase::Code> data;
102 for (std::size_t i = 0; i != insetnames_size; ++i) {
103 InsetName const & var = insetnames[i];
104 data[var.name] = var.code;
111 /// pretty arbitrary dimensions
112 InsetBase::InsetBase(): dim_(10, 10, 10), background_color_(LColor::background)
116 InsetBase::InsetBase(InsetBase const & i)
117 : dim_(i.dim_), background_color_(i.background_color_)
121 std::auto_ptr<InsetBase> InsetBase::clone() const
123 std::auto_ptr<InsetBase> b = doClone();
124 BOOST_ASSERT(typeid(*b) == typeid(*this));
129 InsetBase::Code InsetBase::translate(std::string const & name)
131 static TranslatorMap const translator = build_translator();
133 TranslatorMap::const_iterator it = translator.find(name);
134 return it == translator.end() ? NO_CODE : it->second;
138 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
140 cur.updateFlags(Update::Force | Update::FitCursor);
142 doDispatch(cur, cmd);
146 void InsetBase::doDispatch(LCursor & cur, FuncRequest &)
153 bool InsetBase::getStatus(LCursor &, FuncRequest const & cmd,
154 FuncStatus & flag) const
156 // LFUN_INSET_APPLY is sent from the dialogs when the data should
157 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
158 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
159 // not belong to us, i. e. the dialog was open, and the user moved
160 // the cursor in our inset) in LyXFunc::getStatus().
161 // Dialogs::checkStatus() ensures that the dialog is deactivated if
162 // LFUN_INSET_APPLY is disabled.
164 switch (cmd.action) {
165 case LFUN_INSET_MODIFY:
166 // Allow modification of our data.
167 // This needs to be handled in the doDispatch method of our
168 // instantiatable children.
172 case LFUN_INSET_INSERT:
173 // Don't allow insertion of new insets.
174 // Every inset that wants to allow new insets from open
175 // dialogs needs to override this.
185 void InsetBase::edit(LCursor &, bool)
187 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
188 << ": edit left/right" << std::endl;
192 InsetBase * InsetBase::editXY(LCursor &, int x, int y)
194 LYXERR(Debug::INSETS) << BOOST_CURRENT_FUNCTION
195 << ": x=" << x << " y= " << y
201 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
204 lyxerr << BOOST_CURRENT_FUNCTION
205 << ": illegal row: " << row << std::endl;
207 lyxerr << BOOST_CURRENT_FUNCTION
208 << ": illegal col: " << col << std::endl;
213 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
215 return from <= idx && idx <= to;
219 bool InsetBase::idxUpDown(LCursor &, bool) const
225 int InsetBase::docbook(Buffer const &,
226 odocstream &, OutputParams const &) const
232 bool InsetBase::directWrite() const
238 InsetBase::EDITABLE InsetBase::editable() const
244 bool InsetBase::autoDelete() const
250 docstring const InsetBase::editMessage() const
252 return _("Opened inset");
256 docstring const & InsetBase::getInsetName() const
258 static docstring const name = from_ascii("unknown");
263 void InsetBase::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
264 bool, int & x, int & y) const
266 lyxerr << "InsetBase::cursorPos called directly" << std::endl;
272 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
274 dim.wid += 2 * framesize;
275 dim.asc += framesize;
279 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
281 dim.wid += 2 * framesize;
282 dim.asc += framesize;
283 dim.des += framesize;
287 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
289 LColor::color pen_color = editing(pi.base.bv)?
290 LColor::mathframe : LColor::background;
292 int const t = x + width() - 1;
293 int const d = y + 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 InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
304 LColor::color pen_color = editing(pi.base.bv)?
305 LColor::mathframe : LColor::background;
307 drawMarkers(pi, x, y);
308 int const t = x + width() - 1;
309 int const a = y - 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 InsetBase::editing(BufferView * bv) const
320 return bv->cursor().isInside(this);
324 int InsetBase::xo(BufferView const & bv) const
326 return bv.coordCache().getInsets().x(this);
330 int InsetBase::yo(BufferView const & bv) const
332 return bv.coordCache().getInsets().y(this);
336 bool InsetBase::covers(BufferView const & bv, int x, int y) const
338 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
339 // << " xo: " << xo(bv) << " yo: " << yo()
340 // << " x1: " << xo(bv) << " x2: " << xo() + width()
341 // << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent()
343 return bv.coordCache().getInsets().has(this)
345 && x <= xo(bv) + width()
346 && y >= yo(bv) - ascent()
347 && y <= yo(bv) + descent();
351 void InsetBase::dump() const
353 Buffer buf("foo", 1);
358 void InsetBase::setBackgroundColor(LColor_color color)
360 background_color_ = color;
364 LColor_color InsetBase::backgroundColor() const
366 return LColor::color(background_color_);
370 /////////////////////////////////////////
372 bool isEditableInset(InsetBase const * inset)
374 return inset && inset->editable();
378 bool isHighlyEditableInset(InsetBase const * inset)
380 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;