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