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