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