]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
More fixes.
[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         virtual void insetButtonRelease(BufferView *, int, int, int) {}
163         /// This is called when the user moves the mouse inside an inset
164         virtual void insetMotionNotify(BufferView *, int , int , int) {}
165         ///
166         virtual bool isTextInset() const { return false; }
167         ///
168         virtual bool doClearArea() const { return true; }
169         ///
170         virtual bool autoDelete() const;
171         /// returns true the inset can hold an inset of given type
172         virtual bool insetAllowed(Inset::Code) const { return false; }
173         /// wrapper around the above
174         bool insetAllowed(Inset * in) const;
175         ///
176         virtual void write(Buffer const *, std::ostream &) const = 0;
177         ///
178         virtual void read(Buffer const *, LyXLex & lex) = 0;
179         /** returns the number of rows (\n's) of generated tex code.
180             fragile == true means, that the inset should take care about
181             fragile commands by adding a \protect before.
182             If the free_spc (freespacing) variable is set, then this inset
183             is in a free-spacing paragraph.
184         */
185         virtual int latex(Buffer const *, std::ostream &, bool fragile,
186                           bool free_spc) const = 0;
187         ///
188         virtual int ascii(Buffer const *,
189                           std::ostream &, int linelen = 0) const = 0;
190         ///
191         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
192         ///
193         virtual int docbook(Buffer const *, std::ostream &) const = 0;
194         /// Updates needed features for this inset.
195         virtual void validate(LaTeXFeatures & features) const;
196         ///
197         virtual bool deletable() const;
198         
199         /// returns LyX code associated with the inset. Used for TOC, ...)
200         virtual Inset::Code lyxCode() const { return NO_CODE; }
201         
202         virtual std::vector<string> const getLabelList() const {
203                 return std::vector<string>();
204         }
205
206         ///
207         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
208         
209         /// returns true to override begin and end inset in file
210         virtual bool directWrite() const;
211
212         /// Returns true if the inset should be centered alone
213         virtual bool display() const { return false; }
214         /// Changes the display state of the inset
215         virtual void display(bool) {}
216         ///
217         /// returns true if this inset needs a row on it's own
218         ///
219         virtual bool needFullRow() const { return false; }
220         ///
221         void setInsetName(string const & s) { name_ = s; }
222         ///
223         string const & getInsetName() const { return name_; }
224         ///
225         void setOwner(Inset * inset) { owner_ = inset; }
226         ///
227         Inset * owner() const { return owner_; }
228         ///
229         void setBackgroundColor(LColor::color);
230         ///
231         LColor::color backgroundColor() const;
232         ///
233         int x() const { return top_x; }
234         ///
235         int y() const { return top_baseline; }
236         //
237         // because we could have fake text insets and have to call this
238         // inside them without cast!!!
239         ///
240         virtual LyXText * getLyXText(BufferView const *,
241                                      bool const recursive = false) const;
242         ///
243         virtual void deleteLyXText(BufferView *, bool = true) const {}
244         ///
245         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
246         /// returns the actuall scroll-value
247         virtual int scroll(bool recursive=true) const {
248                 if (!recursive || !owner_)
249                         return scx;
250                 return 0;
251         }
252         /// try to get a paragraph pointer from it's id if we have a
253         /// paragraph to give back!
254         virtual Paragraph * getParFromID(int /* id */) const {
255                 return 0;
256         }
257         /// try to get a inset pointer from it's id if we have
258         /// an inset to give back!
259         virtual Inset * getInsetFromID(int /* id */) const {
260                 return 0;
261         }
262         /// if this insets owns paragraphs (f.ex. InsetText) then it
263         /// should return it's very first one!
264         virtual Paragraph * firstParagraph() const {
265                 return 0;
266         }
267
268         ///
269         virtual Paragraph * getFirstParagraph(int /*num*/) const {
270                 return 0;
271         }
272
273         /// return the cursor if we own one otherwise giv'em just the
274         /// BufferView cursor to work with.
275         virtual LyXCursor const & cursor(BufferView * bview) const;
276         /// id functions
277         int id() const;
278         ///
279         void id(int id_arg);
280
281         /// used to toggle insets
282         // is the inset open?
283         virtual bool isOpen() const { return false; }
284         /// open the inset
285         virtual void open(BufferView *) {}
286         /// close the inset
287         virtual void close(BufferView *) const {}
288         /// check if the font of the char we want inserting is correct
289         /// and modify it if it is not.
290         virtual bool checkInsertChar(LyXFont &);
291         /// we need this here because collapsed insets are only EDITABLE
292         virtual void setFont(BufferView *, LyXFont const &,
293                          bool toggleall = false, bool selectall = false);
294         ///
295         // needed for spellchecking text
296         ///
297         virtual bool allowSpellcheck() { return false; }
298
299         // is this equivalent to a letter?
300         virtual bool isLetter() const { return false; }
301         // is this equivalent to a space?
302         virtual bool isSpace() const { return false; }
303
304 protected:
305         ///
306         mutable int top_x;
307         ///
308         mutable bool topx_set; /* have we already drawn ourself! */
309         ///
310         mutable int top_baseline;
311         ///
312         mutable int scx;
313         ///
314         unsigned int id_;
315         ///
316         static unsigned int inset_id;
317
318 private:
319         ///
320         Inset * owner_;
321         ///
322         string name_;
323         ///
324         LColor::color background_color_;
325 };
326
327
328 inline
329 bool Inset::insetAllowed(Inset * in) const
330
331         return insetAllowed(in->lyxCode()); 
332 }
333
334
335 inline
336 bool Inset::checkInsertChar(LyXFont &)
337 {
338         return false;
339 }
340
341 //  Updatable Insets. These insets can be locked and receive
342 //  directly user interaction. Currently used only for mathed.
343 //  Note that all pure methods from Inset class are pure here too.
344 //  [Alejandro 080596] 
345
346 /** Extracted from Matthias notes:
347  * 
348  * An inset can simple call LockInset in it's edit call and *ONLY* 
349  * in it's edit call.
350  *
351  * Unlocking is either done by LyX or the inset itself with a 
352  * UnlockInset-call
353  *
354  * During the lock, all button and keyboard events will be modified
355  * and send to the inset through the following inset-features. Note that
356  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
357  * to contain the code for restoring the menus and things like this.
358  * 
359  * If a inset wishes any redraw and/or update it just has to call
360  * UpdateInset(this).
361  * 
362  * It's is completly irrelevant, where the inset is. UpdateInset will
363  * find it in any paragraph in any buffer. 
364  * Of course the_locking_inset and the insets in the current paragraph/buffer
365  *  are checked first, so no performance problem should occur.
366  */
367 class UpdatableInset : public Inset {
368 public:
369         /** Dispatch result codes
370             Now that nested updatable insets are allowed, the local dispatch
371             becomes a bit complex, just two possible results (boolean)
372             are not enough. 
373          
374             DISPATCHED          = the inset catched the action
375             DISPATCHED_NOUPDATE = the inset catched the action and no update
376                                   is needed here to redraw the inset
377             FINISHED            = the inset must be unlocked as a result
378                                   of the action
379             FINISHED_RIGHT      = FINISHED, but put the cursor to the RIGHT of
380                                   the inset.
381             FINISHED_UP         = FINISHED, but put the cursor UP of
382                                   the inset.
383             FINISHED_DOWN       = FINISHED, but put the cursor DOWN of
384                                   the inset.
385             UNDISPATCHED        = the action was not catched, it should be
386                                   dispatched by lower level insets
387         */ 
388         enum RESULT {
389                 UNDISPATCHED = 0,
390                 DISPATCHED,
391                 DISPATCHED_NOUPDATE,
392                 FINISHED,
393                 FINISHED_RIGHT,
394                 FINISHED_UP,
395                 FINISHED_DOWN
396         };
397     
398         /// To convert old binary dispatch results
399         RESULT DISPATCH_RESULT(bool b) {
400                 return b ? DISPATCHED : FINISHED;
401         }
402
403         ///
404         UpdatableInset();
405         ///
406         UpdatableInset(UpdatableInset const & in, bool same_id = false);
407
408         /// check if the font of the char we want inserting is correct
409         /// and modify it if it is not.
410         virtual bool checkInsertChar(LyXFont &);
411         ///
412         virtual EDITABLE editable() const;
413         
414         /// may call ToggleLockedInsetCursor
415         virtual void toggleInsetCursor(BufferView *);
416         ///
417         virtual void showInsetCursor(BufferView *, bool show = true);
418         ///
419         virtual void hideInsetCursor(BufferView *);
420         ///
421         virtual void fitInsetCursor(BufferView *) const;
422         ///
423         virtual void getCursorPos(BufferView *, int &, int &) const {}
424         ///
425         virtual void insetButtonPress(BufferView *, int x, int y, int button);
426         ///
427         virtual void insetButtonRelease(BufferView *,
428                                         int x, int y, int button);
429         ///
430         virtual void insetKeyPress(XKeyEvent * ev);
431         ///
432         virtual void insetMotionNotify(BufferView *, int x, int y, int state);
433         ///
434         virtual void insetUnlock(BufferView *);
435         ///
436         virtual void edit(BufferView *, int x, int y, unsigned int button);
437         ///
438         virtual void edit(BufferView *, bool front = true);
439         ///
440         virtual void draw(BufferView *, LyXFont const &,
441                           int baseline, float & x, bool cleared) const;
442         ///
443         virtual bool insertInset(BufferView *, Inset *) { return false; }
444         ///
445         virtual UpdatableInset * getLockingInset() const {
446                 return const_cast<UpdatableInset *>(this);
447         }
448         ///
449         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
450                 { return (c == lyxCode()) ? this : 0; }
451         ///
452         virtual unsigned int insetInInsetY() { return 0; }
453         ///
454         virtual bool updateInsetInInset(BufferView *, Inset *)
455                 { return false; }
456         ///
457         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
458                 { return false; }
459         ///
460         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
461                                         bool /*lr*/ = false)
462                 { return false; }
463         ///  An updatable inset could handle lyx editing commands
464         virtual RESULT localDispatch(BufferView *, kb_action, string const &);
465         ///
466         bool isCursorVisible() const { return cursor_visible_; }
467         ///
468         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
469         ///
470         int scroll(bool recursive = true) const {
471                 // We need this method to not clobber the real method in Inset
472                 return Inset::scroll(recursive);
473         }
474         ///
475         virtual bool showInsetDialog(BufferView *) const { return false; }
476         ///
477         virtual void nodraw(bool b) const {
478                 block_drawing_ = b;
479         }
480         ///
481         virtual bool nodraw() const {
482                 return block_drawing_;
483         }
484         ///
485         // needed for spellchecking text
486         ///
487         virtual bool allowSpellcheck() { return false; }
488         ///
489         virtual string const selectNextWordToSpellcheck(BufferView *, float & value) const;
490         ///
491         virtual void selectSelectedWord(BufferView *) { return; }
492         ///
493         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
494                 return;
495         }
496         ///
497         // needed for search/replace functionality
498         ///
499         virtual bool searchForward(BufferView *, string const &,
500                                    bool const & = true, bool const & = false);
501         ///
502         virtual bool searchBackward(BufferView *, string const &,
503                                     bool const & = true, bool const & = false);
504
505 protected:
506         ///
507         void toggleCursorVisible() const {
508                 cursor_visible_ = !cursor_visible_;
509         }
510         ///
511         void setCursorVisible(bool b) const {
512                 cursor_visible_ = b;
513         }
514         /// scrolls to absolute position in bufferview-workwidth * sx units
515         void scroll(BufferView *, float sx) const;
516         /// scrolls offset pixels
517         void scroll(BufferView *, int offset) const;
518
519 private:
520         ///
521         mutable bool cursor_visible_;
522         ///
523         mutable bool block_drawing_;
524 };
525
526 inline
527 bool UpdatableInset::checkInsertChar(LyXFont &)
528 {
529         return true;
530 }
531
532 /**
533  * returns true if pointer argument is valid
534  * and points to an editable inset
535  */
536 inline bool isEditableInset(Inset * i)
537 {
538         return i && i->editable(); 
539 }
540  
541 /**
542  * returns true if pointer argument is valid
543  * and points to a highly editable inset
544  */
545 inline bool isHighlyEditableInset(Inset * i)
546 {
547         return i && i->editable() == Inset::HIGHLY_EDITABLE;
548 }
549
550 #endif