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