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"
25 #include "metricsinfo.h"
27 #include "frontends/Painter.h"
36 InsetName(std::string const & n, InsetBase::Code c)
43 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
46 TranslatorMap const build_translator()
48 InsetName const insetnames[] = {
49 InsetName("toc", InsetBase::TOC_CODE),
50 InsetName("quote", InsetBase::QUOTE_CODE),
51 InsetName("ref", InsetBase::REF_CODE),
52 InsetName("url", InsetBase::URL_CODE),
53 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
54 InsetName("separator", InsetBase::SEPARATOR_CODE),
55 InsetName("ending", InsetBase::ENDING_CODE),
56 InsetName("label", InsetBase::LABEL_CODE),
57 InsetName("note", InsetBase::NOTE_CODE),
58 InsetName("accent", InsetBase::ACCENT_CODE),
59 InsetName("math", InsetBase::MATH_CODE),
60 InsetName("index", InsetBase::INDEX_CODE),
61 InsetName("include", InsetBase::INCLUDE_CODE),
62 InsetName("graphics", InsetBase::GRAPHICS_CODE),
63 InsetName("bibitem", InsetBase::BIBITEM_CODE),
64 InsetName("bibtex", InsetBase::BIBTEX_CODE),
65 InsetName("text", InsetBase::TEXT_CODE),
66 InsetName("ert", InsetBase::ERT_CODE),
67 InsetName("foot", InsetBase::FOOT_CODE),
68 InsetName("margin", InsetBase::MARGIN_CODE),
69 InsetName("float", InsetBase::FLOAT_CODE),
70 InsetName("wrap", InsetBase::WRAP_CODE),
71 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
72 InsetName("tabular", InsetBase::TABULAR_CODE),
73 InsetName("external", InsetBase::EXTERNAL_CODE),
74 InsetName("caption", InsetBase::CAPTION_CODE),
75 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
76 InsetName("error", InsetBase::ERROR_CODE),
77 InsetName("cite", InsetBase::CITE_CODE),
78 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
79 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
80 InsetName("optarg", InsetBase::OPTARG_CODE),
81 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
82 InsetName("hfill", InsetBase::HFILL_CODE),
83 InsetName("newline", InsetBase::NEWLINE_CODE),
84 InsetName("line", InsetBase::LINE_CODE),
85 InsetName("branch", InsetBase::BRANCH_CODE),
86 InsetName("box", InsetBase::BOX_CODE),
87 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
88 InsetName("vspace", InsetBase::VSPACE_CODE),
89 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
92 std::size_t const insetnames_size =
93 sizeof(insetnames) / sizeof(insetnames[0]);
95 std::map<std::string, InsetBase::Code> data;
96 for (std::size_t i = 0; i != insetnames_size; ++i) {
97 InsetName const & var = insetnames[i];
98 data[var.name] = var.code;
107 InsetBase::InsetBase()
111 InsetBase::InsetBase(InsetBase const &)
115 InsetBase::Code InsetBase::translate(std::string const & name)
117 static TranslatorMap const translator = build_translator();
119 TranslatorMap::const_iterator it = translator.find(name);
120 return it == translator.end() ? NO_CODE : it->second;
124 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
128 doDispatch(cur, cmd);
132 void InsetBase::doDispatch(LCursor & cur, FuncRequest &)
139 bool InsetBase::getStatus(LCursor &, FuncRequest const &, FuncStatus &) const
145 void InsetBase::edit(LCursor &, bool)
147 lyxerr << "InsetBase: edit left/right" << std::endl;
151 InsetBase * InsetBase::editXY(LCursor &, int x, int y) const
153 lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
154 return const_cast<InsetBase*>(this);
158 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
161 lyxerr << "illegal row: " << row << std::endl;
163 lyxerr << "illegal col: " << col << std::endl;
168 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
170 return from <= idx && idx <= to;
174 bool InsetBase::idxUpDown(LCursor &, bool) const
180 int InsetBase::plaintext(Buffer const &,
181 std::ostream &, OutputParams const &) const
187 int InsetBase::linuxdoc(Buffer const &,
188 std::ostream &, OutputParams const &) const
194 int InsetBase::docbook(Buffer const &,
195 std::ostream &, OutputParams const &) const
201 bool InsetBase::directWrite() const
207 InsetBase::EDITABLE InsetBase::editable() const
213 bool InsetBase::autoDelete() const
219 std::string const InsetBase::editMessage() const
221 return _("Opened inset");
225 std::string const & InsetBase::getInsetName() const
227 static std::string const name = "unknown";
232 void InsetBase::markErased()
236 void InsetBase::getCursorPos(CursorSlice const &, int & x, int & y) const
238 lyxerr << "InsetBase::getCursorPos called directly" << std::endl;
244 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
246 dim.wid += 2 * framesize;
247 dim.asc += framesize;
251 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
253 dim.wid += 2 * framesize;
254 dim.asc += framesize;
255 dim.des += framesize;
259 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
261 if (!editing(pi.base.bv))
263 int const t = x + width() - 1;
264 int const d = y + descent();
265 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
266 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
267 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
268 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
269 setPosCache(pi, x, y);
273 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
275 if (!editing(pi.base.bv))
277 drawMarkers(pi, x, y);
278 int const t = x + width() - 1;
279 int const a = y - ascent();
280 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
281 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
282 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
283 pi.pain.line(t - 3, a, t, a, LColor::mathframe);
284 setPosCache(pi, x, y);
288 bool InsetBase::editing(BufferView * bv) const
290 return bv->cursor().isInside(this);
294 int InsetBase::xo() const
296 return theCoords.getInsets().x(this);
300 int InsetBase::yo() const
302 return theCoords.getInsets().y(this);
306 bool InsetBase::covers(int x, int y) const
308 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
309 // << " xo: " << xo() << " yo: " << yo()
310 // << " x1: " << xo() << " x2: " << xo() + width()
311 // << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
313 return theCoords.getInsets().has(this)
315 && x <= xo() + width()
316 && y >= yo() - ascent()
317 && y <= yo() + descent();
321 void InsetBase::dump() const
323 Buffer buf("foo", 1);
328 /////////////////////////////////////////
330 bool isEditableInset(InsetBase const * inset)
332 return inset && inset->editable();
336 bool isHighlyEditableInset(InsetBase const * inset)
338 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;