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