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