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 "ColorCode.h"
19 #include "InsetCode.h"
21 #include "Dimension.h"
23 #include "support/docstream.h"
50 class ParConstIterator;
58 namespace graphics { class PreviewLoader; }
61 /// Common base class to all insets
63 // Do not add _any_ (non-static) data members as this would inflate
64 // everything storing large quantities of insets. Mathed e.g. would
70 typedef ptrdiff_t difference_type;
71 /// short of anything else reasonable
72 typedef size_t size_type;
73 /// type for cell indices
74 typedef size_t idx_type;
75 /// type for cursor positions
76 typedef ptrdiff_t pos_type;
77 /// type for row numbers
78 typedef size_t row_type;
79 /// type for column numbers
80 typedef size_t col_type;
82 /// virtual base class destructor
85 /// identification as math inset
86 virtual InsetMath * asInsetMath() { return 0; }
87 /// true for 'math' math inset, but not for e.g. mbox
88 virtual bool inMathed() const { return false; }
89 /// is this inset based on the TextInset class?
90 virtual InsetText * asTextInset() { return 0; }
91 /// is this inset based on the TextInset class?
92 virtual InsetText const * asTextInset() const { return 0; }
94 /// the real dispatcher
95 void dispatch(Cursor & cur, FuncRequest & cmd);
97 * \returns true if this function made a definitive decision on
98 * whether the inset wants to handle the request \p cmd or not.
99 * The result of this decision is put into \p status.
101 * Every request that is enabled in this method needs to be handled
102 * in doDispatch(). Normally we have a 1:1 relationship between the
103 * requests handled in getStatus() and doDispatch(), but there are
105 * - A request that is disabled in getStatus() does not need to
106 * appear in doDispatch(). It is guaranteed that doDispatch()
107 * is never called with this request.
108 * - A few requests are en- or disabled in Inset::getStatus().
109 * These need to be handled in the doDispatch() methods of the
110 * derived insets, since Inset::doDispatch() has not enough
111 * information to handle them.
112 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
113 * are dispatched directly
115 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
116 FuncStatus & status) const;
119 virtual void edit(Cursor & cur, bool left);
121 virtual Inset * editXY(Cursor & cur, int x, int y);
123 /// compute the size of the object returned in dim
124 /// \retval true if metrics changed.
125 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
126 /// draw inset and update (xo, yo)-cache
127 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
128 /// draw inset selection if necessary
129 virtual void drawSelection(PainterInfo &, int, int) const {}
131 virtual bool editing(BufferView * bv) const;
133 virtual bool showInsetDialog(BufferView *) const { return false; }
135 /// draw inset decoration if necessary.
136 /// This can use \c drawMarkers() for example.
137 virtual void drawDecoration(PainterInfo &, int, int) const {}
138 /// draw four angular markers
139 void drawMarkers(PainterInfo & pi, int x, int y) const;
140 /// draw two angular markers
141 void drawMarkers2(PainterInfo & pi, int x, int y) const;
142 /// add space for markers
143 void metricsMarkers(Dimension & dim, int framesize = 1) const;
144 /// add space for markers
145 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
146 /// last drawn position for 'important' insets
147 int xo(BufferView const & bv) const;
148 /// last drawn position for 'important' insets
149 int yo(BufferView const & bv) const;
150 /// set x/y drawing position cache if available
151 virtual void setPosCache(PainterInfo const &, int, int) const;
153 void setDimCache(MetricsInfo const &, Dimension const &) const;
154 /// do we cover screen position x/y?
155 virtual bool covers(BufferView const & bv, int x, int y) const;
156 /// get the screen positions of the cursor (see note in Cursor.cpp)
157 virtual void cursorPos(BufferView const & bv,
158 CursorSlice const & sl, bool boundary, int & x, int & y) const;
161 virtual bool isFreeSpacing() const { return false; }
163 virtual bool allowEmpty() const { return false; }
165 /// is this an inset that can be moved into?
166 /// FIXME: merge with editable()
167 virtual bool isActive() const { return nargs() > 0; }
168 /// Where should we go when we press the up or down cursor key?
169 virtual bool idxUpDown(Cursor & cur, bool up) const;
170 /// Move one cell to the left
171 virtual bool idxLeft(Cursor &) const { return false; }
172 /// Move one cell to the right
173 virtual bool idxRight(Cursor &) const { return false; }
175 /// Move one physical cell up
176 virtual bool idxNext(Cursor &) const { return false; }
177 /// Move one physical cell down
178 virtual bool idxPrev(Cursor &) const { return false; }
180 /// Target pos when we enter the inset from the left by pressing "Right"
181 virtual bool idxFirst(Cursor &) const { return false; }
182 /// Target pos when we enter the inset from the right by pressing "Left"
183 virtual bool idxLast(Cursor &) const { return false; }
185 /// Delete a cell and move cursor
186 virtual bool idxDelete(idx_type &) { return false; }
187 /// pulls cell after pressing erase
188 virtual void idxGlue(idx_type) {}
189 /// returns list of cell indices that are "between" from and to for
190 /// selection purposes
191 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
193 /// to which column belongs a cell with a given index?
194 virtual col_type col(idx_type) const { return 0; }
195 /// to which row belongs a cell with a given index?
196 virtual row_type row(idx_type) const { return 0; }
197 /// cell idex corresponding to row and column;
198 virtual idx_type index(row_type row, col_type col) const;
199 /// any additional x-offset when drawing a cell?
200 virtual int cellXOffset(idx_type) const { return 0; }
201 /// any additional y-offset when drawing a cell?
202 virtual int cellYOffset(idx_type) const { return 0; }
203 /// number of embedded cells
204 virtual size_t nargs() const { return 0; }
205 /// number of rows in gridlike structures
206 virtual size_t nrows() const { return 0; }
207 /// number of columns in gridlike structures
208 virtual size_t ncols() const { return 0; }
209 /// is called when the cursor leaves this inset
210 // returns true if cursor is now invalid.
211 virtual bool notifyCursorLeaves(Cursor &) { return false; }
212 /// is called when the mouse enter or leave this inset
213 /// return true if this inset needs repaint
214 virtual bool setMouseHover(bool) { return false; }
215 /// return true if this inset is hovered (under mouse)
216 /// This is by now only used by mathed to draw corners
217 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
218 /// Other insets do not have to redefine this function to
219 /// return the correct status of mouseHovered.
220 virtual bool mouseHovered() const { return false; }
222 /// request "external features"
223 virtual void validate(LaTeXFeatures &) const {}
224 /// Appends \c list with all labels found within this inset.
225 virtual void getLabelList(Buffer const &,
226 std::vector<docstring> & /* list */) const {}
228 /// describe content if cursor inside
229 virtual void infoize(odocstream &) const {}
230 /// describe content if cursor behind
231 virtual void infoize2(odocstream &) const {}
234 PLAINTEXT_NEWLINE = 10000
237 /// plain text output in ucs4 encoding
238 /// return the number of characters; in case of multiple lines of
239 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
240 virtual int plaintext(Buffer const &, odocstream &,
241 OutputParams const &) const = 0;
243 virtual int docbook(Buffer const &, odocstream & os,
244 OutputParams const &) const;
245 /// the string that is passed to the TOC
246 virtual void textString(Buffer const &, odocstream &) const {}
248 /** This enum indicates by which means the inset can be modified:
249 - NOT_EDITABLE: the inset's content cannot be modified at all
250 (e.g. printindex, insetspecialchar)
251 - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, href)
252 - HIGHLY_EDITABLE: content can be edited on screen (normally means that
253 insettext is contained, e.g. collapsables, tabular) */
254 // FIXME: This has not yet been fully implemented to math insets
263 /// what appears in the minibuffer when opening
264 virtual docstring const editMessage() const;
266 virtual EDITABLE editable() const;
267 /// can we go further down on mouse click?
268 virtual bool descendable() const { return false; }
269 /// does this contain text that can be change track marked in DVI?
270 virtual bool canTrackChanges() const { return false; }
271 /// return true if the inset should be removed automatically
272 virtual bool autoDelete() const;
274 /// returns true if the inset can hold an inset of given type
275 virtual bool insetAllowed(InsetCode) const { return false; }
276 /// if this inset has paragraphs should they be output all as default
277 /// paragraphs with the default layout of the text class?
278 virtual bool forceDefaultParagraphs(idx_type) const { return false; }
279 /// Is the width forced to some value?
280 virtual bool hasFixedWidth() const { return false; }
283 virtual docstring name() const { return from_ascii("unknown"); }
285 virtual InsetLayout const & getLayout(BufferParams const & bp) const;
286 /// used to toggle insets
287 /// is the inset open?
288 /// should this inset be handled like a normal charater
289 virtual bool isChar() const { return false; }
290 /// is this equivalent to a letter?
291 virtual bool isLetter() const { return false; }
292 /// is this equivalent to a space (which is BTW different from
293 /// a line separator)?
294 virtual bool isSpace() const { return false; }
303 /// should we have a non-filled line before this inset?
304 virtual DisplayType display() const { return Inline; }
305 /// should we break lines after this inset?
306 virtual bool isLineSeparator() const { return false; }
307 /// should paragraph indendation be ommitted in any case?
308 virtual bool neverIndent(Buffer const &) const { return false; }
309 /// dumps content to lyxerr
310 virtual void dump() const;
311 /// write inset in .lyx format
312 virtual void write(Buffer const &, std::ostream &) const {}
313 /// read inset in .lyx format
314 virtual void read(Buffer const &, Lexer &) {}
315 /** Export the inset to LaTeX.
316 * Don't use a temporary stringstream if the final output is
317 * supposed to go to a file.
318 * \sa Buffer::writeLaTeXSource for the reason.
319 * \return the number of rows (\n's) of generated LaTeX code.
321 virtual int latex(Buffer const &, odocstream &,
322 OutputParams const &) const { return 0; }
323 /// returns true to override begin and end inset in file
324 virtual bool directWrite() const;
326 virtual bool allowSpellCheck() const { return false; }
328 /// if this insets owns text cells (e.g. InsetText) return cell num
329 virtual Text * getText(int /*num*/) const { return 0; }
331 /** Adds a LaTeX snippet to the Preview Loader for transformation
332 * into a bitmap image. Does not start the laoding process.
334 * Most insets have no interest in this capability, so the method
337 virtual void addPreview(graphics::PreviewLoader &) const {}
338 /// Add an entry to the TocList
339 /// pit is the ParConstIterator of the paragraph containing the inset
340 virtual void addToToc(TocList &, Buffer const &, ParConstIterator const &) const {}
341 /// report files that can be embedded with the lyx file
342 virtual void registerEmbeddedFiles(Buffer const &, EmbeddedFiles &) const {};
343 /// use embedded or external file after the embedding status of a file is changed
344 virtual void updateEmbeddedFile(Buffer const &, EmbeddedFile const &) {}
345 /// Fill keys with BibTeX information
346 virtual void fillWithBibKeys(Buffer const &,
347 BiblioInfo &, InsetIterator const &) const { return; }
348 /// Update the counters of this inset and of its contents
349 virtual void updateLabels(Buffer const &, ParIterator const &) {}
353 /// returns LyX code associated with the inset. Used for TOC, ...)
354 virtual InsetCode lyxCode() const { return NO_CODE; }
356 /// -1: text mode, 1: math mode, 0 undecided
357 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
358 /// return text or mathmode if that is possible to determine
359 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
360 /// returns whether this inset is allowed in other insets of given mode
361 virtual bool allowedIn(mode_type) const { return true; }
363 * Is this inset allowed within a font change?
365 * FIXME: noFontChange means currently that the font change is closed
366 * in LaTeX before the inset, and that the contents of the inset
367 * will be in default font. This should be changed so that the inset
368 * changes the font again.
370 virtual bool noFontChange() const { return false; }
372 /// set the change for the entire inset
373 virtual void setChange(Change const &) {}
374 /// accept the changes within the inset
375 virtual void acceptChanges(BufferParams const &) {};
376 /// reject the changes within the inset
377 virtual void rejectChanges(BufferParams const &) {};
380 virtual Dimension const dimension(BufferView const &) const;
382 int scroll() const { return 0; }
384 virtual ColorCode backgroundColor() const;
386 enum CollapseStatus {
391 virtual void setStatus(Cursor &, CollapseStatus) {}
393 enum { TEXT_TO_INSET_OFFSET = 4 };
398 /// replicate ourselves
399 friend class InsetList;
400 friend class MathAtom;
401 virtual Inset * clone() const = 0;
403 /** The real dispatcher.
404 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
405 * assumes the common case of 'LFUN handled, need update'.
406 * This has to be overriden by calling Cursor::undispatched() or
407 * Cursor::noUpdate() if appropriate.
408 * If you need to call the dispatch method of some inset directly
409 * you may have to explicitly request an update at that place. Don't
410 * do it in doDispatch(), since that causes nested updates when
411 * called from Cursor::dispatch(), and these can lead to crashes.
414 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);