]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.h
addToToc, pass parent ParConstIterator, fix bug 3711
[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 <boost/signal.hpp>
24
25 #include <memory>
26 #include <vector>
27
28 namespace lyx {
29
30 class Buffer;
31 class BufferParams;
32 class BufferView;
33 class ParConstIterator;
34 class CursorSlice;
35 class FuncRequest;
36 class FuncStatus;
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         virtual bool isActive() const { return nargs() > 0; }
155         /// Where should we go when we press the up or down cursor key?
156         virtual bool idxUpDown(Cursor & cur, bool up) const;
157         /// Move one cell to the left
158         virtual bool idxLeft(Cursor &) const { return false; }
159         /// Move one cell to the right
160         virtual bool idxRight(Cursor &) const { return false; }
161
162         /// Move one physical cell up
163         virtual bool idxNext(Cursor &) const { return false; }
164         /// Move one physical cell down
165         virtual bool idxPrev(Cursor &) const { return false; }
166
167         /// Target pos when we enter the inset from the left by pressing "Right"
168         virtual bool idxFirst(Cursor &) const { return false; }
169         /// Target pos when we enter the inset from the right by pressing "Left"
170         virtual bool idxLast(Cursor &) const { return false; }
171
172         /// Delete a cell and move cursor
173         virtual bool idxDelete(idx_type &) { return false; }
174         /// pulls cell after pressing erase
175         virtual void idxGlue(idx_type) {}
176         /// returns list of cell indices that are "between" from and to for
177         /// selection purposes
178         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
179
180         /// to which column belongs a cell with a given index?
181         virtual col_type col(idx_type) const { return 0; }
182         /// to which row belongs a cell with a given index?
183         virtual row_type row(idx_type) const { return 0; }
184         /// cell idex corresponding to row and column;
185         virtual idx_type index(row_type row, col_type col) const;
186         /// any additional x-offset when drawing a cell?
187         virtual int cellXOffset(idx_type) const { return 0; }
188         /// any additional y-offset when drawing a cell?
189         virtual int cellYOffset(idx_type) const { return 0; }
190         /// number of embedded cells
191         virtual size_t nargs() const { return 0; }
192         /// number of rows in gridlike structures
193         virtual size_t nrows() const { return 0; }
194         /// number of columns in gridlike structures
195         virtual size_t ncols() const { return 0; }
196         /// is called when the cursor leaves this inset
197         //  returns true if cursor is now invalid.
198         virtual bool notifyCursorLeaves(Cursor &) { return false; }
199         /// is called when the mouse enter or leave this inset
200         /// return true if this inset needs repaint
201         virtual bool setMouseHover(bool) { return false; }
202
203         /// request "external features"
204         virtual void validate(LaTeXFeatures &) const {}
205         /// Appends \c list with all labels found within this inset.
206         virtual void getLabelList(Buffer const &,
207                                   std::vector<docstring> & /* list */) const {}
208
209         /// describe content if cursor inside
210         virtual void infoize(odocstream &) const {}
211         /// describe content if cursor behind
212         virtual void infoize2(odocstream &) const {}
213
214         enum {
215                 PLAINTEXT_NEWLINE = 10000
216         };
217
218         /// plain text output in ucs4 encoding
219         /// return the number of characters; in case of multiple lines of
220         /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
221         virtual int plaintext(Buffer const &, odocstream &,
222                               OutputParams const &) const = 0;
223         /// docbook output
224         virtual int docbook(Buffer const &, odocstream & os,
225                             OutputParams const &) const;
226         /// the string that is passed to the TOC
227         virtual void textString(Buffer const &, odocstream &) const {}
228
229         /** This enum indicates by which means the inset can be modified:
230         - NOT_EDITABLE: the inset's content cannot be modified at all
231           (e.g. printindex, insetspecialchar)
232         - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, url)
233         - HIGHLY_EDITABLE: content can be edited on screen (normally means that
234           insettext is contained, e.g. collapsables, tabular) */
235         // FIXME: This has not yet been fully implemented to math insets
236         enum EDITABLE {
237                 ///
238                 NOT_EDITABLE = 0,
239                 ///
240                 IS_EDITABLE,
241                 ///
242                 HIGHLY_EDITABLE
243         };
244         /// what appears in the minibuffer when opening
245         virtual docstring const editMessage() const;
246         ///
247         virtual EDITABLE editable() const;
248         /// can we go further down on mouse click?
249         virtual bool descendable() const { return false; }
250         /// does this contain text that can be change track marked in DVI?
251         virtual bool canTrackChanges() const { return false; }
252         /// return true if the inset should be removed automatically
253         virtual bool autoDelete() const;
254
255         /** This is not quite the correct place for this enum. I think
256             the correct would be to let each subclass of Inset declare
257             its own enum code. Actually the notion of an Inset::Code
258             should be avoided, but I am not sure how this could be done
259             in a cleaner way. */
260         enum Code {
261                 ///
262                 NO_CODE, // 0
263                 ///
264                 TOC_CODE,  // do these insets really need a code? (ale)
265                 ///
266                 QUOTE_CODE,
267                 ///
268                 MARK_CODE,
269                 ///
270                 REF_CODE,
271                 ///
272                 URL_CODE, // 5
273                 ///
274                 HTMLURL_CODE,
275                 ///
276                 SEPARATOR_CODE,
277                 ///
278                 ENDING_CODE,
279                 ///
280                 LABEL_CODE,
281                 ///
282                 NOTE_CODE, // 10
283                 ///
284                 ACCENT_CODE,
285                 ///
286                 MATH_CODE,
287                 ///
288                 INDEX_CODE,
289                 ///
290                 INCLUDE_CODE,
291                 ///
292                 GRAPHICS_CODE, // 15
293                 ///
294                 BIBITEM_CODE,
295                 ///
296                 BIBTEX_CODE,
297                 ///
298                 TEXT_CODE,
299                 ///
300                 ERT_CODE,
301                 ///
302                 FOOT_CODE, // 20
303                 ///
304                 MARGIN_CODE,
305                 ///
306                 FLOAT_CODE,
307                 ///
308                 WRAP_CODE,
309                 ///
310                 SPACE_CODE, // 25
311                 ///
312                 SPECIALCHAR_CODE,
313                 ///
314                 TABULAR_CODE,
315                 ///
316                 EXTERNAL_CODE,
317 #if 0
318                 ///
319                 THEOREM_CODE,
320 #endif
321                 ///
322                 CAPTION_CODE,
323                 ///
324                 MATHMACRO_CODE, // 30
325                 ///
326                 CITE_CODE,
327                 ///
328                 FLOAT_LIST_CODE,
329                 ///
330                 INDEX_PRINT_CODE,
331                 ///
332                 OPTARG_CODE, // 35
333                 ///
334                 ENVIRONMENT_CODE,
335                 ///
336                 HFILL_CODE,
337                 ///
338                 NEWLINE_CODE,
339                 ///
340                 LINE_CODE,
341                 ///
342                 BRANCH_CODE, // 40
343                 ///
344                 BOX_CODE,
345                 ///
346                 CHARSTYLE_CODE,
347                 ///
348                 VSPACE_CODE,
349                 ///
350                 MATHMACROARG_CODE,
351                 ///
352                 NOMENCL_CODE, // 45
353                 ///
354                 NOMENCL_PRINT_CODE,
355                 ///
356                 PAGEBREAK_CODE,
357                 ///
358                 LISTINGS_CODE
359         };
360
361         /** returns the Code corresponding to the \c name.
362          *  Eg, translate("branch") == BRANCH_CODE
363          */
364         static Code translate(std::string const & name);
365
366         /// returns true if the inset can hold an inset of given type
367         virtual bool insetAllowed(Code) const { return false; }
368         /// if this inset has paragraphs should they be output all as default
369         /// paragraphs with the default layout of the text class?
370         virtual bool forceDefaultParagraphs(idx_type) const { return false; }
371         /// Is the width forced to some value?
372         virtual bool hasFixedWidth() const { return false; }
373
374         ///
375         virtual docstring name() const { return from_ascii("unknown"); }
376         /// used to toggle insets
377         /// is the inset open?
378         /// should this inset be handled like a normal charater
379         virtual bool isChar() const { return false; }
380         /// is this equivalent to a letter?
381         virtual bool isLetter() const { return false; }
382         /// is this equivalent to a space (which is BTW different from
383         /// a line separator)?
384         virtual bool isSpace() const { return false; }
385
386         enum DisplayType {
387                 Inline = 0,
388                 AlignLeft,
389                 AlignCenter,
390                 AlignRight
391         };
392
393         /// should we have a non-filled line before this inset?
394         virtual DisplayType display() const { return Inline; }
395         /// should we break lines after this inset?
396         virtual bool isLineSeparator() const { return false; }
397         /// should paragraph indendation be ommitted in any case?
398         virtual bool neverIndent(Buffer const &) const { return false; }
399         /// dumps content to lyxerr
400         virtual void dump() const;
401         /// write inset in .lyx format
402         virtual void write(Buffer const &, std::ostream &) const {}
403         /// read inset in .lyx format
404         virtual void read(Buffer const &, Lexer &) {}
405         /** Export the inset to LaTeX.
406          *  Don't use a temporary stringstream if the final output is
407          *  supposed to go to a file.
408          *  \sa Buffer::writeLaTeXSource for the reason.
409          *  \return the number of rows (\n's) of generated LaTeX code.
410          */
411         virtual int latex(Buffer const &, odocstream &,
412                           OutputParams const &) const { return 0; }
413         /// returns true to override begin and end inset in file
414         virtual bool directWrite() const;
415         ///
416         virtual bool allowSpellCheck() const { return false; }
417
418         /// if this insets owns text cells (e.g. InsetText) return cell num
419         virtual Text * getText(int /*num*/) const { return 0; }
420
421         /** Adds a LaTeX snippet to the Preview Loader for transformation
422          *  into a bitmap image. Does not start the laoding process.
423          *
424          *  Most insets have no interest in this capability, so the method
425          *  defaults to empty.
426          */
427         virtual void addPreview(graphics::PreviewLoader &) const {}
428         /// Add an entry to the TocList
429         /// pit is the ParConstIterator of the paragraph containing the inset
430         virtual void addToToc(TocList &, Buffer const &, ParConstIterator & pit) const {}
431
432 public:
433         /// returns LyX code associated with the inset. Used for TOC, ...)
434         virtual Code lyxCode() const { return NO_CODE; }
435
436         /// -1: text mode, 1: math mode, 0 undecided
437         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
438         /// return text or mathmode if that is possible to determine
439         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
440         /// returns whether this inset is allowed in other insets of given mode
441         virtual bool allowedIn(mode_type) const { return true; }
442         /**
443          * Is this inset allowed within a font change?
444          *
445          * FIXME: noFontChange means currently that the font change is closed
446          * in LaTeX before the inset, and that the contents of the inset
447          * will be in default font. This should be changed so that the inset
448          * changes the font again.
449          */
450         virtual bool noFontChange() const { return false; }
451
452         /// set the change for the entire inset
453         virtual void setChange(Change const &) {}
454         /// accept the changes within the inset
455         virtual void acceptChanges(BufferParams const &) {};
456         /// reject the changes within the inset
457         virtual void rejectChanges(BufferParams const &) {};
458
459         /// inset width.
460         int width() const { return dim_.wid; }
461         /// inset ascent.
462         int ascent() const { return dim_.asc; }
463         /// inset descent.
464         int descent() const { return dim_.des; }
465         ///
466         int scroll() const { return 0; }
467         ///
468         void setBackgroundColor(Color_color);
469         ///
470         Color_color backgroundColor() const;
471         ///
472         enum CollapseStatus {
473                 Collapsed,
474                 Inlined,
475                 Open
476         };
477         ///
478         virtual void setStatus(Cursor &, CollapseStatus) {}
479         //
480         enum { TEXT_TO_INSET_OFFSET = 4 };
481
482         /// Signal for inset destruction.
483         /** This signal is emitted at the inset destruction for insets that
484         *   support this.
485         */
486         virtual boost::signal<void()> * destroyedSignal() { return 0; }
487
488 protected:
489         Inset();
490
491         /** The real dispatcher.
492          *  Gets normally called from Cursor::dispatch(). Cursor::dispatch()
493          *  assumes the common case of 'LFUN handled, need update'.
494          *  This has to be overriden by calling Cursor::undispatched() or
495          *  Cursor::noUpdate() if appropriate.
496          *  If you need to call the dispatch method of some inset directly
497          *  you may have to explicitly request an update at that place. Don't
498          *  do it in doDispatch(), since that causes nested updates when
499          *  called from Cursor::dispatch(), and these can lead to crashes.
500          *  \sa getStatus
501          */
502         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
503
504         /// Cached dimensions of the inset.
505         mutable Dimension dim_;
506 private:
507         virtual std::auto_ptr<Inset> doClone() const = 0;
508         /** We store the Color::color value as an int to get Color.h out
509          *  of the header file.
510          */
511         int background_color_;
512 };
513
514
515 /**
516  * returns true if pointer argument is valid
517  * and points to an editable inset
518  */
519 bool isEditableInset(Inset const * inset);
520
521
522 /**
523  * returns true if pointer argument is valid
524  * and points to a highly editable inset
525  */
526 bool isHighlyEditableInset(Inset const * inset);
527
528 /** \c Inset_code is a wrapper for Inset::Code.
529  *  It can be forward-declared and passed as a function argument without
530  *  having to expose Inset.h.
531  */
532
533 class Inset_code {
534         Inset::Code val_;
535 public:
536         Inset_code(Inset::Code val) : val_(val) {}
537         operator Inset::Code() const { return val_; }
538 };
539
540
541
542 } // namespace lyx
543
544 #endif