]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
Now generate previews when loading the buffer for insets in insets.
[lyx.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 "commandtags.h"
21 #include "frontends/mouse_state.h"
22 #include "LColor.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
33 struct LaTeXFeatures;
34
35 namespace grfx {
36         class PreviewLoader;
37 }
38
39 /// Insets
40 class Inset {
41 public:
42         /** This is not quite the correct place for this enum. I think
43             the correct would be to let each subclass of Inset declare
44             its own enum code. Actually the notion of an Inset::Code
45             should be avoided, but I am not sure how this could be done
46             in a cleaner way. */
47         enum Code {
48                 ///
49                 NO_CODE,
50                 ///
51                 TOC_CODE,  // do these insets really need a code? (ale)
52                 ///
53                 QUOTE_CODE,
54                 ///
55                 MARK_CODE,
56                 ///
57                 REF_CODE, // 5
58                 ///
59                 URL_CODE,
60                 ///
61                 HTMLURL_CODE,
62                 ///
63                 SEPARATOR_CODE,
64                 ///
65                 ENDING_CODE,
66                 ///
67                 LABEL_CODE, // 10
68                 ///
69                 IGNORE_CODE,
70                 ///
71                 ACCENT_CODE,
72                 ///
73                 MATH_CODE,
74                 ///
75                 INDEX_CODE,
76                 ///
77                 INCLUDE_CODE, // 15
78                 ///
79                 GRAPHICS_CODE,
80                 ///
81                 PARENT_CODE,
82                 ///
83                 BIBTEX_CODE,
84                 ///
85                 TEXT_CODE,
86                 ///
87                 ERT_CODE, // 20
88                 ///
89                 FOOT_CODE,
90                 ///
91                 MARGIN_CODE,
92                 ///
93                 FLOAT_CODE,
94                 ///
95                 MINIPAGE_CODE,
96                 ///
97                 SPECIALCHAR_CODE, // 25
98                 ///
99                 TABULAR_CODE,
100                 ///
101                 EXTERNAL_CODE,
102 #if 0
103                 ///
104                 THEOREM_CODE,
105 #endif
106                 ///
107                 CAPTION_CODE,
108                 ///
109                 MATHMACRO_CODE, // 30
110                 ///
111                 ERROR_CODE,
112                 ///
113                 CITE_CODE,
114                 ///
115                 FLOAT_LIST_CODE,
116                 ///
117                 INDEX_PRINT_CODE
118         };
119
120         ///
121         enum {
122                 ///
123                 TEXT_TO_INSET_OFFSET = 2
124         };
125
126         ///
127         enum EDITABLE {
128                 ///
129                 NOT_EDITABLE = 0,
130                 ///
131                 IS_EDITABLE,
132                 ///
133                 HIGHLY_EDITABLE
134         };
135
136         ///
137         Inset();
138         ///
139         Inset(Inset const & in, bool same_id = false);
140         ///
141         virtual ~Inset() {}
142         ///
143         virtual int ascent(BufferView *, LyXFont const &) const = 0;
144         ///
145         virtual int descent(BufferView *, LyXFont const &) const = 0;
146         ///
147         virtual int width(BufferView *, LyXFont const &) const = 0;
148         ///
149         virtual void draw(BufferView *, LyXFont const &,
150                           int baseline, float & x, bool cleared) const = 0;
151         /// update the inset representation
152         virtual void update(BufferView *, LyXFont const &, bool = false)
153                 {}
154         /// what appears in the minibuffer when opening
155         virtual string const editMessage() const;
156         ///
157         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
158         ///
159         virtual void edit(BufferView *, bool front = true);
160         ///
161         virtual EDITABLE editable() const;
162         /// This is called when the user clicks inside an inset
163         virtual void insetButtonPress(BufferView *, int, int, mouse_button::state) {}
164         /// This is called when the user releases the button inside an inset
165         // the bool return is used to see if we opened a dialog so that we can
166         // check this from an outer inset and open the dialog of the
167         // outer inset if that one has one!
168         virtual bool insetButtonRelease(BufferView *, int, int, mouse_button::state)
169                 { return editable() == IS_EDITABLE; }
170         /// This is called when the user moves the mouse inside an inset
171         virtual void insetMotionNotify(BufferView *, int , int, mouse_button::state) {}
172         ///
173         virtual bool isTextInset() const { return false; }
174         ///
175         virtual bool doClearArea() const { return true; }
176         ///
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         virtual bool deletable() const;
205
206         /// returns LyX code associated with the inset. Used for TOC, ...)
207         virtual Inset::Code lyxCode() const { return NO_CODE; }
208
209         virtual std::vector<string> const getLabelList() const {
210                 return std::vector<string>();
211         }
212
213         ///
214         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
215
216         /// returns true to override begin and end inset in file
217         virtual bool directWrite() const;
218
219         /// Returns true if the inset should be centered alone
220         virtual bool display() const { return false; }
221         /// Changes the display state of the inset
222         virtual void display(bool) {}
223         ///
224         /// returns true if this inset needs a row on it's own
225         ///
226         virtual bool needFullRow() const { return false; }
227         ///
228         void setInsetName(string const & s) { name_ = s; }
229         ///
230         string const & getInsetName() const { return name_; }
231         ///
232         void setOwner(Inset * inset) { owner_ = inset; }
233         ///
234         Inset * owner() const { return owner_; }
235         ///
236         void parOwner(Paragraph * par) { par_owner_ = par; }
237         ///
238         Paragraph * parOwner() const {return par_owner_; }
239         ///
240         void setBackgroundColor(LColor::color);
241         ///
242         LColor::color backgroundColor() const;
243         ///
244         int x() const { return top_x; }
245         ///
246         int y() const { return top_baseline; }
247         //
248         // because we could have fake text insets and have to call this
249         // inside them without cast!!!
250         ///
251         virtual LyXText * getLyXText(BufferView const *,
252                                      bool const recursive = false) const;
253         ///
254         virtual void deleteLyXText(BufferView *, bool = true) const {}
255         ///
256         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
257         /// returns the actuall scroll-value
258         virtual int scroll(bool recursive=true) const {
259                 if (!recursive || !owner_)
260                         return scx;
261                 return 0;
262         }
263         /// try to get a paragraph pointer from it's id if we have a
264         /// paragraph to give back!
265         virtual Paragraph * getParFromID(int /* id */) const {
266                 return 0;
267         }
268         /// try to get a inset pointer from it's id if we have
269         /// an inset to give back!
270         virtual Inset * getInsetFromID(int /* id */) const {
271                 return 0;
272         }
273         /// if this insets owns paragraphs (f.ex. InsetText) then it
274         /// should return it's very first one!
275         virtual Paragraph * firstParagraph() const {
276                 return 0;
277         }
278
279         ///
280         virtual Paragraph * getFirstParagraph(int /*num*/) const {
281                 return 0;
282         }
283
284         /// return the cursor if we own one otherwise giv'em just the
285         /// BufferView cursor to work with.
286         virtual LyXCursor const & cursor(BufferView * bview) const;
287         /// id functions
288         int id() const;
289         ///
290         void id(int id_arg);
291
292         /// used to toggle insets
293         // is the inset open?
294         virtual bool isOpen() const { return false; }
295         /// open the inset
296         virtual void open(BufferView *) {}
297         /// close the inset
298         virtual void close(BufferView *) const {}
299         /// check if the font of the char we want inserting is correct
300         /// and modify it if it is not.
301         virtual bool checkInsertChar(LyXFont &);
302         /// we need this here because collapsed insets are only EDITABLE
303         virtual void setFont(BufferView *, LyXFont const &,
304                          bool toggleall = false, bool selectall = false);
305         ///
306         // needed for spellchecking text
307         ///
308         virtual bool allowSpellcheck() { return false; }
309
310         // should this inset be handled like a normal charater
311         virtual bool isChar() const { return false; }
312         // is this equivalent to a letter?
313         virtual bool isLetter() const { return false; }
314         // is this equivalent to a space (which is BTW different from
315         // a line separator)?
316         virtual bool isSpace() const { return false; }
317         // should we break lines after this inset?
318         virtual bool isLineSeparator() const { return false; }
319         // if this inset has paragraphs should they be output all as default
320         // paragraphs with "Standard" layout?
321         virtual bool forceDefaultParagraphs(Inset const *) const;
322         /** returns true if, when outputing LaTeX, font changes should
323             be closed before generating this inset. This is needed for
324             insets that may contain several paragraphs */
325         virtual bool noFontChange() const { return false; }
326         //
327         virtual void getDrawFont(LyXFont &) const {}
328         /* needed for widths which are % of something
329            returns the value of \textwidth in this inset. Most of the
330            time this is the width of the workarea, but if there is a
331            minipage somewhere, it will be the width of this minipage */
332         virtual int latexTextWidth(BufferView *) const;
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 protected:
351         ///
352         mutable int top_x;
353         ///
354         mutable bool topx_set; /* have we already drawn ourself! */
355         ///
356         mutable int top_baseline;
357         ///
358         mutable int scx;
359         ///
360         unsigned int id_;
361         ///
362         static unsigned int inset_id;
363
364 private:
365         ///
366         Inset * owner_;
367         /// the paragraph in which this inset has been inserted
368         Paragraph * par_owner_;
369         ///
370         string name_;
371         ///
372         LColor::color background_color_;
373 };
374
375
376 inline
377 bool Inset::insetAllowed(Inset * in) const
378 {
379         return insetAllowed(in->lyxCode());
380 }
381
382
383 inline
384 bool Inset::checkInsertChar(LyXFont &)
385 {
386         return false;
387 }
388
389 //  Updatable Insets. These insets can be locked and receive
390 //  directly user interaction. Currently used only for mathed.
391 //  Note that all pure methods from Inset class are pure here too.
392 //  [Alejandro 080596]
393
394 /** Extracted from Matthias notes:
395  *
396  * An inset can simple call LockInset in it's edit call and *ONLY*
397  * in it's edit call.
398  *
399  * Unlocking is either done by LyX or the inset itself with a
400  * UnlockInset-call
401  *
402  * During the lock, all button and keyboard events will be modified
403  * and send to the inset through the following inset-features. Note that
404  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
405  * to contain the code for restoring the menus and things like this.
406  *
407  * If a inset wishes any redraw and/or update it just has to call
408  * updateInset(this).
409  *
410  * It's is completly irrelevant, where the inset is. UpdateInset will
411  * find it in any paragraph in any buffer.
412  * Of course the_locking_inset and the insets in the current paragraph/buffer
413  *  are checked first, so no performance problem should occur.
414  */
415 class UpdatableInset : public Inset {
416 public:
417         /** Dispatch result codes
418             Now that nested updatable insets are allowed, the local dispatch
419             becomes a bit complex, just two possible results (boolean)
420             are not enough.
421
422             DISPATCHED          = the inset catched the action
423             DISPATCHED_NOUPDATE = the inset catched the action and no update
424                                   is needed here to redraw the inset
425             FINISHED            = the inset must be unlocked as a result
426                                   of the action
427             FINISHED_RIGHT      = FINISHED, but put the cursor to the RIGHT of
428                                   the inset.
429             FINISHED_UP         = FINISHED, but put the cursor UP of
430                                   the inset.
431             FINISHED_DOWN       = FINISHED, but put the cursor DOWN of
432                                   the inset.
433             UNDISPATCHED        = the action was not catched, it should be
434                                   dispatched by lower level insets
435         */
436         enum RESULT {
437                 UNDISPATCHED = 0,
438                 DISPATCHED,
439                 DISPATCHED_NOUPDATE,
440                 FINISHED,
441                 FINISHED_RIGHT,
442                 FINISHED_UP,
443                 FINISHED_DOWN
444         };
445
446         /// To convert old binary dispatch results
447         RESULT DISPATCH_RESULT(bool b) {
448                 return b ? DISPATCHED : FINISHED;
449         }
450
451         ///
452         UpdatableInset();
453         ///
454         UpdatableInset(UpdatableInset const & in, bool same_id = false);
455
456         /// check if the font of the char we want inserting is correct
457         /// and modify it if it is not.
458         virtual bool checkInsertChar(LyXFont &);
459         ///
460         virtual EDITABLE editable() const;
461
462         ///
463         virtual void toggleInsetCursor(BufferView *);
464         ///
465         virtual void showInsetCursor(BufferView *, bool show = true);
466         ///
467         virtual void hideInsetCursor(BufferView *);
468         ///
469         virtual void fitInsetCursor(BufferView *) const;
470         ///
471         virtual void getCursorPos(BufferView *, int &, int &) const {}
472         ///
473         virtual void insetButtonPress(BufferView *, int x, int y, mouse_button::state button);
474         ///
475         // the bool return is used to see if we opened a dialog so that we can
476         // check this from an outer inset and open the dialog of the outer inset
477         // if that one has one!
478         ///
479         virtual bool insetButtonRelease(BufferView *,
480                                         int x, int y, mouse_button::state button);
481         ///
482         virtual void insetMotionNotify(BufferView *, int x, int y, mouse_button::state state);
483         ///
484         virtual void insetUnlock(BufferView *);
485         ///
486         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
487         ///
488         virtual void edit(BufferView *, bool front = true);
489         ///
490         virtual void draw(BufferView *, LyXFont const &,
491                           int baseline, float & x, bool cleared) const;
492         ///
493         virtual bool insertInset(BufferView *, Inset *) { return false; }
494         ///
495         virtual UpdatableInset * getLockingInset() const {
496                 return const_cast<UpdatableInset *>(this);
497         }
498         ///
499         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
500                 { return (c == lyxCode()) ? this : 0; }
501         ///
502         virtual int insetInInsetY() const { return 0; }
503         ///
504         virtual bool updateInsetInInset(BufferView *, Inset *)
505                 { return false; }
506         ///
507         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
508                 { return false; }
509         ///
510         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
511                                         bool /*lr*/ = false)
512                 { return false; }
513         ///  An updatable inset could handle lyx editing commands
514         virtual RESULT localDispatch(BufferView *, kb_action, string const &);
515         ///
516         bool isCursorVisible() const { return cursor_visible_; }
517         ///
518         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
519         ///
520         int scroll(bool recursive = true) const {
521                 // We need this method to not clobber the real method in Inset
522                 return Inset::scroll(recursive);
523         }
524         ///
525         virtual bool showInsetDialog(BufferView *) const { return false; }
526         ///
527         virtual void nodraw(bool b) const {
528                 block_drawing_ = b;
529         }
530         ///
531         virtual bool nodraw() const {
532                 return block_drawing_;
533         }
534         ///
535         // needed for spellchecking text
536         ///
537         virtual bool allowSpellcheck() { return false; }
538         ///
539         virtual string const selectNextWordToSpellcheck(BufferView *, float & value) const;
540         ///
541         virtual void selectSelectedWord(BufferView *) { return; }
542         ///
543         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
544                 return;
545         }
546         ///
547         // needed for search/replace functionality
548         ///
549         virtual bool searchForward(BufferView *, string const &,
550                                    bool = true, bool = false);
551         ///
552         virtual bool searchBackward(BufferView *, string const &,
553                                     bool = true, bool = false);
554
555 protected:
556         ///
557         void toggleCursorVisible() const {
558                 cursor_visible_ = !cursor_visible_;
559         }
560         ///
561         void setCursorVisible(bool b) const {
562                 cursor_visible_ = b;
563         }
564         /// scrolls to absolute position in bufferview-workwidth * sx units
565         void scroll(BufferView *, float sx) const;
566         /// scrolls offset pixels
567         void scroll(BufferView *, int offset) const;
568
569 private:
570         ///
571         mutable bool cursor_visible_;
572         ///
573         mutable bool block_drawing_;
574 };
575
576 inline
577 bool UpdatableInset::checkInsertChar(LyXFont &)
578 {
579         return true;
580 }
581
582 /**
583  * returns true if pointer argument is valid
584  * and points to an editable inset
585  */
586 inline bool isEditableInset(Inset * i)
587 {
588         return i && i->editable();
589 }
590
591 /**
592  * returns true if pointer argument is valid
593  * and points to a highly editable inset
594  */
595 inline bool isHighlyEditableInset(Inset * i)
596 {
597         return i && i->editable() == Inset::HIGHLY_EDITABLE;
598 }
599
600 #endif