]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
b66d6ee9e969314a96cae55a2b12b5751dda17de
[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 "output_xhtml.h"
32 #include "Text.h"
33 #include "TextClass.h"
34
35 #include "frontends/Application.h"
36 #include "frontends/Painter.h"
37
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"
43
44 #include <map>
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace lyx {
50
51 class InsetName {
52 public:
53         InsetName(string const & n, InsetCode c) : name(n), code(c) {}
54         string name;
55         InsetCode code;
56 };
57
58
59 typedef map<string, InsetCode> TranslatorMap;
60
61
62 static TranslatorMap const build_translator()
63 {
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", MATH_MACROARG_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),
111                 InsetName("mathamsarray", MATH_AMSARRAY_CODE),
112                 InsetName("matharray", MATH_ARRAY_CODE),
113                 InsetName("mathbig", MATH_BIG_CODE),
114                 InsetName("mathboldsymbol", MATH_BOLDSYMBOL_CODE),
115                 InsetName("mathbox", MATH_BOX_CODE),
116                 InsetName("mathbrace", MATH_BRACE_CODE),
117                 InsetName("mathcases", MATH_CASES_CODE),
118                 InsetName("mathchar", MATH_CHAR_CODE),
119                 InsetName("mathcolor", MATH_COLOR_CODE),
120                 InsetName("mathcomment", MATH_COMMENT_CODE),
121                 InsetName("mathdecoration", MATH_DECORATION_CODE),
122                 InsetName("mathdelim", MATH_DELIM_CODE),
123                 InsetName("mathdiff", MATH_DIFF_CODE),
124                 InsetName("mathdots", MATH_DOTS_CODE),
125                 InsetName("mathensuremath", MATH_ENSUREMATH_CODE),
126                 InsetName("mathenv", MATH_ENV_CODE),
127                 InsetName("mathexfunc", MATH_EXFUNC_CODE),
128                 InsetName("mathexint", MATH_EXINT_CODE),
129                 InsetName("mathfont", MATH_FONT_CODE),
130                 InsetName("mathfontold", MATH_FONTOLD_CODE),
131                 InsetName("mathfrac", MATH_FRAC_CODE),
132                 InsetName("mathgrid", MATH_GRID_CODE),
133                 InsetName("math", MATH_CODE),
134                 InsetName("mathhull", MATH_HULL_CODE),
135                 InsetName("mathkern", MATH_KERN_CODE),
136                 InsetName("mathlefteqn", MATH_LEFTEQN_CODE),
137                 InsetName("mathlim", MATH_LIM_CODE),
138                 InsetName("mathmatrix", MATH_MATRIX_CODE),
139                 InsetName("mathmbox", MATH_MBOX_CODE),
140                 InsetName("mathnest", MATH_NEST_CODE),
141                 InsetName("mathnumber", MATH_NUMBER_CODE),
142                 InsetName("mathoverset", MATH_OVERSET_CODE),
143                 InsetName("mathpar", MATH_PAR_CODE),
144                 InsetName("mathphantom", MATH_PHANTOM_CODE),
145                 InsetName("mathref", MATH_REF_CODE),
146                 InsetName("mathroot", MATH_ROOT_CODE),
147                 InsetName("mathscript", MATH_SCRIPT_CODE),
148                 InsetName("mathsize", MATH_SIZE_CODE),
149                 InsetName("mathspace", MATH_SPACE_CODE),
150                 InsetName("mathspecialchar", MATH_SPECIALCHAR_CODE),
151                 InsetName("mathsplit", MATH_SPLIT_CODE),
152                 InsetName("mathsqrt", MATH_SQRT_CODE),
153                 InsetName("mathstackrel", MATH_STACKREL_CODE),
154                 InsetName("mathstring", MATH_STRING_CODE),
155                 InsetName("mathsubstack", MATH_SUBSTACK_CODE),
156                 InsetName("mathsymbol", MATH_SYMBOL_CODE),
157                 InsetName("mathtabular", MATH_TABULAR_CODE),
158                 InsetName("mathunderset", MATH_UNDERSET_CODE),
159                 InsetName("mathunknown", MATH_UNKNOWN_CODE),
160                 InsetName("mathxarrow", MATH_XARROW_CODE),
161                 InsetName("mathxyarrow", MATH_XYARROW_CODE),
162                 InsetName("mathxymatrix", MATH_XYMATRIX_CODE),
163                 InsetName("mathmacro", MATH_MACRO_CODE),
164         };
165
166         size_t const insetnames_size =
167                 sizeof(insetnames) / sizeof(insetnames[0]);
168
169         map<string, InsetCode> data;
170         for (size_t i = 0; i != insetnames_size; ++i) {
171                 InsetName const & var = insetnames[i];
172                 data[var.name] = var.code;
173         }
174
175         return data;
176 }
177
178
179 void Inset::setBuffer(Buffer & buffer)
180 {
181         buffer_ = &buffer;
182 }
183
184
185 Buffer & Inset::buffer()
186 {
187         if (!buffer_) {
188                 odocstringstream s;
189                 lyxerr << "Inset: " << this
190                                         << " LyX Code: " << lyxCode()
191                                         << " name: " << insetName(lyxCode())
192                                         << std::endl;
193                 s << "LyX Code: " << lyxCode() << " name: " << name();
194                 LASSERT(false, /**/);
195                 throw ExceptionMessage(BufferException, 
196                         from_ascii("Inset::buffer_ member not initialized!"), s.str());
197         }
198         return *buffer_;
199 }
200
201
202 Buffer const & Inset::buffer() const
203 {
204         return const_cast<Inset *>(this)->buffer();
205 }
206
207
208 bool Inset::isBufferValid() const
209 {
210         return buffer_ && theBufferList().isLoaded(buffer_);
211 }
212
213
214 docstring Inset::name() const
215 {
216         return from_ascii("unknown");
217 }
218
219
220 bool Inset::isFreeSpacing() const 
221 {
222         return getLayout().isFreeSpacing();
223 }
224
225
226 bool Inset::allowEmpty() const
227 {
228         return getLayout().isKeepEmpty();
229 }
230
231 bool Inset::forceLTR() const
232 {
233         return getLayout().forceLTR();
234 }
235
236 void Inset::initView()
237 {
238         if (isLabeled())
239                 buffer().updateLabels();
240 }
241
242
243 docstring Inset::toolTip(BufferView const &, int, int) const
244 {
245         return docstring();
246 }
247
248
249 docstring Inset::contextMenu(BufferView const &, int, int) const
250 {
251         return docstring();
252 }
253
254
255 Dimension const Inset::dimension(BufferView const & bv) const
256 {
257         return bv.coordCache().getInsets().dim(this);
258 }
259
260
261 InsetCode insetCode(string const & name)
262 {
263         static TranslatorMap const translator = build_translator();
264
265         TranslatorMap::const_iterator it = translator.find(name);
266         return it == translator.end() ? NO_CODE : it->second;
267 }
268
269
270 string insetName(InsetCode c) 
271 {
272         static TranslatorMap const translator = build_translator();
273
274         TranslatorMap::const_iterator it =  translator.begin();
275         TranslatorMap::const_iterator end = translator.end();
276         for (; it != end; ++it) {
277                 if (it->second == c)
278                         return it->first;
279         }
280         return string();
281 }
282
283
284 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
285 {
286         if (buffer_ == 0) {
287                 lyxerr << "Unassigned buffer_ member in Inset::dispatch()" << std::endl;
288                 lyxerr << "LyX Code: " << lyxCode() << " name: " << insetName(lyxCode()) << std::endl;
289         } else if (cur.buffer() != buffer_)
290                 lyxerr << "cur.buffer() != buffer_ in Inset::dispatch()" << std::endl;
291         cur.updateFlags(Update::Force | Update::FitCursor);
292         cur.dispatched();
293         doDispatch(cur, cmd);
294 }
295
296
297 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
298 {
299         switch (cmd.action) {
300         case LFUN_MOUSE_RELEASE:
301                 // if the derived inset did not explicitly handle mouse_release,
302                 // we assume we request the settings dialog
303                 if (!cur.selection() && cmd.button() == mouse_button::button1
304                           && hasSettings()) {
305                         FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
306                         dispatch(cur, tmpcmd);
307                 }
308                 break;
309
310         case LFUN_INSET_SETTINGS:
311                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
312                         showInsetDialog(&cur.bv());
313                         cur.dispatched();
314                 } else
315                         cur.undispatched();
316                 break;
317
318         default:
319                 cur.noUpdate();
320                 cur.undispatched();
321                 break;
322         }
323 }
324
325
326 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
327         FuncStatus & flag) const
328 {
329         // LFUN_INSET_APPLY is sent from the dialogs when the data should
330         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
331         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
332         // not belong to us, i. e. the dialog was open, and the user moved
333         // the cursor in our inset) in LyXFunc::getStatus().
334         // Dialogs::checkStatus() ensures that the dialog is deactivated if
335         // LFUN_INSET_APPLY is disabled.
336
337         switch (cmd.action) {
338         case LFUN_INSET_MODIFY:
339                 // Allow modification of our data.
340                 // This needs to be handled in the doDispatch method of our
341                 // instantiatable children.
342                 flag.setEnabled(true);
343                 return true;
344
345         case LFUN_INSET_INSERT:
346                 // Don't allow insertion of new insets.
347                 // Every inset that wants to allow new insets from open
348                 // dialogs needs to override this.
349                 flag.setEnabled(false);
350                 return true;
351
352         case LFUN_INSET_SETTINGS:
353                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
354                         bool const enable = hasSettings();
355                         flag.setEnabled(enable);
356                         return true;
357                 } else {
358                         flag.setEnabled(false);
359                         return false;
360                 }
361
362         default:
363                 break;
364         }
365         return false;
366 }
367
368
369 void Inset::edit(Cursor &, bool, EntryDirection)
370 {
371         LYXERR(Debug::INSETS, "edit left/right");
372 }
373
374
375 Inset * Inset::editXY(Cursor &, int x, int y)
376 {
377         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
378         return this;
379 }
380
381
382 Inset::idx_type Inset::index(row_type row, col_type col) const
383 {
384         if (row != 0)
385                 LYXERR0("illegal row: " << row);
386         if (col != 0)
387                 LYXERR0("illegal col: " << col);
388         return 0;
389 }
390
391
392 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
393 {
394         return from <= idx && idx <= to;
395 }
396
397
398 bool Inset::idxUpDown(Cursor &, bool) const
399 {
400         return false;
401 }
402
403
404 int Inset::docbook(odocstream &, OutputParams const &) const
405 {
406         return 0;
407 }
408
409
410 docstring Inset::xhtml(XHTMLStream & xs, OutputParams const &) const
411 {
412         xs << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
413         return docstring();
414 }
415
416
417 bool Inset::directWrite() const
418 {
419         return false;
420 }
421
422
423 bool Inset::editable() const
424 {
425         return false;
426 }
427
428
429 bool Inset::hasSettings() const
430 {
431         return false;
432 }
433
434
435
436 bool Inset::autoDelete() const
437 {
438         return false;
439 }
440
441
442 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
443                 bool, int & x, int & y) const
444 {
445         LYXERR0("Inset::cursorPos called directly");
446         x = 100;
447         y = 100;
448 }
449
450
451 void Inset::metricsMarkers(Dimension & dim, int framesize) const
452 {
453         dim.wid += 2 * framesize;
454         dim.des += framesize;
455 }
456
457
458 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
459 {
460         dim.wid += 2 * framesize;
461         dim.asc += framesize;
462         dim.des += framesize;
463 }
464
465
466 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
467 {
468         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
469                 Color_mathframe : Color_mathcorners;
470
471         Dimension const dim = dimension(*pi.base.bv);
472
473         int const t = x + dim.width() - 1;
474         int const d = y + dim.descent();
475         pi.pain.line(x, d - 3, x, d, pen_color);
476         pi.pain.line(t, d - 3, t, d, pen_color);
477         pi.pain.line(x, d, x + 3, d, pen_color);
478         pi.pain.line(t - 3, d, t, d, pen_color);
479         setPosCache(pi, x, y);
480 }
481
482
483 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
484 {
485         ColorCode pen_color = mouseHovered() || editing(pi.base.bv)?
486                 Color_mathframe : Color_mathcorners;
487
488         drawMarkers(pi, x, y);
489         Dimension const dim = dimension(*pi.base.bv);
490         int const t = x + dim.width() - 1;
491         int const a = y - dim.ascent();
492         pi.pain.line(x, a + 3, x, a, pen_color);
493         pi.pain.line(t, a + 3, t, a, pen_color);
494         pi.pain.line(x, a, x + 3, a, pen_color);
495         pi.pain.line(t - 3, a, t, a, pen_color);
496         setPosCache(pi, x, y);
497 }
498
499
500 bool Inset::editing(BufferView const * bv) const
501 {
502         return bv->cursor().isInside(this);
503 }
504
505
506 int Inset::xo(BufferView const & bv) const
507 {
508         return bv.coordCache().getInsets().x(this);
509 }
510
511
512 int Inset::yo(BufferView const & bv) const
513 {
514         return bv.coordCache().getInsets().y(this);
515 }
516
517
518 bool Inset::covers(BufferView const & bv, int x, int y) const
519 {
520         return bv.coordCache().getInsets().covers(this, x, y);
521 }
522
523
524 InsetLayout const & Inset::getLayout() const
525 {
526         return buffer().params().documentClass().insetLayout(name());
527 }
528
529
530 bool Inset::undefined() const
531 {
532         docstring const & n = getLayout().name();
533         return n.empty() || n == DocumentClass::plainInsetLayout().name();
534 }
535
536
537 void Inset::dump() const
538 {
539         write(lyxerr);
540 }
541
542
543 ColorCode Inset::backgroundColor() const
544 {
545         return Color_none;
546 }
547
548
549 ColorCode Inset::labelColor() const
550 {
551         return Color_foreground;
552 }
553
554
555 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
556 {
557         //LYXERR("Inset: set position cache to " << x << " " << y);
558         pi.base.bv->coordCache().insets().add(this, x, y);
559 }
560
561
562 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
563 {
564         mi.base.bv->coordCache().insets().add(this, dim);
565 }
566
567
568 Buffer const * Inset::updateFrontend() const
569 {
570         return theApp() ? theApp()->updateInset(this) : 0;
571 }
572
573
574 docstring Inset::completionPrefix(Cursor const &) const 
575 {
576         return docstring();
577 }
578
579 } // namespace lyx