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