]> git.lyx.org Git - lyx.git/blob - src/insets/lyxinset.h
2938f87601361a56335b9b735b67ae5cb84dd5ac
[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          */
127         virtual int Latex(ostream &, signed char fragile) const = 0;
128 #ifndef USE_OSTREAM_ONLY
129         ///
130         virtual int Latex(string & file, signed char fragile) const = 0;
131         ///
132         virtual int Linuxdoc(string & /*file*/) const = 0;
133         ///
134         virtual int DocBook(string & /*file*/) const = 0;
135 #else
136         ///
137         virtual int Linuxdoc(ostream &) const = 0;
138         ///
139         virtual int DocBook(ostream &) const = 0;
140 #endif
141         /// Updates needed features for this inset.
142         virtual void Validate(LaTeXFeatures & features) const;
143         ///
144         virtual bool Deletable() const;
145
146         /// returns LyX code associated with the inset. Used for TOC, ...)
147         virtual Inset::Code LyxCode() const = 0;
148   
149         /// Get the label that appears at screen
150         virtual string getLabel(int) const {
151                 return string();
152         }
153
154         ///
155         virtual Inset * Clone() const = 0;
156
157         /// returns true to override begin and end inset in file
158         virtual bool DirectWrite() const;
159
160         /// Returns true if the inset should be centered alone
161         virtual bool display() const { return false; }  
162         /// Changes the display state of the inset
163         virtual void display(bool) {}  
164         ///
165         virtual int GetNumberOfLabels() const {
166                 return 0;
167         }
168         ///
169         virtual void init(BufferView *) {}
170
171 };
172
173
174 //  Updatable Insets. These insets can be locked and receive
175 //  directly user interaction. Currently used only for mathed.
176 //  Note that all pure methods from Inset class are pure here too.
177 //  [Alejandro 080596] 
178
179 /** Extracted from Matthias notes:
180   * 
181   * An inset can simple call LockInset in it's edit call and *ONLY* 
182   * in it's edit call.
183   *
184   * Unlocking is either done by LyX or the inset itself with a 
185   * UnlockInset-call
186   *
187   * During the lock, all button and keyboard events will be modified
188   * and send to the inset through the following inset-features. Note that
189   * Inset::InsetUnlock will be called from inside UnlockInset. It is meant
190   * to contain the code for restoring the menus and things like this.
191   * 
192   * If a inset wishes any redraw and/or update it just has to call
193   * UpdateInset(this).
194   * 
195   * It's is completly irrelevant, where the inset is. UpdateInset will
196   * find it in any paragraph in any buffer. 
197   * Of course the_locking_inset and the insets in the current paragraph/buffer
198   *  are checked first, so no performance problem should occur.
199   */
200 class UpdatableInset: public Inset {
201 public:
202         /** Dispatch result codes
203             Now that nested updatable insets are allowed, the local dispatch
204             becomes a bit complex, just two possible results (boolean)
205             are not enough. 
206          
207             DISPATCHED   = the inset catched the action
208             FINISHED     = the inset must be unlocked as a result
209                            of the action
210             UNDISPATCHED = the action was not catched, it should be
211                            dispatched by lower level insets
212         */ 
213         enum RESULT {
214             UNDISPATCHED = 0,
215             DISPATCHED,
216             FINISHED
217         };
218     
219         /// To convert old binary dispatch results
220         RESULT DISPATCH_RESULT(bool b) {
221                 return b ? DISPATCHED : FINISHED;
222         }
223
224         ///
225         UpdatableInset() {
226             scx = mx_scx = 0;
227             owner_ = 0;
228         }
229         ///
230         //virtual ~UpdatableInset() {}
231         ///
232         virtual EDITABLE Editable() const;
233    
234         /// may call ToggleLockedInsetCursor
235         virtual void ToggleInsetCursor(BufferView *);
236         ///
237         virtual void GetCursorPos(int &, int &) const {}
238         ///
239         virtual void InsetButtonPress(BufferView *, int x, int y, int button);
240         ///
241         virtual void InsetButtonRelease(BufferView *,
242                                         int x, int y, int button);
243         ///
244         virtual void InsetKeyPress(XKeyEvent * ev);
245         ///
246         virtual void InsetMotionNotify(BufferView *, int x, int y, int state);
247         ///
248         virtual void InsetUnlock(BufferView *);
249         ///
250         virtual void Edit(BufferView *, int x, int y, unsigned int button);
251         ///
252         virtual void draw(Painter &, LyXFont const &,
253                           int baseline, float & x) const;
254         ///
255         virtual void SetFont(BufferView *, LyXFont const &,
256                              bool toggleall = false);
257         ///
258         virtual bool InsertInset(BufferView *, Inset *) { return false; }
259         ///
260         virtual UpdatableInset * GetLockingInset() { return this; }
261         ///
262         virtual int InsetInInsetY() { return 0; }
263         ///
264         virtual bool UpdateInsetInInset(BufferView *, Inset *)
265                 { return false; }
266         ///
267         virtual bool UnlockInsetInInset(BufferView *, Inset *,
268                                         bool /*lr*/ = false)
269                 { return false; }
270         ///  An updatable inset could handle lyx editing commands
271         virtual RESULT LocalDispatch(BufferView *, int, string const &);
272         ///
273         virtual bool isCursorVisible() const { return cursor_visible; }
274         ///
275         virtual int getMaxWidth(Painter & pain) const;
276         ///
277         virtual void setOwner(UpdatableInset * inset) { owner_ = inset; }
278         ///
279         virtual UpdatableInset * owner() { return owner_; }
280
281 protected:
282         ///
283         // virtual void UpdateLocal(bool flag=true);
284         ///
285         mutable int top_x;
286         mutable int top_baseline;
287         mutable bool cursor_visible;
288
289 private:
290         ///
291         int mx_scx;
292         mutable int scx;
293         ///
294         UpdatableInset * owner_;
295
296 };
297 #endif