]> git.lyx.org Git - features.git/blob - src/insets/inset.h
IU of clone() and getLabelList()
[features.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 grfx {
38         class PreviewLoader;
39 }
40
41 /// Insets
42 class Inset : public InsetBase {
43 public:
44         /** This is not quite the correct place for this enum. I think
45             the correct would be to let each subclass of Inset declare
46             its own enum code. Actually the notion of an Inset::Code
47             should be avoided, but I am not sure how this could be done
48             in a cleaner way. */
49         enum Code {
50                 ///
51                 NO_CODE,
52                 ///
53                 TOC_CODE,  // do these insets really need a code? (ale)
54                 ///
55                 QUOTE_CODE,
56                 ///
57                 MARK_CODE,
58                 ///
59                 REF_CODE, // 5
60                 ///
61                 URL_CODE,
62                 ///
63                 HTMLURL_CODE,
64                 ///
65                 SEPARATOR_CODE,
66                 ///
67                 ENDING_CODE,
68                 ///
69                 LABEL_CODE, // 10
70                 ///
71                 NOTE_CODE,
72                 ///
73                 ACCENT_CODE,
74                 ///
75                 MATH_CODE,
76                 ///
77                 INDEX_CODE,
78                 ///
79                 INCLUDE_CODE, // 15
80                 ///
81                 GRAPHICS_CODE,
82                 ///
83                 BIBITEM_CODE,
84                 ///
85                 BIBTEX_CODE,
86                 ///
87                 TEXT_CODE,
88                 ///
89                 ERT_CODE, // 20
90                 ///
91                 FOOT_CODE,
92                 ///
93                 MARGIN_CODE,
94                 ///
95                 FLOAT_CODE,
96                 ///
97                 WRAP_CODE,
98                 ///
99                 MINIPAGE_CODE,
100                 ///
101                 SPACE_CODE,
102                 ///
103                 SPECIALCHAR_CODE, // 25
104                 ///
105                 TABULAR_CODE,
106                 ///
107                 EXTERNAL_CODE,
108 #if 0
109                 ///
110                 THEOREM_CODE,
111 #endif
112                 ///
113                 CAPTION_CODE,
114                 ///
115                 MATHMACRO_CODE, // 30
116                 ///
117                 ERROR_CODE,
118                 ///
119                 CITE_CODE,
120                 ///
121                 FLOAT_LIST_CODE,
122                 ///
123                 INDEX_PRINT_CODE,
124                 ///
125                 OPTARG_CODE,
126                 ///
127                 ENVIRONMENT_CODE,
128                 ///
129                 HFILL_CODE,
130                 ///
131                 NEWLINE_CODE
132         };
133
134         ///
135         enum {
136                 ///
137                 TEXT_TO_INSET_OFFSET = 2
138         };
139
140         ///
141         enum EDITABLE {
142                 ///
143                 NOT_EDITABLE = 0,
144                 ///
145                 IS_EDITABLE,
146                 ///
147                 HIGHLY_EDITABLE
148         };
149
150         ///
151         typedef dispatch_result RESULT;
152
153         ///
154         Inset();
155         ///
156         Inset(Inset const & in);
157         ///
158         int ascent(BufferView *, LyXFont const &) const;
159         ///
160         int descent(BufferView *, LyXFont const &) const;
161         ///
162         int width(BufferView *, LyXFont const &) const;
163         /// update the inset representation
164         virtual void update(BufferView *, bool = false)
165                 {}
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(Inset::Code) const { return false; }
176         /// wrapper around the above
177         bool insetAllowed(Inset * 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 Inset::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 const 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 Inset * 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(Inset 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(grfx::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         ///
332         unsigned int id_;
333         ///
334         static unsigned int inset_id;
335
336 private:
337         ///
338         UpdatableInset * owner_;
339         /// the paragraph in which this inset has been inserted
340         Paragraph * par_owner_;
341         ///
342         string name_;
343         ///
344         LColor::color background_color_;
345 };
346
347
348 inline
349 bool Inset::insetAllowed(Inset * in) const
350 {
351         return insetAllowed(in->lyxCode());
352 }
353
354
355 inline
356 bool Inset::checkInsertChar(LyXFont &)
357 {
358         return false;
359 }
360
361 /**
362  * returns true if pointer argument is valid
363  * and points to an editable inset
364  */
365 inline bool isEditableInset(Inset const * i)
366 {
367         return i && i->editable();
368 }
369
370 /**
371  * returns true if pointer argument is valid
372  * and points to a highly editable inset
373  */
374 inline bool isHighlyEditableInset(Inset const * i)
375 {
376         return i && i->editable() == Inset::HIGHLY_EDITABLE;
377 }
378
379 #endif