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