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