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