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