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