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