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