4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
16 #include "dimension.h"
18 #include "support/docstream.h"
43 namespace graphics { class PreviewLoader; }
46 /// Common base class to all insets
48 // Do not add _any_ (non-static) data members as this would inflate
49 // everything storing large quantities of insets. Mathed e.g. would
55 typedef ptrdiff_t difference_type;
56 /// short of anything else reasonable
57 typedef size_t size_type;
58 /// type for cell indices
59 typedef size_t idx_type;
60 /// type for cursor positions
61 typedef ptrdiff_t pos_type;
62 /// type for row numbers
63 typedef size_t row_type;
64 /// type for column numbers
65 typedef size_t col_type;
67 /// virtual base class destructor
68 virtual ~InsetBase() {}
69 /// replicate ourselves
70 std::auto_ptr<InsetBase> clone() const;
72 /// identification as math inset
73 virtual InsetMath * asInsetMath() { return 0; }
74 /// true for 'math' math inset, but not for e.g. mbox
75 virtual bool inMathed() const { return false; }
77 /// the real dispatcher
78 void dispatch(LCursor & cur, FuncRequest & cmd);
80 * \returns true if this function made a definitive decision on
81 * whether the inset wants to handle the request \p cmd or not.
82 * The result of this decision is put into \p status.
84 * Every request that is enabled in this method needs to be handled
85 * in doDispatch(). Normally we have a 1:1 relationship between the
86 * requests handled in getStatus() and doDispatch(), but there are
88 * - A request that is disabled in getStatus() does not need to
89 * appear in doDispatch(). It is guaranteed that doDispatch()
90 * is never called with this request.
91 * - A few requests are en- or disabled in InsetBase::getStatus().
92 * These need to be handled in the doDispatch() methods of the
93 * derived insets, since InsetBase::doDispatch() has not enough
94 * information to handle them.
95 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
96 * are dispatched directly
98 virtual bool getStatus(LCursor & cur, FuncRequest const & cmd,
99 FuncStatus & status) const;
102 virtual void edit(LCursor & cur, bool left);
104 virtual InsetBase * editXY(LCursor & cur, int x, int y);
106 /// compute the size of the object returned in dim
107 /// \retval true if metrics changed.
108 virtual bool metrics(MetricsInfo & mi, Dimension & dim) const = 0;
109 /// draw inset and update (xo, yo)-cache
110 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
111 /// draw inset selection if necessary
112 virtual void drawSelection(PainterInfo &, int, int) const {}
114 virtual bool editing(BufferView * bv) const;
116 virtual bool showInsetDialog(BufferView *) const { return false; }
118 /// draw inset decoration if necessary.
119 /// This can use \c drawMarkers() for example.
120 virtual void drawDecoration(PainterInfo &, int, int) const {}
121 /// draw four angular markers
122 void drawMarkers(PainterInfo & pi, int x, int y) const;
123 /// draw two angular markers
124 void drawMarkers2(PainterInfo & pi, int x, int y) const;
125 /// add space for markers
126 void metricsMarkers(Dimension & dim, int framesize = 1) const;
127 /// add space for markers
128 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
129 /// last drawn position for 'important' insets
130 int xo(BufferView const & bv) const;
131 /// last drawn position for 'important' insets
132 int yo(BufferView const & bv) const;
133 /// set x/y drawing position cache if available
134 virtual void setPosCache(PainterInfo const &, int, int) const {}
135 /// do we cover screen position x/y?
136 virtual bool covers(BufferView const & bv, int x, int y) const;
137 /// get the screen positions of the cursor (see note in cursor.C)
138 virtual void cursorPos(BufferView const & bv,
139 CursorSlice const & sl, bool boundary, int & x, int & y) const;
141 /// is this an inset that can be moved into?
142 virtual bool isActive() const { return nargs() > 0; }
143 /// Where should we go when we press the up or down cursor key?
144 virtual bool idxUpDown(LCursor & cur, bool up) const;
145 /// Move one cell to the left
146 virtual bool idxLeft(LCursor &) const { return false; }
147 /// Move one cell to the right
148 virtual bool idxRight(LCursor &) const { return false; }
150 /// Move one physical cell up
151 virtual bool idxNext(LCursor &) const { return false; }
152 /// Move one physical cell down
153 virtual bool idxPrev(LCursor &) const { return false; }
155 /// Target pos when we enter the inset from the left by pressing "Right"
156 virtual bool idxFirst(LCursor &) const { return false; }
157 /// Target pos when we enter the inset from the right by pressing "Left"
158 virtual bool idxLast(LCursor &) const { return false; }
160 /// Delete a cell and move cursor
161 virtual bool idxDelete(idx_type &) { return false; }
162 /// pulls cell after pressing erase
163 virtual void idxGlue(idx_type) {}
164 /// returns list of cell indices that are "between" from and to for
165 /// selection purposes
166 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
168 /// to which column belongs a cell with a given index?
169 virtual col_type col(idx_type) const { return 0; }
170 /// to which row belongs a cell with a given index?
171 virtual row_type row(idx_type) const { return 0; }
172 /// cell idex corresponding to row and column;
173 virtual idx_type index(row_type row, col_type col) const;
174 /// any additional x-offset when drawing a cell?
175 virtual int cellXOffset(idx_type) const { return 0; }
176 /// any additional y-offset when drawing a cell?
177 virtual int cellYOffset(idx_type) const { return 0; }
178 /// number of embedded cells
179 virtual size_t nargs() const { return 0; }
180 /// number of rows in gridlike structures
181 virtual size_t nrows() const { return 0; }
182 /// number of columns in gridlike structures
183 virtual size_t ncols() const { return 0; }
184 /// is called when the cursor leaves this inset
185 // returns true if cursor is now invalid.
186 virtual bool notifyCursorLeaves(LCursor &) { return false; }
187 /// is called when the mouse enter or leave this inset
188 /// return true if this inset needs repaint
189 virtual bool setMouseHover(bool) { return false; }
191 /// request "external features"
192 virtual void validate(LaTeXFeatures &) const {}
193 /// Appends \c list with all labels found within this inset.
194 virtual void getLabelList(Buffer const &,
195 std::vector<docstring> & /* list */) const {}
197 /// describe content if cursor inside
198 virtual void infoize(odocstream &) const {}
199 /// describe content if cursor behind
200 virtual void infoize2(odocstream &) const {}
202 /// plain text output in ucs4 encoding
203 virtual int plaintext(Buffer const &, odocstream &,
204 OutputParams const &) const;
206 virtual int docbook(Buffer const &, odocstream & os,
207 OutputParams const &) const;
208 /// the string that is passed to the TOC
209 virtual void textString(Buffer const &, odocstream &) const {}
211 /** This enum indicates by which means the inset can be modified:
212 - NOT_EDITABLE: the inset's content cannot be modified at all
213 (e.g. printindex, insetspecialchar)
214 - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, url)
215 - HIGHLY_EDITABLE: content can be edited on screen (normally means that
216 insettext is contained, e.g. collapsables, tabular) */
217 // FIXME: This has not yet been fully implemented to math insets
226 /// what appears in the minibuffer when opening
227 virtual docstring const editMessage() const;
229 virtual EDITABLE editable() const;
230 /// can we go further down on mouse click?
231 virtual bool descendable() const { return false; }
232 /// does this contain text that can be change track marked in DVI?
233 virtual bool canTrackChanges() const { return false; }
234 /// is this inset based on the TextInset class?
235 virtual InsetText const * asTextInset() const { return 0; }
236 /// return true if the inset should be removed automatically
237 virtual bool autoDelete() const;
239 /** This is not quite the correct place for this enum. I think
240 the correct would be to let each subclass of Inset declare
241 its own enum code. Actually the notion of an InsetBase::Code
242 should be avoided, but I am not sure how this could be done
248 TOC_CODE, // do these insets really need a code? (ale)
308 MATHMACRO_CODE, // 30
341 /** returns the Code corresponding to the \c name.
342 * Eg, translate("branch") == BRANCH_CODE
344 static Code translate(std::string const & name);
346 /// returns true if the inset can hold an inset of given type
347 virtual bool insetAllowed(Code) const { return false; }
348 /// if this inset has paragraphs should they be output all as default
349 /// paragraphs with the default layout of the text class?
350 virtual bool forceDefaultParagraphs(idx_type) const { return false; }
353 virtual docstring const & getInsetName() const;
354 /// used to toggle insets
355 /// is the inset open?
356 /// should this inset be handled like a normal charater
357 virtual bool isChar() const { return false; }
358 /// is this equivalent to a letter?
359 virtual bool isLetter() const { return false; }
360 /// is this equivalent to a space (which is BTW different from
361 /// a line separator)?
362 virtual bool isSpace() const { return false; }
363 /// should we have a non-filled line before this inset?
364 virtual bool display() const { return false; }
365 /// should we break lines after this inset?
366 virtual bool isLineSeparator() const { return false; }
367 /// should paragraph indendation be ommitted in any case?
368 virtual bool neverIndent(Buffer const &) const { return false; }
369 /// dumps content to lyxerr
370 virtual void dump() const;
371 /// write inset in .lyx format
372 virtual void write(Buffer const &, std::ostream &) const {}
373 /// read inset in .lyx format
374 virtual void read(Buffer const &, LyXLex &) {}
375 /** Export the inset to LaTeX.
376 * Don't use a temporary stringstream if the final output is
377 * supposed to go to a file.
378 * \sa Buffer::writeLaTeXSource for the reason.
379 * \return the number of rows (\n's) of generated LaTeX code.
381 virtual int latex(Buffer const &, odocstream &,
382 OutputParams const &) const { return 0; }
383 /// returns true to override begin and end inset in file
384 virtual bool directWrite() const;
386 virtual bool allowSpellCheck() const { return false; }
388 /// if this insets owns text cells (e.g. InsetText) return cell num
389 virtual LyXText * getText(int /*num*/) const { return 0; }
391 /** Adds a LaTeX snippet to the Preview Loader for transformation
392 * into a bitmap image. Does not start the laoding process.
394 * Most insets have no interest in this capability, so the method
397 virtual void addPreview(graphics::PreviewLoader &) const {}
398 /// Add an entry to the TocList
399 virtual void addToToc(TocList &, Buffer const &) const {}
402 /// returns LyX code associated with the inset. Used for TOC, ...)
403 virtual Code lyxCode() const { return NO_CODE; }
405 /// -1: text mode, 1: math mode, 0 undecided
406 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
407 /// return text or mathmode if that is possible to determine
408 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
409 /// returns whether this inset is allowed in other insets of given mode
410 virtual bool allowedIn(mode_type) const { return true; }
412 * Is this inset allowed within a font change?
414 * FIXME: noFontChange means currently that the font change is closed
415 * in LaTeX before the inset, and that the contents of the inset
416 * will be in default font. This should be changed so that the inset
417 * changes the font again.
419 virtual bool noFontChange() const { return false; }
421 /// set the change for the entire inset
422 virtual void setChange(Change const &) {}
423 /// accept the changes within the inset
424 virtual void acceptChanges() {};
425 /// reject the changes within the inset
426 virtual void rejectChanges() {};
429 virtual int width() const { return 10; }
431 virtual int ascent() const { return 10; }
433 virtual int descent() const { return 10; }
435 int scroll() const { return 0; }
437 enum CollapseStatus {
443 virtual void setStatus(LCursor &, CollapseStatus) {}
446 InsetBase(InsetBase const &) {}
447 /** The real dispatcher.
448 * Gets normally called from LCursor::dispatch(). LCursor::dispatch()
449 * assumes the common case of 'LFUN handled, need update'.
450 * This has to be overriden by calling LCursor::undispatched() or
451 * LCursor::noUpdate() if appropriate.
452 * If you need to call the dispatch method of some inset directly
453 * you may have to explicitly request an update at that place. Don't
454 * do it in doDispatch(), since that causes nested updates when
455 * called from LCursor::dispatch(), and these can lead to crashes.
458 virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
460 /// Cached dimensions of the inset.
461 mutable Dimension dim_;
463 virtual std::auto_ptr<InsetBase> doClone() const = 0;
468 * returns true if pointer argument is valid
469 * and points to an editable inset
471 bool isEditableInset(InsetBase const * inset);
475 * returns true if pointer argument is valid
476 * and points to a highly editable inset
478 bool isHighlyEditableInset(InsetBase const * inset);