3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Lars Gullik Bjønnes
9 * \author Matthias Ettrich
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
19 #include "buffer_funcs.h"
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "CoordCache.h"
26 #include "Dimension.h"
27 #include "DispatchResult.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "MetricsInfo.h"
32 #include "TextClass.h"
34 #include "frontends/Application.h"
35 #include "frontends/Painter.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/ExceptionMessage.h"
40 #include "support/gettext.h"
41 #include "support/lassert.h"
46 using namespace lyx::support;
52 InsetName(string const & n, InsetCode c) : name(n), code(c) {}
58 typedef map<string, InsetCode> TranslatorMap;
61 static TranslatorMap const build_translator()
63 InsetName const insetnames[] = {
64 InsetName("toc", TOC_CODE),
65 InsetName("quote", QUOTE_CODE),
66 InsetName("ref", REF_CODE),
67 InsetName("href", HYPERLINK_CODE),
68 InsetName("separator", SEPARATOR_CODE),
69 InsetName("ending", ENDING_CODE),
70 InsetName("label", LABEL_CODE),
71 InsetName("note", NOTE_CODE),
72 InsetName("phantom", PHANTOM_CODE),
73 InsetName("accent", ACCENT_CODE),
74 InsetName("math", MATH_CODE),
75 InsetName("index", INDEX_CODE),
76 InsetName("nomenclature", NOMENCL_CODE),
77 InsetName("include", INCLUDE_CODE),
78 InsetName("graphics", GRAPHICS_CODE),
79 InsetName("bibitem", BIBITEM_CODE),
80 InsetName("bibtex", BIBTEX_CODE),
81 InsetName("text", TEXT_CODE),
82 InsetName("ert", ERT_CODE),
83 InsetName("foot", FOOT_CODE),
84 InsetName("margin", MARGIN_CODE),
85 InsetName("float", FLOAT_CODE),
86 InsetName("wrap", WRAP_CODE),
87 InsetName("specialchar", SPECIALCHAR_CODE),
88 InsetName("tabular", TABULAR_CODE),
89 InsetName("external", EXTERNAL_CODE),
90 InsetName("caption", CAPTION_CODE),
91 InsetName("mathmacro", MATHMACRO_CODE),
92 InsetName("citation", CITE_CODE),
93 InsetName("floatlist", FLOAT_LIST_CODE),
94 InsetName("index_print", INDEX_PRINT_CODE),
95 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
96 InsetName("optarg", OPTARG_CODE),
97 InsetName("newline", NEWLINE_CODE),
98 InsetName("line", LINE_CODE),
99 InsetName("branch", BRANCH_CODE),
100 InsetName("box", BOX_CODE),
101 InsetName("flex", FLEX_CODE),
102 InsetName("space", SPACE_CODE),
103 InsetName("vspace", VSPACE_CODE),
104 InsetName("mathmacroarg", MATHMACROARG_CODE),
105 InsetName("listings", LISTINGS_CODE),
106 InsetName("info", INFO_CODE),
107 InsetName("collapsable", COLLAPSABLE_CODE),
108 InsetName("newpage", NEWPAGE_CODE),
109 InsetName("tablecell", CELL_CODE)
112 size_t const insetnames_size =
113 sizeof(insetnames) / sizeof(insetnames[0]);
115 map<string, InsetCode> data;
116 for (size_t i = 0; i != insetnames_size; ++i) {
117 InsetName const & var = insetnames[i];
118 data[var.name] = var.code;
125 void Inset::setBuffer(Buffer & buffer)
131 Buffer & Inset::buffer()
135 lyxerr << "LyX Code: " << lyxCode() << " name: " << name() << std::endl;
136 s << "LyX Code: " << lyxCode() << " name: " << name();
137 LASSERT(false, /**/);
138 throw ExceptionMessage(BufferException,
139 from_ascii("Inset::buffer_ member not initialized!"), s.str());
145 Buffer const & Inset::buffer() const
147 return const_cast<Inset *>(this)->buffer();
151 bool Inset::isBufferValid() const
153 return buffer_ && theBufferList().isLoaded(buffer_);
157 docstring Inset::name() const
159 return from_ascii("unknown");
163 void Inset::initView()
166 buffer().updateLabels();
170 docstring Inset::toolTip(BufferView const &, int, int) const
176 docstring Inset::contextMenu(BufferView const &, int, int) const
182 Dimension const Inset::dimension(BufferView const & bv) const
184 return bv.coordCache().getInsets().dim(this);
188 InsetCode insetCode(string const & name)
190 static TranslatorMap const translator = build_translator();
192 TranslatorMap::const_iterator it = translator.find(name);
193 return it == translator.end() ? NO_CODE : it->second;
197 string insetName(InsetCode c)
199 static TranslatorMap const translator = build_translator();
201 TranslatorMap::const_iterator it = translator.begin();
202 TranslatorMap::const_iterator end = translator.end();
203 for (; it != end; ++it) {
211 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
213 cur.updateFlags(Update::Force | Update::FitCursor);
215 doDispatch(cur, cmd);
219 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
221 switch (cmd.action) {
222 case LFUN_MOUSE_RELEASE:
223 // if the derived inset did not explicitly handle mouse_release,
224 // we assume we request the settings dialog
225 if (!cur.selection() && cmd.button() == mouse_button::button1
227 FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
228 dispatch(cur, tmpcmd);
232 case LFUN_INSET_SETTINGS:
233 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
234 showInsetDialog(&cur.bv());
248 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
249 FuncStatus & flag) const
251 // LFUN_INSET_APPLY is sent from the dialogs when the data should
252 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
253 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
254 // not belong to us, i. e. the dialog was open, and the user moved
255 // the cursor in our inset) in LyXFunc::getStatus().
256 // Dialogs::checkStatus() ensures that the dialog is deactivated if
257 // LFUN_INSET_APPLY is disabled.
259 switch (cmd.action) {
260 case LFUN_INSET_MODIFY:
261 // Allow modification of our data.
262 // This needs to be handled in the doDispatch method of our
263 // instantiatable children.
264 flag.setEnabled(true);
267 case LFUN_INSET_INSERT:
268 // Don't allow insertion of new insets.
269 // Every inset that wants to allow new insets from open
270 // dialogs needs to override this.
271 flag.setEnabled(false);
274 case LFUN_INSET_SETTINGS:
275 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
276 bool const enable = hasSettings();
277 flag.setEnabled(enable);
280 flag.setEnabled(false);
291 void Inset::edit(Cursor &, bool, EntryDirection)
293 LYXERR(Debug::INSETS, "edit left/right");
297 Inset * Inset::editXY(Cursor &, int x, int y)
299 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
304 Inset::idx_type Inset::index(row_type row, col_type col) const
307 LYXERR0("illegal row: " << row);
309 LYXERR0("illegal col: " << col);
314 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
316 return from <= idx && idx <= to;
320 bool Inset::idxUpDown(Cursor &, bool) const
326 int Inset::docbook(odocstream &, OutputParams const &) const
332 void Inset::xhtml(odocstream & od, OutputParams const &) const
334 od << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
337 bool Inset::directWrite() const
343 bool Inset::editable() const
349 bool Inset::hasSettings() const
356 bool Inset::autoDelete() const
362 docstring Inset::editMessage() const
364 return _("Opened inset");
368 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
369 bool, int & x, int & y) const
371 LYXERR0("Inset::cursorPos called directly");
377 void Inset::metricsMarkers(Dimension & dim, int framesize) const
379 dim.wid += 2 * framesize;
380 dim.des += framesize;
384 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
386 dim.wid += 2 * framesize;
387 dim.asc += framesize;
388 dim.des += framesize;
392 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
394 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
395 Color_mathframe : Color_mathcorners;
397 Dimension const dim = dimension(*pi.base.bv);
399 int const t = x + dim.width() - 1;
400 int const d = y + dim.descent();
401 pi.pain.line(x, d - 3, x, d, pen_color);
402 pi.pain.line(t, d - 3, t, d, pen_color);
403 pi.pain.line(x, d, x + 3, d, pen_color);
404 pi.pain.line(t - 3, d, t, d, pen_color);
405 setPosCache(pi, x, y);
409 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
411 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
412 Color_mathframe : Color_mathcorners;
414 drawMarkers(pi, x, y);
415 Dimension const dim = dimension(*pi.base.bv);
416 int const t = x + dim.width() - 1;
417 int const a = y - dim.ascent();
418 pi.pain.line(x, a + 3, x, a, pen_color);
419 pi.pain.line(t, a + 3, t, a, pen_color);
420 pi.pain.line(x, a, x + 3, a, pen_color);
421 pi.pain.line(t - 3, a, t, a, pen_color);
422 setPosCache(pi, x, y);
426 bool Inset::editing(BufferView const * bv) const
428 return bv->cursor().isInside(this);
432 int Inset::xo(BufferView const & bv) const
434 return bv.coordCache().getInsets().x(this);
438 int Inset::yo(BufferView const & bv) const
440 return bv.coordCache().getInsets().y(this);
444 bool Inset::covers(BufferView const & bv, int x, int y) const
446 return bv.coordCache().getInsets().covers(this, x, y);
450 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
452 return bp.documentClass().insetLayout(name());
456 void Inset::dump() const
462 ColorCode Inset::backgroundColor() const
468 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
470 //LYXERR("Inset: set position cache to " << x << " " << y);
471 pi.base.bv->coordCache().insets().add(this, x, y);
475 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
477 mi.base.bv->coordCache().insets().add(this, dim);
481 Buffer const * Inset::updateFrontend() const
483 return theApp() ? theApp()->updateInset(this) : 0;
487 docstring Inset::completionPrefix(Cursor const &) const