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