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