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 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("error", InsetBase::ERROR_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("optarg", InsetBase::OPTARG_CODE),
86 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
87 InsetName("hfill", InsetBase::HFILL_CODE),
88 InsetName("newline", InsetBase::NEWLINE_CODE),
89 InsetName("line", InsetBase::LINE_CODE),
90 InsetName("branch", InsetBase::BRANCH_CODE),
91 InsetName("box", InsetBase::BOX_CODE),
92 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
93 InsetName("vspace", InsetBase::VSPACE_CODE),
94 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
97 std::size_t const insetnames_size =
98 sizeof(insetnames) / sizeof(insetnames[0]);
100 std::map<std::string, InsetBase::Code> data;
101 for (std::size_t i = 0; i != insetnames_size; ++i) {
102 InsetName const & var = insetnames[i];
103 data[var.name] = var.code;
112 InsetBase::InsetBase()
116 InsetBase::InsetBase(InsetBase const &)
120 std::auto_ptr<InsetBase> InsetBase::clone() const
122 std::auto_ptr<InsetBase> b = doClone();
123 BOOST_ASSERT(typeid(*b) == typeid(*this));
128 InsetBase::Code InsetBase::translate(std::string const & name)
130 static TranslatorMap const translator = build_translator();
132 TranslatorMap::const_iterator it = translator.find(name);
133 return it == translator.end() ? NO_CODE : it->second;
137 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
141 doDispatch(cur, cmd);
145 void InsetBase::doDispatch(LCursor & cur, FuncRequest &)
152 bool InsetBase::getStatus(LCursor &, FuncRequest const & cmd,
153 FuncStatus & flag) const
155 // LFUN_INSET_APPLY is sent from the dialogs when the data should
156 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
157 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
158 // not belong to us, i. e. the dialog was open, and the user moved
159 // the cursor in our inset) in LyXFunc::getStatus().
160 // Dialogs::checkStatus() ensures that the dialog is deactivated if
161 // LFUN_INSET_APPLY is disabled.
163 switch (cmd.action) {
164 case LFUN_INSET_MODIFY:
165 // Allow modification of our data.
166 // This needs to be handled in the doDispatch method of our
167 // instantiatable children.
171 case LFUN_INSET_INSERT:
172 // Don't allow insertion of new insets.
173 // Every inset that wants to allow new insets from open
174 // dialogs needs to override this.
184 void InsetBase::edit(LCursor &, bool)
186 lyxerr[Debug::INSETS] << BOOST_CURRENT_FUNCTION
187 << ": edit left/right" << std::endl;
191 InsetBase * InsetBase::editXY(LCursor &, int x, int y)
193 lyxerr[Debug::INSETS] << BOOST_CURRENT_FUNCTION
194 << ": x=" << x << " y= " << y
200 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
203 lyxerr << BOOST_CURRENT_FUNCTION
204 << ": illegal row: " << row << std::endl;
206 lyxerr << BOOST_CURRENT_FUNCTION
207 << ": illegal col: " << col << std::endl;
212 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
214 return from <= idx && idx <= to;
218 bool InsetBase::idxUpDown(LCursor &, bool) const
224 int InsetBase::plaintext(Buffer const &,
225 std::ostream &, OutputParams const &) const
231 int InsetBase::linuxdoc(Buffer const &,
232 std::ostream &, OutputParams const &) const
238 int InsetBase::docbook(Buffer const &,
239 std::ostream &, OutputParams const &) const
245 bool InsetBase::directWrite() const
251 InsetBase::EDITABLE InsetBase::editable() const
257 bool InsetBase::autoDelete() const
263 std::string const InsetBase::editMessage() const
265 return _("Opened inset");
269 std::string const & InsetBase::getInsetName() const
271 static std::string const name = "unknown";
276 void InsetBase::markErased(bool)
280 void InsetBase::cursorPos(CursorSlice const &, bool, int & x, int & y) const
282 lyxerr << "InsetBase::cursorPos called directly" << std::endl;
288 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
290 dim.wid += 2 * framesize;
291 dim.asc += framesize;
295 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
297 dim.wid += 2 * framesize;
298 dim.asc += framesize;
299 dim.des += framesize;
303 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
305 if (!editing(pi.base.bv))
307 int const t = x + width() - 1;
308 int const d = y + descent();
309 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
310 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
311 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
312 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
313 setPosCache(pi, x, y);
317 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
319 if (!editing(pi.base.bv))
321 drawMarkers(pi, x, y);
322 int const t = x + width() - 1;
323 int const a = y - ascent();
324 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
325 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
326 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
327 pi.pain.line(t - 3, a, t, a, LColor::mathframe);
328 setPosCache(pi, x, y);
332 bool InsetBase::editing(BufferView * bv) const
334 return bv->cursor().isInside(this);
338 int InsetBase::xo() const
340 return theCoords.getInsets().x(this);
344 int InsetBase::yo() const
346 return theCoords.getInsets().y(this);
350 bool InsetBase::covers(int x, int y) const
352 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
353 // << " xo: " << xo() << " yo: " << yo()
354 // << " x1: " << xo() << " x2: " << xo() + width()
355 // << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
357 return theCoords.getInsets().has(this)
359 && x <= xo() + width()
360 && y >= yo() - ascent()
361 && y <= yo() + descent();
365 void InsetBase::dump() const
367 Buffer buf("foo", 1);
372 /////////////////////////////////////////
374 bool isEditableInset(InsetBase const * inset)
376 return inset && inset->editable();
380 bool isHighlyEditableInset(InsetBase const * inset)
382 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;