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 "support/strfwd.h"
23 #include <boost/shared_ptr.hpp>
40 class InsetCollapsable;
52 class ParConstIterator;
57 class EmbeddedFileList;
60 namespace graphics { class PreviewLoader; }
63 /// Common base class to all insets
65 // Do not add _any_ (non-static) data members as this would inflate
66 // everything storing large quantities of insets. Mathed e.g. would
73 ENTRY_DIRECTION_IGNORE,
74 ENTRY_DIRECTION_RIGHT,
78 typedef ptrdiff_t difference_type;
79 /// short of anything else reasonable
80 typedef size_t size_type;
81 /// type for cell indices
82 typedef size_t idx_type;
83 /// type for cursor positions
84 typedef ptrdiff_t pos_type;
85 /// type for row numbers
86 typedef size_t row_type;
87 /// type for column numbers
88 typedef size_t col_type;
90 /// virtual base class destructor
93 /// change associated Buffer
94 virtual void setBuffer(Buffer & buffer);
95 /// retrieve associated Buffer
96 virtual Buffer & buffer();
97 virtual Buffer const & buffer() const;
99 /// identification as math inset
100 virtual InsetMath * asInsetMath() { return 0; }
101 /// true for 'math' math inset, but not for e.g. mbox
102 virtual bool inMathed() const { return false; }
103 /// is this inset based on the InsetText class?
104 virtual InsetText * asInsetText() { return 0; }
105 /// is this inset based on the InsetText class?
106 virtual InsetText const * asInsetText() const { return 0; }
107 /// is this inset based on the InsetCollapsable class?
108 virtual InsetCollapsable * asInsetCollapsable() { return 0; }
109 /// is this inset based on the InsetCollapsable class?
110 virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
112 /// the real dispatcher
113 void dispatch(Cursor & cur, FuncRequest & cmd);
115 * \returns true if this function made a definitive decision on
116 * whether the inset wants to handle the request \p cmd or not.
117 * The result of this decision is put into \p status.
119 * Every request that is enabled in this method needs to be handled
120 * in doDispatch(). Normally we have a 1:1 relationship between the
121 * requests handled in getStatus() and doDispatch(), but there are
123 * - A request that is disabled in getStatus() does not need to
124 * appear in doDispatch(). It is guaranteed that doDispatch()
125 * is never called with this request.
126 * - A few requests are en- or disabled in Inset::getStatus().
127 * These need to be handled in the doDispatch() methods of the
128 * derived insets, since Inset::doDispatch() has not enough
129 * information to handle them.
130 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
131 * are dispatched directly
133 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
134 FuncStatus & status) const;
137 virtual void edit(Cursor & cur, bool front,
138 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
140 virtual Inset * editXY(Cursor & cur, int x, int y);
142 /// compute the size of the object returned in dim
143 /// \retval true if metrics changed.
144 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
145 /// draw inset and update (xo, yo)-cache
146 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
147 /// draw inset selection if necessary
148 virtual void drawSelection(PainterInfo &, int, int) const {}
150 virtual bool editing(BufferView const * bv) const;
152 virtual bool showInsetDialog(BufferView *) const { return false; }
154 /// draw inset decoration if necessary.
155 /// This can use \c drawMarkers() for example.
156 virtual void drawDecoration(PainterInfo &, int, int) const {}
157 /// draw four angular markers
158 void drawMarkers(PainterInfo & pi, int x, int y) const;
159 /// draw two angular markers
160 void drawMarkers2(PainterInfo & pi, int x, int y) const;
161 /// add space for markers
162 void metricsMarkers(Dimension & dim, int framesize = 1) const;
163 /// add space for markers
164 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
165 /// last drawn position for 'important' insets
166 int xo(BufferView const & bv) const;
167 /// last drawn position for 'important' insets
168 int yo(BufferView const & bv) const;
169 /// set x/y drawing position cache if available
170 virtual void setPosCache(PainterInfo const &, int, int) const;
172 void setDimCache(MetricsInfo const &, Dimension const &) const;
173 /// do we cover screen position x/y?
174 virtual bool covers(BufferView const & bv, int x, int y) const;
175 /// get the screen positions of the cursor (see note in Cursor.cpp)
176 virtual void cursorPos(BufferView const & bv,
177 CursorSlice const & sl, bool boundary, int & x, int & y) const;
180 virtual bool isFreeSpacing() const { return false; }
182 virtual bool allowEmpty() const { return false; }
183 /// Force inset into LTR environment if surroundings are RTL?
184 virtual bool forceLTR() const { return false; }
186 /// is this an inset that can be moved into?
187 /// FIXME: merge with editable()
188 virtual bool isActive() const { return nargs() > 0; }
189 /// Where should we go when we press the up or down cursor key?
190 virtual bool idxUpDown(Cursor & cur, bool up) const;
191 /// Move one cell backwards
192 virtual bool idxBackward(Cursor &) const { return false; }
193 /// Move one cell forward
194 virtual bool idxForward(Cursor &) const { return false; }
196 /// Move to the next cell
197 virtual bool idxNext(Cursor &) const { return false; }
198 /// Move to the previous cell
199 virtual bool idxPrev(Cursor &) const { return false; }
201 /// Target pos when we enter the inset while moving forward
202 virtual bool idxFirst(Cursor &) const { return false; }
203 /// Target pos when we enter the inset while moving backwards
204 virtual bool idxLast(Cursor &) const { return false; }
206 /// Delete a cell and move cursor
207 virtual bool idxDelete(idx_type &) { return false; }
208 /// pulls cell after pressing erase
209 virtual void idxGlue(idx_type) {}
210 /// returns list of cell indices that are "between" from and to for
211 /// selection purposes
212 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
214 /// to which column belongs a cell with a given index?
215 virtual col_type col(idx_type) const { return 0; }
216 /// to which row belongs a cell with a given index?
217 virtual row_type row(idx_type) const { return 0; }
218 /// cell idex corresponding to row and column;
219 virtual idx_type index(row_type row, col_type col) const;
220 /// any additional x-offset when drawing a cell?
221 virtual int cellXOffset(idx_type) const { return 0; }
222 /// any additional y-offset when drawing a cell?
223 virtual int cellYOffset(idx_type) const { return 0; }
224 /// number of embedded cells
225 virtual size_t nargs() const { return 0; }
226 /// number of rows in gridlike structures
227 virtual size_t nrows() const { return 0; }
228 /// number of columns in gridlike structures
229 virtual size_t ncols() const { return 0; }
230 /// Is called when the cursor leaves this inset.
231 /// Returns true if cursor is now invalid, e.g. if former
232 /// insets in higher cursor slices of \c old do not exist
234 /// \c old is the old cursor, i.e. there is a slice pointing to this.
235 /// \c cur is the new cursor. Use the update flags to cause a redraw.
236 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
238 /// is called when the mouse enter or leave this inset
239 /// return true if this inset needs repaint
240 virtual bool setMouseHover(bool) { return false; }
241 /// return true if this inset is hovered (under mouse)
242 /// This is by now only used by mathed to draw corners
243 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
244 /// Other insets do not have to redefine this function to
245 /// return the correct status of mouseHovered.
246 virtual bool mouseHovered() const { return false; }
248 /// request "external features"
249 virtual void validate(LaTeXFeatures &) const {}
250 /// Appends \c list with all labels found within this inset.
251 virtual void getLabelList(std::vector<docstring> & /* list */) const {}
253 /// describe content if cursor inside
254 virtual void infoize(odocstream &) const {}
255 /// describe content if cursor behind
256 virtual void infoize2(odocstream &) const {}
258 enum { PLAINTEXT_NEWLINE = 10000 };
260 /// plain text output in ucs4 encoding
261 /// return the number of characters; in case of multiple lines of
262 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
263 virtual int plaintext(odocstream &, OutputParams const &) const = 0;
265 virtual int docbook(odocstream & os, OutputParams const &) const;
266 /// the string that is passed to the TOC
267 virtual void textString(odocstream &) const {}
269 /** This enum indicates by which means the inset can be modified:
270 - NOT_EDITABLE: the inset's content cannot be modified at all
271 (e.g. printindex, insetspecialchar)
272 - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, href)
273 - HIGHLY_EDITABLE: content can be edited on screen (normally means that
274 insettext is contained, e.g. collapsables, tabular) */
275 // FIXME: This has not yet been fully implemented to math insets
284 /// what appears in the minibuffer when opening
285 virtual docstring editMessage() const;
287 virtual EDITABLE editable() const;
288 /// can we go further down on mouse click?
289 virtual bool descendable() const { return false; }
290 /// does this contain text that can be change track marked in DVI?
291 virtual bool canTrackChanges() const { return false; }
292 /// return true if the inset should be removed automatically
293 virtual bool autoDelete() const;
295 class CompletionList {
298 virtual ~CompletionList() {}
300 virtual bool sorted() const =0;
302 virtual size_t size() const =0;
303 /// returns the string shown in the gui.
304 virtual docstring const & data(size_t idx) const = 0;
305 /// returns the resource string used to load an icon.
306 virtual std::string icon(size_t /*idx*/) const { return std::string(); }
309 /// Returns true if the inset supports completions.
310 virtual bool completionSupported(Cursor const &) const { return false; }
311 /// Returns true if the inset supports inline completions at the
312 /// cursor position. In this case the completion might be stored
313 /// in the BufferView's inlineCompletion property.
314 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
316 /// Return true if the inline completion should be automatic.
317 virtual bool automaticInlineCompletion() const { return true; }
318 /// Return true if the popup completion should be automatic.
319 virtual bool automaticPopupCompletion() const { return true; }
320 /// Returns completion suggestions at cursor position. Return an
321 /// null pointer if no completion is a available or possible.
322 /// The caller is responsible to free the returned object!
323 virtual CompletionList const * createCompletionList(Cursor const &) const
325 /// Returns the completion prefix to filter the suggestions for completion.
326 /// This is only called if completionList returned a non-null list.
327 virtual docstring completionPrefix(Cursor const &) const
328 { return docstring(); }
329 /// Do a completion at the cursor position. Return true on success.
330 /// The completion does not contain the prefix. If finished is true, the
331 /// completion is final. If finished is false, completion might only be
332 /// a partial completion.
333 virtual bool insertCompletion(Cursor & /*cur*/,
334 docstring const & /*completion*/, bool /*finished*/)
336 /// Get the completion inset position and size
337 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
338 Dimension & /*dim*/) const {}
340 /// returns true if the inset can hold an inset of given type
341 virtual bool insetAllowed(InsetCode) const { return false; }
342 /// should this inset use the empty layout by default rather than
343 /// the standard layout? (default: only if that is forced.)
344 virtual bool useEmptyLayout() const { return forceEmptyLayout(); }
345 /// if this inset has paragraphs should they be forced to use the
347 virtual bool forceEmptyLayout() const { return false; }
348 /// if this inset has paragraphs should the user be allowed to
349 /// customize alignment, etc?
350 virtual bool allowParagraphCustomization(idx_type) const { return true; }
351 /// Is the width forced to some value?
352 virtual bool hasFixedWidth() const { return false; }
354 /// \return Tool tip for this inset.
355 /// This default implementation returns an empty string.
356 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
358 /// \return Context menu identifier for this inset.
359 /// This default implementation returns an empty string.
360 virtual docstring contextMenu(BufferView const & bv, int x, int y) const;
362 // FIXME This should really disappear in favor of
363 // docstring name() const { return from_ascii(insetName(lyxCode()))); }
364 // There's no reason to be using different names in different places.
365 // But to do this we would need to change the file format, since the names
366 // used there don't correspond to what is used here.
368 virtual docstring name() const;
370 virtual InsetLayout const & getLayout(BufferParams const & bp) const;
371 /// used to toggle insets
372 /// is the inset open?
373 /// should this inset be handled like a normal charater
374 virtual bool isChar() const { return false; }
375 /// is this equivalent to a letter?
376 virtual bool isLetter() const { return false; }
377 /// is this equivalent to a space (which is BTW different from
378 /// a line separator)?
379 virtual bool isSpace() const { return false; }
388 /// should we have a non-filled line before this inset?
389 virtual DisplayType display() const { return Inline; }
390 /// should we break lines after this inset?
391 virtual bool isLineSeparator() const { return false; }
392 /// should paragraph indendation be ommitted in any case?
393 virtual bool neverIndent() const { return false; }
394 /// dumps content to lyxerr
395 virtual void dump() const;
396 /// write inset in .lyx format
397 virtual void write(std::ostream &) const {}
398 /// read inset in .lyx format
399 virtual void read(Lexer &) {}
400 /** Export the inset to LaTeX.
401 * Don't use a temporary stringstream if the final output is
402 * supposed to go to a file.
403 * \sa Buffer::writeLaTeXSource for the reason.
404 * \return the number of rows (\n's) of generated LaTeX code.
406 virtual int latex(odocstream &, OutputParams const &) const { return 0; }
407 /// returns true to override begin and end inset in file
408 virtual bool directWrite() const;
410 virtual bool allowSpellCheck() const { return false; }
412 /// if this insets owns text cells (e.g. InsetText) return cell num
413 virtual Text * getText(int /*num*/) const { return 0; }
415 /** Adds a LaTeX snippet to the Preview Loader for transformation
416 * into a bitmap image. Does not start the laoding process.
418 * Most insets have no interest in this capability, so the method
421 virtual void addPreview(graphics::PreviewLoader &) const {}
422 /// Add an entry to the TocList
423 /// pit is the ParConstIterator of the paragraph containing the inset
424 virtual void addToToc(ParConstIterator const &) const {}
425 /// report files that can be embedded with the lyx file
426 virtual void registerEmbeddedFiles(Buffer const &, EmbeddedFileList &) const {}
427 /// use embedded or external file after the embedding status of a file is changed
428 virtual void updateEmbeddedFile(Buffer const &, EmbeddedFile const &) {}
429 /// Fill keys with BibTeX information
430 virtual void fillWithBibKeys(BiblioInfo &, InsetIterator const &) const {}
431 /// Update the counters of this inset and of its contents
432 virtual void updateLabels(ParIterator const &) {}
434 /// Updates the inset's dialog
435 virtual Buffer const * updateFrontend() const;
438 /// returns LyX code associated with the inset. Used for TOC, ...)
439 virtual InsetCode lyxCode() const { return NO_CODE; }
441 /// -1: text mode, 1: math mode, 0 undecided
442 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
443 /// return text or mathmode if that is possible to determine
444 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
445 /// returns whether this inset is allowed in other insets of given mode
446 virtual bool allowedIn(mode_type) const { return true; }
448 * Is this inset allowed within a font change?
450 * FIXME: noFontChange means currently that the font change is closed
451 * in LaTeX before the inset, and that the contents of the inset
452 * will be in default font. This should be changed so that the inset
453 * changes the font again.
455 virtual bool noFontChange() const { return false; }
457 /// set the change for the entire inset
458 virtual void setChange(Change const &) {}
459 /// accept the changes within the inset
460 virtual void acceptChanges(BufferParams const &) {};
461 /// reject the changes within the inset
462 virtual void rejectChanges(BufferParams const &) {};
465 virtual Dimension const dimension(BufferView const &) const;
467 int scroll() const { return 0; }
469 virtual ColorCode backgroundColor() const;
471 enum CollapseStatus {
476 virtual void setStatus(Cursor &, CollapseStatus) {}
478 enum { TEXT_TO_INSET_OFFSET = 4 };
482 explicit Inset() : buffer_(0) {}
484 /// replicate ourselves
485 friend class InsetList;
486 friend class MathAtom;
487 virtual Inset * clone() const = 0;
489 /** The real dispatcher.
490 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
491 * assumes the common case of 'LFUN handled, need update'.
492 * This has to be overriden by calling Cursor::undispatched() or
493 * Cursor::noUpdate() if appropriate.
494 * If you need to call the dispatch method of some inset directly
495 * you may have to explicitly request an update at that place. Don't
496 * do it in doDispatch(), since that causes nested updates when
497 * called from Cursor::dispatch(), and these can lead to crashes.
500 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);