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