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