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