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