]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.cpp
#7379 avoid the wrap spell check question when buffer is empty
[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_CASES_CODE] = InsetName("mathcases");
127         insetnames[MATH_CHAR_CODE] = InsetName("mathchar");
128         insetnames[MATH_COLOR_CODE] = InsetName("mathcolor");
129         insetnames[MATH_COMMENT_CODE] = InsetName("mathcomment");
130         insetnames[MATH_DECORATION_CODE] = InsetName("mathdecoration");
131         insetnames[MATH_DELIM_CODE] = InsetName("mathdelim");
132         insetnames[MATH_DIFF_CODE] = InsetName("mathdiff");
133         insetnames[MATH_DOTS_CODE] = InsetName("mathdots");
134         insetnames[MATH_ENSUREMATH_CODE] = InsetName("mathensuremath");
135         insetnames[MATH_ENV_CODE] = InsetName("mathenv");
136         insetnames[MATH_EXFUNC_CODE] = InsetName("mathexfunc");
137         insetnames[MATH_EXINT_CODE] = InsetName("mathexint");
138         insetnames[MATH_FONT_CODE] = InsetName("mathfont");
139         insetnames[MATH_FONTOLD_CODE] = InsetName("mathfontold");
140         insetnames[MATH_FRAC_CODE] = InsetName("mathfrac");
141         insetnames[MATH_GRID_CODE] = InsetName("mathgrid");
142         insetnames[MATH_CODE] = InsetName("math");
143         insetnames[MATH_HULL_CODE] = InsetName("mathhull");
144         insetnames[MATH_KERN_CODE] = InsetName("mathkern");
145         insetnames[MATH_LEFTEQN_CODE] = InsetName("mathlefteqn");
146         insetnames[MATH_LIM_CODE] = InsetName("mathlim");
147         insetnames[MATH_MATRIX_CODE] = InsetName("mathmatrix");
148         insetnames[MATH_MBOX_CODE] = InsetName("mathmbox");
149         insetnames[MATH_NEST_CODE] = InsetName("mathnest");
150         insetnames[MATH_NUMBER_CODE] = InsetName("mathnumber");
151         insetnames[MATH_OVERSET_CODE] = InsetName("mathoverset");
152         insetnames[MATH_PAR_CODE] = InsetName("mathpar");
153         insetnames[MATH_PHANTOM_CODE] = InsetName("mathphantom");
154         insetnames[MATH_REF_CODE] = InsetName("mathref");
155         insetnames[MATH_ROOT_CODE] = InsetName("mathroot");
156         insetnames[MATH_SCRIPT_CODE] = InsetName("mathscript");
157         insetnames[MATH_SIZE_CODE] = InsetName("mathsize");
158         insetnames[MATH_SPACE_CODE] = InsetName("mathspace", _("Horizontal Math Space"));
159         insetnames[MATH_SPECIALCHAR_CODE] = InsetName("mathspecialchar");
160         insetnames[MATH_SPLIT_CODE] = InsetName("mathsplit");
161         insetnames[MATH_SQRT_CODE] = InsetName("mathsqrt");
162         insetnames[MATH_STACKREL_CODE] = InsetName("mathstackrel");
163         insetnames[MATH_STRING_CODE] = InsetName("mathstring");
164         insetnames[MATH_SUBSTACK_CODE] = InsetName("mathsubstack");
165         insetnames[MATH_SYMBOL_CODE] = InsetName("mathsymbol");
166         insetnames[MATH_TABULAR_CODE] = InsetName("mathtabular");
167         insetnames[MATH_UNDERSET_CODE] = InsetName("mathunderset");
168         insetnames[MATH_UNKNOWN_CODE] = InsetName("mathunknown");
169         insetnames[MATH_XARROW_CODE] = InsetName("mathxarrow");
170         insetnames[MATH_XYARROW_CODE] = InsetName("mathxyarrow");
171         insetnames[MATH_XYMATRIX_CODE] = InsetName("mathxymatrix");
172         insetnames[MATH_DIAGRAM_CODE] = InsetName("mathdiagram");
173         insetnames[MATH_MACRO_CODE] = InsetName("mathmacro");
174
175         passed = true;
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::isBufferLoaded() const
209 {
210         return buffer_ && theBufferList().isLoaded(buffer_);
211 }
212
213
214 bool Inset::isBufferValid() const
215 {
216         return buffer_ 
217                 && (isBufferLoaded() || buffer_->isClone());
218 }
219
220
221 docstring Inset::name() const
222 {
223         return from_ascii("unknown");
224 }
225
226
227 bool Inset::isFreeSpacing() const 
228 {
229         return getLayout().isFreeSpacing();
230 }
231
232
233 bool Inset::allowEmpty() const
234 {
235         return getLayout().isKeepEmpty();
236 }
237
238
239 bool Inset::forceLTR() const
240 {
241         return getLayout().forceLTR();
242 }
243
244
245 bool Inset::isInToc() const
246 {
247         return getLayout().isInToc();
248 }
249
250
251 docstring Inset::toolTip(BufferView const &, int, int) const
252 {
253         return docstring();
254 }
255
256
257 void Inset::forToc(docstring &, size_t) const
258 {
259 }
260
261
262 docstring Inset::contextMenu(BufferView const &, int, int) const
263 {
264         return contextMenuName();
265 }
266
267
268 docstring Inset::contextMenuName() const
269 {
270         return docstring();
271 }
272
273
274 Dimension const Inset::dimension(BufferView const & bv) const
275 {
276         return bv.coordCache().getInsets().dim(this);
277 }
278
279
280 InsetCode insetCode(string const & name)
281 {
282         build_translator();
283         for (int i = 1; i != int(INSET_CODE_SIZE); ++i) {
284                 if (insetnames[i].name == name)
285                         return InsetCode(i);
286         }
287         return NO_CODE;
288 }
289
290
291 string insetName(InsetCode c) 
292 {
293         build_translator();
294         return insetnames[c].name;
295 }
296
297
298 docstring insetDisplayName(InsetCode c) 
299 {
300         build_translator();
301         return insetnames[c].display_name;
302 }
303
304
305 void Inset::dispatch(Cursor & cur, FuncRequest & cmd)
306 {
307         if (buffer_ == 0) {
308                 lyxerr << "Unassigned buffer_ member in Inset::dispatch()" << std::endl;
309                 lyxerr << "LyX Code: " << lyxCode() << " name: " 
310                        << insetName(lyxCode()) << std::endl;
311         } else if (cur.buffer() != buffer_)
312                 lyxerr << "cur.buffer() != buffer_ in Inset::dispatch()" << std::endl;
313         cur.screenUpdateFlags(Update::Force | Update::FitCursor);
314         cur.dispatched();
315         doDispatch(cur, cmd);
316 }
317
318
319 bool Inset::showInsetDialog(BufferView * bv) const
320 {
321         InsetCode const code = lyxCode();
322         switch (code){
323         case ERT_CODE:
324         case FLOAT_CODE:
325         case BOX_CODE:
326         case BIBITEM_CODE:
327         case BRANCH_CODE:
328         case INFO_CODE:
329         case MATH_SPACE_CODE:
330         case SPACE_CODE:
331         case TABULAR_CODE:
332         case VSPACE_CODE:
333                 bv->showDialog(insetName(code));
334                 break;
335         default:
336                 return false;
337         }
338         return true;
339 }
340
341
342 void Inset::doDispatch(Cursor & cur, FuncRequest &cmd)
343 {
344         switch (cmd.action()) {
345         case LFUN_MOUSE_RELEASE:
346                 // if the derived inset did not explicitly handle mouse_release,
347                 // we assume we request the settings dialog
348                 if (!cur.selection() && cmd.button() == mouse_button::button1
349                           && hasSettings()) {
350                         FuncRequest tmpcmd(LFUN_INSET_SETTINGS);
351                         dispatch(cur, tmpcmd);
352                 }
353                 break;
354
355         case LFUN_INSET_SETTINGS:
356                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
357                         showInsetDialog(&cur.bv());
358                         cur.dispatched();
359                 } else
360                         cur.undispatched();
361                 break;
362
363         default:
364                 cur.noScreenUpdate();
365                 cur.undispatched();
366                 break;
367         }
368 }
369
370
371 bool Inset::getStatus(Cursor &, FuncRequest const & cmd,
372         FuncStatus & flag) const
373 {
374         // LFUN_INSET_APPLY is sent from the dialogs when the data should
375         // be applied. This is either changed to LFUN_INSET_MODIFY (if the
376         // dialog belongs to us) or LFUN_INSET_INSERT (if the dialog does
377         // not belong to us, i. e. the dialog was open, and the user moved
378         // the cursor in our inset) in lyx::getStatus().
379         // Dialogs::checkStatus() ensures that the dialog is deactivated if
380         // LFUN_INSET_APPLY is disabled.
381
382         switch (cmd.action()) {
383         case LFUN_INSET_MODIFY:
384                 // Allow modification of our data.
385                 // This needs to be handled in the doDispatch method of our
386                 // instantiatable children.
387                 // FIXME: Why don't we let the insets determine whether this
388                 // should be enabled or not ? Now we need this check for 
389                 // the tabular features. (vfr)
390                 if (cmd.getArg(0) == "tabular")
391                         return false;
392                 flag.setEnabled(true);
393                 return true;
394
395         case LFUN_INSET_INSERT:
396                 // Don't allow insertion of new insets.
397                 // Every inset that wants to allow new insets from open
398                 // dialogs needs to override this.
399                 flag.setEnabled(false);
400                 return true;
401
402         case LFUN_INSET_SETTINGS:
403                 if (cmd.argument().empty() || cmd.getArg(0) == insetName(lyxCode())) {
404                         bool const enable = hasSettings();
405                         flag.setEnabled(enable);
406                         return true;
407                 } else {
408                         return false;
409                 }
410         
411         case LFUN_IN_MATHMACROTEMPLATE:
412                 // By default we're not in a MathMacroTemplate inset
413                 flag.setEnabled(false);
414                 return true;
415
416         default:
417                 break;
418         }
419         return false;
420 }
421
422
423 void Inset::edit(Cursor &, bool, EntryDirection)
424 {
425         LYXERR(Debug::INSETS, "edit left/right");
426 }
427
428
429 Inset * Inset::editXY(Cursor &, int x, int y)
430 {
431         LYXERR(Debug::INSETS, "x: " << x << " y: " << y);
432         return this;
433 }
434
435
436 Inset::idx_type Inset::index(row_type row, col_type col) const
437 {
438         if (row != 0)
439                 LYXERR0("illegal row: " << row);
440         if (col != 0)
441                 LYXERR0("illegal col: " << col);
442         return 0;
443 }
444
445
446 bool Inset::idxBetween(idx_type idx, idx_type from, idx_type to) const
447 {
448         return from <= idx && idx <= to;
449 }
450
451
452 bool Inset::idxUpDown(Cursor &, bool) const
453 {
454         return false;
455 }
456
457
458 int Inset::docbook(odocstream &, OutputParams const &) const
459 {
460         return 0;
461 }
462
463
464 docstring Inset::xhtml(XHTMLStream & xs, OutputParams const &) const
465 {
466         xs << "[[Inset: " << from_ascii(insetName(lyxCode())) << "]]";
467         return docstring();
468 }
469
470
471 bool Inset::directWrite() const
472 {
473         return false;
474 }
475
476
477 bool Inset::editable() const
478 {
479         return false;
480 }
481
482
483 bool Inset::hasSettings() const
484 {
485         return false;
486 }
487
488
489
490 bool Inset::autoDelete() const
491 {
492         return false;
493 }
494
495
496 void Inset::cursorPos(BufferView const & /*bv*/, CursorSlice const &,
497                 bool, int & x, int & y) const
498 {
499         LYXERR0("Inset::cursorPos called directly");
500         x = 100;
501         y = 100;
502 }
503
504
505 void Inset::metricsMarkers(Dimension & dim, int framesize) const
506 {
507         dim.wid += 2 * framesize;
508         dim.des += framesize;
509 }
510
511
512 void Inset::metricsMarkers2(Dimension & dim, int framesize) const
513 {
514         dim.wid += 2 * framesize;
515         dim.asc += framesize;
516         dim.des += framesize;
517 }
518
519
520 void Inset::drawMarkers(PainterInfo & pi, int x, int y) const
521 {
522         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
523                 Color_mathframe : Color_mathcorners;
524
525         Dimension const dim = dimension(*pi.base.bv);
526
527         int const t = x + dim.width() - 1;
528         int const d = y + dim.descent();
529         pi.pain.line(x, d - 3, x, d, pen_color);
530         pi.pain.line(t, d - 3, t, d, pen_color);
531         pi.pain.line(x, d, x + 3, d, pen_color);
532         pi.pain.line(t - 3, d, t, d, pen_color);
533         setPosCache(pi, x, y);
534 }
535
536
537 void Inset::drawMarkers2(PainterInfo & pi, int x, int y) const
538 {
539         ColorCode pen_color = mouseHovered(pi.base.bv) || editing(pi.base.bv)?
540                 Color_mathframe : Color_mathcorners;
541
542         drawMarkers(pi, x, y);
543         Dimension const dim = dimension(*pi.base.bv);
544         int const t = x + dim.width() - 1;
545         int const a = y - dim.ascent();
546         pi.pain.line(x, a + 3, x, a, pen_color);
547         pi.pain.line(t, a + 3, t, a, pen_color);
548         pi.pain.line(x, a, x + 3, a, pen_color);
549         pi.pain.line(t - 3, a, t, a, pen_color);
550         setPosCache(pi, x, y);
551 }
552
553
554 bool Inset::editing(BufferView const * bv) const
555 {
556         return bv->cursor().isInside(this);
557 }
558
559
560 int Inset::xo(BufferView const & bv) const
561 {
562         return bv.coordCache().getInsets().x(this);
563 }
564
565
566 int Inset::yo(BufferView const & bv) const
567 {
568         return bv.coordCache().getInsets().y(this);
569 }
570
571
572 bool Inset::covers(BufferView const & bv, int x, int y) const
573 {
574         return bv.coordCache().getInsets().covers(this, x, y);
575 }
576
577
578 InsetLayout const & Inset::getLayout() const
579 {
580         if (!buffer_)
581                 return DocumentClass::plainInsetLayout();
582         return buffer().params().documentClass().insetLayout(name());
583 }
584
585
586 bool Inset::undefined() const
587 {
588         docstring const & n = getLayout().name();
589         return n.empty() || n == DocumentClass::plainInsetLayout().name();
590 }
591
592
593 void Inset::dump() const
594 {
595         write(lyxerr);
596 }
597
598
599 ColorCode Inset::backgroundColor(PainterInfo const & /*pi*/) const
600 {
601         return Color_none;
602 }
603
604
605 ColorCode Inset::labelColor() const
606 {
607         return Color_foreground;
608 }
609
610
611 void Inset::setPosCache(PainterInfo const & pi, int x, int y) const
612 {
613         //LYXERR("Inset: set position cache to " << x << " " << y);
614         pi.base.bv->coordCache().insets().add(this, x, y);
615 }
616
617
618 void Inset::setDimCache(MetricsInfo const & mi, Dimension const & dim) const
619 {
620         mi.base.bv->coordCache().insets().add(this, dim);
621 }
622
623
624 Buffer const * Inset::updateFrontend() const
625 {
626         //FIXME (Abdel 03/12/10): see bugs #6814 and #6949
627         // If the Buffer is null and we end up here this is most probably because we
628         // are in the CutAndPaste stack. See InsetGraphics, RenderGraphics and
629         // RenderPreview.
630         if (!buffer_)
631                 return 0;
632         return theApp() ? theApp()->updateInset(this) : 0;
633 }
634
635
636 docstring Inset::completionPrefix(Cursor const &) const 
637 {
638         return docstring();
639 }
640
641 } // namespace lyx