]> git.lyx.org Git - features.git/blob - src/insets/inset.h
c3ff56608b68a985ae3b29e2ea0f90ee5a16cf66
[features.git] / src / insets / inset.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  *
5  *           LyX, The Document Processor
6  *
7  *           Copyright 1995-2001 the LyX Team.
8  *
9  * ====================================================== */
10
11 #ifndef INSET_H
12 #define INSET_H
13
14 #ifdef __GNUG__
15 #pragma interface
16 #endif
17
18 #include <vector>
19 #include "LString.h"
20 #include "LColor.h"
21 #include "frontends/mouse_state.h"
22 #include "WordLangTuple.h"
23
24 class LyXFont;
25 class BufferView;
26 class Buffer;
27 class Painter;
28 class LyXText;
29 class LyXLex;
30 class Paragraph;
31 class LyXCursor;
32 class FuncRequest;
33
34 struct LaTeXFeatures;
35
36 namespace grfx {
37         class PreviewLoader;
38 }
39
40 /// Insets
41 class Inset {
42 public:
43         /** This is not quite the correct place for this enum. I think
44             the correct would be to let each subclass of Inset declare
45             its own enum code. Actually the notion of an Inset::Code
46             should be avoided, but I am not sure how this could be done
47             in a cleaner way. */
48         enum Code {
49                 ///
50                 NO_CODE,
51                 ///
52                 TOC_CODE,  // do these insets really need a code? (ale)
53                 ///
54                 QUOTE_CODE,
55                 ///
56                 MARK_CODE,
57                 ///
58                 REF_CODE, // 5
59                 ///
60                 URL_CODE,
61                 ///
62                 HTMLURL_CODE,
63                 ///
64                 SEPARATOR_CODE,
65                 ///
66                 ENDING_CODE,
67                 ///
68                 LABEL_CODE, // 10
69                 ///
70                 NOTE_CODE,
71                 ///
72                 ACCENT_CODE,
73                 ///
74                 MATH_CODE,
75                 ///
76                 INDEX_CODE,
77                 ///
78                 INCLUDE_CODE, // 15
79                 ///
80                 GRAPHICS_CODE,
81                 ///
82                 PARENT_CODE,
83                 ///
84                 BIBTEX_CODE,
85                 ///
86                 TEXT_CODE,
87                 ///
88                 ERT_CODE, // 20
89                 ///
90                 FOOT_CODE,
91                 ///
92                 MARGIN_CODE,
93                 ///
94                 FLOAT_CODE,
95                 ///
96                 MINIPAGE_CODE,
97                 ///
98                 SPECIALCHAR_CODE, // 25
99                 ///
100                 TABULAR_CODE,
101                 ///
102                 EXTERNAL_CODE,
103 #if 0
104                 ///
105                 THEOREM_CODE,
106 #endif
107                 ///
108                 CAPTION_CODE,
109                 ///
110                 MATHMACRO_CODE, // 30
111                 ///
112                 ERROR_CODE,
113                 ///
114                 CITE_CODE,
115                 ///
116                 FLOAT_LIST_CODE,
117                 ///
118                 INDEX_PRINT_CODE
119         };
120
121         ///
122         enum {
123                 ///
124                 TEXT_TO_INSET_OFFSET = 2
125         };
126
127         ///
128         enum EDITABLE {
129                 ///
130                 NOT_EDITABLE = 0,
131                 ///
132                 IS_EDITABLE,
133                 ///
134                 HIGHLY_EDITABLE
135         };
136
137         ///
138         Inset();
139         ///
140         Inset(Inset const & in, bool same_id = false);
141         ///
142         virtual ~Inset() {}
143         ///
144         virtual int ascent(BufferView *, LyXFont const &) const = 0;
145         ///
146         virtual int descent(BufferView *, LyXFont const &) const = 0;
147         ///
148         virtual int width(BufferView *, LyXFont const &) const = 0;
149         ///
150         virtual void draw(BufferView *, LyXFont const &,
151                           int baseline, float & x, bool cleared) const = 0;
152         /// update the inset representation
153         virtual void update(BufferView *, LyXFont const &, bool = false)
154                 {}
155         /// what appears in the minibuffer when opening
156         virtual string const editMessage() const;
157         ///
158         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
159         ///
160         virtual void edit(BufferView *, bool front = true);
161         ///
162         virtual EDITABLE editable() const;
163         /// This is called when the user clicks inside an inset
164         virtual void insetButtonPress(BufferView *, int, int, mouse_button::state) {}
165         /// This is called when the user releases the button inside an inset
166         // the bool return is used to see if we opened a dialog so that we can
167         // check this from an outer inset and open the dialog of the
168         // outer inset if that one has one!
169         virtual bool insetButtonRelease(BufferView *, int, int, mouse_button::state)
170                 { return editable() == IS_EDITABLE; }
171         /// This is called when the user moves the mouse inside an inset
172         virtual void insetMotionNotify(BufferView *, int , int, mouse_button::state) {}
173         ///
174         virtual bool isTextInset() const { return false; }
175         ///
176         virtual bool doClearArea() const { return true; }
177         ///
178         virtual bool autoDelete() const;
179         /// returns true the inset can hold an inset of given type
180         virtual bool insetAllowed(Inset::Code) const { return false; }
181         /// wrapper around the above
182         bool insetAllowed(Inset * in) const;
183         ///
184         virtual void write(Buffer const *, std::ostream &) const = 0;
185         ///
186         virtual void read(Buffer const *, LyXLex & lex) = 0;
187         /** returns the number of rows (\n's) of generated tex code.
188             fragile == true means, that the inset should take care about
189             fragile commands by adding a \protect before.
190             If the free_spc (freespacing) variable is set, then this inset
191             is in a free-spacing paragraph.
192         */
193         virtual int latex(Buffer const *, std::ostream &, bool fragile,
194                           bool free_spc) const = 0;
195         ///
196         virtual int ascii(Buffer const *,
197                           std::ostream &, int linelen = 0) const = 0;
198         ///
199         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
200         ///
201         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
202         /// Updates needed features for this inset.
203         virtual void validate(LaTeXFeatures & features) const;
204         ///
205         virtual bool deletable() 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         /// try to get a paragraph pointer from it's id if we have a
265         /// paragraph to give back!
266         virtual Paragraph * getParFromID(int /* id */) const {
267                 return 0;
268         }
269         /// try to get a inset pointer from it's id if we have
270         /// an inset to give back!
271         virtual Inset * getInsetFromID(int /* id */) const {
272                 return 0;
273         }
274         /// if this insets owns paragraphs (f.ex. InsetText) then it
275         /// should return it's very first one!
276         virtual Paragraph * firstParagraph() const {
277                 return 0;
278         }
279
280         ///
281         virtual Paragraph * getFirstParagraph(int /*num*/) const {
282                 return 0;
283         }
284
285         /// return the cursor if we own one otherwise giv'em just the
286         /// BufferView cursor to work with.
287         virtual LyXCursor const & cursor(BufferView * bview) const;
288         /// id functions
289         int id() const;
290         ///
291         void id(int id_arg);
292
293         /// used to toggle insets
294         // is the inset open?
295         virtual bool isOpen() const { return false; }
296         /// open the inset
297         virtual void open(BufferView *) {}
298         /// close the inset
299         virtual void close(BufferView *) const {}
300         /// check if the font of the char we want inserting is correct
301         /// and modify it if it is not.
302         virtual bool checkInsertChar(LyXFont &);
303         /// we need this here because collapsed insets are only EDITABLE
304         virtual void setFont(BufferView *, LyXFont const &,
305                          bool toggleall = false, bool selectall = false);
306         ///
307         // needed for spellchecking text
308         ///
309         virtual bool allowSpellcheck() { return false; }
310
311         // should this inset be handled like a normal charater
312         virtual bool isChar() const { return false; }
313         // is this equivalent to a letter?
314         virtual bool isLetter() const { return false; }
315         // is this equivalent to a space (which is BTW different from
316         // a line separator)?
317         virtual bool isSpace() const { return false; }
318         // should we break lines after this inset?
319         virtual bool isLineSeparator() const { return false; }
320         // if this inset has paragraphs should they be output all as default
321         // paragraphs with "Standard" layout?
322         virtual bool forceDefaultParagraphs(Inset const *) const;
323         /** returns true if, when outputing LaTeX, font changes should
324             be closed before generating this inset. This is needed for
325             insets that may contain several paragraphs */
326         virtual bool noFontChange() const { return false; }
327         //
328         virtual void getDrawFont(LyXFont &) const {}
329         /* needed for widths which are % of something
330            returns the value of \textwidth in this inset. Most of the
331            time this is the width of the workarea, but if there is a
332            minipage somewhere, it will be the width of this minipage */
333         virtual int latexTextWidth(BufferView *) const;
334
335         /** Adds a LaTeX snippet to the Preview Loader for transformation
336          *  into a bitmap image. Does not start the laoding process.
337          *
338          *  Most insets have no interest in this capability, so the method
339          *  defaults to empty.
340          */
341         virtual void addPreview(grfx::PreviewLoader &) const {}
342
343         /** Find the PreviewLoader, add a LaTeX snippet to it and
344          *  start the loading process.
345          *
346          *  Most insets have no interest in this capability, so the method
347          *  defaults to empty.
348          */
349         virtual void generatePreview() const {}
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         /** Dispatch result codes
419             Now that nested updatable insets are allowed, the local dispatch
420             becomes a bit complex, just two possible results (boolean)
421             are not enough.
422
423             DISPATCHED          = the inset catched the action
424             DISPATCHED_NOUPDATE = the inset catched the action and no update
425                                   is needed here to redraw the inset
426             FINISHED            = the inset must be unlocked as a result
427                                   of the action
428             FINISHED_RIGHT      = FINISHED, but put the cursor to the RIGHT of
429                                   the inset.
430             FINISHED_UP         = FINISHED, but put the cursor UP of
431                                   the inset.
432             FINISHED_DOWN       = FINISHED, but put the cursor DOWN of
433                                   the inset.
434             UNDISPATCHED        = the action was not catched, it should be
435                                   dispatched by lower level insets
436         */
437         enum RESULT {
438                 UNDISPATCHED = 0,
439                 DISPATCHED,
440                 DISPATCHED_NOUPDATE,
441                 FINISHED,
442                 FINISHED_RIGHT,
443                 FINISHED_UP,
444                 FINISHED_DOWN
445         };
446
447         /// To convert old binary dispatch results
448         RESULT DISPATCH_RESULT(bool b) {
449                 return b ? DISPATCHED : FINISHED;
450         }
451
452         ///
453         UpdatableInset();
454         ///
455         UpdatableInset(UpdatableInset const & in, bool same_id = false);
456
457         /// check if the font of the char we want inserting is correct
458         /// and modify it if it is not.
459         virtual bool checkInsertChar(LyXFont &);
460         ///
461         virtual EDITABLE editable() const;
462
463         ///
464         virtual void toggleInsetCursor(BufferView *);
465         ///
466         virtual void showInsetCursor(BufferView *, bool show = true);
467         ///
468         virtual void hideInsetCursor(BufferView *);
469         ///
470         virtual void fitInsetCursor(BufferView *) const;
471         ///
472         virtual void getCursorPos(BufferView *, int &, int &) const {}
473         ///
474         virtual void insetButtonPress(BufferView *, int x, int y, mouse_button::state button);
475         ///
476         // the bool return is used to see if we opened a dialog so that we can
477         // check this from an outer inset and open the dialog of the outer inset
478         // if that one has one!
479         ///
480         virtual bool insetButtonRelease(BufferView *,
481                                         int x, int y, mouse_button::state button);
482         ///
483         virtual void insetMotionNotify(BufferView *, int x, int y, mouse_button::state state);
484         ///
485         virtual void insetUnlock(BufferView *);
486         ///
487         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
488         ///
489         virtual void edit(BufferView *, bool front = true);
490         ///
491         virtual void draw(BufferView *, LyXFont const &,
492                           int baseline, float & x, bool cleared) const;
493         ///
494         virtual bool insertInset(BufferView *, Inset *) { return false; }
495         ///
496         virtual UpdatableInset * getLockingInset() const {
497                 return const_cast<UpdatableInset *>(this);
498         }
499         ///
500         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
501                 { return (c == lyxCode()) ? this : 0; }
502         ///
503         virtual int insetInInsetY() const { return 0; }
504         ///
505         virtual bool updateInsetInInset(BufferView *, Inset *)
506                 { return false; }
507         ///
508         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
509                 { return false; }
510         ///
511         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
512                                         bool /*lr*/ = false)
513                 { return false; }
514         ///  An updatable inset could handle lyx editing commands
515         virtual RESULT localDispatch(BufferView *, FuncRequest const & ev);
516         ///
517         bool isCursorVisible() const { return cursor_visible_; }
518         ///
519         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
520         ///
521         int scroll(bool recursive = true) const {
522                 // We need this method to not clobber the real method in Inset
523                 return Inset::scroll(recursive);
524         }
525         ///
526         virtual bool showInsetDialog(BufferView *) const { return false; }
527         ///
528         virtual void nodraw(bool b) const {
529                 block_drawing_ = b;
530         }
531         ///
532         virtual bool nodraw() const {
533                 return block_drawing_;
534         }
535         ///
536         // needed for spellchecking text
537         ///
538         virtual bool allowSpellcheck() { return false; }
539         ///
540         virtual WordLangTuple selectNextWordToSpellcheck(BufferView *, float & value) const;
541         ///
542         virtual void selectSelectedWord(BufferView *) { return; }
543         ///
544         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
545                 return;
546         }
547         ///
548         // needed for search/replace functionality
549         ///
550         virtual bool searchForward(BufferView *, string const &,
551                                    bool = true, bool = false);
552         ///
553         virtual bool searchBackward(BufferView *, string const &,
554                                     bool = true, bool = false);
555
556 protected:
557         ///
558         void toggleCursorVisible() const {
559                 cursor_visible_ = !cursor_visible_;
560         }
561         ///
562         void setCursorVisible(bool b) const {
563                 cursor_visible_ = b;
564         }
565         /// scrolls to absolute position in bufferview-workwidth * sx units
566         void scroll(BufferView *, float sx) const;
567         /// scrolls offset pixels
568         void scroll(BufferView *, int offset) const;
569
570 private:
571         ///
572         mutable bool cursor_visible_;
573         ///
574         mutable bool block_drawing_;
575 };
576
577 inline
578 bool UpdatableInset::checkInsertChar(LyXFont &)
579 {
580         return true;
581 }
582
583 /**
584  * returns true if pointer argument is valid
585  * and points to an editable inset
586  */
587 inline bool isEditableInset(Inset * i)
588 {
589         return i && i->editable();
590 }
591
592 /**
593  * returns true if pointer argument is valid
594  * and points to a highly editable inset
595  */
596 inline bool isHighlyEditableInset(Inset * i)
597 {
598         return i && i->editable() == Inset::HIGHLY_EDITABLE;
599 }
600
601 #endif