]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.h
* Inset: Prepare for an eventual merge of updateLabels() and addToToc()
[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 "ColorCode.h"
19 #include "InsetCode.h"
20
21 #include "support/strfwd.h"
22
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 Cursor;
33 class CursorSlice;
34 class Dimension;
35 class FuncRequest;
36 class FuncStatus;
37 class InsetCollapsable;
38 class InsetIterator;
39 class InsetLayout;
40 class InsetList;
41 class InsetMath;
42 class InsetText;
43 class LaTeXFeatures;
44 class Lexer;
45 class MathAtom;
46 class MetricsInfo;
47 class OutputParams;
48 class PainterInfo;
49 class ParConstIterator;
50 class ParIterator;
51 class Text;
52 class TocList;
53 class EmbeddedFile;
54 class EmbeddedFileList;
55
56
57 namespace graphics { class PreviewLoader; }
58
59
60 /// Common base class to all insets
61
62 // Do not add _any_ (non-static) data members as this would inflate
63 // everything storing large quantities of insets. Mathed e.g. would
64 // suffer.
65
66 class Inset {
67 public:
68         ///
69         enum EntryDirection {
70                 ENTRY_DIRECTION_IGNORE,
71                 ENTRY_DIRECTION_RIGHT,
72                 ENTRY_DIRECTION_LEFT,
73         };
74         ///
75         typedef ptrdiff_t  difference_type;
76         /// short of anything else reasonable
77         typedef size_t     size_type;
78         /// type for cell indices
79         typedef size_t     idx_type;
80         /// type for cursor positions
81         typedef ptrdiff_t  pos_type;
82         /// type for row numbers
83         typedef size_t     row_type;
84         /// type for column numbers
85         typedef size_t     col_type;
86
87         /// virtual base class destructor
88         virtual ~Inset() {}
89
90         /// identification as math inset
91         virtual InsetMath * asInsetMath() { return 0; }
92         /// true for 'math' math inset, but not for e.g. mbox
93         virtual bool inMathed() const { return false; }
94         /// is this inset based on the InsetText class?
95         virtual InsetText * asInsetText() { return 0; }
96         /// is this inset based on the InsetText class?
97         virtual InsetText const * asInsetText() const { return 0; }
98         /// is this inset based on the InsetCollapsable class?
99         virtual InsetCollapsable * asInsetCollapsable() { return 0; }
100         /// is this inset based on the InsetCollapsable class?
101         virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
102
103         /// the real dispatcher
104         void dispatch(Cursor & cur, FuncRequest & cmd);
105         /**
106          * \returns true if this function made a definitive decision on
107          * whether the inset wants to handle the request \p cmd or not.
108          * The result of this decision is put into \p status.
109          *
110          * Every request that is enabled in this method needs to be handled
111          * in doDispatch(). Normally we have a 1:1 relationship between the
112          * requests handled in getStatus() and doDispatch(), but there are
113          * some exceptions:
114          * - A request that is disabled in getStatus() does not need to
115          *   appear in doDispatch(). It is guaranteed that doDispatch()
116          *   is never called with this request.
117          * - A few requests are en- or disabled in Inset::getStatus().
118          *   These need to be handled in the doDispatch() methods of the
119          *   derived insets, since Inset::doDispatch() has not enough
120          *   information to handle them.
121          * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
122          *   are dispatched directly
123          */
124         virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
125                 FuncStatus & status) const;
126
127         /// cursor enters
128         virtual void edit(Cursor & cur, bool front, 
129                 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
130         /// cursor enters
131         virtual Inset * editXY(Cursor & cur, int x, int y);
132
133         /// compute the size of the object returned in dim
134         /// \retval true if metrics changed.
135         virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
136         /// draw inset and update (xo, yo)-cache
137         virtual void draw(PainterInfo & pi, int x, int y) const = 0;
138         /// draw inset selection if necessary
139         virtual void drawSelection(PainterInfo &, int, int) const {}
140         ///
141         virtual bool editing(BufferView const * bv) const;
142         ///
143         virtual bool showInsetDialog(BufferView *) const { return false; }
144
145         /// draw inset decoration if necessary.
146         /// This can use \c drawMarkers() for example.
147         virtual void drawDecoration(PainterInfo &, int, int) const {}
148         /// draw four angular markers
149         void drawMarkers(PainterInfo & pi, int x, int y) const;
150         /// draw two angular markers
151         void drawMarkers2(PainterInfo & pi, int x, int y) const;
152         /// add space for markers
153         void metricsMarkers(Dimension & dim, int framesize = 1) const;
154         /// add space for markers
155         void metricsMarkers2(Dimension & dim, int framesize = 1) const;
156         /// last drawn position for 'important' insets
157         int xo(BufferView const & bv) const;
158         /// last drawn position for 'important' insets
159         int yo(BufferView const & bv) const;
160         /// set x/y drawing position cache if available
161         virtual void setPosCache(PainterInfo const &, int, int) const;
162         ///
163         void setDimCache(MetricsInfo const &, Dimension const &) const;
164         /// do we cover screen position x/y?
165         virtual bool covers(BufferView const & bv, int x, int y) const;
166         /// get the screen positions of the cursor (see note in Cursor.cpp)
167         virtual void cursorPos(BufferView const & bv,
168                 CursorSlice const & sl, bool boundary, int & x, int & y) const;
169
170         ///
171         virtual bool isFreeSpacing() const { return false; }
172         ///
173         virtual bool allowEmpty() const { return false; }
174         /// Force inset into LTR environment if surroundings are RTL?
175         virtual bool forceLTR() const { return false; }
176
177         /// is this an inset that can be moved into?
178         /// FIXME: merge with editable()
179         virtual bool isActive() const { return nargs() > 0; }
180         /// Where should we go when we press the up or down cursor key?
181         virtual bool idxUpDown(Cursor & cur, bool up) const;
182         /// Move one cell backwards
183         virtual bool idxBackward(Cursor &) const { return false; }
184         /// Move one cell forward
185         virtual bool idxForward(Cursor &) const { return false; }
186
187         /// Move to the next cell
188         virtual bool idxNext(Cursor &) const { return false; }
189         /// Move to the previous cell
190         virtual bool idxPrev(Cursor &) const { return false; }
191
192         /// Target pos when we enter the inset while moving forward
193         virtual bool idxFirst(Cursor &) const { return false; }
194         /// Target pos when we enter the inset while moving backwards
195         virtual bool idxLast(Cursor &) const { return false; }
196
197         /// Delete a cell and move cursor
198         virtual bool idxDelete(idx_type &) { return false; }
199         /// pulls cell after pressing erase
200         virtual void idxGlue(idx_type) {}
201         /// returns list of cell indices that are "between" from and to for
202         /// selection purposes
203         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
204
205         /// to which column belongs a cell with a given index?
206         virtual col_type col(idx_type) const { return 0; }
207         /// to which row belongs a cell with a given index?
208         virtual row_type row(idx_type) const { return 0; }
209         /// cell idex corresponding to row and column;
210         virtual idx_type index(row_type row, col_type col) const;
211         /// any additional x-offset when drawing a cell?
212         virtual int cellXOffset(idx_type) const { return 0; }
213         /// any additional y-offset when drawing a cell?
214         virtual int cellYOffset(idx_type) const { return 0; }
215         /// number of embedded cells
216         virtual size_t nargs() const { return 0; }
217         /// number of rows in gridlike structures
218         virtual size_t nrows() const { return 0; }
219         /// number of columns in gridlike structures
220         virtual size_t ncols() const { return 0; }
221         /// is called when the cursor leaves this inset
222         /// returns true if cursor is now invalid. The cursor parameter
223         /// is _not_ necessarily pointing to the inset.
224         virtual bool notifyCursorLeaves(Cursor &) { return false; }
225         /// is called when the mouse enter or leave this inset
226         /// return true if this inset needs repaint
227         virtual bool setMouseHover(bool) { return false; }
228         /// return true if this inset is hovered (under mouse)
229         /// This is by now only used by mathed to draw corners 
230         /// (Inset::drawMarkers() and Inset::drawMarkers2()).
231         /// Other insets do not have to redefine this function to 
232         /// return the correct status of mouseHovered.
233         virtual bool mouseHovered() const { return false; }
234
235         /// request "external features"
236         virtual void validate(LaTeXFeatures &) const {}
237         /// Appends \c list with all labels found within this inset.
238         virtual void getLabelList(Buffer const &,
239                                   std::vector<docstring> & /* list */) const {}
240
241         /// describe content if cursor inside
242         virtual void infoize(odocstream &) const {}
243         /// describe content if cursor behind
244         virtual void infoize2(odocstream &) const {}
245
246         enum {
247                 PLAINTEXT_NEWLINE = 10000
248         };
249
250         /// plain text output in ucs4 encoding
251         /// return the number of characters; in case of multiple lines of
252         /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
253         virtual int plaintext(Buffer const &, odocstream &,
254                               OutputParams const &) const = 0;
255         /// docbook output
256         virtual int docbook(Buffer const &, odocstream & os,
257                             OutputParams const &) const;
258         /// the string that is passed to the TOC
259         virtual void textString(Buffer const &, odocstream &) const {}
260
261         /** This enum indicates by which means the inset can be modified:
262         - NOT_EDITABLE: the inset's content cannot be modified at all
263           (e.g. printindex, insetspecialchar)
264         - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, href)
265         - HIGHLY_EDITABLE: content can be edited on screen (normally means that
266           insettext is contained, e.g. collapsables, tabular) */
267         // FIXME: This has not yet been fully implemented to math insets
268         enum EDITABLE {
269                 ///
270                 NOT_EDITABLE = 0,
271                 ///
272                 IS_EDITABLE,
273                 ///
274                 HIGHLY_EDITABLE
275         };
276         /// what appears in the minibuffer when opening
277         virtual docstring const editMessage() const;
278         ///
279         virtual EDITABLE editable() const;
280         /// can we go further down on mouse click?
281         virtual bool descendable() const { return false; }
282         /// does this contain text that can be change track marked in DVI?
283         virtual bool canTrackChanges() const { return false; }
284         /// return true if the inset should be removed automatically
285         virtual bool autoDelete() const;
286
287         /// returns true if the inset can hold an inset of given type
288         virtual bool insetAllowed(InsetCode) const { return false; }
289         /// should this inset use the empty layout by default rather than 
290         /// the standard layout? (default: only if that is forced.)
291         virtual bool useEmptyLayout() const { return forceEmptyLayout(); }
292         /// if this inset has paragraphs should they be forced to use the
293         /// empty layout?
294         virtual bool forceEmptyLayout() const { return false; }
295         /// if this inset has paragraphs should the user be allowed to
296         /// customize alignment, etc?
297         virtual bool allowParagraphCustomization(idx_type) const { return true; }
298         /// Is the width forced to some value?
299         virtual bool hasFixedWidth() const { return false; }
300
301         /// \return Tool tip for this inset.
302         /// This default implementation returns an empty string.
303         virtual docstring toolTip(BufferView const & bv, int x, int y) const;
304         
305         /// \return Context menu identifier for this inset.
306         /// This default implementation returns an empty string.
307         virtual docstring contextMenu(BufferView const & bv, int x, int y) const;
308
309         // FIXME This should really disappear in favor of 
310         //      docstring name() const { return from_ascii(insetName(lyxCode()))); }
311         // There's no reason to be using different names in different places.
312         // But to do this we would need to change the file format, since the names
313         // used there don't correspond to what is used here. 
314         ///
315         virtual docstring name() const;
316         ///
317         virtual InsetLayout const & getLayout(BufferParams const & bp) const;
318         /// used to toggle insets
319         /// is the inset open?
320         /// should this inset be handled like a normal charater
321         virtual bool isChar() const { return false; }
322         /// is this equivalent to a letter?
323         virtual bool isLetter() const { return false; }
324         /// is this equivalent to a space (which is BTW different from
325         /// a line separator)?
326         virtual bool isSpace() const { return false; }
327
328         enum DisplayType {
329                 Inline = 0,
330                 AlignLeft,
331                 AlignCenter,
332                 AlignRight
333         };
334
335         /// should we have a non-filled line before this inset?
336         virtual DisplayType display() const { return Inline; }
337         /// should we break lines after this inset?
338         virtual bool isLineSeparator() const { return false; }
339         /// should paragraph indendation be ommitted in any case?
340         virtual bool neverIndent(Buffer const &) const { return false; }
341         /// dumps content to lyxerr
342         virtual void dump() const;
343         /// write inset in .lyx format
344         virtual void write(Buffer const &, std::ostream &) const {}
345         /// read inset in .lyx format
346         virtual void read(Buffer const &, Lexer &) {}
347         /** Export the inset to LaTeX.
348          *  Don't use a temporary stringstream if the final output is
349          *  supposed to go to a file.
350          *  \sa Buffer::writeLaTeXSource for the reason.
351          *  \return the number of rows (\n's) of generated LaTeX code.
352          */
353         virtual int latex(Buffer const &, odocstream &,
354                           OutputParams const &) const { return 0; }
355         /// returns true to override begin and end inset in file
356         virtual bool directWrite() const;
357         ///
358         virtual bool allowSpellCheck() const { return false; }
359
360         /// if this insets owns text cells (e.g. InsetText) return cell num
361         virtual Text * getText(int /*num*/) const { return 0; }
362
363         /** Adds a LaTeX snippet to the Preview Loader for transformation
364          *  into a bitmap image. Does not start the laoding process.
365          *
366          *  Most insets have no interest in this capability, so the method
367          *  defaults to empty.
368          */
369         virtual void addPreview(graphics::PreviewLoader &) const {}
370         /// Add an entry to the TocList
371         /// pit is the ParConstIterator of the paragraph containing the inset
372         virtual void addToToc(Buffer const &, ParConstIterator const &) const {}
373         /// report files that can be embedded with the lyx file
374         virtual void registerEmbeddedFiles(Buffer const &, EmbeddedFileList &) const {}
375         /// use embedded or external file after the embedding status of a file is changed
376         virtual void updateEmbeddedFile(Buffer const &, EmbeddedFile const &) {}
377         /// Fill keys with BibTeX information
378         virtual void fillWithBibKeys(Buffer const &,
379                 BiblioInfo &, InsetIterator const &) const {}
380         /// Update the counters of this inset and of its contents
381         virtual void updateLabels(Buffer const &, ParIterator const &) {}
382
383         /// Updates the inset's dialog
384         virtual Buffer const * updateFrontend() const;
385
386 public:
387         /// returns LyX code associated with the inset. Used for TOC, ...)
388         virtual InsetCode lyxCode() const { return NO_CODE; }
389
390         /// -1: text mode, 1: math mode, 0 undecided
391         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
392         /// return text or mathmode if that is possible to determine
393         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
394         /// returns whether this inset is allowed in other insets of given mode
395         virtual bool allowedIn(mode_type) const { return true; }
396         /**
397          * Is this inset allowed within a font change?
398          *
399          * FIXME: noFontChange means currently that the font change is closed
400          * in LaTeX before the inset, and that the contents of the inset
401          * will be in default font. This should be changed so that the inset
402          * changes the font again.
403          */
404         virtual bool noFontChange() const { return false; }
405
406         /// set the change for the entire inset
407         virtual void setChange(Change const &) {}
408         /// accept the changes within the inset
409         virtual void acceptChanges(BufferParams const &) {};
410         /// reject the changes within the inset
411         virtual void rejectChanges(BufferParams const &) {};
412
413         ///
414         virtual Dimension const dimension(BufferView const &) const;
415         ///
416         int scroll() const { return 0; }
417         ///
418         virtual ColorCode backgroundColor() const;
419         ///
420         enum CollapseStatus {
421                 Collapsed,
422                 Open
423         };
424         ///
425         virtual void setStatus(Cursor &, CollapseStatus) {}
426         //
427         enum { TEXT_TO_INSET_OFFSET = 4 };
428
429 protected:
430         Inset() {}
431
432         /// replicate ourselves
433         friend class InsetList;
434         friend class MathAtom;
435         virtual Inset * clone() const = 0;
436
437         /** The real dispatcher.
438          *  Gets normally called from Cursor::dispatch(). Cursor::dispatch()
439          *  assumes the common case of 'LFUN handled, need update'.
440          *  This has to be overriden by calling Cursor::undispatched() or
441          *  Cursor::noUpdate() if appropriate.
442          *  If you need to call the dispatch method of some inset directly
443          *  you may have to explicitly request an update at that place. Don't
444          *  do it in doDispatch(), since that causes nested updates when
445          *  called from Cursor::dispatch(), and these can lead to crashes.
446          *  \sa getStatus
447          */
448         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
449 };
450
451 } // namespace lyx
452
453 #endif