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