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