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