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