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