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