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