]> git.lyx.org Git - features.git/blob - src/insets/lyxinset.h
29559385725e7aba7607a7b78cf8b5a54f51e785
[features.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
30 struct LaTeXFeatures;
31
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,
49                 ///
50                 LOT_CODE,
51                 ///
52                 LOA_CODE,
53                 ///
54                 QUOTE_CODE,
55                 ///
56                 MARK_CODE,
57                 ///
58                 REF_CODE,
59                 ///
60                 URL_CODE,
61                 ///
62                 HTMLURL_CODE,
63                 ///
64                 SEPARATOR_CODE,
65                 ///
66                 ENDING_CODE,
67                 ///
68                 LABEL_CODE,
69                 ///
70                 IGNORE_CODE,
71                 ///
72                 ACCENT_CODE,
73                 ///
74                 MATH_CODE,
75                 ///
76                 INDEX_CODE,
77                 ///
78                 INCLUDE_CODE,
79                 ///
80                 GRAPHICS_CODE,
81                 ///
82                 PARENT_CODE,
83                 ///
84                 BIBTEX_CODE,
85                 ///
86                 TEXT_CODE,
87                 ///
88                 ERT_CODE,
89                 ///
90                 FOOT_CODE,
91                 ///
92                 MARGIN_CODE,
93                 ///
94                 SPECIALCHAR_CODE,
95                 ///
96                 TABULAR_CODE,
97                 ///
98                 EXTERNAL_CODE
99         };
100
101         enum EDITABLE {
102             NOT_EDITABLE = 0,
103             IS_EDITABLE,
104             HIGHLY_EDITABLE
105         };
106
107         ///
108         Inset() { owner_ = 0; top_x = top_baseline = 0; }
109         ///
110         virtual ~Inset() {}
111         ///
112         virtual int ascent(Painter &, LyXFont const &) const = 0;
113         ///
114         virtual int descent(Painter &, LyXFont const &) const = 0;
115         ///
116         virtual int width(Painter &, LyXFont const &) const = 0;
117         ///
118         virtual void draw(BufferView *, LyXFont const &,
119                           int baseline, float & x, bool cleared) const = 0;
120         /// update the inset representation
121         virtual void update(BufferView *, LyXFont const &, bool =false)
122                 {}
123         ///
124         virtual LyXFont ConvertFont(LyXFont font);
125         /// what appears in the minibuffer when opening
126         virtual const char * EditMessage() const;
127         ///
128         virtual void Edit(BufferView *, int x, int y, unsigned int button);
129         ///
130         virtual EDITABLE Editable() const;
131         /// This is called when the user clicks inside an inset
132         virtual void InsetButtonPress(BufferView *, int, int, int) {}
133         /// This is called when the user releases the button inside an inset
134         virtual void InsetButtonRelease(BufferView *, int, int, int) {}
135         /// This is caleld when the user moves the mouse inside an inset
136         virtual void InsetMotionNotify(BufferView *, int , int , int) {}
137         ///
138         virtual bool IsTextInset() const { return false; }
139         ///
140         virtual bool doClearArea() const { return true; }
141         ///
142         virtual bool AutoDelete() const;
143         ///
144         virtual void Write(Buffer const *, std::ostream &) const = 0;
145         ///
146         virtual void Read(Buffer const *, LyXLex & lex) = 0;
147         /** returns the number of rows (\n's) of generated tex code.
148          fragile == true means, that the inset should take care about
149          fragile commands by adding a \protect before.
150          If the free_spc (freespacing) variable is set, then this inset
151          is in a free-spacing paragraph.
152          */
153         virtual int Latex(Buffer const *, std::ostream &, bool fragile,
154                           bool free_spc) const = 0;
155         ///
156         virtual int Ascii(Buffer const *, std::ostream &) const = 0;
157         ///
158         virtual int Linuxdoc(Buffer const *, std::ostream &) const = 0;
159         ///
160         virtual int DocBook(Buffer const *, std::ostream &) const = 0;
161         /// Updates needed features for this inset.
162         virtual void Validate(LaTeXFeatures & features) const;
163         ///
164         virtual bool Deletable() const;
165
166         /// returns LyX code associated with the inset. Used for TOC, ...)
167         virtual Inset::Code LyxCode() const { return NO_CODE; }
168   
169         virtual std::vector<string> getLabelList() const {
170                 return std::vector<string>();
171         }
172
173         ///
174         virtual Inset * Clone() const = 0;
175
176         /// returns true to override begin and end inset in file
177         virtual bool DirectWrite() const;
178
179         /// Returns true if the inset should be centered alone
180         virtual bool display() const { return false; }  
181         /// Changes the display state of the inset
182         virtual void display(bool) {}  
183         ///
184         virtual bool InsertInsetAllowed(Inset *) const { return false; }
185         ///
186         virtual void setInsetName(const char * s) { name = s; }
187         ///
188         virtual string getInsetName() const { return name; }
189         ///
190         virtual void setOwner(Inset * inset) { owner_ = inset; }
191         ///
192         virtual Inset * owner() const { return owner_; }
193         ///
194         int x() const { return top_x; }
195         ///
196         int y() const { return top_baseline; }
197
198 protected:
199         ///
200         mutable int top_x;
201         mutable int top_baseline;
202
203 private:
204         ///
205         Inset * owner_;
206         ///
207         string name;
208 };
209
210
211 //  Updatable Insets. These insets can be locked and receive
212 //  directly user interaction. Currently used only for mathed.
213 //  Note that all pure methods from Inset class are pure here too.
214 //  [Alejandro 080596] 
215
216 /** Extracted from Matthias notes:
217   * 
218   * An inset can simple call LockInset in it's edit call and *ONLY* 
219   * in it's edit call.
220   *
221   * Unlocking is either done by LyX or the inset itself with a 
222   * UnlockInset-call
223   *
224   * During the lock, all button and keyboard events will be modified
225   * and send to the inset through the following inset-features. Note that
226   * Inset::InsetUnlock will be called from inside UnlockInset. It is meant
227   * to contain the code for restoring the menus and things like this.
228   * 
229   * If a inset wishes any redraw and/or update it just has to call
230   * UpdateInset(this).
231   * 
232   * It's is completly irrelevant, where the inset is. UpdateInset will
233   * find it in any paragraph in any buffer. 
234   * Of course the_locking_inset and the insets in the current paragraph/buffer
235   *  are checked first, so no performance problem should occur.
236   */
237 class UpdatableInset: public Inset {
238 public:
239         /** Dispatch result codes
240             Now that nested updatable insets are allowed, the local dispatch
241             becomes a bit complex, just two possible results (boolean)
242             are not enough. 
243          
244             DISPATCHED   = the inset catched the action
245             DISPATCHED_NOUPDATE = the inset catched the action and no update
246                                   is needed here to redraw the inset
247             FINISHED     = the inset must be unlocked as a result
248                            of the action
249             UNDISPATCHED = the action was not catched, it should be
250                            dispatched by lower level insets
251         */ 
252         enum RESULT {
253             UNDISPATCHED = 0,
254             DISPATCHED,
255             DISPATCHED_NOUPDATE,
256             FINISHED
257         };
258     
259         /// To convert old binary dispatch results
260         RESULT DISPATCH_RESULT(bool b) {
261                 return b ? DISPATCHED : FINISHED;
262         }
263
264         ///
265         UpdatableInset() { scx = mx_scx = 0; }
266         ///
267         //virtual ~UpdatableInset() {}
268         ///
269         virtual EDITABLE Editable() const;
270    
271         /// may call ToggleLockedInsetCursor
272         virtual void ToggleInsetCursor(BufferView *);
273         ///
274         virtual void ShowInsetCursor(BufferView *);
275         ///
276         virtual void HideInsetCursor(BufferView *);
277         ///
278         virtual void GetCursorPos(BufferView *, int &, int &) const {}
279         ///
280         virtual void InsetButtonPress(BufferView *, int x, int y, int button);
281         ///
282         virtual void InsetButtonRelease(BufferView *,
283                                         int x, int y, int button);
284         ///
285         virtual void InsetKeyPress(XKeyEvent * ev);
286         ///
287         virtual void InsetMotionNotify(BufferView *, int x, int y, int state);
288         ///
289         virtual void InsetUnlock(BufferView *);
290         ///
291         virtual void Edit(BufferView *, int x, int y, unsigned int button);
292         ///
293         virtual void draw(BufferView *, LyXFont const &,
294                           int baseline, float & x, bool cleared) const;
295         ///
296         virtual void SetFont(BufferView *, LyXFont const &,
297                              bool toggleall = false);
298         ///
299         virtual bool InsertInset(BufferView *, Inset *) { return false; }
300         ///
301         virtual bool InsertInsetAllowed(Inset *) const { return true; }
302         ///
303         virtual UpdatableInset * GetLockingInset() { return this; }
304         ///
305         virtual UpdatableInset * GetFirstLockingInsetOfType(Inset::Code c)
306                 { return (c == LyxCode()) ? this : 0; }
307         ///
308         virtual int InsetInInsetY() { return 0; }
309         ///
310         virtual bool UpdateInsetInInset(BufferView *, Inset *)
311                 { return false; }
312         ///
313         virtual bool LockInsetInInset(BufferView *, UpdatableInset *)
314                 { return false; }
315         ///
316         virtual bool UnlockInsetInInset(BufferView *, UpdatableInset *,
317                                         bool /*lr*/ = false)
318                 { return false; }
319         ///  An updatable inset could handle lyx editing commands
320         virtual RESULT LocalDispatch(BufferView *, int, string const &);
321         ///
322         virtual bool isCursorVisible() const { return cursor_visible; }
323         ///
324         virtual int getMaxWidth(Painter & pain, UpdatableInset const *) const;
325
326 protected:
327         ///
328         mutable bool cursor_visible;
329
330 private:
331         ///
332         int mx_scx;
333         mutable int scx;
334 };
335 #endif