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