]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
edit->LFUN_INSET_EDIT + CHangeLog + working URL insets.
[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 "frontends/mouse_state.h"
22 #include "support/types.h"
23
24 #include <vector>
25
26 class LyXFont;
27 class Buffer;
28 class Painter;
29 class LyXText;
30 class LyXLex;
31 class Paragraph;
32 class ParagraphList;
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                 ENVIRONMENT_CODE,
130                 ///
131                 HFILL_CODE,
132                 ///
133                 NEWLINE_CODE
134         };
135
136         ///
137         enum {
138                 ///
139                 TEXT_TO_INSET_OFFSET = 2
140         };
141
142         ///
143         enum EDITABLE {
144                 ///
145                 NOT_EDITABLE = 0,
146                 ///
147                 IS_EDITABLE,
148                 ///
149                 HIGHLY_EDITABLE
150         };
151
152         ///
153         typedef dispatch_result RESULT;
154
155         ///
156         Inset();
157         ///
158         Inset(Inset const & in, bool same_id = false);
159         ///
160         virtual int ascent(BufferView *, LyXFont const &) const = 0;
161         ///
162         virtual int descent(BufferView *, LyXFont const &) const = 0;
163         ///
164         virtual int width(BufferView *, LyXFont const &) const = 0;
165         ///
166         virtual void draw(BufferView *, LyXFont const &, int baseline, float & x) const = 0;
167         /// update the inset representation
168         virtual void update(BufferView *, bool = false)
169                 {}
170         /// what appears in the minibuffer when opening
171         virtual string const editMessage() const;
172         ///
173         virtual EDITABLE editable() const;
174         ///
175         virtual bool isTextInset() const { return false; }
176         /// return true if the inset should be removed automatically
177         virtual bool autoDelete() const;
178         /// returns true the inset can hold an inset of given type
179         virtual bool insetAllowed(Inset::Code) const { return false; }
180         /// wrapper around the above
181         bool insetAllowed(Inset * in) const;
182         ///
183         virtual void write(Buffer const *, std::ostream &) const = 0;
184         ///
185         virtual void read(Buffer const *, LyXLex & lex) = 0;
186         /** returns the number of rows (\n's) of generated tex code.
187             fragile == true means, that the inset should take care about
188             fragile commands by adding a \protect before.
189             If the free_spc (freespacing) variable is set, then this inset
190             is in a free-spacing paragraph.
191         */
192         virtual int latex(Buffer const *, std::ostream &, bool fragile,
193                           bool free_spc) const = 0;
194         ///
195         virtual int ascii(Buffer const *,
196                           std::ostream &, int linelen = 0) const = 0;
197         ///
198         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
199         ///
200         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
201         /// Updates needed features for this inset.
202         virtual void validate(LaTeXFeatures & features) const;
203
204         /// returns LyX code associated with the inset. Used for TOC, ...)
205         virtual Inset::Code lyxCode() const { return NO_CODE; }
206
207         virtual std::vector<string> const getLabelList() const {
208                 return std::vector<string>();
209         }
210
211         ///
212         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
213
214         /// returns true to override begin and end inset in file
215         virtual bool directWrite() const;
216
217         /// Returns true if the inset should be centered alone
218         virtual bool display() const { return false; }
219         /// Changes the display state of the inset
220         virtual void display(bool) {}
221         ///
222         /// returns true if this inset needs a row on it's own
223         ///
224         virtual bool needFullRow() const { return false; }
225         ///
226         void setInsetName(string const & s) { name_ = s; }
227         ///
228         string const & getInsetName() const { return name_; }
229         ///
230         void setOwner(Inset * inset) { owner_ = inset; }
231         ///
232         Inset * owner() const { return owner_; }
233         ///
234         void parOwner(Paragraph * par) { par_owner_ = par; }
235         ///
236         Paragraph * parOwner() const { return par_owner_; }
237         ///
238         void setBackgroundColor(LColor::color);
239         ///
240         LColor::color backgroundColor() const;
241         ///
242         int x() const { return top_x; }
243         ///
244         int y() const { return top_baseline; }
245         //
246         // because we could have fake text insets and have to call this
247         // inside them without cast!!!
248         ///
249         virtual LyXText * getLyXText(BufferView const *,
250                                      bool const recursive = false) const;
251         ///
252         virtual void deleteLyXText(BufferView *, bool = true) const {}
253         ///
254         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
255         /// returns the actuall scroll-value
256         virtual int scroll(bool recursive=true) const {
257                 if (!recursive || !owner_)
258                         return scx;
259                 return 0;
260         }
261
262         /// try to get a inset pointer from it's id if we have
263         /// an inset to give back!
264         virtual Inset * getInsetFromID(int /*id*/) const { return 0; }
265         /// if this insets owns paragraphs (f.ex. InsetText) then it
266         /// should return it's very first one!
267         virtual ParagraphList * getParagraphs(int /*num*/) const { return 0; }
268         ///
269         virtual bool haveParagraphs() const {
270                 return false;
271         }
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 int top_baseline;
347         ///
348         mutable int scx;
349         ///
350         unsigned int id_;
351         ///
352         static unsigned int inset_id;
353
354 private:
355         ///
356         Inset * owner_;
357         /// the paragraph in which this inset has been inserted
358         Paragraph * par_owner_;
359         ///
360         string name_;
361         ///
362         LColor::color background_color_;
363 };
364
365
366 inline
367 bool Inset::insetAllowed(Inset * in) const
368 {
369         return insetAllowed(in->lyxCode());
370 }
371
372
373 inline
374 bool Inset::checkInsertChar(LyXFont &)
375 {
376         return false;
377 }
378
379 /**
380  * returns true if pointer argument is valid
381  * and points to an editable inset
382  */
383 inline bool isEditableInset(Inset const * i)
384 {
385         return i && i->editable();
386 }
387
388 /**
389  * returns true if pointer argument is valid
390  * and points to a highly editable inset
391  */
392 inline bool isHighlyEditableInset(Inset const * i)
393 {
394         return i && i->editable() == Inset::HIGHLY_EDITABLE;
395 }
396
397 #endif