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"
39 InsetName(std::string const & n, InsetBase::Code c)
46 typedef std::map<std::string, InsetBase::Code> TranslatorMap;
49 TranslatorMap const build_translator()
51 InsetName const insetnames[] = {
52 InsetName("toc", InsetBase::TOC_CODE),
53 InsetName("quote", InsetBase::QUOTE_CODE),
54 InsetName("ref", InsetBase::REF_CODE),
55 InsetName("url", InsetBase::URL_CODE),
56 InsetName("htmlurl", InsetBase::HTMLURL_CODE),
57 InsetName("separator", InsetBase::SEPARATOR_CODE),
58 InsetName("ending", InsetBase::ENDING_CODE),
59 InsetName("label", InsetBase::LABEL_CODE),
60 InsetName("note", InsetBase::NOTE_CODE),
61 InsetName("accent", InsetBase::ACCENT_CODE),
62 InsetName("math", InsetBase::MATH_CODE),
63 InsetName("index", InsetBase::INDEX_CODE),
64 InsetName("include", InsetBase::INCLUDE_CODE),
65 InsetName("graphics", InsetBase::GRAPHICS_CODE),
66 InsetName("bibitem", InsetBase::BIBITEM_CODE),
67 InsetName("bibtex", InsetBase::BIBTEX_CODE),
68 InsetName("text", InsetBase::TEXT_CODE),
69 InsetName("ert", InsetBase::ERT_CODE),
70 InsetName("foot", InsetBase::FOOT_CODE),
71 InsetName("margin", InsetBase::MARGIN_CODE),
72 InsetName("float", InsetBase::FLOAT_CODE),
73 InsetName("wrap", InsetBase::WRAP_CODE),
74 InsetName("specialchar", InsetBase::SPECIALCHAR_CODE),
75 InsetName("tabular", InsetBase::TABULAR_CODE),
76 InsetName("external", InsetBase::EXTERNAL_CODE),
77 InsetName("caption", InsetBase::CAPTION_CODE),
78 InsetName("mathmacro", InsetBase::MATHMACRO_CODE),
79 InsetName("error", InsetBase::ERROR_CODE),
80 InsetName("cite", InsetBase::CITE_CODE),
81 InsetName("float_list", InsetBase::FLOAT_LIST_CODE),
82 InsetName("index_print", InsetBase::INDEX_PRINT_CODE),
83 InsetName("optarg", InsetBase::OPTARG_CODE),
84 InsetName("environment", InsetBase::ENVIRONMENT_CODE),
85 InsetName("hfill", InsetBase::HFILL_CODE),
86 InsetName("newline", InsetBase::NEWLINE_CODE),
87 InsetName("line", InsetBase::LINE_CODE),
88 InsetName("branch", InsetBase::BRANCH_CODE),
89 InsetName("box", InsetBase::BOX_CODE),
90 InsetName("charstyle", InsetBase::CHARSTYLE_CODE),
91 InsetName("vspace", InsetBase::VSPACE_CODE),
92 InsetName("mathmacroarg", InsetBase::MATHMACROARG_CODE),
95 std::size_t const insetnames_size =
96 sizeof(insetnames) / sizeof(insetnames[0]);
98 std::map<std::string, InsetBase::Code> data;
99 for (std::size_t i = 0; i != insetnames_size; ++i) {
100 InsetName const & var = insetnames[i];
101 data[var.name] = var.code;
110 InsetBase::InsetBase()
114 InsetBase::InsetBase(InsetBase const &)
118 std::auto_ptr<InsetBase> InsetBase::clone() const
120 std::auto_ptr<InsetBase> b = doClone();
121 BOOST_ASSERT(typeid(*b) == typeid(*this));
126 InsetBase::Code InsetBase::translate(std::string const & name)
128 static TranslatorMap const translator = build_translator();
130 TranslatorMap::const_iterator it = translator.find(name);
131 return it == translator.end() ? NO_CODE : it->second;
135 void InsetBase::dispatch(LCursor & cur, FuncRequest & cmd)
139 doDispatch(cur, cmd);
143 void InsetBase::doDispatch(LCursor & cur, FuncRequest &)
150 bool InsetBase::getStatus(LCursor &, FuncRequest const & cmd,
151 FuncStatus & flag) const
153 // LFUN_INSET_APPLY is sent from the dialogs when the data should
154 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
155 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
156 // not belong to us, i. e. the dialog was open, and the user moved
157 // the cursor in our inset) in LyXFunc::getStatus().
158 // Dialogs::checkStatus() ensures that the dialog is deactivated if
159 // LFUN_INSET_APPLY is disabled.
161 switch (cmd.action) {
162 case LFUN_INSET_MODIFY:
163 // Allow modification of our data.
164 // This needs to be handled in the doDispatch method of our
165 // instantiatable children.
169 case LFUN_INSET_INSERT:
170 // Don't allow insertion of new insets.
171 // Every inset that wants to allow new insets from open
172 // dialogs needs to override this.
182 void InsetBase::edit(LCursor &, bool)
184 lyxerr << "InsetBase: edit left/right" << std::endl;
188 InsetBase * InsetBase::editXY(LCursor &, int x, int y)
190 lyxerr << "InsetBase: editXY x:" << x << " y: " << y << std::endl;
195 InsetBase::idx_type InsetBase::index(row_type row, col_type col) const
198 lyxerr << "illegal row: " << row << std::endl;
200 lyxerr << "illegal col: " << col << std::endl;
205 bool InsetBase::idxBetween(idx_type idx, idx_type from, idx_type to) const
207 return from <= idx && idx <= to;
211 bool InsetBase::idxUpDown(LCursor &, bool) const
217 int InsetBase::plaintext(Buffer const &,
218 std::ostream &, OutputParams const &) const
224 int InsetBase::linuxdoc(Buffer const &,
225 std::ostream &, OutputParams const &) const
231 int InsetBase::docbook(Buffer const &,
232 std::ostream &, OutputParams const &) const
238 bool InsetBase::directWrite() const
244 InsetBase::EDITABLE InsetBase::editable() const
250 bool InsetBase::autoDelete() const
256 std::string const InsetBase::editMessage() const
258 return _("Opened inset");
262 std::string const & InsetBase::getInsetName() const
264 static std::string const name = "unknown";
269 void InsetBase::markErased()
273 void InsetBase::cursorPos(CursorSlice const &, bool, int & x, int & y) const
275 lyxerr << "InsetBase::cursorPos called directly" << std::endl;
281 void InsetBase::metricsMarkers(Dimension & dim, int framesize) const
283 dim.wid += 2 * framesize;
284 dim.asc += framesize;
288 void InsetBase::metricsMarkers2(Dimension & dim, int framesize) const
290 dim.wid += 2 * framesize;
291 dim.asc += framesize;
292 dim.des += framesize;
296 void InsetBase::drawMarkers(PainterInfo & pi, int x, int y) const
298 if (!editing(pi.base.bv))
300 int const t = x + width() - 1;
301 int const d = y + descent();
302 pi.pain.line(x, d - 3, x, d, LColor::mathframe);
303 pi.pain.line(t, d - 3, t, d, LColor::mathframe);
304 pi.pain.line(x, d, x + 3, d, LColor::mathframe);
305 pi.pain.line(t - 3, d, t, d, LColor::mathframe);
306 setPosCache(pi, x, y);
310 void InsetBase::drawMarkers2(PainterInfo & pi, int x, int y) const
312 if (!editing(pi.base.bv))
314 drawMarkers(pi, x, y);
315 int const t = x + width() - 1;
316 int const a = y - ascent();
317 pi.pain.line(x, a + 3, x, a, LColor::mathframe);
318 pi.pain.line(t, a + 3, t, a, LColor::mathframe);
319 pi.pain.line(x, a, x + 3, a, LColor::mathframe);
320 pi.pain.line(t - 3, a, t, a, LColor::mathframe);
321 setPosCache(pi, x, y);
325 bool InsetBase::editing(BufferView * bv) const
327 return bv->cursor().isInside(this);
331 int InsetBase::xo() const
333 return theCoords.getInsets().x(this);
337 int InsetBase::yo() const
339 return theCoords.getInsets().y(this);
343 bool InsetBase::covers(int x, int y) const
345 //lyxerr << "InsetBase::covers, x: " << x << " y: " << y
346 // << " xo: " << xo() << " yo: " << yo()
347 // << " x1: " << xo() << " x2: " << xo() + width()
348 // << " y1: " << yo() - ascent() << " y2: " << yo() + descent()
350 return theCoords.getInsets().has(this)
352 && x <= xo() + width()
353 && y >= yo() - ascent()
354 && y <= yo() + descent();
358 void InsetBase::dump() const
360 Buffer buf("foo", 1);
365 /////////////////////////////////////////
367 bool isEditableInset(InsetBase const * inset)
369 return inset && inset->editable();
373 bool isHighlyEditableInset(InsetBase const * inset)
375 return inset && inset->editable() == InsetBase::HIGHLY_EDITABLE;