]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
tex2lyx/text.cpp: fix typos
[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 = string(), docstring const & dn = docstring())
54                 : name(n), display_name(dn) {}
55         string name;
56         docstring display_name;
57 };
58
59
60 static InsetName insetnames[INSET_CODE_SIZE];
61
62
63 // This list should be kept in sync with the list of dialogs in
64 // src/frontends/qt4/GuiView.cpp, I.e., if a dialog goes with an
65 // inset, the dialog should have the same name as the inset.
66 // Changes should be also recorded in LFUN_DIALOG_SHOW doxygen
67 // docs in LyXAction.cpp.
68 static void build_translator()
69 {
70         static bool passed = false;
71         if (passed)
72                 return;
73         insetnames[TOC_CODE] = InsetName("toc");
74         insetnames[QUOTE_CODE] = InsetName("quote");
75         insetnames[REF_CODE] = InsetName("ref");
76         insetnames[HYPERLINK_CODE] = InsetName("href");
77         insetnames[SEPARATOR_CODE] = InsetName("separator");
78         insetnames[ENDING_CODE] = InsetName("ending");
79         insetnames[LABEL_CODE] = InsetName("label");
80         insetnames[NOTE_CODE] = InsetName("note");
81         insetnames[PHANTOM_CODE] = InsetName("phantom");
82         insetnames[ACCENT_CODE] = InsetName("accent");
83         insetnames[MATH_CODE] = InsetName("math");
84         insetnames[INDEX_CODE] = InsetName("index");
85         insetnames[NOMENCL_CODE] = InsetName("nomenclature");
86         insetnames[INCLUDE_CODE] = InsetName("include");
87         insetnames[GRAPHICS_CODE] = InsetName("graphics");
88         insetnames[BIBITEM_CODE] = InsetName("bibitem", _("Bibliography Entry"));
89         insetnames[BIBTEX_CODE] = InsetName("bibtex");
90         insetnames[TEXT_CODE] = InsetName("text");
91         insetnames[ERT_CODE] = InsetName("ert", _("TeX Code"));
92         insetnames[FOOT_CODE] = InsetName("foot");
93         insetnames[MARGIN_CODE] = InsetName("margin");
94         insetnames[FLOAT_CODE] = InsetName("float", _("Float"));
95         insetnames[WRAP_CODE] = InsetName("wrap");
96         insetnames[SPECIALCHAR_CODE] = InsetName("specialchar");
97         insetnames[TABULAR_CODE] = InsetName("tabular", _("Table"));
98         insetnames[EXTERNAL_CODE] = InsetName("external");
99         insetnames[CAPTION_CODE] = InsetName("caption");
100         insetnames[MATHMACRO_CODE] = InsetName("mathmacro");
101         insetnames[CITE_CODE] = InsetName("citation");
102         insetnames[FLOAT_LIST_CODE] = InsetName("floatlist");
103         insetnames[INDEX_PRINT_CODE] = InsetName("index_print");
104         insetnames[NOMENCL_PRINT_CODE] = InsetName("nomencl_print");
105         insetnames[ARG_CODE] = InsetName("optarg");
106         insetnames[NEWLINE_CODE] = InsetName("newline");
107         insetnames[LINE_CODE] = InsetName("line");
108         insetnames[BRANCH_CODE] = InsetName("branch", _("Branch"));
109         insetnames[BOX_CODE] = InsetName("box", _("Box"));
110         insetnames[FLEX_CODE] = InsetName("flex");
111         insetnames[SPACE_CODE] = InsetName("space", _("Horizontal Space"));
112         insetnames[VSPACE_CODE] = InsetName("vspace", _("Vertical Space"));
113         insetnames[MATH_MACROARG_CODE] = InsetName("mathmacroarg");
114         insetnames[LISTINGS_CODE] = InsetName("listings");
115         insetnames[INFO_CODE] = InsetName("info", _("Info"));
116         insetnames[COLLAPSABLE_CODE] = InsetName("collapsable");
117         insetnames[NEWPAGE_CODE] = InsetName("newpage");
118         insetnames[SCRIPT_CODE] = InsetName("script");
119         insetnames[CELL_CODE] = InsetName("tablecell");
120         insetnames[MATH_AMSARRAY_CODE] = InsetName("mathamsarray");
121         insetnames[MATH_ARRAY_CODE] = InsetName("matharray");
122         insetnames[MATH_BIG_CODE] = InsetName("mathbig");
123         insetnames[MATH_BOLDSYMBOL_CODE] = InsetName("mathboldsymbol");
124         insetnames[MATH_BOX_CODE] = InsetName("mathbox");
125         insetnames[MATH_BRACE_CODE] = InsetName("mathbrace");
126         insetnames[MATH_CANCEL_CODE] = InsetName("mathcancel");
127         insetnames[MATH_CANCELTO_CODE] = InsetName("mathcancelto");
128         insetnames[MATH_CASES_CODE] = InsetName("mathcases");
129         insetnames[MATH_CHAR_CODE] = InsetName("mathchar");
130         insetnames[MATH_COLOR_CODE] = InsetName("mathcolor");
131         insetnames[MATH_COMMENT_CODE] = InsetName("mathcomment");
132         insetnames[MATH_DECORATION_CODE] = InsetName("mathdecoration");
133         insetnames[MATH_DELIM_CODE] = InsetName("mathdelim");
134         insetnames[MATH_DIFF_CODE] = InsetName("mathdiff");
135         insetnames[MATH_DOTS_CODE] = InsetName("mathdots");
136         insetnames[MATH_ENSUREMATH_CODE] = InsetName("mathensuremath");
137         insetnames[MATH_ENV_CODE] = InsetName("mathenv");
138         insetnames[MATH_EXFUNC_CODE] = InsetName("mathexfunc");
139         insetnames[MATH_EXINT_CODE] = InsetName("mathexint");
140         insetnames[MATH_FONT_CODE] = InsetName("mathfont");
141         insetnames[MATH_FONTOLD_CODE] = InsetName("mathfontold");
142         insetnames[MATH_FRAC_CODE] = InsetName("mathfrac");
143         insetnames[MATH_GRID_CODE] = InsetName("mathgrid");
144         insetnames[MATH_CODE] = InsetName("math");
145         insetnames[MATH_HULL_CODE] = InsetName("mathhull");
146         insetnames[MATH_KERN_CODE] = InsetName("mathkern");
147         insetnames[MATH_LEFTEQN_CODE] = InsetName("mathlefteqn");
148         insetnames[MATH_LIM_CODE] = InsetName("mathlim");
149         insetnames[MATH_MATRIX_CODE] = InsetName("mathmatrix");
150         insetnames[MATH_MBOX_CODE] = InsetName("mathmbox");
151         insetnames[MATH_NEST_CODE] = InsetName("mathnest");
152         insetnames[MATH_NUMBER_CODE] = InsetName("mathnumber");
153         insetnames[MATH_OVERSET_CODE] = InsetName("mathoverset");
154         insetnames[MATH_PAR_CODE] = InsetName("mathpar");
155         insetnames[MATH_PHANTOM_CODE] = InsetName("mathphantom");
156         insetnames[MATH_REF_CODE] = InsetName("mathref");
157         insetnames[MATH_ROOT_CODE] = InsetName("mathroot");
158         insetnames[MATH_SCRIPT_CODE] = InsetName("mathscript");
159         insetnames[MATH_SIZE_CODE] = InsetName("mathsize");
160         insetnames[MATH_SPACE_CODE] = InsetName("mathspace", _("Horizontal Math Space"));
161         insetnames[MATH_SPECIALCHAR_CODE] = InsetName("mathspecialchar");
162         insetnames[MATH_SPLIT_CODE] = InsetName("mathsplit");
163         insetnames[MATH_SQRT_CODE] = InsetName("mathsqrt");
164         insetnames[MATH_STACKREL_CODE] = InsetName("mathstackrel");
165         insetnames[MATH_STRING_CODE] = InsetName("mathstring");
166         insetnames[MATH_SUBSTACK_CODE] = InsetName("mathsubstack");
167         insetnames[MATH_SYMBOL_CODE] = InsetName("mathsymbol");
168         insetnames[MATH_TABULAR_CODE] = InsetName("mathtabular");
169         insetnames[MATH_UNDERSET_CODE] = InsetName("mathunderset");
170         insetnames[MATH_UNKNOWN_CODE] = InsetName("mathunknown");
171         insetnames[MATH_XARROW_CODE] = InsetName("mathxarrow");
172         insetnames[MATH_XYARROW_CODE] = InsetName("mathxyarrow");
173         insetnames[MATH_XYMATRIX_CODE] = InsetName("mathxymatrix");
174         insetnames[MATH_DIAGRAM_CODE] = InsetName("mathdiagram");
175         insetnames[MATH_MACRO_CODE] = InsetName("mathmacro");
176
177         passed = true;
178 }
179
180
181 void Inset::setBuffer(Buffer & buffer)
182 {
183         buffer_ = &buffer;
184 }
185
186
187 Buffer & Inset::buffer()
188 {
189         if (!buffer_) {
190                 odocstringstream s;
191                 string const iname = insetName(lyxCode());
192                 LYXERR0("Inset: " << this << " LyX Code: " << lyxCode()
193                                         << " name: " << iname);
194                 s << "LyX Code: " << lyxCode() << " name: " << iname;
195                 LASSERT(false, /**/);
196                 throw ExceptionMessage(BufferException, 
197                         from_ascii("Inset::buffer_ member not initialized!"), s.str());
198         }
199         return *buffer_;
200 }
201
202
203 Buffer const & Inset::buffer() const
204 {
205         return const_cast<Inset *>(this)->buffer();
206 }
207
208
209 bool Inset::isBufferLoaded() const
210 {
211         return buffer_ && theBufferList().isLoaded(buffer_);
212 }
213
214
215 bool Inset::isBufferValid() const
216 {
217         return buffer_ 
218                 && (isBufferLoaded() || buffer_->isClone());
219 }
220
221
222 docstring Inset::layoutName() const
223 {
224         return from_ascii("unknown");
225 }
226
227
228 bool Inset::isFreeSpacing() const 
229 {
230         return getLayout().isFreeSpacing();
231 }
232
233
234 bool Inset::allowEmpty() const
235 {
236         return getLayout().isKeepEmpty();
237 }
238
239
240 bool Inset::forceLTR() const
241 {
242         return getLayout().forceLTR();
243 }
244
245
246 bool Inset::isInToc() const
247 {
248         return getLayout().isInToc();
249 }
250
251
252 docstring Inset::toolTip(BufferView const &, int, int) const
253 {
254         return docstring();
255 }
256
257
258 void Inset::forToc(docstring &, size_t) const
259 {
260 }
261
262
263 string Inset::contextMenu(BufferView const &, int, int) const
264 {
265         return contextMenuName();
266 }
267
268
269 string Inset::contextMenuName() const
270 {
271         return string();
272 }
273
274
275 Dimension const Inset::dimension(BufferView const & bv) const
276 {
277         return bv.coordCache().getInsets().dim(this);
278 }
279
280
281 InsetCode insetCode(string const & name)
282 {
283         build_translator();
284         for (int i = 1; i != int(INSET_CODE_SIZE); ++i) {
285                 if (insetnames[i].name == name)
286                         return InsetCode(i);
287         }
288         return NO_CODE;
289 }
290
291
292 string insetName(InsetCode c) 
293 {
294         build_translator();
295         return insetnames[c].name;
296 }
297
298
299 docstring insetDisplayName(InsetCode c) 
300 {
301         build_translator();
302         return insetnames[c].display_name;
303 }
304
305
306 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
307 {
308         if (buffer_ == 0) {
309                 lyxerr << "Unassigned buffer_ member in Inset::dispatch()" << std::endl;
310                 lyxerr << "LyX Code: " << lyxCode() << " name: " 
311                        << insetName(lyxCode()) << std::endl;
312         } else if (cur.buffer() != buffer_)
313                 lyxerr << "cur.buffer() != buffer_ in Inset::dispatch()" << std::endl;
314         cur.screenUpdateFlags(Update::Force | Update::FitCursor);
315         cur.dispatched();
316         doDispatch(cur, cmd);
317 }
318
319
320 bool Inset::showInsetDialog(BufferView * bv) const
321 {
322         InsetCode const code = lyxCode();
323         switch (code){
324         case ERT_CODE:
325         case FLOAT_CODE:
326         case BOX_CODE:
327         case BIBITEM_CODE:
328         case BRANCH_CODE:
329         case INFO_CODE:
330         case MATH_SPACE_CODE:
331         case SPACE_CODE:
332         case TABULAR_CODE:
333         case VSPACE_CODE:
334                 bv->showDialog(insetName(code));
335                 break;
336         default:
337                 return false;
338         }
339         return true;
340 }
341
342
343 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
344 {
345         switch (cmd.action()) {
346         case LFUN_MOUSE_RELEASE:
347                 // if the derived inset did not explicitly handle mouse_release,
348                 // we assume we request the settings dialog
349                 if (!cur.selection() && cmd.button() == mouse_button::button1
350                     && clickable(cmd.x(), cmd.y()) && hasSettings()) {
351                         FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
352                         dispatch(cur, tmpcmd);
353                 }
354                 break;
355
356         case LFUN_INSET_SETTINGS:
357                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
358                         showInsetDialog(&cur.bv());
359                         cur.dispatched();
360                 } else
361                         cur.undispatched();
362                 break;
363
364         default:
365                 cur.noScreenUpdate();
366                 cur.undispatched();
367                 break;
368         }
369 }
370
371
372 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
373         FuncStatus & flag) const
374 {
375         // LFUN_INSET_APPLY is sent from the dialogs when the data should
376         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
377         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
378         // not belong to us, i. e. the dialog was open, and the user moved
379         // the cursor in our inset) in lyx::getStatus().
380         // Dialogs::checkStatus() ensures that the dialog is deactivated if
381         // LFUN_INSET_APPLY is disabled.
382
383         switch (cmd.action()) {
384         case LFUN_INSET_MODIFY:
385                 // Allow modification of our data.
386                 // This needs to be handled in the doDispatch method of our
387                 // instantiatable children.
388                 // FIXME: Why don't we let the insets determine whether this
389                 // should be enabled or not ? Now we need this check for 
390                 // the tabular features. (vfr)
391                 if (cmd.getArg(0) == "tabular")
392                         return false;
393                 flag.setEnabled(true);
394                 return true;
395
396         case LFUN_INSET_INSERT:
397                 // Don't allow insertion of new insets.
398                 // Every inset that wants to allow new insets from open
399                 // dialogs needs to override this.
400                 flag.setEnabled(false);
401                 return true;
402
403         case LFUN_INSET_SETTINGS:
404                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
405                         bool const enable = hasSettings();
406                         flag.setEnabled(enable);
407                         return true;
408                 } else {
409                         return false;
410                 }
411         
412         case LFUN_IN_MATHMACROTEMPLATE:
413                 // By default we're not in a MathMacroTemplate inset
414                 flag.setEnabled(false);
415                 return true;
416
417         default:
418                 break;
419         }
420         return false;
421 }
422
423
424 void Inset::edit(Cursor &, bool, EntryDirection)
425 {
426         LYXERR(Debug::INSETS, "edit left/right");
427 }
428
429
430 Inset * Inset::editXY(Cursor &, int x, int y)
431 {
432         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
433         return this;
434 }
435
436
437 Inset::idx_type Inset::index(row_type row, col_type col) const
438 {
439         if (row != 0)
440                 LYXERR0("illegal row: " << row);
441         if (col != 0)
442                 LYXERR0("illegal col: " << col);
443         return 0;
444 }
445
446
447 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
448 {
449         return from <= idx && idx <= to;
450 }
451
452
453 bool Inset::idxUpDown(Cursor &, bool) const
454 {
455         return false;
456 }
457
458
459 int Inset::docbook(odocstream &, OutputParams const &) const
460 {
461         return 0;
462 }
463
464
465 docstring Inset::xhtml(XHTMLStream & xs, OutputParams const &) const
466 {
467         xs << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
468         return docstring();
469 }
470
471
472 bool Inset::directWrite() const
473 {
474         return false;
475 }
476
477
478 bool Inset::editable() const
479 {
480         return false;
481 }
482
483
484 bool Inset::hasSettings() const
485 {
486         return false;
487 }
488
489
490
491 bool Inset::autoDelete() const
492 {
493         return false;
494 }
495
496
497 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
498                 bool, int & x, int & y) const
499 {
500         LYXERR0("Inset::cursorPos called directly");
501         x = 100;
502         y = 100;
503 }
504
505
506 void Inset::metricsMarkers(Dimension & dim, int framesize) const
507 {
508         dim.wid += 2 * framesize;
509         dim.des += framesize;
510 }
511
512
513 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
514 {
515         dim.wid += 2 * framesize;
516         dim.asc += framesize;
517         dim.des += framesize;
518 }
519
520
521 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
522 {
523         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
524                 Color_mathframe : Color_mathcorners;
525
526         Dimension const dim = dimension(*pi.base.bv);
527
528         int const t = x + dim.width() - 1;
529         int const d = y + dim.descent();
530         pi.pain.line(x, d - 3, x, d, pen_color);
531         pi.pain.line(t, d - 3, t, d, pen_color);
532         pi.pain.line(x, d, x + 3, d, pen_color);
533         pi.pain.line(t - 3, d, t, d, pen_color);
534         setPosCache(pi, x, y);
535 }
536
537
538 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
539 {
540         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
541                 Color_mathframe : Color_mathcorners;
542
543         drawMarkers(pi, x, y);
544         Dimension const dim = dimension(*pi.base.bv);
545         int const t = x + dim.width() - 1;
546         int const a = y - dim.ascent();
547         pi.pain.line(x, a + 3, x, a, pen_color);
548         pi.pain.line(t, a + 3, t, a, pen_color);
549         pi.pain.line(x, a, x + 3, a, pen_color);
550         pi.pain.line(t - 3, a, t, a, pen_color);
551         setPosCache(pi, x, y);
552 }
553
554
555 bool Inset::editing(BufferView const * bv) const
556 {
557         return bv->cursor().isInside(this);
558 }
559
560
561 int Inset::xo(BufferView const & bv) const
562 {
563         return bv.coordCache().getInsets().x(this);
564 }
565
566
567 int Inset::yo(BufferView const & bv) const
568 {
569         return bv.coordCache().getInsets().y(this);
570 }
571
572
573 bool Inset::covers(BufferView const & bv, int x, int y) const
574 {
575         return bv.coordCache().getInsets().covers(this, x, y);
576 }
577
578
579 InsetLayout const & Inset::getLayout() const
580 {
581         if (!buffer_)
582                 return DocumentClass::plainInsetLayout();
583         return buffer().params().documentClass().insetLayout(layoutName());
584 }
585
586
587 bool Inset::undefined() const
588 {
589         docstring const & n = getLayout().name();
590         return n.empty() || n == DocumentClass::plainInsetLayout().name();
591 }
592
593
594 void Inset::dump() const
595 {
596         write(lyxerr);
597 }
598
599
600 ColorCode Inset::backgroundColor(PainterInfo const & /*pi*/) const
601 {
602         return Color_none;
603 }
604
605
606 ColorCode Inset::labelColor() const
607 {
608         return Color_foreground;
609 }
610
611
612 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
613 {
614         //LYXERR("Inset: set position cache to " << x << " " << y);
615         pi.base.bv->coordCache().insets().add(this, x, y);
616 }
617
618
619 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
620 {
621         mi.base.bv->coordCache().insets().add(this, dim);
622 }
623
624
625 Buffer const * Inset::updateFrontend() const
626 {
627         //FIXME (Abdel 03/12/10): see bugs #6814 and #6949
628         // If the Buffer is null and we end up here this is most probably because we
629         // are in the CutAndPaste stack. See InsetGraphics, RenderGraphics and
630         // RenderPreview.
631         if (!buffer_)
632                 return 0;
633         return theApp() ? theApp()->updateInset(this) : 0;
634 }
635
636
637 docstring Inset::completionPrefix(Cursor const &) const 
638 {
639         return docstring();
640 }
641
642 } // namespace lyx