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.
15 #include "support/docstream.h"
36 namespace lyx { namespace graphics { class PreviewLoader; } }
40 /// Common base class to all insets
42 // Do not add _any_ (non-static) data members as this would inflate
43 // everything storing large quantities of insets. Mathed e.g. would
49 typedef ptrdiff_t difference_type;
50 /// short of anything else reasonable
51 typedef size_t size_type;
52 /// type for cell indices
53 typedef size_t idx_type;
54 /// type for cursor positions
55 typedef ptrdiff_t pos_type;
56 /// type for row numbers
57 typedef size_t row_type;
58 /// type for column numbers
59 typedef size_t col_type;
61 /// virtual base class destructor
62 virtual ~InsetBase() {}
63 /// replicate ourselves
64 std::auto_ptr<InsetBase> clone() const;
66 /// identification as math inset
67 virtual InsetMath * asInsetMath() { return 0; }
68 /// true for 'math' math inset, but not for e.g. mbox
69 virtual bool inMathed() const { return false; }
71 /// the real dispatcher
72 void dispatch(LCursor & cur, FuncRequest & cmd);
74 * \returns true if this function made a definitive decision on
75 * whether the inset wants to handle the request \p cmd or not.
76 * The result of this decision is put into \p status.
78 * Every request that is enabled in this method needs to be handled
79 * in doDispatch(). Normally we have a 1:1 relationship between the
80 * requests handled in getStatus() and doDispatch(), but there are
82 * - A request that is disabled in getStatus() does not need to
83 * appear in doDispatch(). It is guaranteed that doDispatch()
84 * is never called with this request.
85 * - A few requests are en- or disabled in InsetBase::getStatus().
86 * These need to be handled in the doDispatch() methods of the
87 * derived insets, since InsetBase::doDispatch() has not enough
88 * information to handle them.
89 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
90 * are dispatched directly
92 virtual bool getStatus(LCursor & cur, FuncRequest const & cmd,
93 FuncStatus & status) const;
96 virtual void edit(LCursor & cur, bool left);
98 virtual InsetBase * editXY(LCursor & cur, int x, int y);
100 /// compute the size of the object returned in dim
101 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
102 /// draw inset and update (xo, yo)-cache
103 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
104 /// draw inset selection if necessary
105 virtual void drawSelection(PainterInfo &, int, int) const {}
107 virtual bool editing(BufferView * bv) const;
109 virtual bool showInsetDialog(BufferView *) const { return false; }
110 /// draw four angular markers
111 void drawMarkers(PainterInfo & pi, int x, int y) const;
112 /// draw two angular markers
113 void drawMarkers2(PainterInfo & pi, int x, int y) const;
114 /// add space for markers
115 void metricsMarkers(Dimension & dim, int framesize = 1) const;
116 /// add space for markers
117 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
118 /// last drawn position for 'important' insets
119 int xo(BufferView & bv) const;
120 /// last drawn position for 'important' insets
121 int yo(BufferView & bv) const;
122 /// set x/y drawing position cache if available
123 virtual void setPosCache(PainterInfo const &, int, int) const {}
124 /// do we cover screen position x/y?
125 virtual bool covers(BufferView & bv, int x, int y) const;
126 /// get the screen positions of the cursor (see note in cursor.C)
127 virtual void cursorPos(BufferView const & bv,
128 CursorSlice const & sl, bool boundary, int & x, int & y) const;
130 /// is this an inset that can be moved into?
131 virtual bool isActive() const { return nargs() > 0; }
132 /// Where should we go when we press the up or down cursor key?
133 virtual bool idxUpDown(LCursor & cur, bool up) const;
134 /// Move one cell to the left
135 virtual bool idxLeft(LCursor &) const { return false; }
136 /// Move one cell to the right
137 virtual bool idxRight(LCursor &) const { return false; }
139 /// Move one physical cell up
140 virtual bool idxNext(LCursor &) const { return false; }
141 /// Move one physical cell down
142 virtual bool idxPrev(LCursor &) const { return false; }
144 /// Target pos when we enter the inset from the left by pressing "Right"
145 virtual bool idxFirst(LCursor &) const { return false; }
146 /// Target pos when we enter the inset from the right by pressing "Left"
147 virtual bool idxLast(LCursor &) const { return false; }
149 /// Delete a cell and move cursor
150 virtual bool idxDelete(idx_type &) { return false; }
151 /// pulls cell after pressing erase
152 virtual void idxGlue(idx_type) {}
153 /// returns list of cell indices that are "between" from and to for
154 /// selection purposes
155 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
157 /// to which column belongs a cell with a given index?
158 virtual col_type col(idx_type) const { return 0; }
159 /// to which row belongs a cell with a given index?
160 virtual row_type row(idx_type) const { return 0; }
161 /// cell idex corresponding to row and column;
162 virtual idx_type index(row_type row, col_type col) const;
163 /// any additional x-offset when drawing a cell?
164 virtual int cellXOffset(idx_type) const { return 0; }
165 /// any additional y-offset when drawing a cell?
166 virtual int cellYOffset(idx_type) const { return 0; }
167 /// number of embedded cells
168 virtual size_t nargs() const { return 0; }
169 /// number of rows in gridlike structures
170 virtual size_t nrows() const { return 0; }
171 /// number of columns in gridlike structures
172 virtual size_t ncols() const { return 0; }
173 /// is called when the cursor leaves this inset
174 // returns true if cursor is now invalid.
175 virtual bool notifyCursorLeaves(LCursor &) { return false; }
177 /// request "external features"
178 virtual void validate(LaTeXFeatures &) const {}
179 /// Appends \c list with all labels found within this inset.
180 virtual void getLabelList(Buffer const &,
181 std::vector<lyx::docstring> & /* list */) const {}
183 /// describe content if cursor inside
184 virtual void infoize(std::ostream &) const {}
185 /// describe content if cursor behind
186 virtual void infoize2(std::ostream &) const {}
188 /// plain text output in ucs4 encoding
189 virtual int plaintext(Buffer const &, lyx::odocstream &,
190 OutputParams const &) const;
192 virtual int docbook(Buffer const &, std::ostream & os,
193 OutputParams const &) const;
194 /// the string that is passed to the TOC
195 virtual int textString(Buffer const &, lyx::odocstream &,
196 OutputParams const &) const { return 0; };
198 /** This enum indicates by which means the inset can be modified:
199 - NOT_EDITABLE: the inset's content can not be modified at all
200 (e.g. printindex, insetspecialchar)
201 - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, url)
202 - HIGHLY_EDITABLE: content can be edited on screen (normally means that
203 insettext is contained, e.g. collapsables, tabular) */
204 // FIXME: This has not yet been fully implemented to math insets
213 /// what appears in the minibuffer when opening
214 virtual lyx::docstring const editMessage() const;
216 virtual EDITABLE editable() const;
217 /// can we go further down on mouse click?
218 virtual bool descendable() const { return false; }
219 /// does this contain text that can be change track marked in DVI?
220 virtual bool canTrackChanges() const { return false; }
221 /// is this inset based on the TextInset class?
222 virtual InsetText const * asTextInset() const { return 0; }
223 /// return true if the inset should be removed automatically
224 virtual bool autoDelete() const;
226 /** This is not quite the correct place for this enum. I think
227 the correct would be to let each subclass of Inset declare
228 its own enum code. Actually the notion of an InsetBase::Code
229 should be avoided, but I am not sure how this could be done
235 TOC_CODE, // do these insets really need a code? (ale)
295 MATHMACRO_CODE, // 30
324 /** returns the Code corresponding to the \c name.
325 * Eg, translate("branch") == BRANCH_CODE
327 static Code translate(std::string const & name);
329 /// returns true if the inset can hold an inset of given type
330 virtual bool insetAllowed(Code) const { return false; }
331 /// if this inset has paragraphs should they be output all as default
332 /// paragraphs with "Standard" layout?
333 virtual bool forceDefaultParagraphs(idx_type) const { return false; }
336 virtual std::string const & getInsetName() const;
337 /// used to toggle insets
338 /// is the inset open?
339 /// should this inset be handled like a normal charater
340 virtual bool isChar() const { return false; }
341 /// is this equivalent to a letter?
342 virtual bool isLetter() const { return false; }
343 /// is this equivalent to a space (which is BTW different from
344 /// a line separator)?
345 virtual bool isSpace() const { return false; }
346 /// should we have a non-filled line before this inset?
347 virtual bool display() const { return false; }
348 /// should we break lines after this inset?
349 virtual bool isLineSeparator() const { return false; }
350 /// should paragraph indendation be ommitted in any case?
351 virtual bool neverIndent() const { return false; }
352 /// dumps content to lyxerr
353 virtual void dump() const;
354 /// write inset in .lyx format
355 virtual void write(Buffer const &, std::ostream &) const {}
356 /// read inset in .lyx format
357 virtual void read(Buffer const &, LyXLex &) {}
358 /// returns the number of rows (\n's) of generated tex code.
359 virtual int latex(Buffer const &, lyx::odocstream &,
360 OutputParams const &) const { return 0; }
361 /// returns true to override begin and end inset in file
362 virtual bool directWrite() const;
364 virtual bool allowSpellCheck() const { return false; }
366 /// if this insets owns text cells (e.g. InsetText) return cell num
367 virtual LyXText * getText(int /*num*/) const { return 0; }
369 /** Adds a LaTeX snippet to the Preview Loader for transformation
370 * into a bitmap image. Does not start the laoding process.
372 * Most insets have no interest in this capability, so the method
375 virtual void addPreview(lyx::graphics::PreviewLoader &) const {}
377 /// returns LyX code associated with the inset. Used for TOC, ...)
378 virtual Code lyxCode() const { return NO_CODE; }
380 /// -1: text mode, 1: math mode, 0 undecided
381 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
382 /// return text or mathmode if that is possible to determine
383 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
384 /// returns whether this inset is allowed in other insets of given mode
385 virtual bool allowedIn(mode_type) const { return true; }
387 * Is this inset allowed within a font change?
389 * FIXME: noFontChange means currently that the font change is closed
390 * in LaTeX before the inset, and that the contents of the inset
391 * will be in default font. This should be changed so that the inset
392 * changes the font again.
394 virtual bool noFontChange() const { return false; }
396 /// mark the inset as erased or not
397 virtual void markErased(bool erased);
400 virtual int width() const { return 10; }
402 virtual int ascent() const { return 10; }
404 virtual int descent() const { return 10; }
406 int scroll() const { return 0; }
408 enum CollapseStatus {
414 virtual void setStatus(LCursor &, CollapseStatus) {}
417 InsetBase(InsetBase const &);
418 /** The real dispatcher.
419 * Gets normally called from LCursor::dispatch(). LCursor::dispatch()
420 * assumes the common case of 'LFUN handled, need update'.
421 * This has to be overriden by calling LCursor::undispatched() or
422 * LCursor::noUpdate() if appropriate.
423 * If you need to call the dispatch method of some inset directly
424 * you may have to explicitly request an update at that place. Don't
425 * do it in doDispatch(), since that causes nested updates when
426 * called from LCursor::dispatch(), and these can lead to crashes.
429 virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
431 virtual std::auto_ptr<InsetBase> doClone() const = 0;
436 * returns true if pointer argument is valid
437 * and points to an editable inset
439 bool isEditableInset(InsetBase const * inset);
443 * returns true if pointer argument is valid
444 * and points to a highly editable inset
446 bool isHighlyEditableInset(InsetBase const * inset);