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