]> git.lyx.org Git - features.git/blob - src/insets/inset.h
Replace ugly getParFromID() code with ParIterator-based stuff. I think
[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         /// return true if the inset should be removed automatically
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
292         /// try to get a inset pointer from it's id if we have
293         /// an inset to give back!
294         virtual Inset * getInsetFromID(int /* id */) const {
295                 return 0;
296         }
297         /// if this insets owns paragraphs (f.ex. InsetText) then it
298         /// should return it's very first one!
299         virtual Paragraph * firstParagraph() const {
300                 return 0;
301         }
302
303         ///
304         virtual Paragraph * getFirstParagraph(int /*num*/) const {
305                 return 0;
306         }
307
308         /// return the cursor if we own one otherwise giv'em just the
309         /// BufferView cursor to work with.
310         virtual LyXCursor const & cursor(BufferView * bview) const;
311         /// id functions
312         int id() const;
313         ///
314         void id(int id_arg);
315
316         /// used to toggle insets
317         // is the inset open?
318         virtual bool isOpen() const { return false; }
319         /// open the inset
320         virtual void open(BufferView *) {}
321         /// close the inset
322         virtual void close(BufferView *) const {}
323         /// check if the font of the char we want inserting is correct
324         /// and modify it if it is not.
325         virtual bool checkInsertChar(LyXFont &);
326         /// we need this here because collapsed insets are only EDITABLE
327         virtual void setFont(BufferView *, LyXFont const &,
328                          bool toggleall = false, bool selectall = false);
329         ///
330         // needed for spellchecking text
331         ///
332         virtual bool allowSpellcheck() const { return false; }
333
334         // should this inset be handled like a normal charater
335         virtual bool isChar() const { return false; }
336         // is this equivalent to a letter?
337         virtual bool isLetter() const { return false; }
338         // is this equivalent to a space (which is BTW different from
339         // a line separator)?
340         virtual bool isSpace() const { return false; }
341         // should we break lines after this inset?
342         virtual bool isLineSeparator() const { return false; }
343         // if this inset has paragraphs should they be output all as default
344         // paragraphs with "Standard" layout?
345         virtual bool forceDefaultParagraphs(Inset const *) const;
346         /** returns true if, when outputing LaTeX, font changes should
347             be closed before generating this inset. This is needed for
348             insets that may contain several paragraphs */
349         virtual bool noFontChange() const { return false; }
350         //
351         virtual void getDrawFont(LyXFont &) const {}
352         /* needed for widths which are % of something
353            returns the value of \textwidth in this inset. Most of the
354            time this is the width of the workarea, but if there is a
355            minipage somewhere, it will be the width of this minipage */
356         virtual int latexTextWidth(BufferView *) const;
357
358         /// mark the inset contents as erased (for change tracking)
359         virtual void markErased() {}
360  
361         /** Adds a LaTeX snippet to the Preview Loader for transformation
362          *  into a bitmap image. Does not start the laoding process.
363          *
364          *  Most insets have no interest in this capability, so the method
365          *  defaults to empty.
366          */
367         virtual void addPreview(grfx::PreviewLoader &) const {}
368
369         /** Find the PreviewLoader, add a LaTeX snippet to it and
370          *  start the loading process.
371          *
372          *  Most insets have no interest in this capability, so the method
373          *  defaults to empty.
374          */
375         virtual void generatePreview() const {}
376
377 protected:
378         ///
379         mutable int top_x;
380         ///
381         mutable bool topx_set; /* have we already drawn ourself! */
382         ///
383         mutable int top_baseline;
384         ///
385         mutable int scx;
386         ///
387         unsigned int id_;
388         ///
389         static unsigned int inset_id;
390
391 private:
392         ///
393         Inset * owner_;
394         /// the paragraph in which this inset has been inserted
395         Paragraph * par_owner_;
396         ///
397         string name_;
398         ///
399         LColor::color background_color_;
400 };
401
402
403 inline
404 bool Inset::insetAllowed(Inset * in) const
405 {
406         return insetAllowed(in->lyxCode());
407 }
408
409
410 inline
411 bool Inset::checkInsertChar(LyXFont &)
412 {
413         return false;
414 }
415
416 //  Updatable Insets. These insets can be locked and receive
417 //  directly user interaction. Currently used only for mathed.
418 //  Note that all pure methods from Inset class are pure here too.
419 //  [Alejandro 080596]
420
421 /** Extracted from Matthias notes:
422  *
423  * An inset can simple call LockInset in it's edit call and *ONLY*
424  * in it's edit call.
425  *
426  * Unlocking is either done by LyX or the inset itself with a
427  * UnlockInset-call
428  *
429  * During the lock, all button and keyboard events will be modified
430  * and send to the inset through the following inset-features. Note that
431  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
432  * to contain the code for restoring the menus and things like this.
433  *
434  * If a inset wishes any redraw and/or update it just has to call
435  * updateInset(this).
436  *
437  * It's is completly irrelevant, where the inset is. UpdateInset will
438  * find it in any paragraph in any buffer.
439  * Of course the_locking_inset and the insets in the current paragraph/buffer
440  *  are checked first, so no performance problem should occur.
441  */
442 class UpdatableInset : public Inset {
443 public:
444         ///
445         UpdatableInset();
446         ///
447         UpdatableInset(UpdatableInset const & in, bool same_id = false);
448
449         /// check if the font of the char we want inserting is correct
450         /// and modify it if it is not.
451         virtual bool checkInsertChar(LyXFont &);
452         ///
453         virtual EDITABLE editable() const;
454
455         ///
456         virtual void toggleInsetCursor(BufferView *);
457         ///
458         virtual void showInsetCursor(BufferView *, bool show = true);
459         ///
460         virtual void hideInsetCursor(BufferView *);
461         ///
462         virtual void fitInsetCursor(BufferView *) const;
463         ///
464         virtual void getCursorPos(BufferView *, int &, int &) const {}
465         ///
466         virtual void insetUnlock(BufferView *);
467         ///
468         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
469         ///
470         virtual void edit(BufferView *, bool front = true);
471         ///
472         virtual void draw(BufferView *, LyXFont const &,
473                           int baseline, float & x, bool cleared) const;
474         ///
475         virtual bool insertInset(BufferView *, Inset *) { return false; }
476         ///
477         virtual UpdatableInset * getLockingInset() const {
478                 return const_cast<UpdatableInset *>(this);
479         }
480         ///
481         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
482                 { return (c == lyxCode()) ? this : 0; }
483         ///
484         virtual int insetInInsetY() const { return 0; }
485         ///
486         virtual bool updateInsetInInset(BufferView *, Inset *)
487                 { return false; }
488         ///
489         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
490                 { return false; }
491         ///
492         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
493                                         bool /*lr*/ = false)
494                 { return false; }
495         ///  An updatable inset could handle lyx editing commands
496         virtual RESULT localDispatch(FuncRequest const & cmd);
497         ///
498         bool isCursorVisible() const { return cursor_visible_; }
499         ///
500         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
501         ///
502         int scroll(bool recursive = true) const {
503                 // We need this method to not clobber the real method in Inset
504                 return Inset::scroll(recursive);
505         }
506         ///
507         virtual bool showInsetDialog(BufferView *) const { return false; }
508         ///
509         virtual void nodraw(bool b) const {
510                 block_drawing_ = b;
511         }
512         ///
513         virtual bool nodraw() const {
514                 return block_drawing_;
515         }
516         ///
517         // needed for spellchecking text
518         ///
519         virtual bool allowSpellcheck() const { return false; }
520         ///
521         virtual WordLangTuple const
522         selectNextWordToSpellcheck(BufferView *, float & value) const;
523         ///
524         virtual void selectSelectedWord(BufferView *) {}
525         ///
526         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
527                 return;
528         }
529
530         /// find the next change in the inset
531         virtual bool nextChange(BufferView * bv, lyx::pos_type & length);
532  
533         ///
534         // needed for search/replace functionality
535         ///
536         virtual bool searchForward(BufferView *, string const &,
537                                    bool = true, bool = false);
538         ///
539         virtual bool searchBackward(BufferView *, string const &,
540                                     bool = true, bool = false);
541
542 protected:
543         ///
544         void toggleCursorVisible() const {
545                 cursor_visible_ = !cursor_visible_;
546         }
547         ///
548         void setCursorVisible(bool b) const {
549                 cursor_visible_ = b;
550         }
551         /// scrolls to absolute position in bufferview-workwidth * sx units
552         void scroll(BufferView *, float sx) const;
553         /// scrolls offset pixels
554         void scroll(BufferView *, int offset) const;
555
556 private:
557         ///
558         mutable bool cursor_visible_;
559         ///
560         mutable bool block_drawing_;
561 };
562
563 inline
564 bool UpdatableInset::checkInsertChar(LyXFont &)
565 {
566         return true;
567 }
568
569 /**
570  * returns true if pointer argument is valid
571  * and points to an editable inset
572  */
573 inline bool isEditableInset(Inset * i)
574 {
575         return i && i->editable();
576 }
577
578 /**
579  * returns true if pointer argument is valid
580  * and points to a highly editable inset
581  */
582 inline bool isHighlyEditableInset(Inset * i)
583 {
584         return i && i->editable() == Inset::HIGHLY_EDITABLE;
585 }
586
587 #endif