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