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("include", InsetBase::INCLUDE_CODE),
67 InsetName("graphics", InsetBase::GRAPHICS_CODE),
68 InsetName("bibitem", InsetBase::BIBITEM_CODE),
69 InsetName("bibtex", InsetBase::BIBTEX_CODE),
70 InsetName("text", InsetBase::TEXT_CODE),
71 InsetName("ert", InsetBase::ERT_CODE),
72 InsetName("foot", InsetBase::FOOT_CODE),
73 InsetName("margin", InsetBase::MARGIN_CODE),
74 InsetName("float", InsetBase::FLOAT_CODE),
75 InsetName("wrap", InsetBase::WRAP_CODE),
76 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
77 InsetName("tabular", InsetBase::TABULAR_CODE),
78 InsetName("external", InsetBase::EXTERNAL_CODE),
79 InsetName("caption", InsetBase::CAPTION_CODE),
80 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
81 InsetName("cite", InsetBase::CITE_CODE),
82 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
83 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
84 InsetName("optarg", InsetBase::OPTARG_CODE),
85 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
86 InsetName("hfill", InsetBase::HFILL_CODE),
87 InsetName("newline", InsetBase::NEWLINE_CODE),
88 InsetName("line", InsetBase::LINE_CODE),
89 InsetName("branch", InsetBase::BRANCH_CODE),
90 InsetName("box", InsetBase::BOX_CODE),
91 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
92 InsetName("vspace", InsetBase::VSPACE_CODE),
93 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
96 std::size_t const insetnames_size =
97 sizeof(insetnames) / sizeof(insetnames[0]);
99 std::map<std::string, InsetBase::Code> data;
100 for (std::size_t i = 0; i != insetnames_size; ++i) {
101 InsetName const & var = insetnames[i];
102 data[var.name] = var.code;
109 std::auto_ptr<InsetBase> InsetBase::clone() const
111 std::auto_ptr<InsetBase> b = doClone();
112 BOOST_ASSERT(typeid(*b) == typeid(*this));
117 InsetBase::Code InsetBase::translate(std::string const & name)
119 static TranslatorMap const translator = build_translator();
121 TranslatorMap::const_iterator it = translator.find(name);
122 return it == translator.end() ? NO_CODE : it->second;
126 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
130 doDispatch(cur, cmd);
134 void InsetBase::doDispatch(LCursor & cur, FuncRequest &)
141 bool InsetBase::getStatus(LCursor &, FuncRequest const & cmd,
142 FuncStatus & flag) const
144 // LFUN_INSET_APPLY is sent from the dialogs when the data should
145 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
146 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
147 // not belong to us, i. e. the dialog was open, and the user moved
148 // the cursor in our inset) in LyXFunc::getStatus().
149 // Dialogs::checkStatus() ensures that the dialog is deactivated if
150 // LFUN_INSET_APPLY is disabled.
152 switch (cmd.action) {
153 case LFUN_INSET_MODIFY:
154 // Allow modification of our data.
155 // This needs to be handled in the doDispatch method of our
156 // instantiatable children.
160 case LFUN_INSET_INSERT:
161 // Don't allow insertion of new insets.
162 // Every inset that wants to allow new insets from open
163 // dialogs needs to override this.
173 void InsetBase::edit(LCursor &, bool)
175 lyxerr[Debug::INSETS] << BOOST_CURRENT_FUNCTION
176 << ": edit left/right" << std::endl;
180 InsetBase * InsetBase::editXY(LCursor &, int x, int y)
182 lyxerr[Debug::INSETS] << BOOST_CURRENT_FUNCTION
183 << ": x=" << x << " y= " << y
189 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
192 lyxerr << BOOST_CURRENT_FUNCTION
193 << ": illegal row: " << row << std::endl;
195 lyxerr << BOOST_CURRENT_FUNCTION
196 << ": illegal col: " << col << std::endl;
201 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
203 return from <= idx && idx <= to;
207 bool InsetBase::idxUpDown(LCursor &, bool) const
213 int InsetBase::plaintext(Buffer const &,
214 odocstream &, OutputParams const &) const
220 int InsetBase::docbook(Buffer const &,
221 odocstream &, OutputParams const &) const
227 bool InsetBase::directWrite() const
233 InsetBase::EDITABLE InsetBase::editable() const
239 bool InsetBase::autoDelete() const
245 docstring const InsetBase::editMessage() const
247 return _("Opened inset");
251 docstring const & InsetBase::getInsetName() const
253 static docstring const name = from_ascii("unknown");
258 void InsetBase::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
259 bool, int & x, int & y) const
261 lyxerr << "InsetBase::cursorPos called directly" << std::endl;
267 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
269 dim.wid += 2 * framesize;
270 dim.asc += framesize;
274 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
276 dim.wid += 2 * framesize;
277 dim.asc += framesize;
278 dim.des += framesize;
282 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
284 if (!editing(pi.base.bv))
286 int const t = x + width() - 1;
287 int const d = y + descent();
288 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
289 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
290 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
291 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
292 setPosCache(pi, x, y);
296 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
298 if (!editing(pi.base.bv))
300 drawMarkers(pi, x, y);
301 int const t = x + width() - 1;
302 int const a = y - ascent();
303 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
304 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
305 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
306 pi.pain.line(t - 3, a, t, a, LColor::mathframe);
307 setPosCache(pi, x, y);
311 bool InsetBase::editing(BufferView * bv) const
313 return bv->cursor().isInside(this);
317 int InsetBase::xo(BufferView & bv) const
319 return bv.coordCache().getInsets().x(this);
323 int InsetBase::yo(BufferView & bv) const
325 return bv.coordCache().getInsets().y(this);
329 bool InsetBase::covers(BufferView & bv, int x, int y) const
331 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
332 // << " xo: " << xo(bv) << " yo: " << yo()
333 // << " x1: " << xo(bv) << " x2: " << xo() + width()
334 // << " y1: " << yo(bv) - ascent() << " y2: " << yo() + descent()
336 return bv.coordCache().getInsets().has(this)
338 && x <= xo(bv) + width()
339 && y >= yo(bv) - ascent()
340 && y <= yo(bv) + descent();
344 void InsetBase::dump() const
346 Buffer buf("foo", 1);
351 /////////////////////////////////////////
353 bool isEditableInset(InsetBase const * inset)
355 return inset && inset->editable();
359 bool isHighlyEditableInset(InsetBase const * inset)
361 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;