]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
c6fe84e8a0cae168996d26abda3e1dcc906a0b7a
[lyx.git] / src / insets / Inset.cpp
1 /**
2  * \file Inset.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  * \author Matthias Ettrich
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "Inset.h"
18
19 #include "buffer_funcs.h"
20 #include "Buffer.h"
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "BufferView.h"
24 #include "CoordCache.h"
25 #include "Cursor.h"
26 #include "Dimension.h"
27 #include "DispatchResult.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "MetricsInfo.h"
31 #include "Text.h"
32 #include "TextClass.h"
33
34 #include "frontends/Application.h"
35 #include "frontends/Painter.h"
36
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"
42
43 #include <map>
44
45 using namespace std;
46 using namespace lyx::support;
47
48 namespace lyx {
49
50 class InsetName {
51 public:
52         InsetName(string const & n, InsetCode c) : name(n), code(c) {}
53         string name;
54         InsetCode code;
55 };
56
57
58 typedef map<string, InsetCode> TranslatorMap;
59
60
61 static TranslatorMap const build_translator()
62 {
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)
110         };
111
112         size_t const insetnames_size =
113                 sizeof(insetnames) / sizeof(insetnames[0]);
114
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;
119         }
120
121         return data;
122 }
123
124
125 void Inset::setBuffer(Buffer & buffer)
126 {
127         buffer_ = &buffer;
128 }
129
130
131 Buffer & Inset::buffer()
132 {
133         if (!buffer_) {
134                 odocstringstream s;
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());
140         }
141         return *buffer_;
142 }
143
144
145 Buffer const & Inset::buffer() const
146 {
147         return const_cast<Inset *>(this)->buffer();
148 }
149
150
151 bool Inset::isBufferValid() const
152 {
153         return buffer_ && theBufferList().isLoaded(buffer_);
154 }
155
156
157 docstring Inset::name() const
158 {
159         return from_ascii("unknown");
160 }
161
162
163 bool Inset::isFreeSpacing() const 
164 {
165         return getLayout().isFreeSpacing();
166 }
167
168
169 bool Inset::allowEmpty() const
170 {
171         return getLayout().isKeepEmpty();
172 }
173
174 bool Inset::forceLTR() const
175 {
176         return getLayout().forceLTR();
177 }
178
179 void Inset::initView()
180 {
181         if (isLabeled())
182                 buffer().updateLabels();
183 }
184
185
186 docstring Inset::toolTip(BufferView const &, int, int) const
187 {
188         return docstring();
189 }
190
191
192 docstring Inset::contextMenu(BufferView const &, int, int) const
193 {
194         return docstring();
195 }
196
197
198 Dimension const Inset::dimension(BufferView const & bv) const
199 {
200         return bv.coordCache().getInsets().dim(this);
201 }
202
203
204 InsetCode insetCode(string const & name)
205 {
206         static TranslatorMap const translator = build_translator();
207
208         TranslatorMap::const_iterator it = translator.find(name);
209         return it == translator.end() ? NO_CODE : it->second;
210 }
211
212
213 string insetName(InsetCode c) 
214 {
215         static TranslatorMap const translator = build_translator();
216
217         TranslatorMap::const_iterator it =  translator.begin();
218         TranslatorMap::const_iterator end = translator.end();
219         for (; it != end; ++it) {
220                 if (it->second == c)
221                         return it->first;
222         }
223         return string();
224 }
225
226
227 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
228 {
229         //LASSERT(cur.buffer() == &buffer(), return);
230         cur.updateFlags(Update::Force | Update::FitCursor);
231         cur.dispatched();
232         doDispatch(cur, cmd);
233 }
234
235
236 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
237 {
238         switch (cmd.action) {
239         case LFUN_MOUSE_RELEASE:
240                 // if the derived inset did not explicitly handle mouse_release,
241                 // we assume we request the settings dialog
242                 if (!cur.selection() && cmd.button() == mouse_button::button1
243                           && hasSettings()) {
244                         FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
245                         dispatch(cur, tmpcmd);
246                 }
247                 break;
248
249         case LFUN_INSET_SETTINGS:
250                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
251                         showInsetDialog(&cur.bv());
252                         cur.dispatched();
253                 } else
254                         cur.undispatched();
255                 break;
256
257         default:
258                 cur.noUpdate();
259                 cur.undispatched();
260                 break;
261         }
262 }
263
264
265 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
266         FuncStatus & flag) const
267 {
268         // LFUN_INSET_APPLY is sent from the dialogs when the data should
269         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
270         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
271         // not belong to us, i. e. the dialog was open, and the user moved
272         // the cursor in our inset) in LyXFunc::getStatus().
273         // Dialogs::checkStatus() ensures that the dialog is deactivated if
274         // LFUN_INSET_APPLY is disabled.
275
276         switch (cmd.action) {
277         case LFUN_INSET_MODIFY:
278                 // Allow modification of our data.
279                 // This needs to be handled in the doDispatch method of our
280                 // instantiatable children.
281                 flag.setEnabled(true);
282                 return true;
283
284         case LFUN_INSET_INSERT:
285                 // Don't allow insertion of new insets.
286                 // Every inset that wants to allow new insets from open
287                 // dialogs needs to override this.
288                 flag.setEnabled(false);
289                 return true;
290
291         case LFUN_INSET_SETTINGS:
292                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
293                         bool const enable = hasSettings();
294                         flag.setEnabled(enable);
295                         return true;
296                 } else {
297                         flag.setEnabled(false);
298                         return false;
299                 }
300
301         default:
302                 break;
303         }
304         return false;
305 }
306
307
308 void Inset::edit(Cursor &, bool, EntryDirection)
309 {
310         LYXERR(Debug::INSETS, "edit left/right");
311 }
312
313
314 Inset * Inset::editXY(Cursor &, int x, int y)
315 {
316         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
317         return this;
318 }
319
320
321 Inset::idx_type Inset::index(row_type row, col_type col) const
322 {
323         if (row != 0)
324                 LYXERR0("illegal row: " << row);
325         if (col != 0)
326                 LYXERR0("illegal col: " << col);
327         return 0;
328 }
329
330
331 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
332 {
333         return from <= idx && idx <= to;
334 }
335
336
337 bool Inset::idxUpDown(Cursor &, bool) const
338 {
339         return false;
340 }
341
342
343 int Inset::docbook(odocstream &, OutputParams const &) const
344 {
345         return 0;
346 }
347
348
349 docstring Inset::xhtml(odocstream & od, OutputParams const &) const
350 {
351         od << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
352         return docstring();
353 }
354
355 bool Inset::directWrite() const
356 {
357         return false;
358 }
359
360
361 bool Inset::editable() const
362 {
363         return false;
364 }
365
366
367 bool Inset::hasSettings() const
368 {
369         return false;
370 }
371
372
373
374 bool Inset::autoDelete() const
375 {
376         return false;
377 }
378
379
380 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
381                 bool, int & x, int & y) const
382 {
383         LYXERR0("Inset::cursorPos called directly");
384         x = 100;
385         y = 100;
386 }
387
388
389 void Inset::metricsMarkers(Dimension & dim, int framesize) const
390 {
391         dim.wid += 2 * framesize;
392         dim.des += framesize;
393 }
394
395
396 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
397 {
398         dim.wid += 2 * framesize;
399         dim.asc += framesize;
400         dim.des += framesize;
401 }
402
403
404 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
405 {
406         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
407                 Color_mathframe : Color_mathcorners;
408
409         Dimension const dim = dimension(*pi.base.bv);
410
411         int const t = x + dim.width() - 1;
412         int const d = y + dim.descent();
413         pi.pain.line(x, d - 3, x, d, pen_color);
414         pi.pain.line(t, d - 3, t, d, pen_color);
415         pi.pain.line(x, d, x + 3, d, pen_color);
416         pi.pain.line(t - 3, d, t, d, pen_color);
417         setPosCache(pi, x, y);
418 }
419
420
421 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
422 {
423         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
424                 Color_mathframe : Color_mathcorners;
425
426         drawMarkers(pi, x, y);
427         Dimension const dim = dimension(*pi.base.bv);
428         int const t = x + dim.width() - 1;
429         int const a = y - dim.ascent();
430         pi.pain.line(x, a + 3, x, a, pen_color);
431         pi.pain.line(t, a + 3, t, a, pen_color);
432         pi.pain.line(x, a, x + 3, a, pen_color);
433         pi.pain.line(t - 3, a, t, a, pen_color);
434         setPosCache(pi, x, y);
435 }
436
437
438 bool Inset::editing(BufferView const * bv) const
439 {
440         return bv->cursor().isInside(this);
441 }
442
443
444 int Inset::xo(BufferView const & bv) const
445 {
446         return bv.coordCache().getInsets().x(this);
447 }
448
449
450 int Inset::yo(BufferView const & bv) const
451 {
452         return bv.coordCache().getInsets().y(this);
453 }
454
455
456 bool Inset::covers(BufferView const & bv, int x, int y) const
457 {
458         return bv.coordCache().getInsets().covers(this, x, y);
459 }
460
461
462 InsetLayout const & Inset::getLayout() const
463 {
464         if (!buffer_)
465                 return DocumentClass::plainInsetLayout();
466         return buffer().params().documentClass().insetLayout(name());
467 }
468
469
470 bool Inset::undefined() const
471 {
472         docstring const & n = getLayout().name();
473         return n.empty() || n == DocumentClass::plainInsetLayout().name();
474 }
475
476
477 void Inset::dump() const
478 {
479         write(lyxerr);
480 }
481
482
483 ColorCode Inset::backgroundColor() const
484 {
485         return Color_none;
486 }
487
488
489 ColorCode Inset::labelColor() const
490 {
491         return Color_foreground;
492 }
493
494
495 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
496 {
497         //LYXERR("Inset: set position cache to " << x << " " << y);
498         pi.base.bv->coordCache().insets().add(this, x, y);
499 }
500
501
502 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
503 {
504         mi.base.bv->coordCache().insets().add(this, dim);
505 }
506
507
508 Buffer const * Inset::updateFrontend() const
509 {
510         return theApp() ? theApp()->updateInset(this) : 0;
511 }
512
513
514 docstring Inset::completionPrefix(Cursor const &) const 
515 {
516         return docstring();
517 }
518
519 } // namespace lyx