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