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