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