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