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"
38 InsetName(std::string const & n, InsetBase::Code c)
45 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
48 TranslatorMap const build_translator()
50 InsetName const insetnames[] = {
51 InsetName("toc", InsetBase::TOC_CODE),
52 InsetName("quote", InsetBase::QUOTE_CODE),
53 InsetName("ref", InsetBase::REF_CODE),
54 InsetName("url", InsetBase::URL_CODE),
55 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
56 InsetName("separator", InsetBase::SEPARATOR_CODE),
57 InsetName("ending", InsetBase::ENDING_CODE),
58 InsetName("label", InsetBase::LABEL_CODE),
59 InsetName("note", InsetBase::NOTE_CODE),
60 InsetName("accent", InsetBase::ACCENT_CODE),
61 InsetName("math", InsetBase::MATH_CODE),
62 InsetName("index", InsetBase::INDEX_CODE),
63 InsetName("include", InsetBase::INCLUDE_CODE),
64 InsetName("graphics", InsetBase::GRAPHICS_CODE),
65 InsetName("bibitem", InsetBase::BIBITEM_CODE),
66 InsetName("bibtex", InsetBase::BIBTEX_CODE),
67 InsetName("text", InsetBase::TEXT_CODE),
68 InsetName("ert", InsetBase::ERT_CODE),
69 InsetName("foot", InsetBase::FOOT_CODE),
70 InsetName("margin", InsetBase::MARGIN_CODE),
71 InsetName("float", InsetBase::FLOAT_CODE),
72 InsetName("wrap", InsetBase::WRAP_CODE),
73 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
74 InsetName("tabular", InsetBase::TABULAR_CODE),
75 InsetName("external", InsetBase::EXTERNAL_CODE),
76 InsetName("caption", InsetBase::CAPTION_CODE),
77 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
78 InsetName("error", InsetBase::ERROR_CODE),
79 InsetName("cite", InsetBase::CITE_CODE),
80 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
81 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
82 InsetName("optarg", InsetBase::OPTARG_CODE),
83 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
84 InsetName("hfill", InsetBase::HFILL_CODE),
85 InsetName("newline", InsetBase::NEWLINE_CODE),
86 InsetName("line", InsetBase::LINE_CODE),
87 InsetName("branch", InsetBase::BRANCH_CODE),
88 InsetName("box", InsetBase::BOX_CODE),
89 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
90 InsetName("vspace", InsetBase::VSPACE_CODE),
91 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
94 std::size_t const insetnames_size =
95 sizeof(insetnames) / sizeof(insetnames[0]);
97 std::map<std::string, InsetBase::Code> data;
98 for (std::size_t i = 0; i != insetnames_size; ++i) {
99 InsetName const & var = insetnames[i];
100 data[var.name] = var.code;
109 InsetBase::InsetBase()
113 InsetBase::InsetBase(InsetBase const &)
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 << "InsetBase: edit left/right" << std::endl;
179 InsetBase * InsetBase::editXY(LCursor &, int x, int y) const
181 lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
182 return const_cast<InsetBase*>(this);
186 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
189 lyxerr << "illegal row: " << row << std::endl;
191 lyxerr << "illegal col: " << col << std::endl;
196 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
198 return from <= idx && idx <= to;
202 bool InsetBase::idxUpDown(LCursor &, bool) const
208 int InsetBase::plaintext(Buffer const &,
209 std::ostream &, OutputParams const &) const
215 int InsetBase::linuxdoc(Buffer const &,
216 std::ostream &, OutputParams const &) const
222 int InsetBase::docbook(Buffer const &,
223 std::ostream &, OutputParams const &) const
229 bool InsetBase::directWrite() const
235 InsetBase::EDITABLE InsetBase::editable() const
241 bool InsetBase::autoDelete() const
247 std::string const InsetBase::editMessage() const
249 return _("Opened inset");
253 std::string const & InsetBase::getInsetName() const
255 static std::string const name = "unknown";
260 void InsetBase::markErased()
264 void InsetBase::getCursorPos(CursorSlice const &, int & x, int & y) const
266 lyxerr << "InsetBase::getCursorPos 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 if (!editing(pi.base.bv))
291 int const t = x + width() - 1;
292 int const d = y + descent();
293 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
294 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
295 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
296 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
297 setPosCache(pi, x, y);
301 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
303 if (!editing(pi.base.bv))
305 drawMarkers(pi, x, y);
306 int const t = x + width() - 1;
307 int const a = y - ascent();
308 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
309 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
310 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
311 pi.pain.line(t - 3, a, t, a, LColor::mathframe);
312 setPosCache(pi, x, y);
316 bool InsetBase::editing(BufferView * bv) const
318 return bv->cursor().isInside(this);
322 int InsetBase::xo() const
324 return theCoords.getInsets().x(this);
328 int InsetBase::yo() const
330 return theCoords.getInsets().y(this);
334 bool InsetBase::covers(int x, int y) const
336 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
337 // << " xo: " << xo() << " yo: " << yo()
338 // << " x1: " << xo() << " x2: " << xo() + width()
339 // << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
341 return theCoords.getInsets().has(this)
343 && x <= xo() + width()
344 && y >= yo() - ascent()
345 && y <= yo() + descent();
349 void InsetBase::dump() const
351 Buffer buf("foo", 1);
356 /////////////////////////////////////////
358 bool isEditableInset(InsetBase const * inset)
360 return inset && inset->editable();
364 bool isHighlyEditableInset(InsetBase const * inset)
366 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;