]> git.lyx.org Git - lyx.git/blob - src/insets/lyxinset.h
small changes to ButtonController usage
[lyx.git] / src / insets / lyxinset.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 1995-2000 the LyX Team.
8  *
9  * ====================================================== */
10
11 #ifndef LYXINSET_H
12 #define LYXINSET_H
13
14 #ifdef __GNUG__
15 #pragma interface
16 #endif
17
18 #include <vector>
19
20 #include <X11/Xlib.h>
21
22 #include "gettext.h"
23 #include "lyxfont.h"
24 #include "lyxlex.h"
25
26 class BufferView;
27 class Buffer;
28 class Painter;
29 class LyXText;
30
31 struct LaTeXFeatures;
32
33
34 /// Insets
35 class Inset {
36 public:
37         /** This is not quite the correct place for this enum. I think
38             the correct would be to let each subclass of Inset declare
39             its own enum code. Actually the notion of an Inset::Code
40             should be avoided, but I am not sure how this could be done
41             in a cleaner way. */
42         enum Code {
43                 ///
44                 NO_CODE,
45                 ///
46                 TOC_CODE,  // do these insets really need a code? (ale)
47                 ///
48                 LOF_CODE, // 2
49                 ///
50                 LOT_CODE,
51                 ///
52                 LOA_CODE,
53                 ///
54                 QUOTE_CODE, // 5
55                 ///
56                 MARK_CODE,
57                 ///
58                 REF_CODE,
59                 ///
60                 URL_CODE,
61                 ///
62                 HTMLURL_CODE,
63                 ///
64                 SEPARATOR_CODE, // 10
65                 ///
66                 ENDING_CODE,
67                 ///
68                 LABEL_CODE,
69                 ///
70                 IGNORE_CODE,
71                 ///
72                 ACCENT_CODE,
73                 ///
74                 MATH_CODE, // 15
75                 ///
76                 INDEX_CODE,
77                 ///
78                 INCLUDE_CODE,
79                 ///
80                 GRAPHICS_CODE,
81                 ///
82                 PARENT_CODE,
83                 ///
84                 BIBTEX_CODE, // 20
85                 ///
86                 TEXT_CODE,
87                 ///
88                 ERT_CODE,
89                 ///
90                 FOOT_CODE,
91                 ///
92                 MARGIN_CODE,
93                 ///
94                 FLOAT_CODE, // 25
95                 ///
96                 MINIPAGE_CODE,
97                 ///
98                 SPECIALCHAR_CODE,
99                 ///
100                 TABULAR_CODE,
101                 ///
102                 EXTERNAL_CODE,
103                 ///
104                 THEOREM_CODE, // 30
105                 ///
106                 CAPTION_CODE
107         };
108
109         ///
110         enum { TEXT_TO_INSET_OFFSET = 2 };
111
112         enum EDITABLE {
113             NOT_EDITABLE = 0,
114             IS_EDITABLE,
115             HIGHLY_EDITABLE
116         };
117
118         ///
119         Inset() { owner_ = 0; top_x = top_baseline = 0; scx = 0; }
120         ///
121         virtual ~Inset() {}
122         ///
123         virtual int ascent(BufferView *, LyXFont const &) const = 0;
124         ///
125         virtual int descent(BufferView *, LyXFont const &) const = 0;
126         ///
127         virtual int width(BufferView *, LyXFont const &) const = 0;
128         ///
129         virtual void draw(BufferView *, LyXFont const &,
130                           int baseline, float & x, bool cleared) const = 0;
131         /// update the inset representation
132         virtual void update(BufferView *, LyXFont const &, bool = false)
133                 {}
134         ///
135         virtual LyXFont ConvertFont(LyXFont const & font) const;
136         /// what appears in the minibuffer when opening
137         virtual const char * EditMessage() const;
138         ///
139         virtual void Edit(BufferView *, int x, int y, unsigned int button);
140         ///
141         virtual EDITABLE Editable() const;
142         /// This is called when the user clicks inside an inset
143         virtual void InsetButtonPress(BufferView *, int, int, int) {}
144         /// This is called when the user releases the button inside an inset
145         virtual void InsetButtonRelease(BufferView *, int, int, int) {}
146         /// This is caleld when the user moves the mouse inside an inset
147         virtual void InsetMotionNotify(BufferView *, int , int , int) {}
148         ///
149         virtual bool IsTextInset() const { return false; }
150         ///
151         virtual bool doClearArea() const { return true; }
152         ///
153         virtual bool AutoDelete() const;
154         ///
155         virtual void Write(Buffer const *, std::ostream &) const = 0;
156         ///
157         virtual void Read(Buffer const *, LyXLex & lex) = 0;
158         /** returns the number of rows (\n's) of generated tex code.
159          fragile == true means, that the inset should take care about
160          fragile commands by adding a \protect before.
161          If the free_spc (freespacing) variable is set, then this inset
162          is in a free-spacing paragraph.
163          */
164         virtual int Latex(Buffer const *, std::ostream &, bool fragile,
165                           bool free_spc) const = 0;
166         ///
167         virtual int Ascii(Buffer const *, std::ostream &) const = 0;
168         ///
169         virtual int Linuxdoc(Buffer const *, std::ostream &) const = 0;
170         ///
171         virtual int DocBook(Buffer const *, std::ostream &) const = 0;
172         /// Updates needed features for this inset.
173         virtual void Validate(LaTeXFeatures & features) const;
174         ///
175         virtual bool Deletable() const;
176
177         /// returns LyX code associated with the inset. Used for TOC, ...)
178         virtual Inset::Code LyxCode() const { return NO_CODE; }
179   
180         virtual std::vector<string> getLabelList() const {
181                 return std::vector<string>();
182         }
183
184         ///
185         virtual Inset * Clone() const = 0;
186
187         /// returns true to override begin and end inset in file
188         virtual bool DirectWrite() const;
189
190         /// Returns true if the inset should be centered alone
191         virtual bool display() const { return false; }
192         /// Changes the display state of the inset
193         virtual void display(bool) {}
194         ///
195         /// returns true if this inset needs a row on it's own
196         ///
197         virtual bool needFullRow() const { return false; }
198         ///
199         virtual bool InsertInsetAllowed(Inset *) const { return false; }
200         ///
201         virtual void setInsetName(const char * s) { name = s; }
202         ///
203         virtual string getInsetName() const { return name; }
204         ///
205         virtual void setOwner(Inset * inset) { owner_ = inset; }
206         ///
207         virtual Inset * owner() const { return owner_; }
208         ///
209         int x() const { return top_x; }
210         ///
211         int y() const { return top_baseline; }
212         ///
213         /// because we could have fake text insets and have to call this
214         /// inside them without cast!!!
215         virtual LyXText * getLyXText(BufferView *) const;
216         virtual void deleteLyXText(BufferView *, bool =true) const {}
217         virtual void resizeLyXText(BufferView *) const {}
218         // returns the actuall scroll-value
219         int  scroll() const { return scx; }
220
221 protected:
222         ///
223         mutable int top_x;
224         mutable int top_baseline;
225         mutable int scx;
226
227 private:
228         ///
229         Inset * owner_;
230         ///
231         string name;
232 };
233
234
235 //  Updatable Insets. These insets can be locked and receive
236 //  directly user interaction. Currently used only for mathed.
237 //  Note that all pure methods from Inset class are pure here too.
238 //  [Alejandro 080596] 
239
240 /** Extracted from Matthias notes:
241   * 
242   * An inset can simple call LockInset in it's edit call and *ONLY* 
243   * in it's edit call.
244   *
245   * Unlocking is either done by LyX or the inset itself with a 
246   * UnlockInset-call
247   *
248   * During the lock, all button and keyboard events will be modified
249   * and send to the inset through the following inset-features. Note that
250   * Inset::InsetUnlock will be called from inside UnlockInset. It is meant
251   * to contain the code for restoring the menus and things like this.
252   * 
253   * If a inset wishes any redraw and/or update it just has to call
254   * UpdateInset(this).
255   * 
256   * It's is completly irrelevant, where the inset is. UpdateInset will
257   * find it in any paragraph in any buffer. 
258   * Of course the_locking_inset and the insets in the current paragraph/buffer
259   *  are checked first, so no performance problem should occur.
260   */
261 class UpdatableInset: public Inset {
262 public:
263         /** Dispatch result codes
264             Now that nested updatable insets are allowed, the local dispatch
265             becomes a bit complex, just two possible results (boolean)
266             are not enough. 
267          
268             DISPATCHED   = the inset catched the action
269             DISPATCHED_NOUPDATE = the inset catched the action and no update
270                                   is needed here to redraw the inset
271             FINISHED     = the inset must be unlocked as a result
272                            of the action
273             UNDISPATCHED = the action was not catched, it should be
274                            dispatched by lower level insets
275         */ 
276         enum RESULT {
277             UNDISPATCHED = 0,
278             DISPATCHED,
279             DISPATCHED_NOUPDATE,
280             FINISHED
281         };
282     
283         /// To convert old binary dispatch results
284         RESULT DISPATCH_RESULT(bool b) {
285                 return b ? DISPATCHED : FINISHED;
286         }
287
288         ///
289         UpdatableInset() {}
290         ///
291         virtual EDITABLE Editable() const;
292    
293         /// may call ToggleLockedInsetCursor
294         virtual void ToggleInsetCursor(BufferView *);
295         ///
296         virtual void ShowInsetCursor(BufferView *);
297         ///
298         virtual void HideInsetCursor(BufferView *);
299         ///
300         virtual void GetCursorPos(BufferView *, int &, int &) const {}
301         ///
302         virtual void InsetButtonPress(BufferView *, int x, int y, int button);
303         ///
304         virtual void InsetButtonRelease(BufferView *,
305                                         int x, int y, int button);
306         ///
307         virtual void InsetKeyPress(XKeyEvent * ev);
308         ///
309         virtual void InsetMotionNotify(BufferView *, int x, int y, int state);
310         ///
311         virtual void InsetUnlock(BufferView *);
312         ///
313         virtual void Edit(BufferView *, int x, int y, unsigned int button);
314         ///
315         virtual void draw(BufferView *, LyXFont const &,
316                           int baseline, float & x, bool cleared) const;
317         ///
318         virtual void SetFont(BufferView *, LyXFont const &,
319                              bool toggleall = false);
320         ///
321         virtual bool InsertInset(BufferView *, Inset *) { return false; }
322         ///
323         virtual bool InsertInsetAllowed(Inset *) const { return true; }
324         ///
325         virtual UpdatableInset * GetLockingInset() { return this; }
326         ///
327         virtual UpdatableInset * GetFirstLockingInsetOfType(Inset::Code c)
328                 { return (c == LyxCode()) ? this : 0; }
329         ///
330         virtual int InsetInInsetY() { return 0; }
331         ///
332         virtual bool UpdateInsetInInset(BufferView *, Inset *)
333                 { return false; }
334         ///
335         virtual bool LockInsetInInset(BufferView *, UpdatableInset *)
336                 { return false; }
337         ///
338         virtual bool UnlockInsetInInset(BufferView *, UpdatableInset *,
339                                         bool /*lr*/ = false)
340                 { return false; }
341         ///  An updatable inset could handle lyx editing commands
342         virtual RESULT LocalDispatch(BufferView *, int, string const &);
343         ///
344         virtual bool isCursorVisible() const { return cursor_visible; }
345         ///
346         virtual int getMaxWidth(Painter & pain, UpdatableInset const *) const;
347         ///
348         int scroll() const { return scx; }
349
350 protected:
351         ///
352         mutable bool cursor_visible;
353
354         // scrolls to absolute position in bufferview-workwidth * sx units
355         void scroll(BufferView *, float sx) const;
356         // scrolls offset pixels
357         void scroll(BufferView *, int offset) const;
358 };
359 #endif