]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
9bc531b8c75fa7cabc166c6db3a723682251fdd2
[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
23 class LyXFont;
24 class BufferView;
25 class Buffer;
26 class Painter;
27 class LyXText;
28 class LyXLex;
29
30 struct LaTeXFeatures;
31
32
33 /// Insets
34 class Inset {
35 public:
36         /** This is not quite the correct place for this enum. I think
37             the correct would be to let each subclass of Inset declare
38             its own enum code. Actually the notion of an Inset::Code
39             should be avoided, but I am not sure how this could be done
40             in a cleaner way. */
41         enum Code {
42                 ///
43                 NO_CODE,
44                 ///
45                 TOC_CODE,  // do these insets really need a code? (ale)
46                 ///
47                 QUOTE_CODE,
48                 ///
49                 MARK_CODE,
50                 ///
51                 REF_CODE, // 5
52                 ///
53                 URL_CODE,
54                 ///
55                 HTMLURL_CODE,
56                 ///
57                 SEPARATOR_CODE,
58                 ///
59                 ENDING_CODE,
60                 ///
61                 LABEL_CODE, // 10
62                 ///
63                 IGNORE_CODE,
64                 ///
65                 ACCENT_CODE,
66                 ///
67                 MATH_CODE,
68                 ///
69                 INDEX_CODE,
70                 ///
71                 INCLUDE_CODE, // 15
72                 ///
73                 GRAPHICS_CODE,
74                 ///
75                 PARENT_CODE,
76                 ///
77                 BIBTEX_CODE,
78                 ///
79                 TEXT_CODE,
80                 ///
81                 ERT_CODE, // 20
82                 ///
83                 FOOT_CODE,
84                 ///
85                 MARGIN_CODE,
86                 ///
87                 FLOAT_CODE,
88                 ///
89                 MINIPAGE_CODE,
90                 ///
91                 SPECIALCHAR_CODE, // 25
92                 ///
93                 TABULAR_CODE,
94                 ///
95                 EXTERNAL_CODE,
96                 ///
97                 THEOREM_CODE,
98                 ///
99                 CAPTION_CODE,
100                 ///
101                 MATHMACRO_CODE, // 30
102                 ///
103                 ERROR_CODE,
104                 ///
105                 CITE_CODE,
106                 ///
107                 FLOAT_LIST_CODE
108         };
109
110         ///
111         enum {
112                 ///
113                 TEXT_TO_INSET_OFFSET = 2
114         };
115
116         ///
117         enum EDITABLE {
118                 ///
119                 NOT_EDITABLE = 0,
120                 ///
121                 IS_EDITABLE,
122                 ///
123                 HIGHLY_EDITABLE
124         };
125         
126         ///
127         Inset() : top_x(0), top_baseline(0), scx(0), owner_(0) {}
128         /// Virtual base destructor
129         virtual ~Inset() {}
130         ///
131         virtual int ascent(BufferView *, LyXFont const &) const = 0;
132         ///
133         virtual int descent(BufferView *, LyXFont const &) const = 0;
134         ///
135         virtual int width(BufferView *, LyXFont const &) const = 0;
136         ///
137         virtual void draw(BufferView *, LyXFont const &,
138                           int baseline, float & x, bool cleared) const = 0;
139         /// update the inset representation
140         virtual void update(BufferView *, LyXFont const &, bool = false)
141                 {}
142         ///
143         virtual LyXFont const convertFont(LyXFont const & font) const;
144         /// what appears in the minibuffer when opening
145         virtual string const editMessage() const;
146         ///
147         virtual void edit(BufferView *, int x, int y, unsigned int button);
148         ///
149         virtual EDITABLE editable() const;
150         /// This is called when the user clicks inside an inset
151         virtual void insetButtonPress(BufferView *, int, int, int) {}
152         /// This is called when the user releases the button inside an inset
153         virtual void insetButtonRelease(BufferView *, int, int, int) {}
154         /// This is caleld when the user moves the mouse inside an inset
155         virtual void insetMotionNotify(BufferView *, int , int , int) {}
156         ///
157         virtual bool isTextInset() const { return false; }
158         ///
159         virtual bool doClearArea() const { return true; }
160         ///
161         virtual bool autoDelete() const;
162         ///
163         virtual void write(Buffer const *, std::ostream &) const = 0;
164         ///
165         virtual void read(Buffer const *, LyXLex & lex) = 0;
166         /** returns the number of rows (\n's) of generated tex code.
167             fragile == true means, that the inset should take care about
168             fragile commands by adding a \protect before.
169             If the free_spc (freespacing) variable is set, then this inset
170             is in a free-spacing paragraph.
171         */
172         virtual int latex(Buffer const *, std::ostream &, bool fragile,
173                           bool free_spc) const = 0;
174         ///
175         virtual int ascii(Buffer const *,
176                           std::ostream &, int linelen = 0) const = 0;
177         ///
178         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
179         ///
180         virtual int docBook(Buffer const *, std::ostream &) const = 0;
181         /// Updates needed features for this inset.
182         virtual void validate(LaTeXFeatures & features) const;
183         ///
184         virtual bool deletable() const;
185         
186         /// returns LyX code associated with the inset. Used for TOC, ...)
187         virtual Inset::Code lyxCode() const { return NO_CODE; }
188         
189         virtual std::vector<string> const getLabelList() const {
190                 return std::vector<string>();
191         }
192
193         ///
194         virtual Inset * clone(Buffer const &) const = 0;
195         
196         /// returns true to override begin and end inset in file
197         virtual bool directWrite() const;
198
199         /// Returns true if the inset should be centered alone
200         virtual bool display() const { return false; }
201         /// Changes the display state of the inset
202         virtual void display(bool) {}
203         ///
204         /// returns true if this inset needs a row on it's own
205         ///
206         virtual bool needFullRow() const { return false; }
207         ///
208         virtual bool insertInsetAllowed(Inset *) const { return false; }
209         ///
210         void setInsetName(string const & s) { name = s; }
211         ///
212         string const getInsetName() const { return name; }
213         ///
214         void setOwner(Inset * inset) { owner_ = inset; }
215         ///
216         Inset * owner() const { return owner_; }
217         ///
218         int x() const { return top_x; }
219         ///
220         int y() const { return top_baseline; }
221         //
222         // because we could have fake text insets and have to call this
223         // inside them without cast!!!
224         ///
225         virtual LyXText * getLyXText(BufferView const *,
226                                      bool const recursive = false) const;
227         ///
228         virtual void deleteLyXText(BufferView *, bool = true) const {}
229         ///
230         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
231         /// returns the actuall scroll-value
232         virtual int scroll(bool recursive=true) const {
233                 if (!recursive || !owner_)
234                         return scx;
235                 return 0;
236         }
237
238 protected:
239         ///
240         mutable int top_x;
241         ///
242         mutable int top_baseline;
243         ///
244         mutable int scx;
245 private:
246         ///
247         Inset * owner_;
248         ///
249         string name;
250 };
251
252
253 //  Updatable Insets. These insets can be locked and receive
254 //  directly user interaction. Currently used only for mathed.
255 //  Note that all pure methods from Inset class are pure here too.
256 //  [Alejandro 080596] 
257
258 /** Extracted from Matthias notes:
259  * 
260  * An inset can simple call LockInset in it's edit call and *ONLY* 
261  * in it's edit call.
262  *
263  * Unlocking is either done by LyX or the inset itself with a 
264  * UnlockInset-call
265  *
266  * During the lock, all button and keyboard events will be modified
267  * and send to the inset through the following inset-features. Note that
268  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
269  * to contain the code for restoring the menus and things like this.
270  * 
271  * If a inset wishes any redraw and/or update it just has to call
272  * UpdateInset(this).
273  * 
274  * It's is completly irrelevant, where the inset is. UpdateInset will
275  * find it in any paragraph in any buffer. 
276  * Of course the_locking_inset and the insets in the current paragraph/buffer
277  *  are checked first, so no performance problem should occur.
278  */
279 class UpdatableInset : public Inset {
280 public:
281         /** Dispatch result codes
282             Now that nested updatable insets are allowed, the local dispatch
283             becomes a bit complex, just two possible results (boolean)
284             are not enough. 
285          
286             DISPATCHED   = the inset catched the action
287             DISPATCHED_NOUPDATE = the inset catched the action and no update
288                                   is needed here to redraw the inset
289             FINISHED     = the inset must be unlocked as a result
290                            of the action
291             UNDISPATCHED = the action was not catched, it should be
292                            dispatched by lower level insets
293         */ 
294         enum RESULT {
295                 UNDISPATCHED = 0,
296                 DISPATCHED,
297                 DISPATCHED_NOUPDATE,
298                 FINISHED
299         };
300     
301         /// To convert old binary dispatch results
302         RESULT DISPATCH_RESULT(bool b) {
303                 return b ? DISPATCHED : FINISHED;
304         }
305
306         ///
307         UpdatableInset() : cursor_visible_(false), block_drawing_(false) {}
308
309         ///
310         virtual EDITABLE editable() const;
311         
312         /// may call ToggleLockedInsetCursor
313         virtual void toggleInsetCursor(BufferView *);
314         ///
315         virtual void showInsetCursor(BufferView *, bool show = true);
316         ///
317         virtual void hideInsetCursor(BufferView *);
318         ///
319         virtual void getCursorPos(BufferView *, int &, int &) const {}
320         ///
321         virtual void insetButtonPress(BufferView *, int x, int y, int button);
322         ///
323         virtual void insetButtonRelease(BufferView *,
324                                         int x, int y, int button);
325         ///
326         virtual void insetKeyPress(XKeyEvent * ev);
327         ///
328         virtual void insetMotionNotify(BufferView *, int x, int y, int state);
329         ///
330         virtual void insetUnlock(BufferView *);
331         ///
332         virtual void edit(BufferView *, int x, int y, unsigned int button);
333         ///
334         virtual void draw(BufferView *, LyXFont const &,
335                           int baseline, float & x, bool cleared) const;
336         ///
337         virtual void setFont(BufferView *, LyXFont const &,
338                          bool toggleall = false, bool selectall = false);
339         ///
340         virtual bool insertInset(BufferView *, Inset *) { return false; }
341         ///
342         virtual bool insertInsetAllowed(Inset *) const { return true; }
343         ///
344         virtual UpdatableInset * getLockingInset() { return this; }
345         ///
346         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
347                 { return (c == lyxCode()) ? this : 0; }
348         ///
349         virtual unsigned int insetInInsetY() { return 0; }
350         ///
351         virtual bool updateInsetInInset(BufferView *, Inset *)
352                 { return false; }
353         ///
354         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
355                 { return false; }
356         ///
357         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
358                                         bool /*lr*/ = false)
359                 { return false; }
360         ///  An updatable inset could handle lyx editing commands
361         virtual RESULT localDispatch(BufferView *, kb_action, string const &);
362         ///
363         bool isCursorVisible() const { return cursor_visible_; }
364         ///
365         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
366         ///
367         int scroll(bool recursive = true) const {
368                 // We need this method to not clobber the real method in Inset
369                 return Inset::scroll(recursive);
370         }
371         ///
372         virtual bool showInsetDialog(BufferView *) const { return false; }
373         ///
374         virtual void nodraw(bool b) {
375                 block_drawing_ = b;
376         }
377         ///
378         virtual bool nodraw() const {
379                 return block_drawing_;
380         }
381
382 protected:
383         ///
384         void toggleCursorVisible() const {
385                 cursor_visible_ = !cursor_visible_;
386         }
387         ///
388         void setCursorVisible(bool b) const {
389                 cursor_visible_ = b;
390         }
391         /// scrolls to absolute position in bufferview-workwidth * sx units
392         void scroll(BufferView *, float sx) const;
393         /// scrolls offset pixels
394         void scroll(BufferView *, int offset) const;
395
396 private:
397         ///
398         mutable bool cursor_visible_;
399         ///
400         bool block_drawing_;
401 };
402 #endif