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