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