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