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