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