]> git.lyx.org Git - features.git/blob - src/insets/inset.h
dont use pragma impementation and interface anymore
[features.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 "frontends/mouse_state.h"
23 #include "support/types.h"
24
25 class LyXFont;
26 class BufferView;
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 {
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                 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                 SPECIALCHAR_CODE, // 25
103                 ///
104                 TABULAR_CODE,
105                 ///
106                 EXTERNAL_CODE,
107 #if 0
108                 ///
109                 THEOREM_CODE,
110 #endif
111                 ///
112                 CAPTION_CODE,
113                 ///
114                 MATHMACRO_CODE, // 30
115                 ///
116                 ERROR_CODE,
117                 ///
118                 CITE_CODE,
119                 ///
120                 FLOAT_LIST_CODE,
121                 ///
122                 INDEX_PRINT_CODE,
123                 ///
124                 OPTARG_CODE
125         };
126
127         ///
128         enum {
129                 ///
130                 TEXT_TO_INSET_OFFSET = 2
131         };
132
133         ///
134         enum EDITABLE {
135                 ///
136                 NOT_EDITABLE = 0,
137                 ///
138                 IS_EDITABLE,
139                 ///
140                 HIGHLY_EDITABLE
141         };
142
143         /** Dispatch result codes
144             Now that nested updatable insets are allowed, the local dispatch
145             becomes a bit complex, just two possible results (boolean)
146             are not enough.
147
148             DISPATCHED          = the inset catched the action
149             DISPATCHED_NOUPDATE = the inset catched the action and no update
150                                   is needed here to redraw the inset
151             FINISHED            = the inset must be unlocked as a result
152                                   of the action
153             FINISHED_RIGHT      = FINISHED, but put the cursor to the RIGHT of
154                                   the inset.
155             FINISHED_UP         = FINISHED, but put the cursor UP of
156                                   the inset.
157             FINISHED_DOWN       = FINISHED, but put the cursor DOWN of
158                                   the inset.
159             UNDISPATCHED        = the action was not catched, it should be
160                                   dispatched by lower level insets
161         */
162         enum RESULT {
163                 UNDISPATCHED = 0,
164                 DISPATCHED,
165                 DISPATCHED_NOUPDATE,
166                 FINISHED,
167                 FINISHED_RIGHT,
168                 FINISHED_UP,
169                 FINISHED_DOWN
170         };
171
172         ///
173         Inset();
174         ///
175         Inset(Inset const & in, bool same_id = false);
176         ///
177         virtual ~Inset() {}
178         ///
179         virtual int ascent(BufferView *, LyXFont const &) const = 0;
180         ///
181         virtual int descent(BufferView *, LyXFont const &) const = 0;
182         ///
183         virtual int width(BufferView *, LyXFont const &) const = 0;
184         ///
185         virtual void draw(BufferView *, LyXFont const &,
186                           int baseline, float & x, bool cleared) const = 0;
187         /// update the inset representation
188         virtual void update(BufferView *, LyXFont const &, bool = false)
189                 {}
190         /// what appears in the minibuffer when opening
191         virtual string const editMessage() const;
192         ///
193         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
194         ///
195         virtual void edit(BufferView *, bool front = true);
196         ///
197         virtual EDITABLE editable() const;
198         ///
199         virtual RESULT localDispatch(FuncRequest const & cmd);
200         ///
201         virtual bool isTextInset() const { return false; }
202         ///
203         virtual bool doClearArea() const { return true; }
204         ///
205         virtual bool autoDelete() const;
206         /// returns true the inset can hold an inset of given type
207         virtual bool insetAllowed(Inset::Code) const { return false; }
208         /// wrapper around the above
209         bool insetAllowed(Inset * in) const;
210         ///
211         virtual void write(Buffer const *, std::ostream &) const = 0;
212         ///
213         virtual void read(Buffer const *, LyXLex & lex) = 0;
214         /** returns the number of rows (\n's) of generated tex code.
215             fragile == true means, that the inset should take care about
216             fragile commands by adding a \protect before.
217             If the free_spc (freespacing) variable is set, then this inset
218             is in a free-spacing paragraph.
219         */
220         virtual int latex(Buffer const *, std::ostream &, bool fragile,
221                           bool free_spc) const = 0;
222         ///
223         virtual int ascii(Buffer const *,
224                           std::ostream &, int linelen = 0) const = 0;
225         ///
226         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
227         ///
228         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
229         /// Updates needed features for this inset.
230         virtual void validate(LaTeXFeatures & features) const;
231         ///
232         virtual bool deletable() const;
233
234         /// returns LyX code associated with the inset. Used for TOC, ...)
235         virtual Inset::Code lyxCode() const { return NO_CODE; }
236
237         virtual std::vector<string> const getLabelList() const {
238                 return std::vector<string>();
239         }
240
241         ///
242         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
243
244         /// returns true to override begin and end inset in file
245         virtual bool directWrite() const;
246
247         /// Returns true if the inset should be centered alone
248         virtual bool display() const { return false; }
249         /// Changes the display state of the inset
250         virtual void display(bool) {}
251         ///
252         /// returns true if this inset needs a row on it's own
253         ///
254         virtual bool needFullRow() const { return false; }
255         ///
256         void setInsetName(string const & s) { name_ = s; }
257         ///
258         string const & getInsetName() const { return name_; }
259         ///
260         void setOwner(Inset * inset) { owner_ = inset; }
261         ///
262         Inset * owner() const { return owner_; }
263         ///
264         void parOwner(Paragraph * par) { par_owner_ = par; }
265         ///
266         Paragraph * parOwner() const {return par_owner_; }
267         ///
268         void setBackgroundColor(LColor::color);
269         ///
270         LColor::color backgroundColor() const;
271         ///
272         int x() const { return top_x; }
273         ///
274         int y() const { return top_baseline; }
275         //
276         // because we could have fake text insets and have to call this
277         // inside them without cast!!!
278         ///
279         virtual LyXText * getLyXText(BufferView const *,
280                                      bool const recursive = false) const;
281         ///
282         virtual void deleteLyXText(BufferView *, bool = true) const {}
283         ///
284         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
285         /// returns the actuall scroll-value
286         virtual int scroll(bool recursive=true) const {
287                 if (!recursive || !owner_)
288                         return scx;
289                 return 0;
290         }
291         /// try to get a paragraph pointer from it's id if we have a
292         /// paragraph to give back!
293         virtual Paragraph * getParFromID(int /* id */) const {
294                 return 0;
295         }
296         /// try to get a inset pointer from it's id if we have
297         /// an inset to give back!
298         virtual Inset * getInsetFromID(int /* id */) const {
299                 return 0;
300         }
301         /// if this insets owns paragraphs (f.ex. InsetText) then it
302         /// should return it's very first one!
303         virtual Paragraph * firstParagraph() const {
304                 return 0;
305         }
306
307         ///
308         virtual Paragraph * getFirstParagraph(int /*num*/) const {
309                 return 0;
310         }
311
312         /// return the cursor if we own one otherwise giv'em just the
313         /// BufferView cursor to work with.
314         virtual LyXCursor const & cursor(BufferView * bview) const;
315         /// id functions
316         int id() const;
317         ///
318         void id(int id_arg);
319
320         /// used to toggle insets
321         // is the inset open?
322         virtual bool isOpen() const { return false; }
323         /// open the inset
324         virtual void open(BufferView *) {}
325         /// close the inset
326         virtual void close(BufferView *) const {}
327         /// check if the font of the char we want inserting is correct
328         /// and modify it if it is not.
329         virtual bool checkInsertChar(LyXFont &);
330         /// we need this here because collapsed insets are only EDITABLE
331         virtual void setFont(BufferView *, LyXFont const &,
332                          bool toggleall = false, bool selectall = false);
333         ///
334         // needed for spellchecking text
335         ///
336         virtual bool allowSpellcheck() const { return false; }
337
338         // should this inset be handled like a normal charater
339         virtual bool isChar() const { return false; }
340         // is this equivalent to a letter?
341         virtual bool isLetter() const { return false; }
342         // is this equivalent to a space (which is BTW different from
343         // a line separator)?
344         virtual bool isSpace() const { return false; }
345         // should we break lines after this inset?
346         virtual bool isLineSeparator() const { return false; }
347         // if this inset has paragraphs should they be output all as default
348         // paragraphs with "Standard" layout?
349         virtual bool forceDefaultParagraphs(Inset const *) const;
350         /** returns true if, when outputing LaTeX, font changes should
351             be closed before generating this inset. This is needed for
352             insets that may contain several paragraphs */
353         virtual bool noFontChange() const { return false; }
354         //
355         virtual void getDrawFont(LyXFont &) const {}
356         /* needed for widths which are % of something
357            returns the value of \textwidth in this inset. Most of the
358            time this is the width of the workarea, but if there is a
359            minipage somewhere, it will be the width of this minipage */
360         virtual int latexTextWidth(BufferView *) const;
361
362         /// mark the inset contents as erased (for change tracking)
363         virtual void markErased() {}
364  
365         /** Adds a LaTeX snippet to the Preview Loader for transformation
366          *  into a bitmap image. Does not start the laoding process.
367          *
368          *  Most insets have no interest in this capability, so the method
369          *  defaults to empty.
370          */
371         virtual void addPreview(grfx::PreviewLoader &) const {}
372
373         /** Find the PreviewLoader, add a LaTeX snippet to it and
374          *  start the loading process.
375          *
376          *  Most insets have no interest in this capability, so the method
377          *  defaults to empty.
378          */
379         virtual void generatePreview() const {}
380
381 protected:
382         ///
383         mutable int top_x;
384         ///
385         mutable bool topx_set; /* have we already drawn ourself! */
386         ///
387         mutable int top_baseline;
388         ///
389         mutable int scx;
390         ///
391         unsigned int id_;
392         ///
393         static unsigned int inset_id;
394
395 private:
396         ///
397         Inset * owner_;
398         /// the paragraph in which this inset has been inserted
399         Paragraph * par_owner_;
400         ///
401         string name_;
402         ///
403         LColor::color background_color_;
404 };
405
406
407 inline
408 bool Inset::insetAllowed(Inset * in) const
409 {
410         return insetAllowed(in->lyxCode());
411 }
412
413
414 inline
415 bool Inset::checkInsertChar(LyXFont &)
416 {
417         return false;
418 }
419
420 //  Updatable Insets. These insets can be locked and receive
421 //  directly user interaction. Currently used only for mathed.
422 //  Note that all pure methods from Inset class are pure here too.
423 //  [Alejandro 080596]
424
425 /** Extracted from Matthias notes:
426  *
427  * An inset can simple call LockInset in it's edit call and *ONLY*
428  * in it's edit call.
429  *
430  * Unlocking is either done by LyX or the inset itself with a
431  * UnlockInset-call
432  *
433  * During the lock, all button and keyboard events will be modified
434  * and send to the inset through the following inset-features. Note that
435  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
436  * to contain the code for restoring the menus and things like this.
437  *
438  * If a inset wishes any redraw and/or update it just has to call
439  * updateInset(this).
440  *
441  * It's is completly irrelevant, where the inset is. UpdateInset will
442  * find it in any paragraph in any buffer.
443  * Of course the_locking_inset and the insets in the current paragraph/buffer
444  *  are checked first, so no performance problem should occur.
445  */
446 class UpdatableInset : public Inset {
447 public:
448         ///
449         UpdatableInset();
450         ///
451         UpdatableInset(UpdatableInset const & in, bool same_id = false);
452
453         /// check if the font of the char we want inserting is correct
454         /// and modify it if it is not.
455         virtual bool checkInsertChar(LyXFont &);
456         ///
457         virtual EDITABLE editable() const;
458
459         ///
460         virtual void toggleInsetCursor(BufferView *);
461         ///
462         virtual void showInsetCursor(BufferView *, bool show = true);
463         ///
464         virtual void hideInsetCursor(BufferView *);
465         ///
466         virtual void fitInsetCursor(BufferView *) const;
467         ///
468         virtual void getCursorPos(BufferView *, int &, int &) const {}
469         ///
470         virtual void insetUnlock(BufferView *);
471         ///
472         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
473         ///
474         virtual void edit(BufferView *, bool front = true);
475         ///
476         virtual void draw(BufferView *, LyXFont const &,
477                           int baseline, float & x, bool cleared) const;
478         ///
479         virtual bool insertInset(BufferView *, Inset *) { return false; }
480         ///
481         virtual UpdatableInset * getLockingInset() const {
482                 return const_cast<UpdatableInset *>(this);
483         }
484         ///
485         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
486                 { return (c == lyxCode()) ? this : 0; }
487         ///
488         virtual int insetInInsetY() const { return 0; }
489         ///
490         virtual bool updateInsetInInset(BufferView *, Inset *)
491                 { return false; }
492         ///
493         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
494                 { return false; }
495         ///
496         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
497                                         bool /*lr*/ = false)
498                 { return false; }
499         ///  An updatable inset could handle lyx editing commands
500         virtual RESULT localDispatch(FuncRequest const & cmd);
501         ///
502         bool isCursorVisible() const { return cursor_visible_; }
503         ///
504         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
505         ///
506         int scroll(bool recursive = true) const {
507                 // We need this method to not clobber the real method in Inset
508                 return Inset::scroll(recursive);
509         }
510         ///
511         virtual bool showInsetDialog(BufferView *) const { return false; }
512         ///
513         virtual void nodraw(bool b) const {
514                 block_drawing_ = b;
515         }
516         ///
517         virtual bool nodraw() const {
518                 return block_drawing_;
519         }
520         ///
521         // needed for spellchecking text
522         ///
523         virtual bool allowSpellcheck() const { return false; }
524         ///
525         virtual WordLangTuple const
526         selectNextWordToSpellcheck(BufferView *, float & value) const;
527         ///
528         virtual void selectSelectedWord(BufferView *) {}
529         ///
530         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
531                 return;
532         }
533
534         /// find the next change in the inset
535         virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
536  
537         ///
538         // needed for search/replace functionality
539         ///
540         virtual bool searchForward(BufferView *, string const &,
541                                    bool = true, bool = false);
542         ///
543         virtual bool searchBackward(BufferView *, string const &,
544                                     bool = true, bool = false);
545
546 protected:
547         ///
548         void toggleCursorVisible() const {
549                 cursor_visible_ = !cursor_visible_;
550         }
551         ///
552         void setCursorVisible(bool b) const {
553                 cursor_visible_ = b;
554         }
555         /// scrolls to absolute position in bufferview-workwidth * sx units
556         void scroll(BufferView *, float sx) const;
557         /// scrolls offset pixels
558         void scroll(BufferView *, int offset) const;
559
560 private:
561         ///
562         mutable bool cursor_visible_;
563         ///
564         mutable bool block_drawing_;
565 };
566
567 inline
568 bool UpdatableInset::checkInsertChar(LyXFont &)
569 {
570         return true;
571 }
572
573 /**
574  * returns true if pointer argument is valid
575  * and points to an editable inset
576  */
577 inline bool isEditableInset(Inset * i)
578 {
579         return i && i->editable();
580 }
581
582 /**
583  * returns true if pointer argument is valid
584  * and points to a highly editable inset
585  */
586 inline bool isHighlyEditableInset(Inset * i)
587 {
588         return i && i->editable() == Inset::HIGHLY_EDITABLE;
589 }
590
591 #endif