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