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