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