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