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