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>
37 using lyx::odocstream;
44 InsetName(std::string const & n, InsetBase::Code c)
51 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
54 TranslatorMap const build_translator()
56 InsetName const insetnames[] = {
57 InsetName("toc", InsetBase::TOC_CODE),
58 InsetName("quote", InsetBase::QUOTE_CODE),
59 InsetName("ref", InsetBase::REF_CODE),
60 InsetName("url", InsetBase::URL_CODE),
61 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
62 InsetName("separator", InsetBase::SEPARATOR_CODE),
63 InsetName("ending", InsetBase::ENDING_CODE),
64 InsetName("label", InsetBase::LABEL_CODE),
65 InsetName("note", InsetBase::NOTE_CODE),
66 InsetName("accent", InsetBase::ACCENT_CODE),
67 InsetName("math", InsetBase::MATH_CODE),
68 InsetName("index", InsetBase::INDEX_CODE),
69 InsetName("include", InsetBase::INCLUDE_CODE),
70 InsetName("graphics", InsetBase::GRAPHICS_CODE),
71 InsetName("bibitem", InsetBase::BIBITEM_CODE),
72 InsetName("bibtex", InsetBase::BIBTEX_CODE),
73 InsetName("text", InsetBase::TEXT_CODE),
74 InsetName("ert", InsetBase::ERT_CODE),
75 InsetName("foot", InsetBase::FOOT_CODE),
76 InsetName("margin", InsetBase::MARGIN_CODE),
77 InsetName("float", InsetBase::FLOAT_CODE),
78 InsetName("wrap", InsetBase::WRAP_CODE),
79 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
80 InsetName("tabular", InsetBase::TABULAR_CODE),
81 InsetName("external", InsetBase::EXTERNAL_CODE),
82 InsetName("caption", InsetBase::CAPTION_CODE),
83 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
84 InsetName("cite", InsetBase::CITE_CODE),
85 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
86 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
87 InsetName("optarg", InsetBase::OPTARG_CODE),
88 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
89 InsetName("hfill", InsetBase::HFILL_CODE),
90 InsetName("newline", InsetBase::NEWLINE_CODE),
91 InsetName("line", InsetBase::LINE_CODE),
92 InsetName("branch", InsetBase::BRANCH_CODE),
93 InsetName("box", InsetBase::BOX_CODE),
94 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
95 InsetName("vspace", InsetBase::VSPACE_CODE),
96 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
99 std::size_t const insetnames_size =
100 sizeof(insetnames) / sizeof(insetnames[0]);
102 std::map<std::string, InsetBase::Code> data;
103 for (std::size_t i = 0; i != insetnames_size; ++i) {
104 InsetName const & var = insetnames[i];
105 data[var.name] = var.code;
114 InsetBase::InsetBase()
118 InsetBase::InsetBase(InsetBase const &)
122 std::auto_ptr<InsetBase> InsetBase::clone() const
124 std::auto_ptr<InsetBase> b = doClone();
125 BOOST_ASSERT(typeid(*b) == typeid(*this));
130 InsetBase::Code InsetBase::translate(std::string const & name)
132 static TranslatorMap const translator = build_translator();
134 TranslatorMap::const_iterator it = translator.find(name);
135 return it == translator.end() ? NO_CODE : it->second;
139 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
143 doDispatch(cur, cmd);
147 void InsetBase::doDispatch(LCursor & cur, FuncRequest &)
154 bool InsetBase::getStatus(LCursor &, FuncRequest const & cmd,
155 FuncStatus & flag) const
157 // LFUN_INSET_APPLY is sent from the dialogs when the data should
158 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
159 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
160 // not belong to us, i. e. the dialog was open, and the user moved
161 // the cursor in our inset) in LyXFunc::getStatus().
162 // Dialogs::checkStatus() ensures that the dialog is deactivated if
163 // LFUN_INSET_APPLY is disabled.
165 switch (cmd.action) {
166 case LFUN_INSET_MODIFY:
167 // Allow modification of our data.
168 // This needs to be handled in the doDispatch method of our
169 // instantiatable children.
173 case LFUN_INSET_INSERT:
174 // Don't allow insertion of new insets.
175 // Every inset that wants to allow new insets from open
176 // dialogs needs to override this.
186 void InsetBase::edit(LCursor &, bool)
188 lyxerr[Debug::INSETS] << BOOST_CURRENT_FUNCTION
189 << ": edit left/right" << std::endl;
193 InsetBase * InsetBase::editXY(LCursor &, int x, int y)
195 lyxerr[Debug::INSETS] << BOOST_CURRENT_FUNCTION
196 << ": x=" << x << " y= " << y
202 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
205 lyxerr << BOOST_CURRENT_FUNCTION
206 << ": illegal row: " << row << std::endl;
208 lyxerr << BOOST_CURRENT_FUNCTION
209 << ": illegal col: " << col << std::endl;
214 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
216 return from <= idx && idx <= to;
220 bool InsetBase::idxUpDown(LCursor &, bool) const
226 int InsetBase::plaintext(Buffer const &,
227 odocstream &, OutputParams const &) const
233 int InsetBase::docbook(Buffer const &,
234 std::ostream &, OutputParams const &) const
240 bool InsetBase::directWrite() const
246 InsetBase::EDITABLE InsetBase::editable() const
252 bool InsetBase::autoDelete() const
258 lyx::docstring const InsetBase::editMessage() const
260 return _("Opened inset");
264 std::string const & InsetBase::getInsetName() const
266 static std::string const name = "unknown";
271 void InsetBase::markErased(bool)
275 void InsetBase::cursorPos(BufferView const & bv, CursorSlice const &,
276 bool, int & x, int & y) const
278 lyxerr << "InsetBase::cursorPos called directly" << std::endl;
284 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
286 dim.wid += 2 * framesize;
287 dim.asc += framesize;
291 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
293 dim.wid += 2 * framesize;
294 dim.asc += framesize;
295 dim.des += framesize;
299 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
301 if (!editing(pi.base.bv))
303 int const t = x + width() - 1;
304 int const d = y + descent();
305 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
306 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
307 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
308 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
309 setPosCache(pi, x, y);
313 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
315 if (!editing(pi.base.bv))
317 drawMarkers(pi, x, y);
318 int const t = x + width() - 1;
319 int const a = y - ascent();
320 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
321 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
322 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
323 pi.pain.line(t - 3, a, t, a, LColor::mathframe);
324 setPosCache(pi, x, y);
328 bool InsetBase::editing(BufferView * bv) const
330 return bv->cursor().isInside(this);
334 int InsetBase::xo(BufferView & bv) const
336 return bv.coordCache().getInsets().x(this);
340 int InsetBase::yo(BufferView & bv) const
342 return bv.coordCache().getInsets().y(this);
346 bool InsetBase::covers(BufferView & bv, int x, int y) const
348 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
349 // << " xo: " << xo(bv) << " yo: " << yo()
350 // << " x1: " << xo(bv) << " x2: " << xo() + width()
351 // << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent()
353 return bv.coordCache().getInsets().has(this)
355 && x <= xo(bv) + width()
356 && y >= yo(bv) - ascent()
357 && y <= yo(bv) + descent();
361 void InsetBase::dump() const
363 Buffer buf("foo", 1);
368 /////////////////////////////////////////
370 bool isEditableInset(InsetBase const * inset)
372 return inset && inset->editable();
376 bool isHighlyEditableInset(InsetBase const * inset)
378 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;