]> git.lyx.org Git - lyx.git/blob - src/insets/insetbase.h
disable open dialogs if applying them is not allowed
[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          */
101         virtual bool getStatus(LCursor & cur, FuncRequest const & cmd,
102                 FuncStatus & status) const;
103
104         /// cursor enters
105         virtual void edit(LCursor & cur, bool left);
106         /// cursor enters
107         virtual InsetBase * editXY(LCursor & cur, int x, int y) const;
108
109         /// compute the size of the object returned in dim
110         virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
111         /// draw inset and update (xo, yo)-cache
112         virtual void draw(PainterInfo & pi, int x, int y) const = 0;
113         /// draw inset selection if necessary
114         virtual void drawSelection(PainterInfo &, int, int) const {}
115         ///
116         virtual bool editing(BufferView * bv) const;
117         /// draw four angular markers
118         void drawMarkers(PainterInfo & pi, int x, int y) const;
119         /// draw two angular markers
120         void drawMarkers2(PainterInfo & pi, int x, int y) const;
121         /// add space for markers
122         void metricsMarkers(Dimension & dim, int framesize = 1) const;
123         /// add space for markers
124         void metricsMarkers2(Dimension & dim, int framesize = 1) const;
125         /// last drawn position for 'important' insets
126         int xo() const;
127         /// last drawn position for 'important' insets
128         int yo() const;
129         /// set x/y drawing position cache if available
130         virtual void setPosCache(PainterInfo const &, int, int) const {}
131         /// do we cover screen position x/y?
132         virtual bool covers(int x, int y) const;
133         /// get the screen positions of the cursor (see note in cursor.C)
134         virtual void getCursorPos(CursorSlice const & sl, int & x, int & y) const;
135
136         /// is this an inset that can be moved into?
137         virtual bool isActive() const { return nargs() > 0; }
138         /// Where should we go when we press the up or down cursor key?
139         virtual bool idxUpDown(LCursor & cur, bool up) const;
140         /// Move one cell to the left
141         virtual bool idxLeft(LCursor &) const { return false; }
142         /// Move one cell to the right
143         virtual bool idxRight(LCursor &) const { return false; }
144
145         /// Move one physical cell up
146         virtual bool idxNext(LCursor &) const { return false; }
147         /// Move one physical cell down
148         virtual bool idxPrev(LCursor &) const { return false; }
149
150         /// Target pos when we enter the inset from the left by pressing "Right"
151         virtual bool idxFirst(LCursor &) const { return false; }
152         /// Target pos when we enter the inset from the right by pressing "Left"
153         virtual bool idxLast(LCursor &) const { return false; }
154
155         /// Delete a cell and move cursor
156         virtual bool idxDelete(idx_type &) { return false; }
157         /// pulls cell after pressing erase
158         virtual void idxGlue(idx_type) {}
159         /// returns list of cell indices that are "between" from and to for
160         /// selection purposes
161         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
162
163         /// to which column belongs a cell with a given index?
164         virtual col_type col(idx_type) const { return 0; }
165         /// to which row belongs a cell with a given index?
166         virtual row_type row(idx_type) const { return 0; }
167         /// cell idex corresponding to row and column;
168         virtual idx_type index(row_type row, col_type col) const;
169         /// any additional x-offset when drawing a cell?
170         virtual int cellXOffset(idx_type) const { return 0; }
171         /// any additional y-offset when drawing a cell?
172         virtual int cellYOffset(idx_type) const { return 0; }
173         /// number of embedded cells
174         virtual size_t nargs() const { return 0; }
175         /// number of rows in gridlike structures
176         virtual size_t nrows() const { return 0; }
177         /// number of columns in gridlike structures
178         virtual size_t ncols() const { return 0; }
179         /// is called when the cursor leaves this inset
180         virtual void notifyCursorLeaves(LCursor &) {}
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<std::string> & /* list */) const {}
187
188         /// describe content if cursor inside
189         virtual void infoize(std::ostream &) const {}
190         /// describe content if cursor behind
191         virtual void infoize2(std::ostream &) const {}
192
193         /// plain ascii output
194         virtual int plaintext(Buffer const &, std::ostream & os,
195                 OutputParams const &) const;
196         /// linuxdoc output
197         virtual int linuxdoc(Buffer const &, std::ostream & os,
198                 OutputParams const &) const;
199         /// docbook output
200         virtual int docbook(Buffer const &, std::ostream & os,
201                 OutputParams const &) const;
202
203         ///
204         enum EDITABLE {
205                 ///
206                 NOT_EDITABLE = 0,
207                 ///
208                 IS_EDITABLE,
209                 ///
210                 HIGHLY_EDITABLE
211         };
212         /// what appears in the minibuffer when opening
213         virtual std::string const editMessage() const;
214         ///
215         virtual EDITABLE editable() const;
216         /// can we go further down on mouse click?
217         virtual bool descendable() const { return false; }
218         ///
219         virtual bool isTextInset() const { return false; }
220         /// return true if the inset should be removed automatically
221         virtual bool autoDelete() const;
222
223         /** This is not quite the correct place for this enum. I think
224             the correct would be to let each subclass of Inset declare
225             its own enum code. Actually the notion of an InsetBase::Code
226             should be avoided, but I am not sure how this could be done
227             in a cleaner way. */
228         enum Code {
229                 ///
230                 NO_CODE, // 0
231                 ///
232                 TOC_CODE,  // do these insets really need a code? (ale)
233                 ///
234                 QUOTE_CODE,
235                 ///
236                 MARK_CODE,
237                 ///
238                 REF_CODE,
239                 ///
240                 URL_CODE, // 5
241                 ///
242                 HTMLURL_CODE,
243                 ///
244                 SEPARATOR_CODE,
245                 ///
246                 ENDING_CODE,
247                 ///
248                 LABEL_CODE,
249                 ///
250                 NOTE_CODE, // 10
251                 ///
252                 ACCENT_CODE,
253                 ///
254                 MATH_CODE,
255                 ///
256                 INDEX_CODE,
257                 ///
258                 INCLUDE_CODE,
259                 ///
260                 GRAPHICS_CODE, // 15
261                 ///
262                 BIBITEM_CODE,
263                 ///
264                 BIBTEX_CODE,
265                 ///
266                 TEXT_CODE,
267                 ///
268                 ERT_CODE,
269                 ///
270                 FOOT_CODE, // 20
271                 ///
272                 MARGIN_CODE,
273                 ///
274                 FLOAT_CODE,
275                 ///
276                 WRAP_CODE,
277                 ///
278                 SPACE_CODE, // 25
279                 ///
280                 SPECIALCHAR_CODE,
281                 ///
282                 TABULAR_CODE,
283                 ///
284                 EXTERNAL_CODE,
285 #if 0
286                 ///
287                 THEOREM_CODE,
288 #endif
289                 ///
290                 CAPTION_CODE,
291                 ///
292                 MATHMACRO_CODE, // 30
293                 ///
294                 ERROR_CODE,
295                 ///
296                 CITE_CODE,
297                 ///
298                 FLOAT_LIST_CODE,
299                 ///
300                 INDEX_PRINT_CODE,
301                 ///
302                 OPTARG_CODE, // 35
303                 ///
304                 ENVIRONMENT_CODE,
305                 ///
306                 HFILL_CODE,
307                 ///
308                 NEWLINE_CODE,
309                 ///
310                 LINE_CODE,
311                 ///
312                 BRANCH_CODE, // 40
313                 ///
314                 BOX_CODE,
315                 ///
316                 CHARSTYLE_CODE,
317                 ///
318                 VSPACE_CODE,
319                 ///
320                 MATHMACROARG_CODE
321         };
322
323         /** returns the Code corresponding to the \c name.
324          *  Eg, translate("branch") == BRANCH_CODE
325          */
326         static Code translate(std::string const & name);
327
328         /// returns true the inset can hold an inset of given type
329         virtual bool insetAllowed(Code) const { return false; }
330         // if this inset has paragraphs should they be output all as default
331         // paragraphs with "Standard" layout?
332         virtual bool forceDefaultParagraphs(InsetBase const *) const { return false; }
333
334         ///
335         virtual std::string const & getInsetName() const;
336         /// used to toggle insets
337         // is the inset open?
338         virtual bool isOpen() const { return false; }
339         /// open the inset
340         virtual void open() {}
341         /// close the inset
342         virtual void close() {}
343         // should this inset be handled like a normal charater
344         virtual bool isChar() const { return false; }
345         // is this equivalent to a letter?
346         virtual bool isLetter() const { return false; }
347         // is this equivalent to a space (which is BTW different from
348         // a line separator)?
349         virtual bool isSpace() const { return false; }
350         // should we have a non-filled line before this inset?
351         virtual bool display() const { return false; }
352         // should we break lines after this inset?
353         virtual bool isLineSeparator() const { return false; }
354         /// dumps content to lyxerr
355         virtual void dump() const;
356         ///
357         virtual void write(Buffer const &, std::ostream &) const {}
358         ///
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 &, std::ostream &,
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         virtual bool noFontChange() const { return false; }
391
392         ///
393         virtual void markErased();
394         /// pretty arbitrary
395         virtual int width() const { return 10; }
396         /// pretty arbitrary
397         virtual int ascent() const { return 10; }
398         /// pretty arbitrary
399         virtual int descent() const { return 10; }
400 protected:
401         InsetBase();
402         InsetBase(InsetBase const &);
403         /// the real dispatcher.
404         /// \sa getStatus
405         virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
406 private:
407         virtual std::auto_ptr<InsetBase> doClone() const = 0;
408 };
409
410
411 /**
412  * returns true if pointer argument is valid
413  * and points to an editable inset
414  */
415 bool isEditableInset(InsetBase const * inset);
416
417
418 /**
419  * returns true if pointer argument is valid
420  * and points to a highly editable inset
421  */
422 bool isHighlyEditableInset(InsetBase const * inset);
423
424 #endif