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