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