]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
4241b81d70af25256db90245e7a890ea9babbe92
[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 Buffer;
28 class Painter;
29 class LyXText;
30 class LyXLex;
31 class Paragraph;
32 class LyXCursor;
33 class FuncRequest;
34 class WordLangTuple;
35
36 struct LaTeXFeatures;
37
38 namespace grfx {
39         class PreviewLoader;
40 }
41
42 /// Insets
43 class Inset : public InsetBase {
44 public:
45         /** This is not quite the correct place for this enum. I think
46             the correct would be to let each subclass of Inset declare
47             its own enum code. Actually the notion of an Inset::Code
48             should be avoided, but I am not sure how this could be done
49             in a cleaner way. */
50         enum Code {
51                 ///
52                 NO_CODE,
53                 ///
54                 TOC_CODE,  // do these insets really need a code? (ale)
55                 ///
56                 QUOTE_CODE,
57                 ///
58                 MARK_CODE,
59                 ///
60                 REF_CODE, // 5
61                 ///
62                 URL_CODE,
63                 ///
64                 HTMLURL_CODE,
65                 ///
66                 SEPARATOR_CODE,
67                 ///
68                 ENDING_CODE,
69                 ///
70                 LABEL_CODE, // 10
71                 ///
72                 NOTE_CODE,
73                 ///
74                 ACCENT_CODE,
75                 ///
76                 MATH_CODE,
77                 ///
78                 INDEX_CODE,
79                 ///
80                 INCLUDE_CODE, // 15
81                 ///
82                 GRAPHICS_CODE,
83                 ///
84                 PARENT_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                 SPECIALCHAR_CODE, // 25
105                 ///
106                 TABULAR_CODE,
107                 ///
108                 EXTERNAL_CODE,
109 #if 0
110                 ///
111                 THEOREM_CODE,
112 #endif
113                 ///
114                 CAPTION_CODE,
115                 ///
116                 MATHMACRO_CODE, // 30
117                 ///
118                 ERROR_CODE,
119                 ///
120                 CITE_CODE,
121                 ///
122                 FLOAT_LIST_CODE,
123                 ///
124                 INDEX_PRINT_CODE,
125                 ///
126                 OPTARG_CODE,
127                 ///
128                 HFILL_CODE
129         };
130
131         ///
132         enum {
133                 ///
134                 TEXT_TO_INSET_OFFSET = 2
135         };
136
137         ///
138         enum EDITABLE {
139                 ///
140                 NOT_EDITABLE = 0,
141                 ///
142                 IS_EDITABLE,
143                 ///
144                 HIGHLY_EDITABLE
145         };
146
147         ///
148         typedef dispatch_result RESULT;
149
150         ///
151         Inset();
152         ///
153         Inset(Inset const & in, bool same_id = false);
154         ///
155         virtual int ascent(BufferView *, LyXFont const &) const = 0;
156         ///
157         virtual int descent(BufferView *, LyXFont const &) const = 0;
158         ///
159         virtual int width(BufferView *, LyXFont const &) const = 0;
160         ///
161         virtual void draw(BufferView *, LyXFont const &,
162                           int baseline, float & x, bool cleared) const = 0;
163         /// update the inset representation
164         virtual void update(BufferView *, LyXFont const &, bool = false)
165                 {}
166         /// what appears in the minibuffer when opening
167         virtual string const editMessage() const;
168         ///
169         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
170         ///
171         virtual void edit(BufferView *, bool front = true);
172         ///
173         virtual EDITABLE editable() const;
174         ///
175         virtual bool isTextInset() const { return false; }
176         ///
177         virtual bool doClearArea() const { return true; }
178         /// return true if the inset should be removed automatically
179         virtual bool autoDelete() const;
180         /// returns true the inset can hold an inset of given type
181         virtual bool insetAllowed(Inset::Code) const { return false; }
182         /// wrapper around the above
183         bool insetAllowed(Inset * in) const;
184         ///
185         virtual void write(Buffer const *, std::ostream &) const = 0;
186         ///
187         virtual void read(Buffer const *, LyXLex & lex) = 0;
188         /** returns the number of rows (\n's) of generated tex code.
189             fragile == true means, that the inset should take care about
190             fragile commands by adding a \protect before.
191             If the free_spc (freespacing) variable is set, then this inset
192             is in a free-spacing paragraph.
193         */
194         virtual int latex(Buffer const *, std::ostream &, bool fragile,
195                           bool free_spc) const = 0;
196         ///
197         virtual int ascii(Buffer const *,
198                           std::ostream &, int linelen = 0) const = 0;
199         ///
200         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
201         ///
202         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
203         /// Updates needed features for this inset.
204         virtual void validate(LaTeXFeatures & features) const;
205
206         /// returns LyX code associated with the inset. Used for TOC, ...)
207         virtual Inset::Code lyxCode() const { return NO_CODE; }
208
209         virtual std::vector<string> const getLabelList() const {
210                 return std::vector<string>();
211         }
212
213         ///
214         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
215
216         /// returns true to override begin and end inset in file
217         virtual bool directWrite() const;
218
219         /// Returns true if the inset should be centered alone
220         virtual bool display() const { return false; }
221         /// Changes the display state of the inset
222         virtual void display(bool) {}
223         ///
224         /// returns true if this inset needs a row on it's own
225         ///
226         virtual bool needFullRow() const { return false; }
227         ///
228         void setInsetName(string const & s) { name_ = s; }
229         ///
230         string const & getInsetName() const { return name_; }
231         ///
232         void setOwner(Inset * inset) { owner_ = inset; }
233         ///
234         Inset * owner() const { return owner_; }
235         ///
236         void parOwner(Paragraph * par) { par_owner_ = par; }
237         ///
238         Paragraph * parOwner() const { return par_owner_; }
239         ///
240         void setBackgroundColor(LColor::color);
241         ///
242         LColor::color backgroundColor() const;
243         ///
244         int x() const { return top_x; }
245         ///
246         int y() const { return top_baseline; }
247         //
248         // because we could have fake text insets and have to call this
249         // inside them without cast!!!
250         ///
251         virtual LyXText * getLyXText(BufferView const *,
252                                      bool const recursive = false) const;
253         ///
254         virtual void deleteLyXText(BufferView *, bool = true) const {}
255         ///
256         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
257         /// returns the actuall scroll-value
258         virtual int scroll(bool recursive=true) const {
259                 if (!recursive || !owner_)
260                         return scx;
261                 return 0;
262         }
263
264         /// try to get a inset pointer from it's id if we have
265         /// an inset to give back!
266         virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
267         /// if this insets owns paragraphs (f.ex. InsetText) then it
268         /// should return it's very first one!
269         virtual Paragraph * firstParagraph() const { return 0; }
270         ///
271         virtual Paragraph * getFirstParagraph(int /*num*/) const { return 0; }
272
273         /// return the cursor if we own one otherwise giv'em just the
274         /// BufferView cursor to work with.
275         virtual LyXCursor const & cursor(BufferView * bview) const;
276         /// id functions
277         int id() const;
278         ///
279         void id(int id_arg);
280
281         /// used to toggle insets
282         // is the inset open?
283         virtual bool isOpen() const { return false; }
284         /// open the inset
285         virtual void open(BufferView *) {}
286         /// close the inset
287         virtual void close(BufferView *) const {}
288         /// check if the font of the char we want inserting is correct
289         /// and modify it if it is not.
290         virtual bool checkInsertChar(LyXFont &);
291         /// we need this here because collapsed insets are only EDITABLE
292         virtual void setFont(BufferView *, LyXFont const &,
293                          bool toggleall = false, bool selectall = false);
294         ///
295         // needed for spellchecking text
296         ///
297         virtual bool allowSpellcheck() const { return false; }
298
299         // should this inset be handled like a normal charater
300         virtual bool isChar() const { return false; }
301         // is this equivalent to a letter?
302         virtual bool isLetter() const { return false; }
303         // is this equivalent to a space (which is BTW different from
304         // a line separator)?
305         virtual bool isSpace() const { return false; }
306         // should we break lines after this inset?
307         virtual bool isLineSeparator() const { return false; }
308         // if this inset has paragraphs should they be output all as default
309         // paragraphs with "Standard" layout?
310         virtual bool forceDefaultParagraphs(Inset const *) const;
311         /** returns true if, when outputing LaTeX, font changes should
312             be closed before generating this inset. This is needed for
313             insets that may contain several paragraphs */
314         virtual bool noFontChange() const { return false; }
315         //
316         virtual void getDrawFont(LyXFont &) const {}
317         /* needed for widths which are % of something
318            returns the value of \textwidth in this inset. Most of the
319            time this is the width of the workarea, but if there is a
320            minipage somewhere, it will be the width of this minipage */
321         virtual int latexTextWidth(BufferView *) const;
322
323         /// mark the inset contents as erased (for change tracking)
324         virtual void markErased() {}
325  
326         /** Adds a LaTeX snippet to the Preview Loader for transformation
327          *  into a bitmap image. Does not start the laoding process.
328          *
329          *  Most insets have no interest in this capability, so the method
330          *  defaults to empty.
331          */
332         virtual void addPreview(grfx::PreviewLoader &) const {}
333
334         /** Find the PreviewLoader, add a LaTeX snippet to it and
335          *  start the loading process.
336          *
337          *  Most insets have no interest in this capability, so the method
338          *  defaults to empty.
339          */
340         virtual void generatePreview() const {}
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 const * 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 const * i)
395 {
396         return i && i->editable() == Inset::HIGHLY_EDITABLE;
397 }
398
399 #endif