]> git.lyx.org Git - lyx.git/blob - src/insets/inset.h
Rob's latest and greatest dialog tweaking.
[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 "LColor.h"
21 #include "frontends/mouse_state.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 class FuncRequest;
32 class WordLangTuple;
33
34 struct LaTeXFeatures;
35
36 namespace grfx {
37         class PreviewLoader;
38 }
39
40 /// Insets
41 class Inset {
42 public:
43         /** This is not quite the correct place for this enum. I think
44             the correct would be to let each subclass of Inset declare
45             its own enum code. Actually the notion of an Inset::Code
46             should be avoided, but I am not sure how this could be done
47             in a cleaner way. */
48         enum Code {
49                 ///
50                 NO_CODE,
51                 ///
52                 TOC_CODE,  // do these insets really need a code? (ale)
53                 ///
54                 QUOTE_CODE,
55                 ///
56                 MARK_CODE,
57                 ///
58                 REF_CODE, // 5
59                 ///
60                 URL_CODE,
61                 ///
62                 HTMLURL_CODE,
63                 ///
64                 SEPARATOR_CODE,
65                 ///
66                 ENDING_CODE,
67                 ///
68                 LABEL_CODE, // 10
69                 ///
70                 NOTE_CODE,
71                 ///
72                 ACCENT_CODE,
73                 ///
74                 MATH_CODE,
75                 ///
76                 INDEX_CODE,
77                 ///
78                 INCLUDE_CODE, // 15
79                 ///
80                 GRAPHICS_CODE,
81                 ///
82                 PARENT_CODE,
83                 ///
84                 BIBTEX_CODE,
85                 ///
86                 TEXT_CODE,
87                 ///
88                 ERT_CODE, // 20
89                 ///
90                 FOOT_CODE,
91                 ///
92                 MARGIN_CODE,
93                 ///
94                 FLOAT_CODE,
95                 ///
96                 WRAP_CODE,
97                 ///
98                 MINIPAGE_CODE,
99                 ///
100                 SPECIALCHAR_CODE, // 25
101                 ///
102                 TABULAR_CODE,
103                 ///
104                 EXTERNAL_CODE,
105 #if 0
106                 ///
107                 THEOREM_CODE,
108 #endif
109                 ///
110                 CAPTION_CODE,
111                 ///
112                 MATHMACRO_CODE, // 30
113                 ///
114                 ERROR_CODE,
115                 ///
116                 CITE_CODE,
117                 ///
118                 FLOAT_LIST_CODE,
119                 ///
120                 INDEX_PRINT_CODE,
121                 ///
122                 OPTARG_CODE
123         };
124
125         ///
126         enum {
127                 ///
128                 TEXT_TO_INSET_OFFSET = 2
129         };
130
131         ///
132         enum EDITABLE {
133                 ///
134                 NOT_EDITABLE = 0,
135                 ///
136                 IS_EDITABLE,
137                 ///
138                 HIGHLY_EDITABLE
139         };
140
141         /** Dispatch result codes
142             Now that nested updatable insets are allowed, the local dispatch
143             becomes a bit complex, just two possible results (boolean)
144             are not enough.
145
146             DISPATCHED          = the inset catched the action
147             DISPATCHED_NOUPDATE = the inset catched the action and no update
148                                   is needed here to redraw the inset
149             FINISHED            = the inset must be unlocked as a result
150                                   of the action
151             FINISHED_RIGHT      = FINISHED, but put the cursor to the RIGHT of
152                                   the inset.
153             FINISHED_UP         = FINISHED, but put the cursor UP of
154                                   the inset.
155             FINISHED_DOWN       = FINISHED, but put the cursor DOWN of
156                                   the inset.
157             UNDISPATCHED        = the action was not catched, it should be
158                                   dispatched by lower level insets
159         */
160         enum RESULT {
161                 UNDISPATCHED = 0,
162                 DISPATCHED,
163                 DISPATCHED_NOUPDATE,
164                 FINISHED,
165                 FINISHED_RIGHT,
166                 FINISHED_UP,
167                 FINISHED_DOWN
168         };
169
170         /// To convert old binary dispatch results
171         RESULT DISPATCH_RESULT(bool b) {
172                 return b ? DISPATCHED : FINISHED;
173         }
174
175         ///
176         Inset();
177         ///
178         Inset(Inset const & in, bool same_id = false);
179         ///
180         virtual ~Inset() {}
181         ///
182         virtual int ascent(BufferView *, LyXFont const &) const = 0;
183         ///
184         virtual int descent(BufferView *, LyXFont const &) const = 0;
185         ///
186         virtual int width(BufferView *, LyXFont const &) const = 0;
187         ///
188         virtual void draw(BufferView *, LyXFont const &,
189                           int baseline, float & x, bool cleared) const = 0;
190         /// update the inset representation
191         virtual void update(BufferView *, LyXFont const &, bool = false)
192                 {}
193         /// what appears in the minibuffer when opening
194         virtual string const editMessage() const;
195         ///
196         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
197         ///
198         virtual void edit(BufferView *, bool front = true);
199         ///
200         virtual EDITABLE editable() const;
201         /// 
202         virtual RESULT localDispatch(FuncRequest const & cmd);
203         ///
204         virtual bool isTextInset() const { return false; }
205         ///
206         virtual bool doClearArea() const { return true; }
207         ///
208         virtual bool autoDelete() const;
209         /// returns true the inset can hold an inset of given type
210         virtual bool insetAllowed(Inset::Code) const { return false; }
211         /// wrapper around the above
212         bool insetAllowed(Inset * in) const;
213         ///
214         virtual void write(Buffer const *, std::ostream &) const = 0;
215         ///
216         virtual void read(Buffer const *, LyXLex & lex) = 0;
217         /** returns the number of rows (\n's) of generated tex code.
218             fragile == true means, that the inset should take care about
219             fragile commands by adding a \protect before.
220             If the free_spc (freespacing) variable is set, then this inset
221             is in a free-spacing paragraph.
222         */
223         virtual int latex(Buffer const *, std::ostream &, bool fragile,
224                           bool free_spc) const = 0;
225         ///
226         virtual int ascii(Buffer const *,
227                           std::ostream &, int linelen = 0) const = 0;
228         ///
229         virtual int linuxdoc(Buffer const *, std::ostream &) const = 0;
230         ///
231         virtual int docbook(Buffer const *, std::ostream &, bool) const = 0;
232         /// Updates needed features for this inset.
233         virtual void validate(LaTeXFeatures & features) const;
234         ///
235         virtual bool deletable() const;
236
237         /// returns LyX code associated with the inset. Used for TOC, ...)
238         virtual Inset::Code lyxCode() const { return NO_CODE; }
239
240         virtual std::vector<string> const getLabelList() const {
241                 return std::vector<string>();
242         }
243
244         ///
245         virtual Inset * clone(Buffer const &, bool same_ids = false) const = 0;
246
247         /// returns true to override begin and end inset in file
248         virtual bool directWrite() const;
249
250         /// Returns true if the inset should be centered alone
251         virtual bool display() const { return false; }
252         /// Changes the display state of the inset
253         virtual void display(bool) {}
254         ///
255         /// returns true if this inset needs a row on it's own
256         ///
257         virtual bool needFullRow() const { return false; }
258         ///
259         void setInsetName(string const & s) { name_ = s; }
260         ///
261         string const & getInsetName() const { return name_; }
262         ///
263         void setOwner(Inset * inset) { owner_ = inset; }
264         ///
265         Inset * owner() const { return owner_; }
266         ///
267         void parOwner(Paragraph * par) { par_owner_ = par; }
268         ///
269         Paragraph * parOwner() const {return par_owner_; }
270         ///
271         void setBackgroundColor(LColor::color);
272         ///
273         LColor::color backgroundColor() const;
274         ///
275         int x() const { return top_x; }
276         ///
277         int y() const { return top_baseline; }
278         //
279         // because we could have fake text insets and have to call this
280         // inside them without cast!!!
281         ///
282         virtual LyXText * getLyXText(BufferView const *,
283                                      bool const recursive = false) const;
284         ///
285         virtual void deleteLyXText(BufferView *, bool = true) const {}
286         ///
287         virtual void resizeLyXText(BufferView *, bool /*force*/= false) const {}
288         /// returns the actuall scroll-value
289         virtual int scroll(bool recursive=true) const {
290                 if (!recursive || !owner_)
291                         return scx;
292                 return 0;
293         }
294         /// try to get a paragraph pointer from it's id if we have a
295         /// paragraph to give back!
296         virtual Paragraph * getParFromID(int /* id */) const {
297                 return 0;
298         }
299         /// try to get a inset pointer from it's id if we have
300         /// an inset to give back!
301         virtual Inset * getInsetFromID(int /* id */) const {
302                 return 0;
303         }
304         /// if this insets owns paragraphs (f.ex. InsetText) then it
305         /// should return it's very first one!
306         virtual Paragraph * firstParagraph() const {
307                 return 0;
308         }
309
310         ///
311         virtual Paragraph * getFirstParagraph(int /*num*/) const {
312                 return 0;
313         }
314
315         /// return the cursor if we own one otherwise giv'em just the
316         /// BufferView cursor to work with.
317         virtual LyXCursor const & cursor(BufferView * bview) const;
318         /// id functions
319         int id() const;
320         ///
321         void id(int id_arg);
322
323         /// used to toggle insets
324         // is the inset open?
325         virtual bool isOpen() const { return false; }
326         /// open the inset
327         virtual void open(BufferView *) {}
328         /// close the inset
329         virtual void close(BufferView *) const {}
330         /// check if the font of the char we want inserting is correct
331         /// and modify it if it is not.
332         virtual bool checkInsertChar(LyXFont &);
333         /// we need this here because collapsed insets are only EDITABLE
334         virtual void setFont(BufferView *, LyXFont const &,
335                          bool toggleall = false, bool selectall = false);
336         ///
337         // needed for spellchecking text
338         ///
339         virtual bool allowSpellcheck() { return false; }
340
341         // should this inset be handled like a normal charater
342         virtual bool isChar() const { return false; }
343         // is this equivalent to a letter?
344         virtual bool isLetter() const { return false; }
345         // is this equivalent to a space (which is BTW different from
346         // a line separator)?
347         virtual bool isSpace() const { return false; }
348         // should we break lines after this inset?
349         virtual bool isLineSeparator() const { return false; }
350         // if this inset has paragraphs should they be output all as default
351         // paragraphs with "Standard" layout?
352         virtual bool forceDefaultParagraphs(Inset const *) const;
353         /** returns true if, when outputing LaTeX, font changes should
354             be closed before generating this inset. This is needed for
355             insets that may contain several paragraphs */
356         virtual bool noFontChange() const { return false; }
357         //
358         virtual void getDrawFont(LyXFont &) const {}
359         /* needed for widths which are % of something
360            returns the value of \textwidth in this inset. Most of the
361            time this is the width of the workarea, but if there is a
362            minipage somewhere, it will be the width of this minipage */
363         virtual int latexTextWidth(BufferView *) const;
364
365         /** Adds a LaTeX snippet to the Preview Loader for transformation
366          *  into a bitmap image. Does not start the laoding process.
367          *
368          *  Most insets have no interest in this capability, so the method
369          *  defaults to empty.
370          */
371         virtual void addPreview(grfx::PreviewLoader &) const {}
372
373         /** Find the PreviewLoader, add a LaTeX snippet to it and
374          *  start the loading process.
375          *
376          *  Most insets have no interest in this capability, so the method
377          *  defaults to empty.
378          */
379         virtual void generatePreview() const {}
380
381 protected:
382         ///
383         mutable int top_x;
384         ///
385         mutable bool topx_set; /* have we already drawn ourself! */
386         ///
387         mutable int top_baseline;
388         ///
389         mutable int scx;
390         ///
391         unsigned int id_;
392         ///
393         static unsigned int inset_id;
394
395 private:
396         ///
397         Inset * owner_;
398         /// the paragraph in which this inset has been inserted
399         Paragraph * par_owner_;
400         ///
401         string name_;
402         ///
403         LColor::color background_color_;
404 };
405
406
407 inline
408 bool Inset::insetAllowed(Inset * in) const
409 {
410         return insetAllowed(in->lyxCode());
411 }
412
413
414 inline
415 bool Inset::checkInsertChar(LyXFont &)
416 {
417         return false;
418 }
419
420 //  Updatable Insets. These insets can be locked and receive
421 //  directly user interaction. Currently used only for mathed.
422 //  Note that all pure methods from Inset class are pure here too.
423 //  [Alejandro 080596]
424
425 /** Extracted from Matthias notes:
426  *
427  * An inset can simple call LockInset in it's edit call and *ONLY*
428  * in it's edit call.
429  *
430  * Unlocking is either done by LyX or the inset itself with a
431  * UnlockInset-call
432  *
433  * During the lock, all button and keyboard events will be modified
434  * and send to the inset through the following inset-features. Note that
435  * Inset::insetUnlock will be called from inside UnlockInset. It is meant
436  * to contain the code for restoring the menus and things like this.
437  *
438  * If a inset wishes any redraw and/or update it just has to call
439  * updateInset(this).
440  *
441  * It's is completly irrelevant, where the inset is. UpdateInset will
442  * find it in any paragraph in any buffer.
443  * Of course the_locking_inset and the insets in the current paragraph/buffer
444  *  are checked first, so no performance problem should occur.
445  */
446 class UpdatableInset : public Inset {
447 public:
448         ///
449         UpdatableInset();
450         ///
451         UpdatableInset(UpdatableInset const & in, bool same_id = false);
452
453         /// check if the font of the char we want inserting is correct
454         /// and modify it if it is not.
455         virtual bool checkInsertChar(LyXFont &);
456         ///
457         virtual EDITABLE editable() const;
458
459         ///
460         virtual void toggleInsetCursor(BufferView *);
461         ///
462         virtual void showInsetCursor(BufferView *, bool show = true);
463         ///
464         virtual void hideInsetCursor(BufferView *);
465         ///
466         virtual void fitInsetCursor(BufferView *) const;
467         ///
468         virtual void getCursorPos(BufferView *, int &, int &) const {}
469         ///
470         virtual void insetUnlock(BufferView *);
471         ///
472         virtual void edit(BufferView *, int x, int y, mouse_button::state button);
473         ///
474         virtual void edit(BufferView *, bool front = true);
475         ///
476         virtual void draw(BufferView *, LyXFont const &,
477                           int baseline, float & x, bool cleared) const;
478         ///
479         virtual bool insertInset(BufferView *, Inset *) { return false; }
480         ///
481         virtual UpdatableInset * getLockingInset() const {
482                 return const_cast<UpdatableInset *>(this);
483         }
484         ///
485         virtual UpdatableInset * getFirstLockingInsetOfType(Inset::Code c)
486                 { return (c == lyxCode()) ? this : 0; }
487         ///
488         virtual int insetInInsetY() const { return 0; }
489         ///
490         virtual bool updateInsetInInset(BufferView *, Inset *)
491                 { return false; }
492         ///
493         virtual bool lockInsetInInset(BufferView *, UpdatableInset *)
494                 { return false; }
495         ///
496         virtual bool unlockInsetInInset(BufferView *, UpdatableInset *,
497                                         bool /*lr*/ = false)
498                 { return false; }
499         ///  An updatable inset could handle lyx editing commands
500         virtual RESULT localDispatch(FuncRequest const & cmd);
501         ///
502         bool isCursorVisible() const { return cursor_visible_; }
503         ///
504         virtual int getMaxWidth(BufferView * bv, UpdatableInset const *) const;
505         ///
506         int scroll(bool recursive = true) const {
507                 // We need this method to not clobber the real method in Inset
508                 return Inset::scroll(recursive);
509         }
510         ///
511         virtual bool showInsetDialog(BufferView *) const { return false; }
512         ///
513         virtual void nodraw(bool b) const {
514                 block_drawing_ = b;
515         }
516         ///
517         virtual bool nodraw() const {
518                 return block_drawing_;
519         }
520         ///
521         // needed for spellchecking text
522         ///
523         virtual bool allowSpellcheck() { return false; }
524         ///
525         virtual WordLangTuple const
526         selectNextWordToSpellcheck(BufferView *, float & value) const;
527         ///
528         virtual void selectSelectedWord(BufferView *) { return; }
529         ///
530         virtual void toggleSelection(BufferView *, bool /*kill_selection*/) {
531                 return;
532         }
533         ///
534         // needed for search/replace functionality
535         ///
536         virtual bool searchForward(BufferView *, string const &,
537                                    bool = true, bool = false);
538         ///
539         virtual bool searchBackward(BufferView *, string const &,
540                                     bool = true, bool = false);
541
542 protected:
543         ///
544         void toggleCursorVisible() const {
545                 cursor_visible_ = !cursor_visible_;
546         }
547         ///
548         void setCursorVisible(bool b) const {
549                 cursor_visible_ = b;
550         }
551         /// scrolls to absolute position in bufferview-workwidth * sx units
552         void scroll(BufferView *, float sx) const;
553         /// scrolls offset pixels
554         void scroll(BufferView *, int offset) const;
555
556 private:
557         ///
558         mutable bool cursor_visible_;
559         ///
560         mutable bool block_drawing_;
561 };
562
563 inline
564 bool UpdatableInset::checkInsertChar(LyXFont &)
565 {
566         return true;
567 }
568
569 /**
570  * returns true if pointer argument is valid
571  * and points to an editable inset
572  */
573 inline bool isEditableInset(Inset * i)
574 {
575         return i && i->editable();
576 }
577
578 /**
579  * returns true if pointer argument is valid
580  * and points to a highly editable inset
581  */
582 inline bool isHighlyEditableInset(Inset * i)
583 {
584         return i && i->editable() == Inset::HIGHLY_EDITABLE;
585 }
586
587 #endif