]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
First step towards unified 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
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 BufferView;
28 class Buffer;
29 class Painter;
30 class LyXText;
31 class LyXLex;
32 class Paragraph;
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                 BIBTEX_CODE,
88                 ///
89                 TEXT_CODE,
90                 ///
91                 ERT_CODE, // 20
92                 ///
93                 FOOT_CODE,
94                 ///
95                 MARGIN_CODE,
96                 ///
97                 FLOAT_CODE,
98                 ///
99                 WRAP_CODE,
100                 ///
101                 MINIPAGE_CODE,
102                 ///
103                 SPECIALCHAR_CODE, // 25
104                 ///
105                 TABULAR_CODE,
106                 ///
107                 EXTERNAL_CODE,
108 #if 0
109                 ///
110                 THEOREM_CODE,
111 #endif
112                 ///
113                 CAPTION_CODE,
114                 ///
115                 MATHMACRO_CODE, // 30
116                 ///
117                 ERROR_CODE,
118                 ///
119                 CITE_CODE,
120                 ///
121                 FLOAT_LIST_CODE,
122                 ///
123                 INDEX_PRINT_CODE,
124                 ///
125                 OPTARG_CODE
126         };
127
128         ///
129         enum {
130                 ///
131                 TEXT_TO_INSET_OFFSET = 2
132         };
133
134         ///
135         enum EDITABLE {
136                 ///
137                 NOT_EDITABLE = 0,
138                 ///
139                 IS_EDITABLE,
140                 ///
141                 HIGHLY_EDITABLE
142         };
143
144         ///
145         typedef InsetBase::dispatch_result RESULT;
146
147         ///
148         Inset();
149         ///
150         Inset(Inset const & in, bool same_id = false);
151         ///
152         virtual ~Inset() {}
153         ///
154         virtual int ascent(BufferView *, LyXFont const &) const = 0;
155         ///
156         virtual int descent(BufferView *, LyXFont const &) const = 0;
157         ///
158         virtual int width(BufferView *, LyXFont const &) const = 0;
159         ///
160         virtual void draw(BufferView *, LyXFont const &,
161                           int baseline, float & x, bool cleared) const = 0;
162         /// update the inset representation
163         virtual void update(BufferView *, LyXFont const &, bool = false)
164                 {}
165         /// what appears in the minibuffer when opening
166         virtual string const editMessage() const;
167         ///
168         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
169         ///
170         virtual void edit(BufferView *, bool front = true);
171         ///
172         virtual EDITABLE editable() const;
173         ///
174         virtual RESULT localDispatch(FuncRequest const & cmd);
175         ///
176         virtual bool isTextInset() const { return false; }
177         ///
178         virtual bool doClearArea() const { return true; }
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 {
268                 return 0;
269         }
270         /// if this insets owns paragraphs (f.ex. InsetText) then it
271         /// should return it's very first one!
272         virtual Paragraph * firstParagraph() const {
273                 return 0;
274         }
275
276         ///
277         virtual Paragraph * getFirstParagraph(int /*num*/) const {
278                 return 0;
279         }
280
281         /// return the cursor if we own one otherwise giv'em just the
282         /// BufferView cursor to work with.
283         virtual LyXCursor const & cursor(BufferView * bview) const;
284         /// id functions
285         int id() const;
286         ///
287         void id(int id_arg);
288
289         /// used to toggle insets
290         // is the inset open?
291         virtual bool isOpen() const { return false; }
292         /// open the inset
293         virtual void open(BufferView *) {}
294         /// close the inset
295         virtual void close(BufferView *) const {}
296         /// check if the font of the char we want inserting is correct
297         /// and modify it if it is not.
298         virtual bool checkInsertChar(LyXFont &);
299         /// we need this here because collapsed insets are only EDITABLE
300         virtual void setFont(BufferView *, LyXFont const &,
301                          bool toggleall = false, bool selectall = false);
302         ///
303         // needed for spellchecking text
304         ///
305         virtual bool allowSpellcheck() const { return false; }
306
307         // should this inset be handled like a normal charater
308         virtual bool isChar() const { return false; }
309         // is this equivalent to a letter?
310         virtual bool isLetter() const { return false; }
311         // is this equivalent to a space (which is BTW different from
312         // a line separator)?
313         virtual bool isSpace() const { return false; }
314         // should we break lines after this inset?
315         virtual bool isLineSeparator() const { return false; }
316         // if this inset has paragraphs should they be output all as default
317         // paragraphs with "Standard" layout?
318         virtual bool forceDefaultParagraphs(Inset const *) const;
319         /** returns true if, when outputing LaTeX, font changes should
320             be closed before generating this inset. This is needed for
321             insets that may contain several paragraphs */
322         virtual bool noFontChange() const { return false; }
323         //
324         virtual void getDrawFont(LyXFont &) const {}
325         /* needed for widths which are % of something
326            returns the value of \textwidth in this inset. Most of the
327            time this is the width of the workarea, but if there is a
328            minipage somewhere, it will be the width of this minipage */
329         virtual int latexTextWidth(BufferView *) const;
330
331         /// mark the inset contents as erased (for change tracking)
332         virtual void markErased() {}
333  
334         /** Adds a LaTeX snippet to the Preview Loader for transformation
335          *  into a bitmap image. Does not start the laoding process.
336          *
337          *  Most insets have no interest in this capability, so the method
338          *  defaults to empty.
339          */
340         virtual void addPreview(grfx::PreviewLoader &) const {}
341
342         /** Find the PreviewLoader, add a LaTeX snippet to it and
343          *  start the loading process.
344          *
345          *  Most insets have no interest in this capability, so the method
346          *  defaults to empty.
347          */
348         virtual void generatePreview() const {}
349
350
351 protected:
352         ///
353         mutable int top_x;
354         ///
355         mutable bool topx_set; /* have we already drawn ourself! */
356         ///
357         mutable int top_baseline;
358         ///
359         mutable int scx;
360         ///
361         unsigned int id_;
362         ///
363         static unsigned int inset_id;
364
365 private:
366         ///
367         Inset * owner_;
368         /// the paragraph in which this inset has been inserted
369         Paragraph * par_owner_;
370         ///
371         string name_;
372         ///
373         LColor::color background_color_;
374 };
375
376
377 inline
378 bool Inset::insetAllowed(Inset * in) const
379 {
380         return insetAllowed(in->lyxCode());
381 }
382
383
384 inline
385 bool Inset::checkInsertChar(LyXFont &)
386 {
387         return false;
388 }
389
390 //  Updatable Insets. These insets can be locked and receive
391 //  directly user interaction. Currently used only for mathed.
392 //  Note that all pure methods from Inset class are pure here too.
393 //  [Alejandro 080596]
394
395 /** Extracted from Matthias notes:
396  *
397  * An inset can simple call LockInset in it's edit call and *ONLY*
398  * in it's edit call.
399  *
400  * Unlocking is either done by LyX or the inset itself with a
401  * UnlockInset-call
402  *
403  * During the lock, all button and keyboard events will be modified
404  * and send to the inset through the following inset-features. Note that
405  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
406  * to contain the code for restoring the menus and things like this.
407  *
408  * If a inset wishes any redraw and/or update it just has to call
409  * updateInset(this).
410  *
411  * It's is completly irrelevant, where the inset is. UpdateInset will
412  * find it in any paragraph in any buffer.
413  * Of course the_locking_inset and the insets in the current paragraph/buffer
414  *  are checked first, so no performance problem should occur.
415  */
416 class UpdatableInset : public Inset {
417 public:
418         ///
419         UpdatableInset();
420         ///
421         UpdatableInset(UpdatableInset const & in, bool same_id = false);
422
423         /// check if the font of the char we want inserting is correct
424         /// and modify it if it is not.
425         virtual bool checkInsertChar(LyXFont &);
426         ///
427         virtual EDITABLE editable() const;
428
429         ///
430         virtual void toggleInsetCursor(BufferView *);
431         ///
432         virtual void showInsetCursor(BufferView *, bool show = true);
433         ///
434         virtual void hideInsetCursor(BufferView *);
435         ///
436         virtual void fitInsetCursor(BufferView *) const;
437         ///
438         virtual void getCursorPos(BufferView *, int &, int &) const {}
439         ///
440         virtual void insetUnlock(BufferView *);
441         ///
442         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
443         ///
444         virtual void edit(BufferView *, bool front = true);
445         ///
446         virtual void draw(BufferView *, LyXFont const &,
447                           int baseline, float & x, bool cleared) const;
448         ///
449         virtual bool insertInset(BufferView *, Inset *) { return false; }
450         ///
451         virtual UpdatableInset * getLockingInset() const {
452                 return const_cast<UpdatableInset *>(this);
453         }
454         ///
455         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
456                 { return (c == lyxCode()) ? this : 0; }
457         ///
458         virtual int insetInInsetY() const { return 0; }
459         ///
460         virtual bool updateInsetInInset(BufferView *, Inset *)
461                 { return false; }
462         ///
463         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
464                 { return false; }
465         ///
466         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
467                                         bool /*lr*/ = false)
468                 { return false; }
469         ///  An updatable inset could handle lyx editing commands
470         virtual RESULT localDispatch(FuncRequest const & cmd);
471         ///
472         bool isCursorVisible() const { return cursor_visible_; }
473         ///
474         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
475         ///
476         int scroll(bool recursive = true) const {
477                 // We need this method to not clobber the real method in Inset
478                 return Inset::scroll(recursive);
479         }
480         ///
481         virtual bool showInsetDialog(BufferView *) const { return false; }
482         ///
483         virtual void nodraw(bool b) const {
484                 block_drawing_ = b;
485         }
486         ///
487         virtual bool nodraw() const {
488                 return block_drawing_;
489         }
490         ///
491         // needed for spellchecking text
492         ///
493         virtual bool allowSpellcheck() const { return false; }
494         ///
495         virtual WordLangTuple const
496         selectNextWordToSpellcheck(BufferView *, float & value) const;
497         ///
498         virtual void selectSelectedWord(BufferView *) {}
499         ///
500         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
501                 return;
502         }
503
504         /// find the next change in the inset
505         virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
506  
507         ///
508         // needed for search/replace functionality
509         ///
510         virtual bool searchForward(BufferView *, string const &,
511                                    bool = true, bool = false);
512         ///
513         virtual bool searchBackward(BufferView *, string const &,
514                                     bool = true, bool = false);
515
516
517 protected:
518         ///
519         void toggleCursorVisible() const {
520                 cursor_visible_ = !cursor_visible_;
521         }
522         ///
523         void setCursorVisible(bool b) const {
524                 cursor_visible_ = b;
525         }
526         /// scrolls to absolute position in bufferview-workwidth * sx units
527         void scroll(BufferView *, float sx) const;
528         /// scrolls offset pixels
529         void scroll(BufferView *, int offset) const;
530
531 private:
532         ///
533         mutable bool cursor_visible_;
534         ///
535         mutable bool block_drawing_;
536 };
537
538 inline
539 bool UpdatableInset::checkInsertChar(LyXFont &)
540 {
541         return true;
542 }
543
544 /**
545  * returns true if pointer argument is valid
546  * and points to an editable inset
547  */
548 inline bool isEditableInset(Inset * i)
549 {
550         return i && i->editable();
551 }
552
553 /**
554  * returns true if pointer argument is valid
555  * and points to a highly editable inset
556  */
557 inline bool isHighlyEditableInset(Inset * i)
558 {
559         return i && i->editable() == Inset::HIGHLY_EDITABLE;
560 }
561
562 #endif