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