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