]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.h
use bald pointers in clone()
[lyx.git] / src / insets / Inset.h
1 // -*- C++ -*-
2 /**
3  * \file Inset.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author Jürgen Vigna
9  * \author Lars Gullik Bjønnes
10  * \author Matthias Ettrich
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef INSETBASE_H
16 #define INSETBASE_H
17
18 #include "Dimension.h"
19
20 #include "support/docstream.h"
21
22 #include <memory>
23 #include <vector>
24
25 namespace lyx {
26
27 class BiblioInfo;
28 class Buffer;
29 class BufferParams;
30 class BufferView;
31 class Change;
32 class Color_color;
33 class Cursor;
34 class CursorSlice;
35 class FuncRequest;
36 class FuncStatus;
37 class InsetIterator;
38 class InsetLayout;
39 class InsetMath;
40 class InsetText;
41 class LaTeXFeatures;
42 class Lexer;
43 class MathAtom;
44 class MetricsInfo;
45 class OutputParams;
46 class PainterInfo;
47 class Paragraph;
48 class ParConstIterator;
49 class ParIterator;
50 class Text;
51 class TocList;
52
53
54 namespace graphics { class PreviewLoader; }
55
56
57 /// Common base class to all insets
58
59 // Do not add _any_ (non-static) data members as this would inflate
60 // everything storing large quantities of insets. Mathed e.g. would
61 // suffer.
62
63 class Inset {
64 public:
65         ///
66         typedef ptrdiff_t  difference_type;
67         /// short of anything else reasonable
68         typedef size_t     size_type;
69         /// type for cell indices
70         typedef size_t     idx_type;
71         /// type for cursor positions
72         typedef ptrdiff_t  pos_type;
73         /// type for row numbers
74         typedef size_t     row_type;
75         /// type for column numbers
76         typedef size_t     col_type;
77
78         /// virtual base class destructor
79         virtual ~Inset() {}
80
81         /// identification as math inset
82         virtual InsetMath * asInsetMath() { return 0; }
83         /// true for 'math' math inset, but not for e.g. mbox
84         virtual bool inMathed() const { return false; }
85         /// is this inset based on the TextInset class?
86         virtual InsetText * asTextInset() { return 0; }
87         /// is this inset based on the TextInset class?
88         virtual InsetText const * asTextInset() const { return 0; }
89         
90         /// the real dispatcher
91         void dispatch(Cursor & cur, FuncRequest & cmd);
92         /**
93          * \returns true if this function made a definitive decision on
94          * whether the inset wants to handle the request \p cmd or not.
95          * The result of this decision is put into \p status.
96          *
97          * Every request that is enabled in this method needs to be handled
98          * in doDispatch(). Normally we have a 1:1 relationship between the
99          * requests handled in getStatus() and doDispatch(), but there are
100          * some exceptions:
101          * - A request that is disabled in getStatus() does not need to
102          *   appear in doDispatch(). It is guaranteed that doDispatch()
103          *   is never called with this request.
104          * - A few requests are en- or disabled in Inset::getStatus().
105          *   These need to be handled in the doDispatch() methods of the
106          *   derived insets, since Inset::doDispatch() has not enough
107          *   information to handle them.
108          * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
109          *   are dispatched directly
110          */
111         virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
112                 FuncStatus & status) const;
113
114         /// cursor enters
115         virtual void edit(Cursor & cur, bool left);
116         /// cursor enters
117         virtual Inset * editXY(Cursor & cur, int x, int y);
118
119         /// compute the size of the object returned in dim
120         /// \retval true if metrics changed.
121         virtual bool metrics(MetricsInfo & mi, Dimension & dim) const = 0;
122         /// draw inset and update (xo, yo)-cache
123         virtual void draw(PainterInfo & pi, int x, int y) const = 0;
124         /// draw inset selection if necessary
125         virtual void drawSelection(PainterInfo &, int, int) const {}
126         ///
127         virtual bool editing(BufferView * bv) const;
128         ///
129         virtual bool showInsetDialog(BufferView *) const { return false; }
130
131         /// draw inset decoration if necessary.
132         /// This can use \c drawMarkers() for example.
133         virtual void drawDecoration(PainterInfo &, int, int) const {}
134         /// draw four angular markers
135         void drawMarkers(PainterInfo & pi, int x, int y) const;
136         /// draw two angular markers
137         void drawMarkers2(PainterInfo & pi, int x, int y) const;
138         /// add space for markers
139         void metricsMarkers(Dimension & dim, int framesize = 1) const;
140         /// add space for markers
141         void metricsMarkers2(Dimension & dim, int framesize = 1) const;
142         /// last drawn position for 'important' insets
143         int xo(BufferView const & bv) const;
144         /// last drawn position for 'important' insets
145         int yo(BufferView const & bv) const;
146         /// set x/y drawing position cache if available
147         virtual void setPosCache(PainterInfo const &, int, int) const;
148         /// do we cover screen position x/y?
149         virtual bool covers(BufferView const & bv, int x, int y) const;
150         /// get the screen positions of the cursor (see note in Cursor.cpp)
151         virtual void cursorPos(BufferView const & bv,
152                 CursorSlice const & sl, bool boundary, int & x, int & y) const;
153
154         /// is this an inset that can be moved into?
155         /// FIXME: merge with editable()
156         virtual bool isActive() const { return nargs() > 0; }
157         /// Where should we go when we press the up or down cursor key?
158         virtual bool idxUpDown(Cursor & cur, bool up) const;
159         /// Move one cell to the left
160         virtual bool idxLeft(Cursor &) const { return false; }
161         /// Move one cell to the right
162         virtual bool idxRight(Cursor &) const { return false; }
163
164         /// Move one physical cell up
165         virtual bool idxNext(Cursor &) const { return false; }
166         /// Move one physical cell down
167         virtual bool idxPrev(Cursor &) const { return false; }
168
169         /// Target pos when we enter the inset from the left by pressing "Right"
170         virtual bool idxFirst(Cursor &) const { return false; }
171         /// Target pos when we enter the inset from the right by pressing "Left"
172         virtual bool idxLast(Cursor &) const { return false; }
173
174         /// Delete a cell and move cursor
175         virtual bool idxDelete(idx_type &) { return false; }
176         /// pulls cell after pressing erase
177         virtual void idxGlue(idx_type) {}
178         /// returns list of cell indices that are "between" from and to for
179         /// selection purposes
180         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
181
182         /// to which column belongs a cell with a given index?
183         virtual col_type col(idx_type) const { return 0; }
184         /// to which row belongs a cell with a given index?
185         virtual row_type row(idx_type) const { return 0; }
186         /// cell idex corresponding to row and column;
187         virtual idx_type index(row_type row, col_type col) const;
188         /// any additional x-offset when drawing a cell?
189         virtual int cellXOffset(idx_type) const { return 0; }
190         /// any additional y-offset when drawing a cell?
191         virtual int cellYOffset(idx_type) const { return 0; }
192         /// number of embedded cells
193         virtual size_t nargs() const { return 0; }
194         /// number of rows in gridlike structures
195         virtual size_t nrows() const { return 0; }
196         /// number of columns in gridlike structures
197         virtual size_t ncols() const { return 0; }
198         /// is called when the cursor leaves this inset
199         //  returns true if cursor is now invalid.
200         virtual bool notifyCursorLeaves(Cursor &) { return false; }
201         /// is called when the mouse enter or leave this inset
202         /// return true if this inset needs repaint
203         virtual bool setMouseHover(bool) { return false; }
204         /// return true if this inset is hovered (under mouse)
205         /// This is by now only used by mathed to draw corners 
206         /// (Inset::drawMarkers() and Inset::drawMarkers2()).
207         /// Other insets do not have to redefine this function to 
208         /// return the correct status of mouseHovered.
209         virtual bool mouseHovered() const { return false; }
210
211         /// request "external features"
212         virtual void validate(LaTeXFeatures &) const {}
213         /// Appends \c list with all labels found within this inset.
214         virtual void getLabelList(Buffer const &,
215                                   std::vector<docstring> & /* list */) const {}
216
217         /// describe content if cursor inside
218         virtual void infoize(odocstream &) const {}
219         /// describe content if cursor behind
220         virtual void infoize2(odocstream &) const {}
221
222         enum {
223                 PLAINTEXT_NEWLINE = 10000
224         };
225
226         /// plain text output in ucs4 encoding
227         /// return the number of characters; in case of multiple lines of
228         /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
229         virtual int plaintext(Buffer const &, odocstream &,
230                               OutputParams const &) const = 0;
231         /// docbook output
232         virtual int docbook(Buffer const &, odocstream & os,
233                             OutputParams const &) const;
234         /// the string that is passed to the TOC
235         virtual void textString(Buffer const &, odocstream &) const {}
236
237         /** This enum indicates by which means the inset can be modified:
238         - NOT_EDITABLE: the inset's content cannot be modified at all
239           (e.g. printindex, insetspecialchar)
240         - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, url)
241         - HIGHLY_EDITABLE: content can be edited on screen (normally means that
242           insettext is contained, e.g. collapsables, tabular) */
243         // FIXME: This has not yet been fully implemented to math insets
244         enum EDITABLE {
245                 ///
246                 NOT_EDITABLE = 0,
247                 ///
248                 IS_EDITABLE,
249                 ///
250                 HIGHLY_EDITABLE
251         };
252         /// what appears in the minibuffer when opening
253         virtual docstring const editMessage() const;
254         ///
255         virtual EDITABLE editable() const;
256         /// can we go further down on mouse click?
257         virtual bool descendable() const { return false; }
258         /// does this contain text that can be change track marked in DVI?
259         virtual bool canTrackChanges() const { return false; }
260         /// return true if the inset should be removed automatically
261         virtual bool autoDelete() const;
262
263         /** This is not quite the correct place for this enum. I think
264             the correct would be to let each subclass of Inset declare
265             its own enum code. Actually the notion of an Inset::Code
266             should be avoided, but I am not sure how this could be done
267             in a cleaner way. */
268         enum Code {
269                 ///
270                 NO_CODE, // 0
271                 ///
272                 TOC_CODE,  // do these insets really need a code? (ale)
273                 ///
274                 QUOTE_CODE,
275                 ///
276                 MARK_CODE,
277                 ///
278                 REF_CODE,
279                 ///
280                 URL_CODE, // 5
281                 ///
282                 HTMLURL_CODE,
283                 ///
284                 SEPARATOR_CODE,
285                 ///
286                 ENDING_CODE,
287                 ///
288                 LABEL_CODE,
289                 ///
290                 NOTE_CODE, // 10
291                 ///
292                 ACCENT_CODE,
293                 ///
294                 MATH_CODE,
295                 ///
296                 INDEX_CODE,
297                 ///
298                 INCLUDE_CODE,
299                 ///
300                 GRAPHICS_CODE, // 15
301                 ///
302                 BIBITEM_CODE,
303                 ///
304                 BIBTEX_CODE,
305                 ///
306                 TEXT_CODE,
307                 ///
308                 ERT_CODE,
309                 ///
310                 FOOT_CODE, // 20
311                 ///
312                 MARGIN_CODE,
313                 ///
314                 FLOAT_CODE,
315                 ///
316                 WRAP_CODE,
317                 ///
318                 SPACE_CODE, // 25
319                 ///
320                 SPECIALCHAR_CODE,
321                 ///
322                 TABULAR_CODE,
323                 ///
324                 EXTERNAL_CODE,
325 #if 0
326                 ///
327                 THEOREM_CODE,
328 #endif
329                 ///
330                 CAPTION_CODE,
331                 ///
332                 MATHMACRO_CODE, // 30
333                 ///
334                 CITE_CODE,
335                 ///
336                 FLOAT_LIST_CODE,
337                 ///
338                 INDEX_PRINT_CODE,
339                 ///
340                 OPTARG_CODE, // 35
341                 ///
342                 ENVIRONMENT_CODE,
343                 ///
344                 HFILL_CODE,
345                 ///
346                 NEWLINE_CODE,
347                 ///
348                 LINE_CODE,
349                 ///
350                 BRANCH_CODE, // 40
351                 ///
352                 BOX_CODE,
353                 ///
354                 CHARSTYLE_CODE,
355                 ///
356                 VSPACE_CODE,
357                 ///
358                 MATHMACROARG_CODE,
359                 ///
360                 NOMENCL_CODE, // 45
361                 ///
362                 NOMENCL_PRINT_CODE,
363                 ///
364                 PAGEBREAK_CODE,
365                 ///
366                 LISTINGS_CODE
367         };
368
369         /** returns the Code corresponding to the \c name.
370          *  Eg, translate("branch") == BRANCH_CODE
371          */
372         static Code translate(std::string const & name);
373
374         /// returns true if the inset can hold an inset of given type
375         virtual bool insetAllowed(Code) const { return false; }
376         /// if this inset has paragraphs should they be output all as default
377         /// paragraphs with the default layout of the text class?
378         virtual bool forceDefaultParagraphs(idx_type) const { return false; }
379         /// Is the width forced to some value?
380         virtual bool hasFixedWidth() const { return false; }
381
382         ///
383         virtual docstring name() const { return from_ascii("unknown"); }
384         ///
385         virtual InsetLayout const & getLayout(BufferParams const & bp) const;
386         /// used to toggle insets
387         /// is the inset open?
388         /// should this inset be handled like a normal charater
389         virtual bool isChar() const { return false; }
390         /// is this equivalent to a letter?
391         virtual bool isLetter() const { return false; }
392         /// is this equivalent to a space (which is BTW different from
393         /// a line separator)?
394         virtual bool isSpace() const { return false; }
395
396         enum DisplayType {
397                 Inline = 0,
398                 AlignLeft,
399                 AlignCenter,
400                 AlignRight
401         };
402
403         /// should we have a non-filled line before this inset?
404         virtual DisplayType display() const { return Inline; }
405         /// should we break lines after this inset?
406         virtual bool isLineSeparator() const { return false; }
407         /// should paragraph indendation be ommitted in any case?
408         virtual bool neverIndent(Buffer const &) const { return false; }
409         /// dumps content to lyxerr
410         virtual void dump() const;
411         /// write inset in .lyx format
412         virtual void write(Buffer const &, std::ostream &) const {}
413         /// read inset in .lyx format
414         virtual void read(Buffer const &, Lexer &) {}
415         /** Export the inset to LaTeX.
416          *  Don't use a temporary stringstream if the final output is
417          *  supposed to go to a file.
418          *  \sa Buffer::writeLaTeXSource for the reason.
419          *  \return the number of rows (\n's) of generated LaTeX code.
420          */
421         virtual int latex(Buffer const &, odocstream &,
422                           OutputParams const &) const { return 0; }
423         /// returns true to override begin and end inset in file
424         virtual bool directWrite() const;
425         ///
426         virtual bool allowSpellCheck() const { return false; }
427
428         /// if this insets owns text cells (e.g. InsetText) return cell num
429         virtual Text * getText(int /*num*/) const { return 0; }
430
431         /** Adds a LaTeX snippet to the Preview Loader for transformation
432          *  into a bitmap image. Does not start the laoding process.
433          *
434          *  Most insets have no interest in this capability, so the method
435          *  defaults to empty.
436          */
437         virtual void addPreview(graphics::PreviewLoader &) const {}
438         /// Add an entry to the TocList
439         /// pit is the ParConstIterator of the paragraph containing the inset
440         virtual void addToToc(TocList &, Buffer const &, ParConstIterator const &) const {}
441         /// Fill keys with BibTeX information
442         virtual void fillWithBibKeys(Buffer const &,
443                 BiblioInfo &, InsetIterator const &) const { return; }
444         /// Update the counters of this inset and of its contents
445         virtual void updateLabels(Buffer const &, ParIterator const &) {}
446
447
448 public:
449         /// returns LyX code associated with the inset. Used for TOC, ...)
450         virtual Code lyxCode() const { return NO_CODE; }
451
452         /// -1: text mode, 1: math mode, 0 undecided
453         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
454         /// return text or mathmode if that is possible to determine
455         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
456         /// returns whether this inset is allowed in other insets of given mode
457         virtual bool allowedIn(mode_type) const { return true; }
458         /**
459          * Is this inset allowed within a font change?
460          *
461          * FIXME: noFontChange means currently that the font change is closed
462          * in LaTeX before the inset, and that the contents of the inset
463          * will be in default font. This should be changed so that the inset
464          * changes the font again.
465          */
466         virtual bool noFontChange() const { return false; }
467
468         /// set the change for the entire inset
469         virtual void setChange(Change const &) {}
470         /// accept the changes within the inset
471         virtual void acceptChanges(BufferParams const &) {};
472         /// reject the changes within the inset
473         virtual void rejectChanges(BufferParams const &) {};
474
475         /// FIXME: move dim_ out of Inset!
476         Dimension const & dimension() { return dim_; }
477         /// inset width.
478         int width() const { return dim_.wid; }
479         /// inset ascent.
480         int ascent() const { return dim_.asc; }
481         /// inset descent.
482         int descent() const { return dim_.des; }
483         ///
484         int scroll() const { return 0; }
485         ///
486         virtual Color_color backgroundColor() const;
487         ///
488         enum CollapseStatus {
489                 Collapsed,
490                 Open
491         };
492         ///
493         virtual void setStatus(Cursor &, CollapseStatus) {}
494         //
495         enum { TEXT_TO_INSET_OFFSET = 4 };
496
497 protected:
498         Inset();
499
500         /// replicate ourselves
501         friend class Paragraph;
502         friend class MathAtom;
503         virtual Inset * clone() const = 0;
504
505         /** The real dispatcher.
506          *  Gets normally called from Cursor::dispatch(). Cursor::dispatch()
507          *  assumes the common case of 'LFUN handled, need update'.
508          *  This has to be overriden by calling Cursor::undispatched() or
509          *  Cursor::noUpdate() if appropriate.
510          *  If you need to call the dispatch method of some inset directly
511          *  you may have to explicitly request an update at that place. Don't
512          *  do it in doDispatch(), since that causes nested updates when
513          *  called from Cursor::dispatch(), and these can lead to crashes.
514          *  \sa getStatus
515          */
516         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
517
518         /// Cached dimensions of the inset.
519         mutable Dimension dim_;
520 };
521
522
523 /**
524  * returns true if pointer argument is valid
525  * and points to an editable inset
526  */
527 bool isEditableInset(Inset const * inset);
528
529
530 /**
531  * returns true if pointer argument is valid
532  * and points to a highly editable inset
533  */
534 bool isHighlyEditableInset(Inset const * inset);
535
536 /** \c Inset_code is a wrapper for Inset::Code.
537  *  It can be forward-declared and passed as a function argument without
538  *  having to expose Inset.h.
539  */
540 class Inset_code {
541         Inset::Code val_;
542 public:
543         Inset_code(Inset::Code val) : val_(val) {}
544         operator Inset::Code() const { return val_; }
545 };
546
547
548
549 } // namespace lyx
550
551 #endif