]> git.lyx.org Git - lyx.git/blob - src/insets/insetbase.h
* BufferParams:
[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 "changes.h"
16 #include "dimension.h"
17
18 #include "support/docstream.h"
19
20 #include <memory>
21 #include <vector>
22
23 namespace lyx {
24
25 class Buffer;
26 class BufferParams;
27 class BufferView;
28 class CursorSlice;
29 class FuncRequest;
30 class FuncStatus;
31 class InsetMath;
32 class InsetText;
33 class LaTeXFeatures;
34 class LCursor;
35 class LyXLex;
36 class LyXText;
37 class MetricsInfo;
38 class Dimension;
39 class PainterInfo;
40 class OutputParams;
41 class TocList;
42
43
44 namespace graphics { class PreviewLoader; }
45
46
47 /// Common base class to all insets
48
49 // Do not add _any_ (non-static) data members as this would inflate
50 // everything storing large quantities of insets. Mathed e.g. would
51 // suffer.
52
53 class InsetBase {
54 public:
55         ///
56         typedef ptrdiff_t  difference_type;
57         /// short of anything else reasonable
58         typedef size_t     size_type;
59         /// type for cell indices
60         typedef size_t     idx_type;
61         /// type for cursor positions
62         typedef ptrdiff_t  pos_type;
63         /// type for row numbers
64         typedef size_t     row_type;
65         /// type for column numbers
66         typedef size_t     col_type;
67
68         /// virtual base class destructor
69         virtual ~InsetBase() {}
70         /// replicate ourselves
71         std::auto_ptr<InsetBase> clone() const;
72
73         /// identification as math inset
74         virtual InsetMath * asInsetMath() { return 0; }
75         /// true for 'math' math inset, but not for e.g. mbox
76         virtual bool inMathed() const { return false; }
77
78         /// the real dispatcher
79         void dispatch(LCursor & cur, FuncRequest & cmd);
80         /**
81          * \returns true if this function made a definitive decision on
82          * whether the inset wants to handle the request \p cmd or not.
83          * The result of this decision is put into \p status.
84          *
85          * Every request that is enabled in this method needs to be handled
86          * in doDispatch(). Normally we have a 1:1 relationship between the
87          * requests handled in getStatus() and doDispatch(), but there are
88          * some exceptions:
89          * - A request that is disabled in getStatus() does not need to
90          *   appear in doDispatch(). It is guaranteed that doDispatch()
91          *   is never called with this request.
92          * - A few requests are en- or disabled in InsetBase::getStatus().
93          *   These need to be handled in the doDispatch() methods of the
94          *   derived insets, since InsetBase::doDispatch() has not enough
95          *   information to handle them.
96          * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
97          *   are dispatched directly
98          */
99         virtual bool getStatus(LCursor & cur, FuncRequest const & cmd,
100                 FuncStatus & status) const;
101
102         /// cursor enters
103         virtual void edit(LCursor & cur, bool left);
104         /// cursor enters
105         virtual InsetBase * editXY(LCursor & cur, int x, int y);
106
107         /// compute the size of the object returned in dim
108         /// \retval true if metrics changed.
109         virtual bool metrics(MetricsInfo & mi, Dimension & dim) const = 0;
110         /// draw inset and update (xo, yo)-cache
111         virtual void draw(PainterInfo & pi, int x, int y) const = 0;
112         /// draw inset selection if necessary
113         virtual void drawSelection(PainterInfo &, int, int) const {}
114         ///
115         virtual bool editing(BufferView * bv) const;
116         ///
117         virtual bool showInsetDialog(BufferView *) const { return false; }
118
119         /// draw inset decoration if necessary.
120         /// This can use \c drawMarkers() for example.
121         virtual void drawDecoration(PainterInfo &, int, int) const {}
122         /// draw four angular markers
123         void drawMarkers(PainterInfo & pi, int x, int y) const;
124         /// draw two angular markers
125         void drawMarkers2(PainterInfo & pi, int x, int y) const;
126         /// add space for markers
127         void metricsMarkers(Dimension & dim, int framesize = 1) const;
128         /// add space for markers
129         void metricsMarkers2(Dimension & dim, int framesize = 1) const;
130         /// last drawn position for 'important' insets
131         int xo(BufferView const & bv) const;
132         /// last drawn position for 'important' insets
133         int yo(BufferView const & bv) const;
134         /// set x/y drawing position cache if available
135         virtual void setPosCache(PainterInfo const &, int, int) const {}
136         /// do we cover screen position x/y?
137         virtual bool covers(BufferView const & bv, int x, int y) const;
138         /// get the screen positions of the cursor (see note in cursor.C)
139         virtual void cursorPos(BufferView const & bv,
140                 CursorSlice const & sl, bool boundary, int & x, int & y) const;
141
142         /// is this an inset that can be moved into?
143         virtual bool isActive() const { return nargs() > 0; }
144         /// Where should we go when we press the up or down cursor key?
145         virtual bool idxUpDown(LCursor & cur, bool up) const;
146         /// Move one cell to the left
147         virtual bool idxLeft(LCursor &) const { return false; }
148         /// Move one cell to the right
149         virtual bool idxRight(LCursor &) const { return false; }
150
151         /// Move one physical cell up
152         virtual bool idxNext(LCursor &) const { return false; }
153         /// Move one physical cell down
154         virtual bool idxPrev(LCursor &) const { return false; }
155
156         /// Target pos when we enter the inset from the left by pressing "Right"
157         virtual bool idxFirst(LCursor &) const { return false; }
158         /// Target pos when we enter the inset from the right by pressing "Left"
159         virtual bool idxLast(LCursor &) const { return false; }
160
161         /// Delete a cell and move cursor
162         virtual bool idxDelete(idx_type &) { return false; }
163         /// pulls cell after pressing erase
164         virtual void idxGlue(idx_type) {}
165         /// returns list of cell indices that are "between" from and to for
166         /// selection purposes
167         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
168
169         /// to which column belongs a cell with a given index?
170         virtual col_type col(idx_type) const { return 0; }
171         /// to which row belongs a cell with a given index?
172         virtual row_type row(idx_type) const { return 0; }
173         /// cell idex corresponding to row and column;
174         virtual idx_type index(row_type row, col_type col) const;
175         /// any additional x-offset when drawing a cell?
176         virtual int cellXOffset(idx_type) const { return 0; }
177         /// any additional y-offset when drawing a cell?
178         virtual int cellYOffset(idx_type) const { return 0; }
179         /// number of embedded cells
180         virtual size_t nargs() const { return 0; }
181         /// number of rows in gridlike structures
182         virtual size_t nrows() const { return 0; }
183         /// number of columns in gridlike structures
184         virtual size_t ncols() const { return 0; }
185         /// is called when the cursor leaves this inset
186         //  returns true if cursor is now invalid.
187         virtual bool notifyCursorLeaves(LCursor &) { return false; }
188         /// is called when the mouse enter or leave this inset
189         /// return true if this inset needs repaint
190         virtual bool setMouseHover(bool) { return false; }
191
192         /// request "external features"
193         virtual void validate(LaTeXFeatures &) const {}
194         /// Appends \c list with all labels found within this inset.
195         virtual void getLabelList(Buffer const &,
196                                   std::vector<docstring> & /* list */) const {}
197
198         /// describe content if cursor inside
199         virtual void infoize(odocstream &) const {}
200         /// describe content if cursor behind
201         virtual void infoize2(odocstream &) const {}
202
203         enum {
204                 PLAINTEXT_NEWLINE = 10000
205         };
206
207         /// plain text output in ucs4 encoding
208         /// return the number of characters; in case of multiple lines of
209         /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
210         virtual int plaintext(Buffer const &, odocstream &,
211                               OutputParams const &) const = 0;
212         /// docbook output
213         virtual int docbook(Buffer const &, odocstream & os,
214                             OutputParams const &) const;
215         /// the string that is passed to the TOC
216         virtual void textString(Buffer const &, odocstream &) const {}
217
218         /** This enum indicates by which means the inset can be modified:
219         - NOT_EDITABLE: the inset's content cannot be modified at all
220           (e.g. printindex, insetspecialchar)
221         - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, url)
222         - HIGHLY_EDITABLE: content can be edited on screen (normally means that
223           insettext is contained, e.g. collapsables, tabular) */
224         // FIXME: This has not yet been fully implemented to math insets
225         enum EDITABLE {
226                 ///
227                 NOT_EDITABLE = 0,
228                 ///
229                 IS_EDITABLE,
230                 ///
231                 HIGHLY_EDITABLE
232         };
233         /// what appears in the minibuffer when opening
234         virtual docstring const editMessage() const;
235         ///
236         virtual EDITABLE editable() const;
237         /// can we go further down on mouse click?
238         virtual bool descendable() const { return false; }
239         /// does this contain text that can be change track marked in DVI?
240         virtual bool canTrackChanges() const { return false; }
241         /// is this inset based on the TextInset class?
242         virtual InsetText const * asTextInset() const { return 0; }
243         /// return true if the inset should be removed automatically
244         virtual bool autoDelete() const;
245
246         /** This is not quite the correct place for this enum. I think
247             the correct would be to let each subclass of Inset declare
248             its own enum code. Actually the notion of an InsetBase::Code
249             should be avoided, but I am not sure how this could be done
250             in a cleaner way. */
251         enum Code {
252                 ///
253                 NO_CODE, // 0
254                 ///
255                 TOC_CODE,  // do these insets really need a code? (ale)
256                 ///
257                 QUOTE_CODE,
258                 ///
259                 MARK_CODE,
260                 ///
261                 REF_CODE,
262                 ///
263                 URL_CODE, // 5
264                 ///
265                 HTMLURL_CODE,
266                 ///
267                 SEPARATOR_CODE,
268                 ///
269                 ENDING_CODE,
270                 ///
271                 LABEL_CODE,
272                 ///
273                 NOTE_CODE, // 10
274                 ///
275                 ACCENT_CODE,
276                 ///
277                 MATH_CODE,
278                 ///
279                 INDEX_CODE,
280                 ///
281                 INCLUDE_CODE,
282                 ///
283                 GRAPHICS_CODE, // 15
284                 ///
285                 BIBITEM_CODE,
286                 ///
287                 BIBTEX_CODE,
288                 ///
289                 TEXT_CODE,
290                 ///
291                 ERT_CODE,
292                 ///
293                 FOOT_CODE, // 20
294                 ///
295                 MARGIN_CODE,
296                 ///
297                 FLOAT_CODE,
298                 ///
299                 WRAP_CODE,
300                 ///
301                 SPACE_CODE, // 25
302                 ///
303                 SPECIALCHAR_CODE,
304                 ///
305                 TABULAR_CODE,
306                 ///
307                 EXTERNAL_CODE,
308 #if 0
309                 ///
310                 THEOREM_CODE,
311 #endif
312                 ///
313                 CAPTION_CODE,
314                 ///
315                 MATHMACRO_CODE, // 30
316                 ///
317                 CITE_CODE,
318                 ///
319                 FLOAT_LIST_CODE,
320                 ///
321                 INDEX_PRINT_CODE,
322                 ///
323                 OPTARG_CODE, // 35
324                 ///
325                 ENVIRONMENT_CODE,
326                 ///
327                 HFILL_CODE,
328                 ///
329                 NEWLINE_CODE,
330                 ///
331                 LINE_CODE,
332                 ///
333                 BRANCH_CODE, // 40
334                 ///
335                 BOX_CODE,
336                 ///
337                 CHARSTYLE_CODE,
338                 ///
339                 VSPACE_CODE,
340                 ///
341                 MATHMACROARG_CODE,
342                 ///
343                 NOMENCL_CODE, // 45
344                 ///
345                 NOMENCL_PRINT_CODE,
346                 ///
347                 PAGEBREAK_CODE
348         };
349
350         /** returns the Code corresponding to the \c name.
351          *  Eg, translate("branch") == BRANCH_CODE
352          */
353         static Code translate(std::string const & name);
354
355         /// returns true if the inset can hold an inset of given type
356         virtual bool insetAllowed(Code) const { return false; }
357         /// if this inset has paragraphs should they be output all as default
358         /// paragraphs with the default layout of the text class?
359         virtual bool forceDefaultParagraphs(idx_type) const { return false; }
360
361         ///
362         virtual docstring const & getInsetName() const;
363         /// used to toggle insets
364         /// is the inset open?
365         /// should this inset be handled like a normal charater
366         virtual bool isChar() const { return false; }
367         /// is this equivalent to a letter?
368         virtual bool isLetter() const { return false; }
369         /// is this equivalent to a space (which is BTW different from
370         /// a line separator)?
371         virtual bool isSpace() const { return false; }
372         /// should we have a non-filled line before this inset?
373         virtual bool display() const { return false; }
374         /// should we break lines after this inset?
375         virtual bool isLineSeparator() const { return false; }
376         /// should paragraph indendation be ommitted in any case?
377         virtual bool neverIndent(Buffer const &) const { return false; }
378         /// dumps content to lyxerr
379         virtual void dump() const;
380         /// write inset in .lyx format
381         virtual void write(Buffer const &, std::ostream &) const {}
382         /// read inset in .lyx format
383         virtual void read(Buffer const &, LyXLex &) {}
384         /** Export the inset to LaTeX.
385          *  Don't use a temporary stringstream if the final output is
386          *  supposed to go to a file.
387          *  \sa Buffer::writeLaTeXSource for the reason.
388          *  \return the number of rows (\n's) of generated LaTeX code.
389          */
390         virtual int latex(Buffer const &, odocstream &,
391                           OutputParams const &) const { return 0; }
392         /// returns true to override begin and end inset in file
393         virtual bool directWrite() const;
394         ///
395         virtual bool allowSpellCheck() const { return false; }
396
397         /// if this insets owns text cells (e.g. InsetText) return cell num
398         virtual LyXText * getText(int /*num*/) const { return 0; }
399
400         /** Adds a LaTeX snippet to the Preview Loader for transformation
401          *  into a bitmap image. Does not start the laoding process.
402          *
403          *  Most insets have no interest in this capability, so the method
404          *  defaults to empty.
405          */
406         virtual void addPreview(graphics::PreviewLoader &) const {}
407         /// Add an entry to the TocList
408         virtual void addToToc(TocList &, Buffer const &) const {}
409
410 public:
411         /// returns LyX code associated with the inset. Used for TOC, ...)
412         virtual Code lyxCode() const { return NO_CODE; }
413
414         /// -1: text mode, 1: math mode, 0 undecided
415         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
416         /// return text or mathmode if that is possible to determine
417         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
418         /// returns whether this inset is allowed in other insets of given mode
419         virtual bool allowedIn(mode_type) const { return true; }
420         /**
421          * Is this inset allowed within a font change?
422          *
423          * FIXME: noFontChange means currently that the font change is closed
424          * in LaTeX before the inset, and that the contents of the inset
425          * will be in default font. This should be changed so that the inset
426          * changes the font again.
427          */
428         virtual bool noFontChange() const { return false; }
429
430         /// set the change for the entire inset
431         virtual void setChange(Change const &) {}
432         /// accept the changes within the inset
433         virtual void acceptChanges(BufferParams const &) {};
434         /// reject the changes within the inset
435         virtual void rejectChanges(BufferParams const &) {};
436
437         /// pretty arbitrary
438         virtual int width() const { return 10; }
439         /// pretty arbitrary
440         virtual int ascent() const { return 10; }
441         /// pretty arbitrary
442         virtual int descent() const { return 10; }
443         ///
444         int scroll() const { return 0; }
445         ///
446         enum CollapseStatus {
447                 Collapsed,
448                 Inlined,
449                 Open
450         };
451         ///
452         virtual void setStatus(LCursor &, CollapseStatus) {}
453 protected:
454         InsetBase() {}
455         InsetBase(InsetBase const &) {}
456         /** The real dispatcher.
457          *  Gets normally called from LCursor::dispatch(). LCursor::dispatch()
458          *  assumes the common case of 'LFUN handled, need update'.
459          *  This has to be overriden by calling LCursor::undispatched() or
460          *  LCursor::noUpdate() if appropriate.
461          *  If you need to call the dispatch method of some inset directly
462          *  you may have to explicitly request an update at that place. Don't
463          *  do it in doDispatch(), since that causes nested updates when
464          *  called from LCursor::dispatch(), and these can lead to crashes.
465          *  \sa getStatus
466          */
467         virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
468
469         /// Cached dimensions of the inset.
470         mutable Dimension dim_;
471 private:
472         virtual std::auto_ptr<InsetBase> doClone() const = 0;
473 };
474
475
476 /**
477  * returns true if pointer argument is valid
478  * and points to an editable inset
479  */
480 bool isEditableInset(InsetBase const * inset);
481
482
483 /**
484  * returns true if pointer argument is valid
485  * and points to a highly editable inset
486  */
487 bool isHighlyEditableInset(InsetBase const * inset);
488
489 } // namespace lyx
490
491 #endif