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"
35 InsetName(std::string const & n, InsetBase::Code c)
42 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
45 TranslatorMap const build_translator()
47 InsetName const insetnames[] = {
48 InsetName("toc", InsetBase::TOC_CODE),
49 InsetName("quote", InsetBase::QUOTE_CODE),
50 InsetName("ref", InsetBase::REF_CODE),
51 InsetName("url", InsetBase::URL_CODE),
52 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
53 InsetName("separator", InsetBase::SEPARATOR_CODE),
54 InsetName("ending", InsetBase::ENDING_CODE),
55 InsetName("label", InsetBase::LABEL_CODE),
56 InsetName("note", InsetBase::NOTE_CODE),
57 InsetName("accent", InsetBase::ACCENT_CODE),
58 InsetName("math", InsetBase::MATH_CODE),
59 InsetName("index", InsetBase::INDEX_CODE),
60 InsetName("include", InsetBase::INCLUDE_CODE),
61 InsetName("graphics", InsetBase::GRAPHICS_CODE),
62 InsetName("bibitem", InsetBase::BIBITEM_CODE),
63 InsetName("bibtex", InsetBase::BIBTEX_CODE),
64 InsetName("text", InsetBase::TEXT_CODE),
65 InsetName("ert", InsetBase::ERT_CODE),
66 InsetName("foot", InsetBase::FOOT_CODE),
67 InsetName("margin", InsetBase::MARGIN_CODE),
68 InsetName("float", InsetBase::FLOAT_CODE),
69 InsetName("wrap", InsetBase::WRAP_CODE),
70 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
71 InsetName("tabular", InsetBase::TABULAR_CODE),
72 InsetName("external", InsetBase::EXTERNAL_CODE),
73 InsetName("caption", InsetBase::CAPTION_CODE),
74 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
75 InsetName("error", InsetBase::ERROR_CODE),
76 InsetName("cite", InsetBase::CITE_CODE),
77 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
78 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
79 InsetName("optarg", InsetBase::OPTARG_CODE),
80 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
81 InsetName("hfill", InsetBase::HFILL_CODE),
82 InsetName("newline", InsetBase::NEWLINE_CODE),
83 InsetName("line", InsetBase::LINE_CODE),
84 InsetName("branch", InsetBase::BRANCH_CODE),
85 InsetName("box", InsetBase::BOX_CODE),
86 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
87 InsetName("vspace", InsetBase::VSPACE_CODE),
88 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
91 std::size_t const insetnames_size =
92 sizeof(insetnames) / sizeof(insetnames[0]);
94 std::map<std::string, InsetBase::Code> data;
95 for (std::size_t i = 0; i != insetnames_size; ++i) {
96 InsetName const & var = insetnames[i];
97 data[var.name] = var.code;
106 InsetBase::InsetBase()
110 InsetBase::InsetBase(InsetBase const &)
114 InsetBase::Code InsetBase::translate(std::string const & name)
116 static TranslatorMap const translator = build_translator();
118 TranslatorMap::const_iterator it = translator.find(name);
119 return it == translator.end() ? NO_CODE : it->second;
123 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
127 priv_dispatch(cur, cmd);
131 void InsetBase::priv_dispatch(LCursor & cur, FuncRequest &)
138 bool InsetBase::getStatus(LCursor &, FuncRequest const &, FuncStatus &) const
144 void InsetBase::edit(LCursor &, bool)
146 lyxerr << "InsetBase: edit left/right" << std::endl;
150 InsetBase * InsetBase::editXY(LCursor &, int x, int y) const
152 lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
153 return const_cast<InsetBase*>(this);
157 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
160 lyxerr << "illegal row: " << row << std::endl;
162 lyxerr << "illegal col: " << col << std::endl;
167 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
169 return from <= idx && idx <= to;
173 bool InsetBase::idxUpDown(LCursor &, bool) const
179 bool InsetBase::idxUpDown2(LCursor &, bool) const
185 int InsetBase::plaintext(Buffer const &,
186 std::ostream &, OutputParams const &) const
192 int InsetBase::linuxdoc(Buffer const &,
193 std::ostream &, OutputParams const &) const
199 int InsetBase::docbook(Buffer const &,
200 std::ostream &, OutputParams const &) const
206 bool InsetBase::directWrite() const
212 InsetBase::EDITABLE InsetBase::editable() const
218 bool InsetBase::autoDelete() const
224 std::string const InsetBase::editMessage() const
226 return _("Opened inset");
230 std::string const & InsetBase::getInsetName() const
232 static std::string const name = "unknown";
237 void InsetBase::markErased()
241 void InsetBase::getCursorPos(LCursor const &, int & x, int & y) const
243 lyxerr << "InsetBase::getCursorPos called directly" << std::endl;
249 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
251 dim.wid += 2 * framesize;
252 dim.asc += framesize;
256 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
258 dim.wid += 2 * framesize;
259 dim.asc += framesize;
260 dim.des += framesize;
264 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
266 if (!editing(pi.base.bv))
268 int const t = x + width() - 1;
269 int const d = y + descent();
270 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
271 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
272 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
273 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
274 setPosCache(pi, x, y);
278 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
280 if (!editing(pi.base.bv))
282 drawMarkers(pi, x, y);
283 int const t = x + width() - 1;
284 int const a = y - ascent();
285 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
286 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
287 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
288 pi.pain.line(t - 3, a, t, a, LColor::mathframe);
289 setPosCache(pi, x, y);
293 bool InsetBase::editing(BufferView * bv) const
295 return bv->cursor().isInside(this);
299 int InsetBase::xo() const
301 return theCoords.insets_.x(this);
305 int InsetBase::yo() const
307 return theCoords.insets_.y(this);
311 bool InsetBase::covers(int x, int y) const
313 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
314 // << " xo: " << xo() << " yo: " << yo()
315 // << " x1: " << xo() << " x2: " << xo() + width()
316 // << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
318 return theCoords.insets_.has(this)
320 && x <= xo() + width()
321 && y >= yo() - ascent()
322 && y <= yo() + descent();
326 void InsetBase::dump() const
328 Buffer buf("foo", 1);
333 /////////////////////////////////////////
335 bool isEditableInset(InsetBase const * inset)
337 return inset && inset->editable();
341 bool isHighlyEditableInset(InsetBase const * inset)
343 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;