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