]> git.lyx.org Git - lyx.git/blob - src/insets/Inset.h
Show corners of mathed with mouse hover, details see bug 3825
[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         /// add space for markers
134         void metricsMarkers(Dimension & dim, int framesize = 1) const;
135         /// add space for markers
136         void metricsMarkers2(Dimension & dim, int framesize = 1) const;
137         /// last drawn position for 'important' insets
138         int xo(BufferView const & bv) const;
139         /// last drawn position for 'important' insets
140         int yo(BufferView const & bv) const;
141         /// set x/y drawing position cache if available
142         virtual void setPosCache(PainterInfo const &, int, int) const;
143         /// do we cover screen position x/y?
144         virtual bool covers(BufferView const & bv, int x, int y) const;
145         /// get the screen positions of the cursor (see note in Cursor.cpp)
146         virtual void cursorPos(BufferView const & bv,
147                 CursorSlice const & sl, bool boundary, int & x, int & y) const;
148
149         /// is this an inset that can be moved into?
150         virtual bool isActive() const { return nargs() > 0; }
151         /// Where should we go when we press the up or down cursor key?
152         virtual bool idxUpDown(Cursor & cur, bool up) const;
153         /// Move one cell to the left
154         virtual bool idxLeft(Cursor &) const { return false; }
155         /// Move one cell to the right
156         virtual bool idxRight(Cursor &) const { return false; }
157
158         /// Move one physical cell up
159         virtual bool idxNext(Cursor &) const { return false; }
160         /// Move one physical cell down
161         virtual bool idxPrev(Cursor &) const { return false; }
162
163         /// Target pos when we enter the inset from the left by pressing "Right"
164         virtual bool idxFirst(Cursor &) const { return false; }
165         /// Target pos when we enter the inset from the right by pressing "Left"
166         virtual bool idxLast(Cursor &) const { return false; }
167
168         /// Delete a cell and move cursor
169         virtual bool idxDelete(idx_type &) { return false; }
170         /// pulls cell after pressing erase
171         virtual void idxGlue(idx_type) {}
172         /// returns list of cell indices that are "between" from and to for
173         /// selection purposes
174         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
175
176         /// to which column belongs a cell with a given index?
177         virtual col_type col(idx_type) const { return 0; }
178         /// to which row belongs a cell with a given index?
179         virtual row_type row(idx_type) const { return 0; }
180         /// cell idex corresponding to row and column;
181         virtual idx_type index(row_type row, col_type col) const;
182         /// any additional x-offset when drawing a cell?
183         virtual int cellXOffset(idx_type) const { return 0; }
184         /// any additional y-offset when drawing a cell?
185         virtual int cellYOffset(idx_type) const { return 0; }
186         /// number of embedded cells
187         virtual size_t nargs() const { return 0; }
188         /// number of rows in gridlike structures
189         virtual size_t nrows() const { return 0; }
190         /// number of columns in gridlike structures
191         virtual size_t ncols() const { return 0; }
192         /// is called when the cursor leaves this inset
193         //  returns true if cursor is now invalid.
194         virtual bool notifyCursorLeaves(Cursor &) { return false; }
195         /// is called when the mouse enter or leave this inset
196         /// return true if this inset needs repaint
197         virtual bool setMouseHover(bool) { return false; }
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         /// return true if the inset should be removed automatically
249         virtual bool autoDelete() const;
250
251         /** This is not quite the correct place for this enum. I think
252             the correct would be to let each subclass of Inset declare
253             its own enum code. Actually the notion of an Inset::Code
254             should be avoided, but I am not sure how this could be done
255             in a cleaner way. */
256         enum Code {
257                 ///
258                 NO_CODE, // 0
259                 ///
260                 TOC_CODE,  // do these insets really need a code? (ale)
261                 ///
262                 QUOTE_CODE,
263                 ///
264                 MARK_CODE,
265                 ///
266                 REF_CODE,
267                 ///
268                 URL_CODE, // 5
269                 ///
270                 HTMLURL_CODE,
271                 ///
272                 SEPARATOR_CODE,
273                 ///
274                 ENDING_CODE,
275                 ///
276                 LABEL_CODE,
277                 ///
278                 NOTE_CODE, // 10
279                 ///
280                 ACCENT_CODE,
281                 ///
282                 MATH_CODE,
283                 ///
284                 INDEX_CODE,
285                 ///
286                 INCLUDE_CODE,
287                 ///
288                 GRAPHICS_CODE, // 15
289                 ///
290                 BIBITEM_CODE,
291                 ///
292                 BIBTEX_CODE,
293                 ///
294                 TEXT_CODE,
295                 ///
296                 ERT_CODE,
297                 ///
298                 FOOT_CODE, // 20
299                 ///
300                 MARGIN_CODE,
301                 ///
302                 FLOAT_CODE,
303                 ///
304                 WRAP_CODE,
305                 ///
306                 SPACE_CODE, // 25
307                 ///
308                 SPECIALCHAR_CODE,
309                 ///
310                 TABULAR_CODE,
311                 ///
312                 EXTERNAL_CODE,
313 #if 0
314                 ///
315                 THEOREM_CODE,
316 #endif
317                 ///
318                 CAPTION_CODE,
319                 ///
320                 MATHMACRO_CODE, // 30
321                 ///
322                 CITE_CODE,
323                 ///
324                 FLOAT_LIST_CODE,
325                 ///
326                 INDEX_PRINT_CODE,
327                 ///
328                 OPTARG_CODE, // 35
329                 ///
330                 ENVIRONMENT_CODE,
331                 ///
332                 HFILL_CODE,
333                 ///
334                 NEWLINE_CODE,
335                 ///
336                 LINE_CODE,
337                 ///
338                 BRANCH_CODE, // 40
339                 ///
340                 BOX_CODE,
341                 ///
342                 CHARSTYLE_CODE,
343                 ///
344                 VSPACE_CODE,
345                 ///
346                 MATHMACROARG_CODE,
347                 ///
348                 NOMENCL_CODE, // 45
349                 ///
350                 NOMENCL_PRINT_CODE,
351                 ///
352                 PAGEBREAK_CODE,
353                 ///
354                 LISTINGS_CODE
355         };
356
357         /** returns the Code corresponding to the \c name.
358          *  Eg, translate("branch") == BRANCH_CODE
359          */
360         static Code translate(std::string const & name);
361
362         /// returns true if the inset can hold an inset of given type
363         virtual bool insetAllowed(Code) const { return false; }
364         /// if this inset has paragraphs should they be output all as default
365         /// paragraphs with the default layout of the text class?
366         virtual bool forceDefaultParagraphs(idx_type) const { return false; }
367         /// Is the width forced to some value?
368         virtual bool hasFixedWidth() const { return false; }
369
370         ///
371         virtual docstring name() const { return from_ascii("unknown"); }
372         /// used to toggle insets
373         /// is the inset open?
374         /// should this inset be handled like a normal charater
375         virtual bool isChar() const { return false; }
376         /// is this equivalent to a letter?
377         virtual bool isLetter() const { return false; }
378         /// is this equivalent to a space (which is BTW different from
379         /// a line separator)?
380         virtual bool isSpace() const { return false; }
381
382         enum DisplayType {
383                 Inline = 0,
384                 AlignLeft,
385                 AlignCenter,
386                 AlignRight
387         };
388
389         /// should we have a non-filled line before this inset?
390         virtual DisplayType display() const { return Inline; }
391         /// should we break lines after this inset?
392         virtual bool isLineSeparator() const { return false; }
393         /// should paragraph indendation be ommitted in any case?
394         virtual bool neverIndent(Buffer const &) const { return false; }
395         /// dumps content to lyxerr
396         virtual void dump() const;
397         /// write inset in .lyx format
398         virtual void write(Buffer const &, std::ostream &) const {}
399         /// read inset in .lyx format
400         virtual void read(Buffer const &, Lexer &) {}
401         /** Export the inset to LaTeX.
402          *  Don't use a temporary stringstream if the final output is
403          *  supposed to go to a file.
404          *  \sa Buffer::writeLaTeXSource for the reason.
405          *  \return the number of rows (\n's) of generated LaTeX code.
406          */
407         virtual int latex(Buffer const &, odocstream &,
408                           OutputParams const &) const { return 0; }
409         /// returns true to override begin and end inset in file
410         virtual bool directWrite() const;
411         ///
412         virtual bool allowSpellCheck() const { return false; }
413
414         /// if this insets owns text cells (e.g. InsetText) return cell num
415         virtual Text * getText(int /*num*/) const { return 0; }
416
417         /** Adds a LaTeX snippet to the Preview Loader for transformation
418          *  into a bitmap image. Does not start the laoding process.
419          *
420          *  Most insets have no interest in this capability, so the method
421          *  defaults to empty.
422          */
423         virtual void addPreview(graphics::PreviewLoader &) const {}
424         /// Add an entry to the TocList
425         /// pit is the ParConstIterator of the paragraph containing the inset
426         virtual void addToToc(TocList &, Buffer const &, ParConstIterator &) const {}
427
428 public:
429         /// returns LyX code associated with the inset. Used for TOC, ...)
430         virtual Code lyxCode() const { return NO_CODE; }
431
432         /// -1: text mode, 1: math mode, 0 undecided
433         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
434         /// return text or mathmode if that is possible to determine
435         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
436         /// returns whether this inset is allowed in other insets of given mode
437         virtual bool allowedIn(mode_type) const { return true; }
438         /**
439          * Is this inset allowed within a font change?
440          *
441          * FIXME: noFontChange means currently that the font change is closed
442          * in LaTeX before the inset, and that the contents of the inset
443          * will be in default font. This should be changed so that the inset
444          * changes the font again.
445          */
446         virtual bool noFontChange() const { return false; }
447
448         /// set the change for the entire inset
449         virtual void setChange(Change const &) {}
450         /// accept the changes within the inset
451         virtual void acceptChanges(BufferParams const &) {};
452         /// reject the changes within the inset
453         virtual void rejectChanges(BufferParams const &) {};
454
455         /// inset width.
456         int width() const { return dim_.wid; }
457         /// inset ascent.
458         int ascent() const { return dim_.asc; }
459         /// inset descent.
460         int descent() const { return dim_.des; }
461         ///
462         int scroll() const { return 0; }
463         ///
464         void setBackgroundColor(Color_color);
465         ///
466         Color_color backgroundColor() const;
467         ///
468         enum CollapseStatus {
469                 Collapsed,
470                 Inlined,
471                 Open
472         };
473         ///
474         virtual void setStatus(Cursor &, CollapseStatus) {}
475         //
476         enum { TEXT_TO_INSET_OFFSET = 4 };
477
478         /// Signal for inset destruction.
479         /** This signal is emitted at the inset destruction for insets that
480         *   support this.
481         */
482         virtual boost::signal<void()> * destroyedSignal() { return 0; }
483
484 protected:
485         Inset();
486
487         /** The real dispatcher.
488          *  Gets normally called from Cursor::dispatch(). Cursor::dispatch()
489          *  assumes the common case of 'LFUN handled, need update'.
490          *  This has to be overriden by calling Cursor::undispatched() or
491          *  Cursor::noUpdate() if appropriate.
492          *  If you need to call the dispatch method of some inset directly
493          *  you may have to explicitly request an update at that place. Don't
494          *  do it in doDispatch(), since that causes nested updates when
495          *  called from Cursor::dispatch(), and these can lead to crashes.
496          *  \sa getStatus
497          */
498         virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
499
500         /// Cached dimensions of the inset.
501         mutable Dimension dim_;
502 private:
503         virtual std::auto_ptr<Inset> doClone() const = 0;
504         /** We store the Color::color value as an int to get Color.h out
505          *  of the header file.
506          */
507         int background_color_;
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
529 class Inset_code {
530         Inset::Code val_;
531 public:
532         Inset_code(Inset::Code val) : val_(val) {}
533         operator Inset::Code() const { return val_; }
534 };
535
536
537
538 } // namespace lyx
539
540 #endif