]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.h
* InsetCollapsable:
[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 InsetIterator;
38 class InsetLayout;
39 class InsetList;
40 class InsetMath;
41 class InsetText;
42 class LaTeXFeatures;
43 class Lexer;
44 class MathAtom;
45 class MetricsInfo;
46 class OutputParams;
47 class PainterInfo;
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         ///
159         virtual bool isFreeSpacing() const { return false; }
160         ///
161         virtual bool allowEmpty() const { return false; }
162
163         /// is this an inset that can be moved into?
164         /// FIXME: merge with editable()
165         virtual bool isActive() const { return nargs() > 0; }
166         /// Where should we go when we press the up or down cursor key?
167         virtual bool idxUpDown(Cursor & cur, bool up) const;
168         /// Move one cell to the left
169         virtual bool idxLeft(Cursor &) const { return false; }
170         /// Move one cell to the right
171         virtual bool idxRight(Cursor &) const { return false; }
172
173         /// Move one physical cell up
174         virtual bool idxNext(Cursor &) const { return false; }
175         /// Move one physical cell down
176         virtual bool idxPrev(Cursor &) const { return false; }
177
178         /// Target pos when we enter the inset from the left by pressing "Right"
179         virtual bool idxFirst(Cursor &) const { return false; }
180         /// Target pos when we enter the inset from the right by pressing "Left"
181         virtual bool idxLast(Cursor &) const { return false; }
182
183         /// Delete a cell and move cursor
184         virtual bool idxDelete(idx_type &) { return false; }
185         /// pulls cell after pressing erase
186         virtual void idxGlue(idx_type) {}
187         /// returns list of cell indices that are "between" from and to for
188         /// selection purposes
189         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
190
191         /// to which column belongs a cell with a given index?
192         virtual col_type col(idx_type) const { return 0; }
193         /// to which row belongs a cell with a given index?
194         virtual row_type row(idx_type) const { return 0; }
195         /// cell idex corresponding to row and column;
196         virtual idx_type index(row_type row, col_type col) const;
197         /// any additional x-offset when drawing a cell?
198         virtual int cellXOffset(idx_type) const { return 0; }
199         /// any additional y-offset when drawing a cell?
200         virtual int cellYOffset(idx_type) const { return 0; }
201         /// number of embedded cells
202         virtual size_t nargs() const { return 0; }
203         /// number of rows in gridlike structures
204         virtual size_t nrows() const { return 0; }
205         /// number of columns in gridlike structures
206         virtual size_t ncols() const { return 0; }
207         /// is called when the cursor leaves this inset
208         //  returns true if cursor is now invalid.
209         virtual bool notifyCursorLeaves(Cursor &) { return false; }
210         /// is called when the mouse enter or leave this inset
211         /// return true if this inset needs repaint
212         virtual bool setMouseHover(bool) { return false; }
213         /// return true if this inset is hovered (under mouse)
214         /// This is by now only used by mathed to draw corners 
215         /// (Inset::drawMarkers() and Inset::drawMarkers2()).
216         /// Other insets do not have to redefine this function to 
217         /// return the correct status of mouseHovered.
218         virtual bool mouseHovered() const { return false; }
219
220         /// request "external features"
221         virtual void validate(LaTeXFeatures &) const {}
222         /// Appends \c list with all labels found within this inset.
223         virtual void getLabelList(Buffer const &,
224                                   std::vector<docstring> & /* list */) const {}
225
226         /// describe content if cursor inside
227         virtual void infoize(odocstream &) const {}
228         /// describe content if cursor behind
229         virtual void infoize2(odocstream &) const {}
230
231         enum {
232                 PLAINTEXT_NEWLINE = 10000
233         };
234
235         /// plain text output in ucs4 encoding
236         /// return the number of characters; in case of multiple lines of
237         /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
238         virtual int plaintext(Buffer const &, odocstream &,
239                               OutputParams const &) const = 0;
240         /// docbook output
241         virtual int docbook(Buffer const &, odocstream & os,
242                             OutputParams const &) const;
243         /// the string that is passed to the TOC
244         virtual void textString(Buffer const &, odocstream &) const {}
245
246         /** This enum indicates by which means the inset can be modified:
247         - NOT_EDITABLE: the inset's content cannot be modified at all
248           (e.g. printindex, insetspecialchar)
249         - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, href)
250         - HIGHLY_EDITABLE: content can be edited on screen (normally means that
251           insettext is contained, e.g. collapsables, tabular) */
252         // FIXME: This has not yet been fully implemented to math insets
253         enum EDITABLE {
254                 ///
255                 NOT_EDITABLE = 0,
256                 ///
257                 IS_EDITABLE,
258                 ///
259                 HIGHLY_EDITABLE
260         };
261         /// what appears in the minibuffer when opening
262         virtual docstring const editMessage() const;
263         ///
264         virtual EDITABLE editable() const;
265         /// can we go further down on mouse click?
266         virtual bool descendable() const { return false; }
267         /// does this contain text that can be change track marked in DVI?
268         virtual bool canTrackChanges() const { return false; }
269         /// return true if the inset should be removed automatically
270         virtual bool autoDelete() const;
271
272         /// returns true if the inset can hold an inset of given type
273         virtual bool insetAllowed(InsetCode) const { return false; }
274         /// if this inset has paragraphs should they be output all as default
275         /// paragraphs with the default layout of the text class?
276         virtual bool forceDefaultParagraphs(idx_type) const { return false; }
277         /// Is the width forced to some value?
278         virtual bool hasFixedWidth() const { return false; }
279
280         ///
281         virtual docstring name() const;
282         ///
283         virtual InsetLayout const & getLayout(BufferParams const & bp) const;
284         /// used to toggle insets
285         /// is the inset open?
286         /// should this inset be handled like a normal charater
287         virtual bool isChar() const { return false; }
288         /// is this equivalent to a letter?
289         virtual bool isLetter() const { return false; }
290         /// is this equivalent to a space (which is BTW different from
291         /// a line separator)?
292         virtual bool isSpace() const { return false; }
293
294         enum DisplayType {
295                 Inline = 0,
296                 AlignLeft,
297                 AlignCenter,
298                 AlignRight
299         };
300
301         /// should we have a non-filled line before this inset?
302         virtual DisplayType display() const { return Inline; }
303         /// should we break lines after this inset?
304         virtual bool isLineSeparator() const { return false; }
305         /// should paragraph indendation be ommitted in any case?
306         virtual bool neverIndent(Buffer const &) const { return false; }
307         /// dumps content to lyxerr
308         virtual void dump() const;
309         /// write inset in .lyx format
310         virtual void write(Buffer const &, std::ostream &) const {}
311         /// read inset in .lyx format
312         virtual void read(Buffer const &, Lexer &) {}
313         /** Export the inset to LaTeX.
314          *  Don't use a temporary stringstream if the final output is
315          *  supposed to go to a file.
316          *  \sa Buffer::writeLaTeXSource for the reason.
317          *  \return the number of rows (\n's) of generated LaTeX code.
318          */
319         virtual int latex(Buffer const &, odocstream &,
320                           OutputParams const &) const { return 0; }
321         /// returns true to override begin and end inset in file
322         virtual bool directWrite() const;
323         ///
324         virtual bool allowSpellCheck() const { return false; }
325
326         /// if this insets owns text cells (e.g. InsetText) return cell num
327         virtual Text * getText(int /*num*/) const { return 0; }
328
329         /** Adds a LaTeX snippet to the Preview Loader for transformation
330          *  into a bitmap image. Does not start the laoding process.
331          *
332          *  Most insets have no interest in this capability, so the method
333          *  defaults to empty.
334          */
335         virtual void addPreview(graphics::PreviewLoader &) const {}
336         /// Add an entry to the TocList
337         /// pit is the ParConstIterator of the paragraph containing the inset
338         virtual void addToToc(TocList &, Buffer const &, ParConstIterator const &) const {}
339         /// report files that can be embedded with the lyx file
340         virtual void registerEmbeddedFiles(Buffer const &, EmbeddedFiles &) const {}
341         /// use embedded or external file after the embedding status of a file is changed
342         virtual void updateEmbeddedFile(Buffer const &, EmbeddedFile const &) {}
343         /// Fill keys with BibTeX information
344         virtual void fillWithBibKeys(Buffer const &,
345                 BiblioInfo &, InsetIterator const &) const {}
346         /// Update the counters of this inset and of its contents
347         virtual void updateLabels(Buffer const &, ParIterator const &) {}
348
349
350 public:
351         /// returns LyX code associated with the inset. Used for TOC, ...)
352         virtual InsetCode lyxCode() const { return NO_CODE; }
353
354         /// -1: text mode, 1: math mode, 0 undecided
355         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
356         /// return text or mathmode if that is possible to determine
357         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
358         /// returns whether this inset is allowed in other insets of given mode
359         virtual bool allowedIn(mode_type) const { return true; }
360         /**
361          * Is this inset allowed within a font change?
362          *
363          * FIXME: noFontChange means currently that the font change is closed
364          * in LaTeX before the inset, and that the contents of the inset
365          * will be in default font. This should be changed so that the inset
366          * changes the font again.
367          */
368         virtual bool noFontChange() const { return false; }
369
370         /// set the change for the entire inset
371         virtual void setChange(Change const &) {}
372         /// accept the changes within the inset
373         virtual void acceptChanges(BufferParams const &) {};
374         /// reject the changes within the inset
375         virtual void rejectChanges(BufferParams const &) {};
376
377         ///
378         virtual Dimension const dimension(BufferView const &) const;
379         ///
380         int scroll() const { return 0; }
381         ///
382         virtual ColorCode backgroundColor() const;
383         ///
384         enum CollapseStatus {
385                 Collapsed,
386                 Open
387         };
388         ///
389         virtual void setStatus(Cursor &, CollapseStatus) {}
390         //
391         enum { TEXT_TO_INSET_OFFSET = 4 };
392
393 protected:
394         Inset() {}
395
396         /// replicate ourselves
397         friend class InsetList;
398         friend class MathAtom;
399         virtual Inset * clone() const = 0;
400
401         /** The real dispatcher.
402          *  Gets normally called from Cursor::dispatch(). Cursor::dispatch()
403          *  assumes the common case of 'LFUN handled, need update'.
404          *  This has to be overriden by calling Cursor::undispatched() or
405          *  Cursor::noUpdate() if appropriate.
406          *  If you need to call the dispatch method of some inset directly
407          *  you may have to explicitly request an update at that place. Don't
408          *  do it in doDispatch(), since that causes nested updates when
409          *  called from Cursor::dispatch(), and these can lead to crashes.
410          *  \sa getStatus
411          */
412         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
413 };
414
415 } // namespace lyx
416
417 #endif