]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
prevent crash when inserting minipage in table cell,
[lyx.git] / src / insets / inset.h
1 // -*- C++ -*-
2 /**
3  * \file inset.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Alejandro Aguilar Sierra
8  * \author Jürgen Vigna
9  * \author Lars Gullik Bjønnes
10  * \author Matthias Ettrich
11  *
12  * Full author contact details are available in file CREDITS
13  */
14
15 #ifndef INSETOLD_H
16 #define INSETOLD_H
17
18 #include "LColor.h"
19 #include "insetbase.h"
20 #include "dimension.h"
21 #include "support/types.h"
22
23 #include <vector>
24
25 class LyXFont;
26 class Buffer;
27 class Painter;
28 class LatexRunParams;
29 class LyXText;
30 class LyXLex;
31 class Paragraph;
32 class LyXCursor;
33 class FuncRequest;
34 class WordLangTuple;
35 class ParagraphList;
36 class UpdatableInset;
37
38 namespace lyx {
39 namespace graphics {
40         class PreviewLoader;
41 }
42 }
43
44 /// Insets
45 class InsetOld : public InsetBase {
46 public:
47         /** This is not quite the correct place for this enum. I think
48             the correct would be to let each subclass of Inset declare
49             its own enum code. Actually the notion of an InsetOld::Code
50             should be avoided, but I am not sure how this could be done
51             in a cleaner way. */
52         enum Code {
53                 ///
54                 NO_CODE,
55                 ///
56                 TOC_CODE,  // do these insets really need a code? (ale)
57                 ///
58                 QUOTE_CODE,
59                 ///
60                 MARK_CODE,
61                 ///
62                 REF_CODE, // 5
63                 ///
64                 URL_CODE,
65                 ///
66                 HTMLURL_CODE,
67                 ///
68                 SEPARATOR_CODE,
69                 ///
70                 ENDING_CODE,
71                 ///
72                 LABEL_CODE, // 10
73                 ///
74                 NOTE_CODE,
75                 ///
76                 ACCENT_CODE,
77                 ///
78                 MATH_CODE,
79                 ///
80                 INDEX_CODE,
81                 ///
82                 INCLUDE_CODE, // 15
83                 ///
84                 GRAPHICS_CODE,
85                 ///
86                 BIBITEM_CODE,
87                 ///
88                 BIBTEX_CODE,
89                 ///
90                 TEXT_CODE,
91                 ///
92                 ERT_CODE, // 20
93                 ///
94                 FOOT_CODE,
95                 ///
96                 MARGIN_CODE,
97                 ///
98                 FLOAT_CODE,
99                 ///
100                 WRAP_CODE,
101                 ///
102                 MINIPAGE_CODE,
103                 ///
104                 SPACE_CODE,
105                 ///
106                 SPECIALCHAR_CODE, // 25
107                 ///
108                 TABULAR_CODE,
109                 ///
110                 EXTERNAL_CODE,
111 #if 0
112                 ///
113                 THEOREM_CODE,
114 #endif
115                 ///
116                 CAPTION_CODE,
117                 ///
118                 MATHMACRO_CODE, // 30
119                 ///
120                 ERROR_CODE,
121                 ///
122                 CITE_CODE,
123                 ///
124                 FLOAT_LIST_CODE,
125                 ///
126                 INDEX_PRINT_CODE,
127                 ///
128                 OPTARG_CODE,
129                 ///
130                 ENVIRONMENT_CODE,
131                 ///
132                 HFILL_CODE,
133                 ///
134                 NEWLINE_CODE
135         };
136
137         ///
138         enum {
139                 ///
140                 TEXT_TO_INSET_OFFSET = 2
141         };
142
143         ///
144         enum EDITABLE {
145                 ///
146                 NOT_EDITABLE = 0,
147                 ///
148                 IS_EDITABLE,
149                 ///
150                 HIGHLY_EDITABLE
151         };
152
153         ///
154         typedef dispatch_result RESULT;
155
156         ///
157         InsetOld();
158         ///
159         InsetOld(InsetOld const & in);
160         ///
161         int ascent() const;
162         ///
163         int descent() const;
164         ///
165         int width() const;
166         /// what appears in the minibuffer when opening
167         virtual string const editMessage() const;
168         ///
169         virtual EDITABLE editable() const;
170         ///
171         virtual bool isTextInset() const { return false; }
172         /// return true if the inset should be removed automatically
173         virtual bool autoDelete() const;
174         /// returns true the inset can hold an inset of given type
175         virtual bool insetAllowed(InsetOld::Code) const { return false; }
176         /// wrapper around the above
177         bool insetAllowed(InsetOld * in) const;
178         ///
179         virtual void write(Buffer const *, std::ostream &) const = 0;
180         ///
181         virtual void read(Buffer const *, LyXLex & lex) = 0;
182         /// returns the number of rows (\n's) of generated tex code.
183         virtual int latex(Buffer const *, std::ostream &,
184                           LatexRunParams const &) const = 0;
185         ///
186         virtual int ascii(Buffer const *,
187                           std::ostream &, int linelen = 0) const = 0;
188         ///
189         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
190         ///
191         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
192
193         /// returns LyX code associated with the inset. Used for TOC, ...)
194         virtual InsetOld::Code lyxCode() const { return NO_CODE; }
195
196         /// returns true to override begin and end inset in file
197         virtual bool directWrite() const;
198
199         /// Returns true if the inset should be centered alone
200         virtual bool display() const { return false; }
201         /// Changes the display state of the inset
202         virtual void display(bool) {}
203         ///
204         /// returns true if this inset needs a row on it's own
205         ///
206         virtual bool needFullRow() const { return false; }
207         ///
208         void setInsetName(string const & s) { name_ = s; }
209         ///
210         string const & getInsetName() const { return name_; }
211         ///
212         void setOwner(UpdatableInset * inset) { owner_ = inset; }
213         ///
214         UpdatableInset * owner() const { return owner_; }
215         ///
216         void parOwner(Paragraph * par) { par_owner_ = par; }
217         ///
218         Paragraph * parOwner() const { return par_owner_; }
219         ///
220         void setBackgroundColor(LColor::color);
221         ///
222         LColor::color backgroundColor() const;
223         ///
224         int x() const { return top_x; }
225         ///
226         int y() const { return top_baseline; }
227         //
228         // because we could have fake text insets and have to call this
229         // inside them without cast!!!
230         ///
231         virtual LyXText * getLyXText(BufferView const *,
232                                      bool recursive = false) const;
233         ///
234         virtual void deleteLyXText(BufferView *, bool = true) const {}
235         ///
236         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
237         /// returns the actuall scroll-value
238         virtual int scroll(bool recursive=true) const {
239                 if (!recursive || !owner_)
240                         return scx;
241                 return 0;
242         }
243
244         /// try to get a inset pointer from it's id if we have
245         /// an inset to give back!
246         virtual InsetOld * getInsetFromID(int /*id*/) const { return 0; }
247         /// if this insets owns paragraphs (f.ex. InsetText) then it
248         /// should return it's very first one!
249         virtual ParagraphList * getParagraphs(int /*num*/) const { return 0; }
250         ///
251         virtual bool haveParagraphs() const {
252                 return false;
253         }
254
255         /// return the cursor if we own one otherwise giv'em just the
256         /// BufferView cursor to work with.
257         virtual LyXCursor const & cursor(BufferView * bview) const;
258         /// id functions
259         int id() const;
260         ///
261         void id(int id_arg);
262
263         /// used to toggle insets
264         // is the inset open?
265         virtual bool isOpen() const { return false; }
266         /// open the inset
267         virtual void open(BufferView *) {}
268         /// close the inset
269         virtual void close(BufferView *) const {}
270         /// check if the font of the char we want inserting is correct
271         /// and modify it if it is not.
272         virtual bool checkInsertChar(LyXFont &);
273         /// we need this here because collapsed insets are only EDITABLE
274         virtual void setFont(BufferView *, LyXFont const &,
275                          bool toggleall = false, bool selectall = false);
276         ///
277         // needed for spellchecking text
278         ///
279         virtual bool allowSpellcheck() const { return false; }
280
281         // should this inset be handled like a normal charater
282         virtual bool isChar() const { return false; }
283         // is this equivalent to a letter?
284         virtual bool isLetter() const { return false; }
285         // is this equivalent to a space (which is BTW different from
286         // a line separator)?
287         virtual bool isSpace() const { return false; }
288         // should we break lines after this inset?
289         virtual bool isLineSeparator() const { return false; }
290         // if this inset has paragraphs should they be output all as default
291         // paragraphs with "Standard" layout?
292         virtual bool forceDefaultParagraphs(InsetOld const *) const;
293         /** returns true if, when outputing LaTeX, font changes should
294             be closed before generating this inset. This is needed for
295             insets that may contain several paragraphs */
296         virtual bool noFontChange() const { return false; }
297         //
298         virtual void getDrawFont(LyXFont &) const {}
299         /* needed for widths which are % of something
300            returns the value of \textwidth in this inset. Most of the
301            time this is the width of the workarea, but if there is a
302            minipage somewhere, it will be the width of this minipage */
303         virtual int latexTextWidth(BufferView *) const;
304
305         /// mark the inset contents as erased (for change tracking)
306         virtual void markErased() {}
307
308         /** Adds a LaTeX snippet to the Preview Loader for transformation
309          *  into a bitmap image. Does not start the laoding process.
310          *
311          *  Most insets have no interest in this capability, so the method
312          *  defaults to empty.
313          */
314         virtual void addPreview(lyx::graphics::PreviewLoader &) const {}
315
316         /** Find the PreviewLoader, add a LaTeX snippet to it and
317          *  start the loading process.
318          *
319          *  Most insets have no interest in this capability, so the method
320          *  defaults to empty.
321          */
322         virtual void generatePreview() const {}
323
324 protected:
325         ///
326         mutable int top_x;
327         ///
328         mutable int top_baseline;
329         ///
330         mutable int scx;
331         /// Used to identify the inset for cursor positioning when undoing
332         unsigned int id_;
333         ///
334         static unsigned int inset_id;
335         ///
336         mutable Dimension dim_;
337
338 private:
339         ///
340         UpdatableInset * owner_;
341         /// the paragraph in which this inset has been inserted
342         Paragraph * par_owner_;
343         ///
344         string name_;
345         ///
346         LColor::color background_color_;
347 };
348
349
350 inline
351 bool InsetOld::insetAllowed(InsetOld * in) const
352 {
353         return insetAllowed(in->lyxCode());
354 }
355
356
357 inline
358 bool InsetOld::checkInsertChar(LyXFont &)
359 {
360         return false;
361 }
362
363 /**
364  * returns true if pointer argument is valid
365  * and points to an editable inset
366  */
367 inline bool isEditableInset(InsetOld const * i)
368 {
369         return i && i->editable();
370 }
371
372 /**
373  * returns true if pointer argument is valid
374  * and points to a highly editable inset
375  */
376 inline bool isHighlyEditableInset(InsetOld const * i)
377 {
378         return i && i->editable() == InsetOld::HIGHLY_EDITABLE;
379 }
380
381 #endif