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/convert.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/ExceptionMessage.h"
41 #include "support/gettext.h"
42 #include "support/lassert.h"
47 using namespace lyx::support;
53 InsetName(string const & n, InsetCode c) : name(n), code(c) {}
59 typedef map<string, InsetCode> TranslatorMap;
62 static TranslatorMap const build_translator()
64 InsetName const insetnames[] = {
65 InsetName("toc", TOC_CODE),
66 InsetName("quote", QUOTE_CODE),
67 InsetName("ref", REF_CODE),
68 InsetName("href", HYPERLINK_CODE),
69 InsetName("separator", SEPARATOR_CODE),
70 InsetName("ending", ENDING_CODE),
71 InsetName("label", LABEL_CODE),
72 InsetName("note", NOTE_CODE),
73 InsetName("phantom", PHANTOM_CODE),
74 InsetName("accent", ACCENT_CODE),
75 InsetName("math", MATH_CODE),
76 InsetName("index", INDEX_CODE),
77 InsetName("nomenclature", NOMENCL_CODE),
78 InsetName("include", INCLUDE_CODE),
79 InsetName("graphics", GRAPHICS_CODE),
80 InsetName("bibitem", BIBITEM_CODE),
81 InsetName("bibtex", BIBTEX_CODE),
82 InsetName("text", TEXT_CODE),
83 InsetName("ert", ERT_CODE),
84 InsetName("foot", FOOT_CODE),
85 InsetName("margin", MARGIN_CODE),
86 InsetName("float", FLOAT_CODE),
87 InsetName("wrap", WRAP_CODE),
88 InsetName("specialchar", SPECIALCHAR_CODE),
89 InsetName("tabular", TABULAR_CODE),
90 InsetName("external", EXTERNAL_CODE),
91 InsetName("caption", CAPTION_CODE),
92 InsetName("mathmacro", MATHMACRO_CODE),
93 InsetName("citation", CITE_CODE),
94 InsetName("floatlist", FLOAT_LIST_CODE),
95 InsetName("index_print", INDEX_PRINT_CODE),
96 InsetName("nomencl_print", NOMENCL_PRINT_CODE),
97 InsetName("optarg", OPTARG_CODE),
98 InsetName("newline", NEWLINE_CODE),
99 InsetName("line", LINE_CODE),
100 InsetName("branch", BRANCH_CODE),
101 InsetName("box", BOX_CODE),
102 InsetName("flex", FLEX_CODE),
103 InsetName("space", SPACE_CODE),
104 InsetName("vspace", VSPACE_CODE),
105 InsetName("mathmacroarg", MATHMACROARG_CODE),
106 InsetName("listings", LISTINGS_CODE),
107 InsetName("info", INFO_CODE),
108 InsetName("collapsable", COLLAPSABLE_CODE),
109 InsetName("newpage", NEWPAGE_CODE),
110 InsetName("tablecell", CELL_CODE)
113 size_t const insetnames_size =
114 sizeof(insetnames) / sizeof(insetnames[0]);
116 map<string, InsetCode> data;
117 for (size_t i = 0; i != insetnames_size; ++i) {
118 InsetName const & var = insetnames[i];
119 data[var.name] = var.code;
126 void Inset::setBuffer(Buffer & buffer)
132 Buffer & Inset::buffer()
136 lyxerr << "LyX Code: " << lyxCode() << " name: " << name() << std::endl;
137 s << "LyX Code: " << lyxCode() << " name: " << name();
138 LASSERT(false, /**/);
139 throw ExceptionMessage(BufferException,
140 from_ascii("Inset::buffer_ member not initialized!"), s.str());
146 Buffer const & Inset::buffer() const
148 return const_cast<Inset *>(this)->buffer();
152 bool Inset::isBufferValid() const
154 return buffer_ && theBufferList().isLoaded(buffer_);
158 docstring Inset::name() const
160 return from_ascii("unknown");
164 void Inset::initView()
167 buffer().updateLabels();
171 docstring Inset::toolTip(BufferView const &, int, int) const
177 docstring Inset::contextMenu(BufferView const &, int, int) const
183 Dimension const Inset::dimension(BufferView const & bv) const
185 return bv.coordCache().getInsets().dim(this);
189 InsetCode insetCode(string const & name)
191 static TranslatorMap const translator = build_translator();
193 TranslatorMap::const_iterator it = translator.find(name);
194 return it == translator.end() ? NO_CODE : it->second;
198 string insetName(InsetCode c)
200 static TranslatorMap const translator = build_translator();
202 TranslatorMap::const_iterator it = translator.begin();
203 TranslatorMap::const_iterator end = translator.end();
204 for (; it != end; ++it) {
212 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
214 cur.updateFlags(Update::Force | Update::FitCursor);
216 doDispatch(cur, cmd);
220 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
222 switch (cmd.action) {
223 case LFUN_MOUSE_RELEASE:
224 // if the derived inset did not explicitly handle mouse_release,
225 // we assume we request the settings dialog
226 if (!cur.selection() && cmd.button() == mouse_button::button1
228 FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
229 dispatch(cur, tmpcmd);
233 case LFUN_INSET_SETTINGS:
234 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
235 showInsetDialog(&cur.bv());
249 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
250 FuncStatus & flag) const
252 // LFUN_INSET_APPLY is sent from the dialogs when the data should
253 // be applied. This is either changed to LFUN_INSET_MODIFY (if the
254 // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
255 // not belong to us, i. e. the dialog was open, and the user moved
256 // the cursor in our inset) in LyXFunc::getStatus().
257 // Dialogs::checkStatus() ensures that the dialog is deactivated if
258 // LFUN_INSET_APPLY is disabled.
260 switch (cmd.action) {
261 case LFUN_INSET_MODIFY:
262 // Allow modification of our data.
263 // This needs to be handled in the doDispatch method of our
264 // instantiatable children.
265 flag.setEnabled(true);
268 case LFUN_INSET_INSERT:
269 // Don't allow insertion of new insets.
270 // Every inset that wants to allow new insets from open
271 // dialogs needs to override this.
272 flag.setEnabled(false);
275 case LFUN_INSET_SETTINGS:
276 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
277 bool const enable = hasSettings();
278 flag.setEnabled(enable);
281 flag.setEnabled(false);
292 void Inset::edit(Cursor &, bool, EntryDirection)
294 LYXERR(Debug::INSETS, "edit left/right");
298 Inset * Inset::editXY(Cursor &, int x, int y)
300 LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
305 Inset::idx_type Inset::index(row_type row, col_type col) const
308 LYXERR0("illegal row: " << row);
310 LYXERR0("illegal col: " << col);
315 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
317 return from <= idx && idx <= to;
321 bool Inset::idxUpDown(Cursor &, bool) const
327 int Inset::docbook(odocstream &, OutputParams const &) const
333 bool Inset::directWrite() const
339 bool Inset::editable() const
345 bool Inset::hasSettings() const
352 bool Inset::autoDelete() const
358 docstring Inset::editMessage() const
360 return _("Opened inset");
364 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
365 bool, int & x, int & y) const
367 LYXERR0("Inset::cursorPos called directly");
373 void Inset::metricsMarkers(Dimension & dim, int framesize) const
375 dim.wid += 2 * framesize;
376 dim.des += framesize;
380 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
382 dim.wid += 2 * framesize;
383 dim.asc += framesize;
384 dim.des += framesize;
388 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
390 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
391 Color_mathframe : Color_mathcorners;
393 Dimension const dim = dimension(*pi.base.bv);
395 int const t = x + dim.width() - 1;
396 int const d = y + dim.descent();
397 pi.pain.line(x, d - 3, x, d, pen_color);
398 pi.pain.line(t, d - 3, t, d, pen_color);
399 pi.pain.line(x, d, x + 3, d, pen_color);
400 pi.pain.line(t - 3, d, t, d, pen_color);
401 setPosCache(pi, x, y);
405 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
407 ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
408 Color_mathframe : Color_mathcorners;
410 drawMarkers(pi, x, y);
411 Dimension const dim = dimension(*pi.base.bv);
412 int const t = x + dim.width() - 1;
413 int const a = y - dim.ascent();
414 pi.pain.line(x, a + 3, x, a, pen_color);
415 pi.pain.line(t, a + 3, t, a, pen_color);
416 pi.pain.line(x, a, x + 3, a, pen_color);
417 pi.pain.line(t - 3, a, t, a, pen_color);
418 setPosCache(pi, x, y);
422 bool Inset::editing(BufferView const * bv) const
424 return bv->cursor().isInside(this);
428 int Inset::xo(BufferView const & bv) const
430 return bv.coordCache().getInsets().x(this);
434 int Inset::yo(BufferView const & bv) const
436 return bv.coordCache().getInsets().y(this);
440 bool Inset::covers(BufferView const & bv, int x, int y) const
442 return bv.coordCache().getInsets().covers(this, x, y);
446 InsetLayout const & Inset::getLayout(BufferParams const & bp) const
448 return bp.documentClass().insetLayout(name());
452 void Inset::dump() const
458 ColorCode Inset::backgroundColor() const
464 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
466 //LYXERR("Inset: set position cache to " << x << " " << y);
467 pi.base.bv->coordCache().insets().add(this, x, y);
471 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
473 mi.base.bv->coordCache().insets().add(this, dim);
477 Buffer const * Inset::updateFrontend() const
479 return theApp() ? theApp()->updateInset(this) : 0;
483 docstring Inset::completionPrefix(Cursor const &) const