]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
b4f82328bb042be6add25f667bc54afca1a55549
[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                 PARENT_CODE,
87                 ///
88                 BIBITEM_CODE,
89                 ///
90                 BIBTEX_CODE,
91                 ///
92                 TEXT_CODE,
93                 ///
94                 ERT_CODE, // 20
95                 ///
96                 FOOT_CODE,
97                 ///
98                 MARGIN_CODE,
99                 ///
100                 FLOAT_CODE,
101                 ///
102                 WRAP_CODE,
103                 ///
104                 MINIPAGE_CODE,
105                 ///
106                 SPACE_CODE,
107                 ///
108                 SPECIALCHAR_CODE, // 25
109                 ///
110                 TABULAR_CODE,
111                 ///
112                 EXTERNAL_CODE,
113 #if 0
114                 ///
115                 THEOREM_CODE,
116 #endif
117                 ///
118                 CAPTION_CODE,
119                 ///
120                 MATHMACRO_CODE, // 30
121                 ///
122                 ERROR_CODE,
123                 ///
124                 CITE_CODE,
125                 ///
126                 FLOAT_LIST_CODE,
127                 ///
128                 INDEX_PRINT_CODE,
129                 ///
130                 OPTARG_CODE,
131                 ///
132                 ENVIRONMENT_CODE,
133                 ///
134                 HFILL_CODE,
135                 ///
136                 NEWLINE_CODE
137         };
138
139         ///
140         enum {
141                 ///
142                 TEXT_TO_INSET_OFFSET = 2
143         };
144
145         ///
146         enum EDITABLE {
147                 ///
148                 NOT_EDITABLE = 0,
149                 ///
150                 IS_EDITABLE,
151                 ///
152                 HIGHLY_EDITABLE
153         };
154
155         ///
156         typedef dispatch_result RESULT;
157
158         ///
159         Inset();
160         ///
161         Inset(Inset const & in);
162         ///
163         virtual void dimension(BufferView *, LyXFont const &, Dimension &) const = 0;
164         ///
165         int ascent(BufferView *, LyXFont const &) const;
166         ///
167         int descent(BufferView *, LyXFont const &) const;
168         ///
169         int width(BufferView *, LyXFont const &) const;
170         /// update the inset representation
171         virtual void update(BufferView *, bool = false)
172                 {}
173         /// what appears in the minibuffer when opening
174         virtual string const editMessage() const;
175         ///
176         virtual EDITABLE editable() const;
177         ///
178         virtual bool isTextInset() const { return false; }
179         /// return true if the inset should be removed automatically
180         virtual bool autoDelete() const;
181         /// returns true the inset can hold an inset of given type
182         virtual bool insetAllowed(Inset::Code) const { return false; }
183         /// wrapper around the above
184         bool insetAllowed(Inset * in) const;
185         ///
186         virtual void write(Buffer const *, std::ostream &) const = 0;
187         ///
188         virtual void read(Buffer const *, LyXLex & lex) = 0;
189         /// returns the number of rows (\n's) of generated tex code.
190         virtual int latex(Buffer const *, std::ostream &,
191                           LatexRunParams const &) const = 0;
192         ///
193         virtual int ascii(Buffer const *,
194                           std::ostream &, int linelen = 0) const = 0;
195         ///
196         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
197         ///
198         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
199         /// Updates needed features for this inset.
200         virtual void validate(LaTeXFeatures & features) const;
201
202         /// returns LyX code associated with the inset. Used for TOC, ...)
203         virtual Inset::Code lyxCode() const { return NO_CODE; }
204
205         virtual std::vector<string> const getLabelList() const {
206                 return std::vector<string>();
207         }
208
209         ///
210         virtual Inset * clone(Buffer const &) const = 0;
211
212         /// returns true to override begin and end inset in file
213         virtual bool directWrite() const;
214
215         /// Returns true if the inset should be centered alone
216         virtual bool display() const { return false; }
217         /// Changes the display state of the inset
218         virtual void display(bool) {}
219         ///
220         /// returns true if this inset needs a row on it's own
221         ///
222         virtual bool needFullRow() const { return false; }
223         ///
224         void setInsetName(string const & s) { name_ = s; }
225         ///
226         string const & getInsetName() const { return name_; }
227         ///
228         void setOwner(Inset * inset) { owner_ = inset; }
229         ///
230         Inset * owner() const { return owner_; }
231         ///
232         void parOwner(Paragraph * par) { par_owner_ = par; }
233         ///
234         Paragraph * parOwner() const { return par_owner_; }
235         ///
236         void setBackgroundColor(LColor::color);
237         ///
238         LColor::color backgroundColor() const;
239         ///
240         int x() const { return top_x; }
241         ///
242         int y() const { return top_baseline; }
243         //
244         // because we could have fake text insets and have to call this
245         // inside them without cast!!!
246         ///
247         virtual LyXText * getLyXText(BufferView const *,
248                                      bool const recursive = false) const;
249         ///
250         virtual void deleteLyXText(BufferView *, bool = true) const {}
251         ///
252         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
253         /// returns the actuall scroll-value
254         virtual int scroll(bool recursive=true) const {
255                 if (!recursive || !owner_)
256                         return scx;
257                 return 0;
258         }
259
260         /// try to get a inset pointer from it's id if we have
261         /// an inset to give back!
262         virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
263         /// if this insets owns paragraphs (f.ex. InsetText) then it
264         /// should return it's very first one!
265         virtual ParagraphList * getParagraphs(int /*num*/) const { return 0; }
266         ///
267         virtual bool haveParagraphs() const {
268                 return false;
269         }
270
271         /// return the cursor if we own one otherwise giv'em just the
272         /// BufferView cursor to work with.
273         virtual LyXCursor const & cursor(BufferView * bview) const;
274         /// id functions
275         int id() const;
276         ///
277         void id(int id_arg);
278
279         /// used to toggle insets
280         // is the inset open?
281         virtual bool isOpen() const { return false; }
282         /// open the inset
283         virtual void open(BufferView *) {}
284         /// close the inset
285         virtual void close(BufferView *) const {}
286         /// check if the font of the char we want inserting is correct
287         /// and modify it if it is not.
288         virtual bool checkInsertChar(LyXFont &);
289         /// we need this here because collapsed insets are only EDITABLE
290         virtual void setFont(BufferView *, LyXFont const &,
291                          bool toggleall = false, bool selectall = false);
292         ///
293         // needed for spellchecking text
294         ///
295         virtual bool allowSpellcheck() const { return false; }
296
297         // should this inset be handled like a normal charater
298         virtual bool isChar() const { return false; }
299         // is this equivalent to a letter?
300         virtual bool isLetter() const { return false; }
301         // is this equivalent to a space (which is BTW different from
302         // a line separator)?
303         virtual bool isSpace() const { return false; }
304         // should we break lines after this inset?
305         virtual bool isLineSeparator() const { return false; }
306         // if this inset has paragraphs should they be output all as default
307         // paragraphs with "Standard" layout?
308         virtual bool forceDefaultParagraphs(Inset const *) const;
309         /** returns true if, when outputing LaTeX, font changes should
310             be closed before generating this inset. This is needed for
311             insets that may contain several paragraphs */
312         virtual bool noFontChange() const { return false; }
313         //
314         virtual void getDrawFont(LyXFont &) const {}
315         /* needed for widths which are % of something
316            returns the value of \textwidth in this inset. Most of the
317            time this is the width of the workarea, but if there is a
318            minipage somewhere, it will be the width of this minipage */
319         virtual int latexTextWidth(BufferView *) const;
320
321         /// mark the inset contents as erased (for change tracking)
322         virtual void markErased() {}
323
324         /** Adds a LaTeX snippet to the Preview Loader for transformation
325          *  into a bitmap image. Does not start the laoding process.
326          *
327          *  Most insets have no interest in this capability, so the method
328          *  defaults to empty.
329          */
330         virtual void addPreview(grfx::PreviewLoader &) const {}
331
332         /** Find the PreviewLoader, add a LaTeX snippet to it and
333          *  start the loading process.
334          *
335          *  Most insets have no interest in this capability, so the method
336          *  defaults to empty.
337          */
338         virtual void generatePreview() const {}
339
340 protected:
341         ///
342         mutable int top_x;
343         ///
344         mutable int top_baseline;
345         ///
346         mutable int scx;
347         ///
348         unsigned int id_;
349         ///
350         static unsigned int inset_id;
351
352 private:
353         ///
354         Inset * owner_;
355         /// the paragraph in which this inset has been inserted
356         Paragraph * par_owner_;
357         ///
358         string name_;
359         ///
360         LColor::color background_color_;
361 };
362
363
364 inline
365 bool Inset::insetAllowed(Inset * in) const
366 {
367         return insetAllowed(in->lyxCode());
368 }
369
370
371 inline
372 bool Inset::checkInsertChar(LyXFont &)
373 {
374         return false;
375 }
376
377 /**
378  * returns true if pointer argument is valid
379  * and points to an editable inset
380  */
381 inline bool isEditableInset(Inset const * i)
382 {
383         return i && i->editable();
384 }
385
386 /**
387  * returns true if pointer argument is valid
388  * and points to a highly editable inset
389  */
390 inline bool isHighlyEditableInset(Inset const * i)
391 {
392         return i && i->editable() == Inset::HIGHLY_EDITABLE;
393 }
394
395 #endif