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