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