]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
Added copy constructor to inset.h and used it in most insets which permit
[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                 ///
100                 THEOREM_CODE,
101                 ///
102                 CAPTION_CODE,
103                 ///
104                 MATHMACRO_CODE, // 30
105                 ///
106                 ERROR_CODE,
107                 ///
108                 CITE_CODE,
109                 ///
110                 FLOAT_LIST_CODE,
111                 ///
112                 INDEX_PRINT_CODE
113         };
114
115         ///
116         enum {
117                 ///
118                 TEXT_TO_INSET_OFFSET = 2
119         };
120
121         ///
122         enum EDITABLE {
123                 ///
124                 NOT_EDITABLE = 0,
125                 ///
126                 IS_EDITABLE,
127                 ///
128                 HIGHLY_EDITABLE
129         };
130         
131         ///
132         Inset();
133         ///
134         Inset(Inset const & in, bool same_id = false);
135         ///
136         virtual ~Inset() {}
137         ///
138         virtual int ascent(BufferView *, LyXFont const &) const = 0;
139         ///
140         virtual int descent(BufferView *, LyXFont const &) const = 0;
141         ///
142         virtual int width(BufferView *, LyXFont const &) const = 0;
143         ///
144         virtual void draw(BufferView *, LyXFont const &,
145                           int baseline, float & x, bool cleared) const = 0;
146         /// update the inset representation
147         virtual void update(BufferView *, LyXFont const &, bool = false)
148                 {}
149         ///
150         virtual LyXFont const convertFont(LyXFont const & font) const;
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                 return insetAllowed(in->lyxCode()); 
176         }
177         ///
178         virtual void write(Buffer const *, std::ostream &) const = 0;
179         ///
180         virtual void read(Buffer const *, LyXLex & lex) = 0;
181         /** returns the number of rows (\n's) of generated tex code.
182             fragile == true means, that the inset should take care about
183             fragile commands by adding a \protect before.
184             If the free_spc (freespacing) variable is set, then this inset
185             is in a free-spacing paragraph.
186         */
187         virtual int latex(Buffer const *, std::ostream &, bool fragile,
188                           bool free_spc) const = 0;
189         ///
190         virtual int ascii(Buffer const *,
191                           std::ostream &, int linelen = 0) const = 0;
192         ///
193         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
194         ///
195         virtual int docBook(Buffer const *, std::ostream &) const = 0;
196         /// Updates needed features for this inset.
197         virtual void validate(LaTeXFeatures & features) const;
198         ///
199         virtual bool deletable() const;
200         
201         /// returns LyX code associated with the inset. Used for TOC, ...)
202         virtual Inset::Code lyxCode() const { return NO_CODE; }
203         
204         virtual std::vector<string> const getLabelList() const {
205                 return std::vector<string>();
206         }
207
208         ///
209         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
210         
211         /// returns true to override begin and end inset in file
212         virtual bool directWrite() const;
213
214         /// Returns true if the inset should be centered alone
215         virtual bool display() const { return false; }
216         /// Changes the display state of the inset
217         virtual void display(bool) {}
218         ///
219         /// returns true if this inset needs a row on it's own
220         ///
221         virtual bool needFullRow() const { return false; }
222         ///
223         void setInsetName(string const & s) { name = s; }
224         ///
225         string const getInsetName() const { return name; }
226         ///
227         void setOwner(Inset * inset) { owner_ = inset; }
228         ///
229         Inset * owner() const { return owner_; }
230         ///
231         void setBackgroundColor(LColor::color);
232         ///
233         LColor::color backgroundColor() const;
234         ///
235         int x() const { return top_x; }
236         ///
237         int y() const { return top_baseline; }
238         //
239         // because we could have fake text insets and have to call this
240         // inside them without cast!!!
241         ///
242         virtual LyXText * getLyXText(BufferView const *,
243                                      bool const recursive = false) const;
244         ///
245         virtual void deleteLyXText(BufferView *, bool = true) const {}
246         ///
247         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
248         /// returns the actuall scroll-value
249         virtual int scroll(bool recursive=true) const {
250                 if (!recursive || !owner_)
251                         return scx;
252                 return 0;
253         }
254         /// try to get a paragraph pointer from it's id if we have a
255         /// paragraph to give back!
256         virtual Paragraph * getParFromID(int /* id */) const {
257                 return 0;
258         }
259         /// try to get a inset pointer from it's id if we have
260         /// an inset to give back!
261         virtual Inset * getInsetFromID(int /* id */) const {
262                 return 0;
263         }
264         /// if this insets owns paragraphs (f.ex. InsetText) then it
265         /// should return it's very first one!
266         virtual Paragraph * firstParagraph() const {
267                 return 0;
268         }
269         /// return the cursor if we own one otherwise giv'em just the
270         /// BufferView cursor to work with.
271         virtual LyXCursor const & cursor(BufferView * bview) const;
272         /// id functions
273         int id() const;
274         void id(int id_arg);
275
276         /// used to toggle insets
277         // is the inset open?
278         virtual bool isOpen() const { return false; }
279         // open or close the inset, depending on the bool
280         virtual void open(BufferView *, bool) {}
281 protected:
282         ///
283         mutable int top_x;
284         ///
285         mutable int top_baseline;
286         ///
287         mutable int scx;
288         ///
289         unsigned int id_;
290         ///
291         static unsigned int inset_id;
292
293 private:
294         ///
295         Inset * owner_;
296         ///
297         string name;
298         ///
299         LColor::color background_color_;
300 };
301
302
303 //  Updatable Insets. These insets can be locked and receive
304 //  directly user interaction. Currently used only for mathed.
305 //  Note that all pure methods from Inset class are pure here too.
306 //  [Alejandro 080596] 
307
308 /** Extracted from Matthias notes:
309  * 
310  * An inset can simple call LockInset in it's edit call and *ONLY* 
311  * in it's edit call.
312  *
313  * Unlocking is either done by LyX or the inset itself with a 
314  * UnlockInset-call
315  *
316  * During the lock, all button and keyboard events will be modified
317  * and send to the inset through the following inset-features. Note that
318  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
319  * to contain the code for restoring the menus and things like this.
320  * 
321  * If a inset wishes any redraw and/or update it just has to call
322  * UpdateInset(this).
323  * 
324  * It's is completly irrelevant, where the inset is. UpdateInset will
325  * find it in any paragraph in any buffer. 
326  * Of course the_locking_inset and the insets in the current paragraph/buffer
327  *  are checked first, so no performance problem should occur.
328  */
329 class UpdatableInset : public Inset {
330 public:
331         /** Dispatch result codes
332             Now that nested updatable insets are allowed, the local dispatch
333             becomes a bit complex, just two possible results (boolean)
334             are not enough. 
335          
336             DISPATCHED   = the inset catched the action
337             DISPATCHED_NOUPDATE = the inset catched the action and no update
338                                   is needed here to redraw the inset
339             FINISHED     = the inset must be unlocked as a result
340                            of the action
341             UNDISPATCHED = the action was not catched, it should be
342                            dispatched by lower level insets
343         */ 
344         enum RESULT {
345                 UNDISPATCHED = 0,
346                 DISPATCHED,
347                 DISPATCHED_NOUPDATE,
348                 FINISHED
349         };
350     
351         /// To convert old binary dispatch results
352         RESULT DISPATCH_RESULT(bool b) {
353                 return b ? DISPATCHED : FINISHED;
354         }
355
356         ///
357         UpdatableInset();
358         ///
359         UpdatableInset(UpdatableInset const & in, bool same_id = false);
360
361         ///
362         virtual EDITABLE editable() const;
363         
364         /// may call ToggleLockedInsetCursor
365         virtual void toggleInsetCursor(BufferView *);
366         ///
367         virtual void showInsetCursor(BufferView *, bool show = true);
368         ///
369         virtual void hideInsetCursor(BufferView *);
370         ///
371         virtual void fitInsetCursor(BufferView *) const;
372         ///
373         virtual void getCursorPos(BufferView *, int &, int &) const {}
374         ///
375         virtual void insetButtonPress(BufferView *, int x, int y, int button);
376         ///
377         virtual void insetButtonRelease(BufferView *,
378                                         int x, int y, int button);
379         ///
380         virtual void insetKeyPress(XKeyEvent * ev);
381         ///
382         virtual void insetMotionNotify(BufferView *, int x, int y, int state);
383         ///
384         virtual void insetUnlock(BufferView *);
385         ///
386         virtual void edit(BufferView *, int x, int y, unsigned int button);
387         ///
388         virtual void edit(BufferView *, bool front = true);
389         ///
390         virtual void draw(BufferView *, LyXFont const &,
391                           int baseline, float & x, bool cleared) const;
392         ///
393         virtual void setFont(BufferView *, LyXFont const &,
394                          bool toggleall = false, bool selectall = false);
395         ///
396         virtual bool insertInset(BufferView *, Inset *) { return false; }
397         ///
398         virtual UpdatableInset * getLockingInset() const {
399                 return const_cast<UpdatableInset *>(this);
400         }
401         ///
402         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
403                 { return (c == lyxCode()) ? this : 0; }
404         ///
405         virtual unsigned int insetInInsetY() { return 0; }
406         ///
407         virtual bool updateInsetInInset(BufferView *, Inset *)
408                 { return false; }
409         ///
410         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
411                 { return false; }
412         ///
413         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
414                                         bool /*lr*/ = false)
415                 { return false; }
416         ///  An updatable inset could handle lyx editing commands
417         virtual RESULT localDispatch(BufferView *, kb_action, string const &);
418         ///
419         bool isCursorVisible() const { return cursor_visible_; }
420         ///
421         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
422         ///
423         int scroll(bool recursive = true) const {
424                 // We need this method to not clobber the real method in Inset
425                 return Inset::scroll(recursive);
426         }
427         ///
428         virtual bool showInsetDialog(BufferView *) const { return false; }
429         ///
430         virtual void nodraw(bool b) {
431                 block_drawing_ = b;
432         }
433         ///
434         virtual bool nodraw() const {
435                 return block_drawing_;
436         }
437         ///
438         // needed for spellchecking text
439         ///
440         virtual string selectNextWord(BufferView *, float & value) const;
441         virtual void selectSelectedWord(BufferView *) { return; }
442         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
443                 return;
444         }
445         ///
446         // needed for search/replace functionality
447         ///
448         virtual bool searchForward(BufferView *, string const &,
449                                    bool const & = true, bool const & = false);
450         virtual bool searchBackward(BufferView *, string const &,
451                                     bool const & = true, bool const & = false);
452
453 protected:
454         ///
455         void toggleCursorVisible() const {
456                 cursor_visible_ = !cursor_visible_;
457         }
458         ///
459         void setCursorVisible(bool b) const {
460                 cursor_visible_ = b;
461         }
462         /// scrolls to absolute position in bufferview-workwidth * sx units
463         void scroll(BufferView *, float sx) const;
464         /// scrolls offset pixels
465         void scroll(BufferView *, int offset) const;
466
467 private:
468         ///
469         mutable bool cursor_visible_;
470         ///
471         bool block_drawing_;
472 };
473 #endif