]> git.lyx.org Git - lyx.git/blob - src/insets/insetbase.h
* dispatchresult.h: rename 'FINISHED' to 'FINISHED_LEFT'
[lyx.git] / src / insets / insetbase.h
1 // -*- C++ -*-
2 /**
3  * \file insetbase.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author none
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef INSETBASE_H
13 #define INSETBASE_H
14
15 #include <string>
16 #include <vector>
17 #include <memory>
18
19 class Buffer;
20 class BufferView;
21 class CursorSlice;
22 class DispatchResult;
23 class FuncRequest;
24 class FuncStatus;
25 class LaTeXFeatures;
26 class LCursor;
27 class LyXLex;
28 class LyXText;
29 class MathInset;
30 class MetricsInfo;
31 class Dimension;
32 class PainterInfo;
33 class OutputParams;
34 class UpdatableInset;
35
36 namespace lyx { namespace graphics { class PreviewLoader; } }
37
38
39
40 /// Common base class to all insets
41
42 // Do not add _any_ (non-static) data members as this would inflate
43 // everything storing large quantities of insets. Mathed e.g. would
44 // suffer.
45
46 class InsetBase {
47 public:
48         ///
49         typedef ptrdiff_t  difference_type;
50         /// short of anything else reasonable
51         typedef size_t     size_type;
52         /// type for cell indices
53         typedef size_t     idx_type;
54         /// type for cursor positions
55         typedef ptrdiff_t  pos_type;
56         /// type for row numbers
57         typedef size_t     row_type;
58         /// type for column numbers
59         typedef size_t     col_type;
60
61         /// virtual base class destructor
62         virtual ~InsetBase() {}
63         /// replicate ourselves
64         virtual std::auto_ptr<InsetBase> clone() const = 0;
65
66         /// identification as math inset
67         virtual MathInset * asMathInset() { return 0; }
68         /// identification as non-math inset
69         virtual UpdatableInset * asUpdatableInset() { return 0; }
70
71         // the real dispatcher
72         void dispatch(LCursor & cur, FuncRequest const & cmd);
73         /// do we want to handle this event?
74         virtual bool getStatus(LCursor & cur, FuncRequest const & cmd,
75                 FuncStatus & status);
76
77         /// cursor enters
78         virtual void edit(LCursor & cur, bool left);
79         /// cursor enters
80         virtual InsetBase * editXY(LCursor & cur, int x, int y);
81
82         /// compute the size of the object returned in dim
83         virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
84         /// draw inset and update (xo, yo)-cache
85         virtual void draw(PainterInfo & pi, int x, int y) const = 0;
86         /// draw inset selection if necessary
87         virtual void drawSelection(PainterInfo &, int, int) const {}
88         ///
89         virtual bool editing(BufferView * bv) const;
90         /// draw four angular markers
91         void drawMarkers(PainterInfo & pi, int x, int y) const;
92         /// draw two angular markers
93         void drawMarkers2(PainterInfo & pi, int x, int y) const;
94         /// add space for markers
95         void metricsMarkers(Dimension & dim, int framesize = 1) const;
96         /// add space for markers
97         void metricsMarkers2(Dimension & dim, int framesize = 1) const;
98         /// last drawn position for 'important' insets
99         virtual int xo() const { return 0; }
100         /// last drawn position for 'important' insets
101         virtual int yo() const { return 0; }
102         /// set x/y drawing position cache if available
103         virtual void setPosCache(PainterInfo const &, int, int) const {}
104         /// do we cover screen position x/y?
105         virtual bool covers(int x, int y) const;
106         /// get the screen positions of the cursor (see note in cursor.C)
107         virtual void getCursorPos(CursorSlice const & cur, int & x, int & y) const;
108  
109         /// is this an inset that can be moved into?
110         virtual bool isActive() const { return nargs() > 0; }
111         /// Where should we go when we press the up or down cursor key?
112         virtual bool idxUpDown(LCursor & cur, bool up) const;
113         /// Where should we go when we press the up or down cursor key?
114         virtual bool idxUpDown2(LCursor & cur, bool up) const;
115         /// Move one cell to the left
116         virtual bool idxLeft(LCursor &) const { return false; }
117         /// Move one cell to the right
118         virtual bool idxRight(LCursor &) const { return false; }
119
120         /// Move one physical cell up
121         virtual bool idxNext(LCursor &) const { return false; }
122         /// Move one physical cell down
123         virtual bool idxPrev(LCursor &) const { return false; }
124
125         /// Target pos when we enter the inset from the left by pressing "Right"
126         virtual bool idxFirst(LCursor &) const { return false; }
127         /// Target pos when we enter the inset from the right by pressing "Left"
128         virtual bool idxLast(LCursor &) const { return false; }
129
130         /// Where should we go if we press home?
131         virtual bool idxHome(LCursor &) const { return false; }
132         /// Where should we go if we press end?
133         virtual bool idxEnd(LCursor &) const { return false; }
134
135         /// Delete a cell and move cursor
136         virtual bool idxDelete(idx_type &) { return false; }
137         /// pulls cell after pressing erase
138         virtual void idxGlue(idx_type) {}
139         // returns list of cell indices that are "between" from and to for
140         // selection purposes
141         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
142
143         /// to which column belongs a cell with a given index?
144         virtual col_type col(idx_type) const { return 0; }
145         /// to which row belongs a cell with a given index?
146         virtual row_type row(idx_type) const { return 0; }
147         /// cell idex corresponding to row and column;
148         virtual idx_type index(row_type row, col_type col) const;
149         /// any additional x-offset when drawing a cell?
150         virtual int cellXOffset(idx_type) const { return 0; }
151         /// any additional y-offset when drawing a cell?
152         virtual int cellYOffset(idx_type) const { return 0; }
153         /// can we enter this cell?
154         virtual bool validCell(idx_type) const { return true; }
155         /// number of embedded cells
156         virtual size_t nargs() const { return 0; }
157         /// number of rows in gridlike structures
158         virtual size_t nrows() const { return 0; }
159         /// number of columns in gridlike structures
160         virtual size_t ncols() const { return 0; }
161         /// is called when the cursor leaves this inset
162         virtual void notifyCursorLeaves(idx_type) {}
163
164         /// request "external features"
165         virtual void validate(LaTeXFeatures &) const {}
166         /// Appends \c list with all labels found within this inset.
167         virtual void getLabelList(Buffer const &,
168                                   std::vector<std::string> & /* list */) const {}
169
170         /// describe content if cursor inside
171         virtual void infoize(std::ostream &) const {}
172         /// describe content if cursor behind
173         virtual void infoize2(std::ostream &) const {}
174
175         /// plain ascii output
176         virtual int plaintext(Buffer const &, std::ostream & os,
177                 OutputParams const &) const;
178         /// linuxdoc output
179         virtual int linuxdoc(Buffer const &, std::ostream & os,
180                 OutputParams const &) const;
181         /// docbook output
182         virtual int docbook(Buffer const &, std::ostream & os,
183                 OutputParams const &) const;
184
185         ///
186         enum EDITABLE {
187                 ///
188                 NOT_EDITABLE = 0,
189                 ///
190                 IS_EDITABLE,
191                 ///
192                 HIGHLY_EDITABLE
193         };
194         /// what appears in the minibuffer when opening
195         virtual std::string const editMessage() const;
196         ///
197         virtual EDITABLE editable() const;
198         /// can we go further down on mouse click?
199         virtual bool descendable() const { return false; }
200         ///
201         virtual bool isTextInset() const { return false; }
202         /// return true if the inset should be removed automatically
203         virtual bool autoDelete() const;
204
205         /** This is not quite the correct place for this enum. I think
206             the correct would be to let each subclass of Inset declare
207             its own enum code. Actually the notion of an InsetOld::Code
208             should be avoided, but I am not sure how this could be done
209             in a cleaner way. */
210         enum Code {
211                 ///
212                 NO_CODE, // 0
213                 ///
214                 TOC_CODE,  // do these insets really need a code? (ale)
215                 ///
216                 QUOTE_CODE,
217                 ///
218                 MARK_CODE,
219                 ///
220                 REF_CODE,
221                 ///
222                 URL_CODE, // 5
223                 ///
224                 HTMLURL_CODE,
225                 ///
226                 SEPARATOR_CODE,
227                 ///
228                 ENDING_CODE,
229                 ///
230                 LABEL_CODE,
231                 ///
232                 NOTE_CODE, // 10
233                 ///
234                 ACCENT_CODE,
235                 ///
236                 MATH_CODE,
237                 ///
238                 INDEX_CODE,
239                 ///
240                 INCLUDE_CODE,
241                 ///
242                 GRAPHICS_CODE, // 15
243                 ///
244                 BIBITEM_CODE,
245                 ///
246                 BIBTEX_CODE,
247                 ///
248                 TEXT_CODE,
249                 ///
250                 ERT_CODE,
251                 ///
252                 FOOT_CODE, // 20
253                 ///
254                 MARGIN_CODE,
255                 ///
256                 FLOAT_CODE,
257                 ///
258                 WRAP_CODE,
259                 ///
260                 SPACE_CODE, // 25
261                 ///
262                 SPECIALCHAR_CODE,
263                 ///
264                 TABULAR_CODE,
265                 ///
266                 EXTERNAL_CODE,
267 #if 0
268                 ///
269                 THEOREM_CODE,
270 #endif
271                 ///
272                 CAPTION_CODE,
273                 ///
274                 MATHMACRO_CODE, // 30
275                 ///
276                 ERROR_CODE,
277                 ///
278                 CITE_CODE,
279                 ///
280                 FLOAT_LIST_CODE,
281                 ///
282                 INDEX_PRINT_CODE,
283                 ///
284                 OPTARG_CODE, // 35
285                 ///
286                 ENVIRONMENT_CODE,
287                 ///
288                 HFILL_CODE,
289                 ///
290                 NEWLINE_CODE,
291                 ///
292                 LINE_CODE,
293                 ///
294                 BRANCH_CODE, // 40
295                 ///
296                 BOX_CODE,
297                 ///
298                 CHARSTYLE_CODE,
299                 ///
300                 VSPACE_CODE,
301                 ///
302                 MATHGRID_CODE,
303                 ///
304                 MATHHULL_CODE
305         };
306         /// returns true the inset can hold an inset of given type
307         virtual bool insetAllowed(Code) const { return false; }
308         // if this inset has paragraphs should they be output all as default
309         // paragraphs with "Standard" layout?
310         virtual bool forceDefaultParagraphs(InsetBase const *) const { return false; }
311         ///
312         virtual std::string const & getInsetName() const;
313         /// used to toggle insets
314         // is the inset open?
315         virtual bool isOpen() const { return false; }
316         /// open the inset
317         virtual void open() {}
318         /// close the inset
319         virtual void close() {}
320         // should this inset be handled like a normal charater
321         virtual bool isChar() const { return false; }
322         // is this equivalent to a letter?
323         virtual bool isLetter() const { return false; }
324         // is this equivalent to a space (which is BTW different from
325         // a line separator)?
326         virtual bool isSpace() const { return false; }
327         // should we have a non-filled line before this inset?
328         virtual bool display() const { return false; }
329         // should we break lines after this inset?
330         virtual bool isLineSeparator() const { return false; }
331         /// dumps content to lyxerr
332         virtual void dump() const;
333         ///
334         virtual void write(Buffer const &, std::ostream &) const {}
335         ///
336         virtual void read(Buffer const &, LyXLex &) {}
337         /// returns the number of rows (\n's) of generated tex code.
338         virtual int latex(Buffer const &, std::ostream &,
339                           OutputParams const &) const { return 0; }
340         /// returns true to override begin and end inset in file
341         virtual bool directWrite() const;
342         ///
343         virtual bool allowSpellCheck() const { return false; }
344
345         /// if this insets owns text cells (e.g. InsetText) return cell num
346         virtual LyXText * getText(int /*num*/) const { return 0; }
347
348         /** Adds a LaTeX snippet to the Preview Loader for transformation
349          *  into a bitmap image. Does not start the laoding process.
350          *
351          *  Most insets have no interest in this capability, so the method
352          *  defaults to empty.
353          */
354         virtual void addPreview(lyx::graphics::PreviewLoader &) const {}
355 protected:
356         // the real dispatcher
357         virtual void priv_dispatch(LCursor & cur, FuncRequest const & cmd);
358 public:
359         /// returns LyX code associated with the inset. Used for TOC, ...)
360         virtual Code lyxCode() const { return NO_CODE; }
361
362         /// -1: text mode, 1: math mode, 0 undecided
363         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
364         /// return text or mathmode if that is possible to determine
365         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
366
367         /// FIXME: This ought to die.
368         virtual void setOwner(UpdatableInset *) {}
369         ///
370         virtual UpdatableInset * owner() const { return 0; }
371
372         /// is this inset allowed within a font change?
373         virtual bool noFontChange() const { return false; }
374
375         ///
376         virtual void markErased();
377         /// pretty arbitrary
378         virtual int width() const { return 10; }
379         /// pretty arbitrary
380         virtual int ascent() const { return 10; }
381         /// pretty arbitrary
382         virtual int descent() const { return 10; }
383 };
384
385
386 /**
387  * returns true if pointer argument is valid
388  * and points to an editable inset
389  */
390 bool isEditableInset(InsetBase const * inset);
391
392
393 /**
394  * returns true if pointer argument is valid
395  * and points to a highly editable inset
396  */
397 bool isHighlyEditableInset(InsetBase const * inset);
398
399 #endif