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