]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
c309e60a531920097aa39104cb3f69353b0d46c2
[lyx.git] / src / insets / inset.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 1995-2001 the LyX Team.
8  *
9  * ====================================================== */
10
11 #ifndef INSET_H
12 #define INSET_H
13
14 #ifdef __GNUG__
15 #pragma interface
16 #endif
17
18 #include <vector>
19 #include "LString.h"
20 #include <X11/Xlib.h>
21 #include "commandtags.h"
22
23 class LyXFont;
24 class BufferView;
25 class Buffer;
26 class Painter;
27 class LyXText;
28 class LyXLex;
29 class Paragraph;
30 class LyXCursor;
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                 QUOTE_CODE,
50                 ///
51                 MARK_CODE,
52                 ///
53                 REF_CODE, // 5
54                 ///
55                 URL_CODE,
56                 ///
57                 HTMLURL_CODE,
58                 ///
59                 SEPARATOR_CODE,
60                 ///
61                 ENDING_CODE,
62                 ///
63                 LABEL_CODE, // 10
64                 ///
65                 IGNORE_CODE,
66                 ///
67                 ACCENT_CODE,
68                 ///
69                 MATH_CODE,
70                 ///
71                 INDEX_CODE,
72                 ///
73                 INCLUDE_CODE, // 15
74                 ///
75                 GRAPHICS_CODE,
76                 ///
77                 PARENT_CODE,
78                 ///
79                 BIBTEX_CODE,
80                 ///
81                 TEXT_CODE,
82                 ///
83                 ERT_CODE, // 20
84                 ///
85                 FOOT_CODE,
86                 ///
87                 MARGIN_CODE,
88                 ///
89                 FLOAT_CODE,
90                 ///
91                 MINIPAGE_CODE,
92                 ///
93                 SPECIALCHAR_CODE, // 25
94                 ///
95                 TABULAR_CODE,
96                 ///
97                 EXTERNAL_CODE,
98                 ///
99                 THEOREM_CODE,
100                 ///
101                 CAPTION_CODE,
102                 ///
103                 MATHMACRO_CODE, // 30
104                 ///
105                 ERROR_CODE,
106                 ///
107                 CITE_CODE,
108                 ///
109                 FLOAT_LIST_CODE,
110                 ///
111                 INDEX_PRINT_CODE
112         };
113
114         ///
115         enum {
116                 ///
117                 TEXT_TO_INSET_OFFSET = 2
118         };
119
120         ///
121         enum EDITABLE {
122                 ///
123                 NOT_EDITABLE = 0,
124                 ///
125                 IS_EDITABLE,
126                 ///
127                 HIGHLY_EDITABLE
128         };
129         
130         ///
131         Inset() : top_x(0), top_baseline(0), scx(0), id_(inset_id++), owner_(0) {}
132         /// Virtual base destructor
133         virtual ~Inset() {}
134         ///
135         virtual int ascent(BufferView *, LyXFont const &) const = 0;
136         ///
137         virtual int descent(BufferView *, LyXFont const &) const = 0;
138         ///
139         virtual int width(BufferView *, LyXFont const &) const = 0;
140         ///
141         virtual void draw(BufferView *, LyXFont const &,
142                           int baseline, float & x, bool cleared) const = 0;
143         /// update the inset representation
144         virtual void update(BufferView *, LyXFont const &, bool = false)
145                 {}
146         ///
147         virtual LyXFont const convertFont(LyXFont const & font) const;
148         /// what appears in the minibuffer when opening
149         virtual string const editMessage() const;
150         ///
151         virtual void edit(BufferView *, int x, int y, unsigned int button);
152         ///
153         virtual void edit(BufferView *, bool front = true);
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 called 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         /// returns true the inset can hold an inset of given type
169         virtual bool insetAllowed(Inset::Code) const { return false; }
170         /// wrapper around the above
171         bool insetAllowed(Inset * in) const { 
172                 return insetAllowed(in->lyxCode()); 
173         }
174         ///
175         virtual void write(Buffer const *, std::ostream &) const = 0;
176         ///
177         virtual void read(Buffer const *, LyXLex & lex) = 0;
178         /** returns the number of rows (\n's) of generated tex code.
179             fragile == true means, that the inset should take care about
180             fragile commands by adding a \protect before.
181             If the free_spc (freespacing) variable is set, then this inset
182             is in a free-spacing paragraph.
183         */
184         virtual int latex(Buffer const *, std::ostream &, bool fragile,
185                           bool free_spc) const = 0;
186         ///
187         virtual int ascii(Buffer const *,
188                           std::ostream &, int linelen = 0) const = 0;
189         ///
190         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
191         ///
192         virtual int docBook(Buffer const *, std::ostream &) const = 0;
193         /// Updates needed features for this inset.
194         virtual void validate(LaTeXFeatures & features) const;
195         ///
196         virtual bool deletable() const;
197         
198         /// returns LyX code associated with the inset. Used for TOC, ...)
199         virtual Inset::Code lyxCode() const { return NO_CODE; }
200         
201         virtual std::vector<string> const getLabelList() const {
202                 return std::vector<string>();
203         }
204
205         ///
206         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
207         
208         /// returns true to override begin and end inset in file
209         virtual bool directWrite() const;
210
211         /// Returns true if the inset should be centered alone
212         virtual bool display() const { return false; }
213         /// Changes the display state of the inset
214         virtual void display(bool) {}
215         ///
216         /// returns true if this inset needs a row on it's own
217         ///
218         virtual bool needFullRow() const { return false; }
219         ///
220         void setInsetName(string const & s) { name = s; }
221         ///
222         string const getInsetName() const { return name; }
223         ///
224         void setOwner(Inset * inset) { owner_ = inset; }
225         ///
226         Inset * owner() const { return owner_; }
227         ///
228         int x() const { return top_x; }
229         ///
230         int y() const { return top_baseline; }
231         //
232         // because we could have fake text insets and have to call this
233         // inside them without cast!!!
234         ///
235         virtual LyXText * getLyXText(BufferView const *,
236                                      bool const recursive = false) const;
237         ///
238         virtual void deleteLyXText(BufferView *, bool = true) const {}
239         ///
240         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
241         /// returns the actuall scroll-value
242         virtual int scroll(bool recursive=true) const {
243                 if (!recursive || !owner_)
244                         return scx;
245                 return 0;
246         }
247         /// try to get a paragraph pointer from it's id if we have a
248         /// paragraph to give back!
249         virtual Paragraph * getParFromID(int /* id */) const {
250                 return 0;
251         }
252         /// try to get a inset pointer from it's id if we have
253         /// an inset to give back!
254         virtual Inset * getInsetFromID(int /* id */) const {
255                 return 0;
256         }
257         /// if this insets owns paragraphs (f.ex. InsetText) then it
258         /// should return it's very first one!
259         virtual Paragraph * firstParagraph() const {
260                 return 0;
261         }
262         /// return the cursor if we own one otherwise giv'em just the
263         /// BufferView cursor to work with.
264         virtual LyXCursor const & cursor(BufferView * bview) const;
265         /// id functions
266         int id() const;
267         void id(int id_arg);
268
269         /// used to toggle insets
270         // is the inset open?
271         virtual bool isOpen() const { return false; }
272         // open or close the inset, depending on the bool
273         virtual void open(BufferView *, bool) {}
274 protected:
275         ///
276         mutable int top_x;
277         ///
278         mutable int top_baseline;
279         ///
280         mutable int scx;
281         ///
282         unsigned int id_;
283         ///
284         static unsigned int inset_id;
285
286 private:
287         ///
288         Inset * owner_;
289         ///
290         string name;
291 };
292
293
294 //  Updatable Insets. These insets can be locked and receive
295 //  directly user interaction. Currently used only for mathed.
296 //  Note that all pure methods from Inset class are pure here too.
297 //  [Alejandro 080596] 
298
299 /** Extracted from Matthias notes:
300  * 
301  * An inset can simple call LockInset in it's edit call and *ONLY* 
302  * in it's edit call.
303  *
304  * Unlocking is either done by LyX or the inset itself with a 
305  * UnlockInset-call
306  *
307  * During the lock, all button and keyboard events will be modified
308  * and send to the inset through the following inset-features. Note that
309  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
310  * to contain the code for restoring the menus and things like this.
311  * 
312  * If a inset wishes any redraw and/or update it just has to call
313  * UpdateInset(this).
314  * 
315  * It's is completly irrelevant, where the inset is. UpdateInset will
316  * find it in any paragraph in any buffer. 
317  * Of course the_locking_inset and the insets in the current paragraph/buffer
318  *  are checked first, so no performance problem should occur.
319  */
320 class UpdatableInset : public Inset {
321 public:
322         /** Dispatch result codes
323             Now that nested updatable insets are allowed, the local dispatch
324             becomes a bit complex, just two possible results (boolean)
325             are not enough. 
326          
327             DISPATCHED   = the inset catched the action
328             DISPATCHED_NOUPDATE = the inset catched the action and no update
329                                   is needed here to redraw the inset
330             FINISHED     = the inset must be unlocked as a result
331                            of the action
332             UNDISPATCHED = the action was not catched, it should be
333                            dispatched by lower level insets
334         */ 
335         enum RESULT {
336                 UNDISPATCHED = 0,
337                 DISPATCHED,
338                 DISPATCHED_NOUPDATE,
339                 FINISHED
340         };
341     
342         /// To convert old binary dispatch results
343         RESULT DISPATCH_RESULT(bool b) {
344                 return b ? DISPATCHED : FINISHED;
345         }
346
347         ///
348         UpdatableInset() : cursor_visible_(false), block_drawing_(false) {}
349
350         ///
351         virtual EDITABLE editable() const;
352         
353         /// may call ToggleLockedInsetCursor
354         virtual void toggleInsetCursor(BufferView *);
355         ///
356         virtual void showInsetCursor(BufferView *, bool show = true);
357         ///
358         virtual void hideInsetCursor(BufferView *);
359         ///
360         virtual void fitInsetCursor(BufferView *) const;
361         ///
362         virtual void getCursorPos(BufferView *, int &, int &) const {}
363         ///
364         virtual void insetButtonPress(BufferView *, int x, int y, int button);
365         ///
366         virtual void insetButtonRelease(BufferView *,
367                                         int x, int y, int button);
368         ///
369         virtual void insetKeyPress(XKeyEvent * ev);
370         ///
371         virtual void insetMotionNotify(BufferView *, int x, int y, int state);
372         ///
373         virtual void insetUnlock(BufferView *);
374         ///
375         virtual void edit(BufferView *, int x, int y, unsigned int button);
376         ///
377         virtual void edit(BufferView *, bool front = true);
378         ///
379         virtual void draw(BufferView *, LyXFont const &,
380                           int baseline, float & x, bool cleared) const;
381         ///
382         virtual void setFont(BufferView *, LyXFont const &,
383                          bool toggleall = false, bool selectall = false);
384         ///
385         virtual bool insertInset(BufferView *, Inset *) { return false; }
386         ///
387         virtual UpdatableInset * getLockingInset() const {
388                 return const_cast<UpdatableInset *>(this);
389         }
390         ///
391         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
392                 { return (c == lyxCode()) ? this : 0; }
393         ///
394         virtual unsigned int insetInInsetY() { return 0; }
395         ///
396         virtual bool updateInsetInInset(BufferView *, Inset *)
397                 { return false; }
398         ///
399         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
400                 { return false; }
401         ///
402         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
403                                         bool /*lr*/ = false)
404                 { return false; }
405         ///  An updatable inset could handle lyx editing commands
406         virtual RESULT localDispatch(BufferView *, kb_action, string const &);
407         ///
408         bool isCursorVisible() const { return cursor_visible_; }
409         ///
410         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
411         ///
412         int scroll(bool recursive = true) const {
413                 // We need this method to not clobber the real method in Inset
414                 return Inset::scroll(recursive);
415         }
416         ///
417         virtual bool showInsetDialog(BufferView *) const { return false; }
418         ///
419         virtual void nodraw(bool b) {
420                 block_drawing_ = b;
421         }
422         ///
423         virtual bool nodraw() const {
424                 return block_drawing_;
425         }
426         ///
427         // needed for spellchecking text
428         ///
429         virtual string selectNextWord(BufferView *, float & value) const;
430         virtual void selectSelectedWord(BufferView *) { return; }
431         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
432                 return;
433         }
434         ///
435         // needed for search/replace functionality
436         ///
437         virtual bool searchForward(BufferView *, string const &,
438                                    bool const & = true, bool const & = false);
439         virtual bool searchBackward(BufferView *, string const &,
440                                     bool const & = true, bool const & = false);
441
442 protected:
443         ///
444         void toggleCursorVisible() const {
445                 cursor_visible_ = !cursor_visible_;
446         }
447         ///
448         void setCursorVisible(bool b) const {
449                 cursor_visible_ = b;
450         }
451         /// scrolls to absolute position in bufferview-workwidth * sx units
452         void scroll(BufferView *, float sx) const;
453         /// scrolls offset pixels
454         void scroll(BufferView *, int offset) const;
455
456 private:
457         ///
458         mutable bool cursor_visible_;
459         ///
460         bool block_drawing_;
461 };
462 #endif