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