4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Alejandro Aguilar Sierra
9 * \author Lars Gullik Bjønnes
10 * \author Matthias Ettrich
12 * Full author contact details are available in file CREDITS.
18 #include "Dimension.h"
20 #include "support/docstream.h"
48 class ParConstIterator;
55 namespace graphics { class PreviewLoader; }
58 /// Common base class to all insets
60 // Do not add _any_ (non-static) data members as this would inflate
61 // everything storing large quantities of insets. Mathed e.g. would
67 typedef ptrdiff_t difference_type;
68 /// short of anything else reasonable
69 typedef size_t size_type;
70 /// type for cell indices
71 typedef size_t idx_type;
72 /// type for cursor positions
73 typedef ptrdiff_t pos_type;
74 /// type for row numbers
75 typedef size_t row_type;
76 /// type for column numbers
77 typedef size_t col_type;
79 /// virtual base class destructor
82 /// identification as math inset
83 virtual InsetMath * asInsetMath() { return 0; }
84 /// true for 'math' math inset, but not for e.g. mbox
85 virtual bool inMathed() const { return false; }
86 /// is this inset based on the TextInset class?
87 virtual InsetText * asTextInset() { return 0; }
88 /// is this inset based on the TextInset class?
89 virtual InsetText const * asTextInset() const { return 0; }
91 /// the real dispatcher
92 void dispatch(Cursor & cur, FuncRequest & cmd);
94 * \returns true if this function made a definitive decision on
95 * whether the inset wants to handle the request \p cmd or not.
96 * The result of this decision is put into \p status.
98 * Every request that is enabled in this method needs to be handled
99 * in doDispatch(). Normally we have a 1:1 relationship between the
100 * requests handled in getStatus() and doDispatch(), but there are
102 * - A request that is disabled in getStatus() does not need to
103 * appear in doDispatch(). It is guaranteed that doDispatch()
104 * is never called with this request.
105 * - A few requests are en- or disabled in Inset::getStatus().
106 * These need to be handled in the doDispatch() methods of the
107 * derived insets, since Inset::doDispatch() has not enough
108 * information to handle them.
109 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
110 * are dispatched directly
112 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
113 FuncStatus & status) const;
116 virtual void edit(Cursor & cur, bool left);
118 virtual Inset * editXY(Cursor & cur, int x, int y);
120 /// compute the size of the object returned in dim
121 /// \retval true if metrics changed.
122 virtual bool metrics(MetricsInfo & mi, Dimension & dim) const = 0;
123 /// draw inset and update (xo, yo)-cache
124 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
125 /// draw inset selection if necessary
126 virtual void drawSelection(PainterInfo &, int, int) const {}
128 virtual bool editing(BufferView * bv) const;
130 virtual bool showInsetDialog(BufferView *) const { return false; }
132 /// draw inset decoration if necessary.
133 /// This can use \c drawMarkers() for example.
134 virtual void drawDecoration(PainterInfo &, int, int) const {}
135 /// draw four angular markers
136 void drawMarkers(PainterInfo & pi, int x, int y) const;
137 /// draw two angular markers
138 void drawMarkers2(PainterInfo & pi, int x, int y) const;
139 /// add space for markers
140 void metricsMarkers(Dimension & dim, int framesize = 1) const;
141 /// add space for markers
142 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
143 /// last drawn position for 'important' insets
144 int xo(BufferView const & bv) const;
145 /// last drawn position for 'important' insets
146 int yo(BufferView const & bv) const;
147 /// set x/y drawing position cache if available
148 virtual void setPosCache(PainterInfo const &, int, int) const;
149 /// do we cover screen position x/y?
150 virtual bool covers(BufferView const & bv, int x, int y) const;
151 /// get the screen positions of the cursor (see note in Cursor.cpp)
152 virtual void cursorPos(BufferView const & bv,
153 CursorSlice const & sl, bool boundary, int & x, int & y) const;
155 /// is this an inset that can be moved into?
156 /// FIXME: merge with editable()
157 virtual bool isActive() const { return nargs() > 0; }
158 /// Where should we go when we press the up or down cursor key?
159 virtual bool idxUpDown(Cursor & cur, bool up) const;
160 /// Move one cell to the left
161 virtual bool idxLeft(Cursor &) const { return false; }
162 /// Move one cell to the right
163 virtual bool idxRight(Cursor &) const { return false; }
165 /// Move one physical cell up
166 virtual bool idxNext(Cursor &) const { return false; }
167 /// Move one physical cell down
168 virtual bool idxPrev(Cursor &) const { return false; }
170 /// Target pos when we enter the inset from the left by pressing "Right"
171 virtual bool idxFirst(Cursor &) const { return false; }
172 /// Target pos when we enter the inset from the right by pressing "Left"
173 virtual bool idxLast(Cursor &) const { return false; }
175 /// Delete a cell and move cursor
176 virtual bool idxDelete(idx_type &) { return false; }
177 /// pulls cell after pressing erase
178 virtual void idxGlue(idx_type) {}
179 /// returns list of cell indices that are "between" from and to for
180 /// selection purposes
181 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
183 /// to which column belongs a cell with a given index?
184 virtual col_type col(idx_type) const { return 0; }
185 /// to which row belongs a cell with a given index?
186 virtual row_type row(idx_type) const { return 0; }
187 /// cell idex corresponding to row and column;
188 virtual idx_type index(row_type row, col_type col) const;
189 /// any additional x-offset when drawing a cell?
190 virtual int cellXOffset(idx_type) const { return 0; }
191 /// any additional y-offset when drawing a cell?
192 virtual int cellYOffset(idx_type) const { return 0; }
193 /// number of embedded cells
194 virtual size_t nargs() const { return 0; }
195 /// number of rows in gridlike structures
196 virtual size_t nrows() const { return 0; }
197 /// number of columns in gridlike structures
198 virtual size_t ncols() const { return 0; }
199 /// is called when the cursor leaves this inset
200 // returns true if cursor is now invalid.
201 virtual bool notifyCursorLeaves(Cursor &) { return false; }
202 /// is called when the mouse enter or leave this inset
203 /// return true if this inset needs repaint
204 virtual bool setMouseHover(bool) { return false; }
205 /// return true if this inset is hovered (under mouse)
206 /// This is by now only used by mathed to draw corners
207 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
208 /// Other insets do not have to redefine this function to
209 /// return the correct status of mouseHovered.
210 virtual bool mouseHovered() const { return false; }
212 /// request "external features"
213 virtual void validate(LaTeXFeatures &) const {}
214 /// Appends \c list with all labels found within this inset.
215 virtual void getLabelList(Buffer const &,
216 std::vector<docstring> & /* list */) const {}
218 /// describe content if cursor inside
219 virtual void infoize(odocstream &) const {}
220 /// describe content if cursor behind
221 virtual void infoize2(odocstream &) const {}
224 PLAINTEXT_NEWLINE = 10000
227 /// plain text output in ucs4 encoding
228 /// return the number of characters; in case of multiple lines of
229 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
230 virtual int plaintext(Buffer const &, odocstream &,
231 OutputParams const &) const = 0;
233 virtual int docbook(Buffer const &, odocstream & os,
234 OutputParams const &) const;
235 /// the string that is passed to the TOC
236 virtual void textString(Buffer const &, odocstream &) const {}
238 /** This enum indicates by which means the inset can be modified:
239 - NOT_EDITABLE: the inset's content cannot be modified at all
240 (e.g. printindex, insetspecialchar)
241 - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, url)
242 - HIGHLY_EDITABLE: content can be edited on screen (normally means that
243 insettext is contained, e.g. collapsables, tabular) */
244 // FIXME: This has not yet been fully implemented to math insets
253 /// what appears in the minibuffer when opening
254 virtual docstring const editMessage() const;
256 virtual EDITABLE editable() const;
257 /// can we go further down on mouse click?
258 virtual bool descendable() const { return false; }
259 /// does this contain text that can be change track marked in DVI?
260 virtual bool canTrackChanges() const { return false; }
261 /// return true if the inset should be removed automatically
262 virtual bool autoDelete() const;
264 /** This is not quite the correct place for this enum. I think
265 the correct would be to let each subclass of Inset declare
266 its own enum code. Actually the notion of an Inset::Code
267 should be avoided, but I am not sure how this could be done
273 TOC_CODE, // do these insets really need a code? (ale)
333 MATHMACRO_CODE, // 30
370 /** returns the Code corresponding to the \c name.
371 * Eg, translate("branch") == BRANCH_CODE
373 static Code translate(std::string const & name);
375 /// returns true if the inset can hold an inset of given type
376 virtual bool insetAllowed(Code) const { return false; }
377 /// if this inset has paragraphs should they be output all as default
378 /// paragraphs with the default layout of the text class?
379 virtual bool forceDefaultParagraphs(idx_type) const { return false; }
380 /// Is the width forced to some value?
381 virtual bool hasFixedWidth() const { return false; }
384 virtual docstring name() const { return from_ascii("unknown"); }
386 virtual InsetLayout const & getLayout(BufferParams const & bp) const;
387 /// used to toggle insets
388 /// is the inset open?
389 /// should this inset be handled like a normal charater
390 virtual bool isChar() const { return false; }
391 /// is this equivalent to a letter?
392 virtual bool isLetter() const { return false; }
393 /// is this equivalent to a space (which is BTW different from
394 /// a line separator)?
395 virtual bool isSpace() const { return false; }
404 /// should we have a non-filled line before this inset?
405 virtual DisplayType display() const { return Inline; }
406 /// should we break lines after this inset?
407 virtual bool isLineSeparator() const { return false; }
408 /// should paragraph indendation be ommitted in any case?
409 virtual bool neverIndent(Buffer const &) const { return false; }
410 /// dumps content to lyxerr
411 virtual void dump() const;
412 /// write inset in .lyx format
413 virtual void write(Buffer const &, std::ostream &) const {}
414 /// read inset in .lyx format
415 virtual void read(Buffer const &, Lexer &) {}
416 /** Export the inset to LaTeX.
417 * Don't use a temporary stringstream if the final output is
418 * supposed to go to a file.
419 * \sa Buffer::writeLaTeXSource for the reason.
420 * \return the number of rows (\n's) of generated LaTeX code.
422 virtual int latex(Buffer const &, odocstream &,
423 OutputParams const &) const { return 0; }
424 /// returns true to override begin and end inset in file
425 virtual bool directWrite() const;
427 virtual bool allowSpellCheck() const { return false; }
429 /// if this insets owns text cells (e.g. InsetText) return cell num
430 virtual Text * getText(int /*num*/) const { return 0; }
432 /** Adds a LaTeX snippet to the Preview Loader for transformation
433 * into a bitmap image. Does not start the laoding process.
435 * Most insets have no interest in this capability, so the method
438 virtual void addPreview(graphics::PreviewLoader &) const {}
439 /// Add an entry to the TocList
440 /// pit is the ParConstIterator of the paragraph containing the inset
441 virtual void addToToc(TocList &, Buffer const &, ParConstIterator const &) const {}
442 /// report files that can be embedded with the lyx file
443 virtual void registerEmbeddedFiles(Buffer const &, EmbeddedFiles &,
444 ParConstIterator const &) const {};
445 /// Fill keys with BibTeX information
446 virtual void fillWithBibKeys(Buffer const &,
447 BiblioInfo &, InsetIterator const &) const { return; }
448 /// Update the counters of this inset and of its contents
449 virtual void updateLabels(Buffer const &, ParIterator const &) {}
453 /// returns LyX code associated with the inset. Used for TOC, ...)
454 virtual Code lyxCode() const { return NO_CODE; }
456 /// -1: text mode, 1: math mode, 0 undecided
457 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
458 /// return text or mathmode if that is possible to determine
459 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
460 /// returns whether this inset is allowed in other insets of given mode
461 virtual bool allowedIn(mode_type) const { return true; }
463 * Is this inset allowed within a font change?
465 * FIXME: noFontChange means currently that the font change is closed
466 * in LaTeX before the inset, and that the contents of the inset
467 * will be in default font. This should be changed so that the inset
468 * changes the font again.
470 virtual bool noFontChange() const { return false; }
472 /// set the change for the entire inset
473 virtual void setChange(Change const &) {}
474 /// accept the changes within the inset
475 virtual void acceptChanges(BufferParams const &) {};
476 /// reject the changes within the inset
477 virtual void rejectChanges(BufferParams const &) {};
479 /// FIXME: move dim_ out of Inset!
480 Dimension const & dimension() { return dim_; }
482 int width() const { return dim_.wid; }
484 int ascent() const { return dim_.asc; }
486 int descent() const { return dim_.des; }
488 int scroll() const { return 0; }
490 virtual Color_color backgroundColor() const;
492 enum CollapseStatus {
497 virtual void setStatus(Cursor &, CollapseStatus) {}
499 enum { TEXT_TO_INSET_OFFSET = 4 };
504 /// replicate ourselves
505 friend class Paragraph;
506 friend class MathAtom;
507 virtual Inset * clone() const = 0;
509 /** The real dispatcher.
510 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
511 * assumes the common case of 'LFUN handled, need update'.
512 * This has to be overriden by calling Cursor::undispatched() or
513 * Cursor::noUpdate() if appropriate.
514 * If you need to call the dispatch method of some inset directly
515 * you may have to explicitly request an update at that place. Don't
516 * do it in doDispatch(), since that causes nested updates when
517 * called from Cursor::dispatch(), and these can lead to crashes.
520 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);
522 /// Cached dimensions of the inset.
523 mutable Dimension dim_;
528 * returns true if pointer argument is valid
529 * and points to an editable inset
531 bool isEditableInset(Inset const * inset);
535 * returns true if pointer argument is valid
536 * and points to a highly editable inset
538 bool isHighlyEditableInset(Inset const * inset);
540 /** \c Inset_code is a wrapper for Inset::Code.
541 * It can be forward-declared and passed as a function argument without
542 * having to expose Inset.h.
547 Inset_code(Inset::Code val) : val_(val) {}
548 operator Inset::Code() const { return val_; }