]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
f5c96356788b4e2c47ac0794d030993a479c342f
[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         /// what appears in the minibuffer when opening
166         virtual string const editMessage() const;
167         ///
168         virtual EDITABLE editable() const;
169         ///
170         virtual bool isTextInset() const { return false; }
171         /// return true if the inset should be removed automatically
172         virtual bool autoDelete() const;
173         /// returns true the inset can hold an inset of given type
174         virtual bool insetAllowed(Inset::Code) const { return false; }
175         /// wrapper around the above
176         bool insetAllowed(Inset * in) const;
177         ///
178         virtual void write(Buffer const *, std::ostream &) const = 0;
179         ///
180         virtual void read(Buffer const *, LyXLex & lex) = 0;
181         /// returns the number of rows (\n's) of generated tex code.
182         virtual int latex(Buffer const *, std::ostream &,
183                           LatexRunParams const &) const = 0;
184         ///
185         virtual int ascii(Buffer const *,
186                           std::ostream &, int linelen = 0) const = 0;
187         ///
188         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
189         ///
190         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
191
192         /// returns LyX code associated with the inset. Used for TOC, ...)
193         virtual Inset::Code lyxCode() const { return NO_CODE; }
194
195         /// returns true to override begin and end inset in file
196         virtual bool directWrite() const;
197
198         /// Returns true if the inset should be centered alone
199         virtual bool display() const { return false; }
200         /// Changes the display state of the inset
201         virtual void display(bool) {}
202         ///
203         /// returns true if this inset needs a row on it's own
204         ///
205         virtual bool needFullRow() const { return false; }
206         ///
207         void setInsetName(string const & s) { name_ = s; }
208         ///
209         string const & getInsetName() const { return name_; }
210         ///
211         void setOwner(UpdatableInset * inset) { owner_ = inset; }
212         ///
213         UpdatableInset * owner() const { return owner_; }
214         ///
215         void parOwner(Paragraph * par) { par_owner_ = par; }
216         ///
217         Paragraph * parOwner() const { return par_owner_; }
218         ///
219         void setBackgroundColor(LColor::color);
220         ///
221         LColor::color backgroundColor() const;
222         ///
223         int x() const { return top_x; }
224         ///
225         int y() const { return top_baseline; }
226         //
227         // because we could have fake text insets and have to call this
228         // inside them without cast!!!
229         ///
230         virtual LyXText * getLyXText(BufferView const *,
231                                      bool recursive = false) const;
232         ///
233         virtual void deleteLyXText(BufferView *, bool = true) const {}
234         ///
235         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
236         /// returns the actuall scroll-value
237         virtual int scroll(bool recursive=true) const {
238                 if (!recursive || !owner_)
239                         return scx;
240                 return 0;
241         }
242
243         /// try to get a inset pointer from it's id if we have
244         /// an inset to give back!
245         virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
246         /// if this insets owns paragraphs (f.ex. InsetText) then it
247         /// should return it's very first one!
248         virtual ParagraphList * getParagraphs(int /*num*/) const { return 0; }
249         ///
250         virtual bool haveParagraphs() const {
251                 return false;
252         }
253
254         /// return the cursor if we own one otherwise giv'em just the
255         /// BufferView cursor to work with.
256         virtual LyXCursor const & cursor(BufferView * bview) const;
257         /// id functions
258         int id() const;
259         ///
260         void id(int id_arg);
261
262         /// used to toggle insets
263         // is the inset open?
264         virtual bool isOpen() const { return false; }
265         /// open the inset
266         virtual void open(BufferView *) {}
267         /// close the inset
268         virtual void close(BufferView *) const {}
269         /// check if the font of the char we want inserting is correct
270         /// and modify it if it is not.
271         virtual bool checkInsertChar(LyXFont &);
272         /// we need this here because collapsed insets are only EDITABLE
273         virtual void setFont(BufferView *, LyXFont const &,
274                          bool toggleall = false, bool selectall = false);
275         ///
276         // needed for spellchecking text
277         ///
278         virtual bool allowSpellcheck() const { return false; }
279
280         // should this inset be handled like a normal charater
281         virtual bool isChar() const { return false; }
282         // is this equivalent to a letter?
283         virtual bool isLetter() const { return false; }
284         // is this equivalent to a space (which is BTW different from
285         // a line separator)?
286         virtual bool isSpace() const { return false; }
287         // should we break lines after this inset?
288         virtual bool isLineSeparator() const { return false; }
289         // if this inset has paragraphs should they be output all as default
290         // paragraphs with "Standard" layout?
291         virtual bool forceDefaultParagraphs(Inset const *) const;
292         /** returns true if, when outputing LaTeX, font changes should
293             be closed before generating this inset. This is needed for
294             insets that may contain several paragraphs */
295         virtual bool noFontChange() const { return false; }
296         //
297         virtual void getDrawFont(LyXFont &) const {}
298         /* needed for widths which are % of something
299            returns the value of \textwidth in this inset. Most of the
300            time this is the width of the workarea, but if there is a
301            minipage somewhere, it will be the width of this minipage */
302         virtual int latexTextWidth(BufferView *) const;
303
304         /// mark the inset contents as erased (for change tracking)
305         virtual void markErased() {}
306
307         /** Adds a LaTeX snippet to the Preview Loader for transformation
308          *  into a bitmap image. Does not start the laoding process.
309          *
310          *  Most insets have no interest in this capability, so the method
311          *  defaults to empty.
312          */
313         virtual void addPreview(lyx::graphics::PreviewLoader &) const {}
314
315         /** Find the PreviewLoader, add a LaTeX snippet to it and
316          *  start the loading process.
317          *
318          *  Most insets have no interest in this capability, so the method
319          *  defaults to empty.
320          */
321         virtual void generatePreview() const {}
322
323 protected:
324         ///
325         mutable int top_x;
326         ///
327         mutable int top_baseline;
328         ///
329         mutable int scx;
330         ///
331         unsigned int id_;
332         ///
333         static unsigned int inset_id;
334
335 private:
336         ///
337         UpdatableInset * owner_;
338         /// the paragraph in which this inset has been inserted
339         Paragraph * par_owner_;
340         ///
341         string name_;
342         ///
343         LColor::color background_color_;
344 };
345
346
347 inline
348 bool Inset::insetAllowed(Inset * in) const
349 {
350         return insetAllowed(in->lyxCode());
351 }
352
353
354 inline
355 bool Inset::checkInsertChar(LyXFont &)
356 {
357         return false;
358 }
359
360 /**
361  * returns true if pointer argument is valid
362  * and points to an editable inset
363  */
364 inline bool isEditableInset(Inset const * i)
365 {
366         return i && i->editable();
367 }
368
369 /**
370  * returns true if pointer argument is valid
371  * and points to a highly editable inset
372  */
373 inline bool isHighlyEditableInset(Inset const * i)
374 {
375         return i && i->editable() == Inset::HIGHLY_EDITABLE;
376 }
377
378 #endif