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