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