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