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"
22 #include "support/strfwd.h"
23 #include "support/types.h"
40 class InsetCollapsable;
53 class ParConstIterator;
58 namespace graphics { class PreviewLoader; }
61 /** returns the InsetCode corresponding to the \c name.
62 * Eg, insetCode("branch") == BRANCH_CODE
63 * Implemented in 'Inset.cpp'.
65 InsetCode insetCode(std::string const & name);
67 std::string insetName(InsetCode);
69 /// Common base class to all insets
71 // Do not add _any_ (non-static) data members as this would inflate
72 // everything storing large quantities of insets. Mathed e.g. would
79 ENTRY_DIRECTION_IGNORE,
80 ENTRY_DIRECTION_RIGHT,
84 typedef ptrdiff_t difference_type;
85 /// short of anything else reasonable
86 typedef size_t size_type;
87 /// type for cell indices
88 typedef size_t idx_type;
89 /// type for cursor positions
90 typedef ptrdiff_t pos_type;
91 /// type for row numbers
92 typedef size_t row_type;
93 /// type for column numbers
94 typedef size_t col_type;
96 /// virtual base class destructor
99 /// change associated Buffer
100 /// FIXME this should go.
101 virtual void setBuffer(Buffer & buffer);
102 /// retrieve associated Buffer
103 virtual Buffer & buffer();
104 virtual Buffer const & buffer() const;
105 /// This checks whether the Buffer * actually points to an open
106 /// Buffer. It might not if that Buffer has been closed.
107 bool isBufferValid() const;
109 /// initialize view for this inset.
111 * This is typically used after this inset is created interactively.
112 * Intented purpose is to sanitize internal state with regard to current
113 * Buffer. The default implementation calls updateLabels(buffer()) is
114 * the inset is labeled.
118 virtual void initView();
119 /// \return true if this inset is labeled.
120 virtual bool isLabeled() const { return false; }
122 /// identification as math inset
123 virtual InsetMath * asInsetMath() { return 0; }
124 /// true for 'math' math inset, but not for e.g. mbox
125 virtual bool inMathed() const { return false; }
126 /// is this inset based on the InsetText class?
127 virtual InsetText * asInsetText() { return 0; }
128 /// is this inset based on the InsetText class?
129 virtual InsetText const * asInsetText() const { return 0; }
130 /// is this inset based on the InsetCollapsable class?
131 virtual InsetCollapsable * asInsetCollapsable() { return 0; }
132 /// is this inset based on the InsetCollapsable class?
133 virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
134 /// is this inset based on the InsetTabular class?
135 virtual InsetTabular * asInsetTabular() { return 0; }
136 /// is this inset based on the InsetTabular class?
137 virtual InsetTabular const * asInsetTabular() const { return 0; }
139 /// the real dispatcher
140 void dispatch(Cursor & cur, FuncRequest & cmd);
142 * \returns true if this function made a definitive decision on
143 * whether the inset wants to handle the request \p cmd or not.
144 * The result of this decision is put into \p status.
146 * Every request that is enabled in this method needs to be handled
147 * in doDispatch(). Normally we have a 1:1 relationship between the
148 * requests handled in getStatus() and doDispatch(), but there are
150 * - A request that is disabled in getStatus() does not need to
151 * appear in doDispatch(). It is guaranteed that doDispatch()
152 * is never called with this request.
153 * - A few requests are en- or disabled in Inset::getStatus().
154 * These need to be handled in the doDispatch() methods of the
155 * derived insets, since Inset::doDispatch() has not enough
156 * information to handle them.
157 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
158 * are dispatched directly
160 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
161 FuncStatus & status) const;
164 virtual void edit(Cursor & cur, bool front,
165 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
167 virtual Inset * editXY(Cursor & cur, int x, int y);
169 /// compute the size of the object returned in dim
170 /// \retval true if metrics changed.
171 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
172 /// draw inset and update (xo, yo)-cache
173 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
174 /// draw inset selection if necessary
175 virtual void drawSelection(PainterInfo &, int, int) const {}
176 /// draw inset background if the inset has an own background and a
177 /// selection is drawn by drawSelection.
178 virtual void drawBackground(PainterInfo &, int, int) const {}
180 virtual bool editing(BufferView const * bv) const;
182 virtual bool showInsetDialog(BufferView *) const { return false; }
184 /// draw inset decoration if necessary.
185 /// This can use \c drawMarkers() for example.
186 virtual void drawDecoration(PainterInfo &, int, int) const {}
187 /// draw four angular markers
188 void drawMarkers(PainterInfo & pi, int x, int y) const;
189 /// draw two angular markers
190 void drawMarkers2(PainterInfo & pi, int x, int y) const;
191 /// add space for markers
192 void metricsMarkers(Dimension & dim, int framesize = 1) const;
193 /// add space for markers
194 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
195 /// last drawn position for 'important' insets
196 int xo(BufferView const & bv) const;
197 /// last drawn position for 'important' insets
198 int yo(BufferView const & bv) const;
199 /// set x/y drawing position cache if available
200 virtual void setPosCache(PainterInfo const &, int, int) const;
202 void setDimCache(MetricsInfo const &, Dimension const &) const;
203 /// do we cover screen position x/y?
204 virtual bool covers(BufferView const & bv, int x, int y) const;
205 /// get the screen positions of the cursor (see note in Cursor.cpp)
206 virtual void cursorPos(BufferView const & bv,
207 CursorSlice const & sl, bool boundary, int & x, int & y) const;
210 virtual bool isFreeSpacing() const { return false; }
212 virtual bool allowEmpty() const { return false; }
213 /// Force inset into LTR environment if surroundings are RTL?
214 virtual bool forceLTR() const { return false; }
216 /// is this an inset that can be moved into?
217 /// FIXME: merge with editable()
218 virtual bool isActive() const { return nargs() > 0; }
219 /// Where should we go when we press the up or down cursor key?
220 virtual bool idxUpDown(Cursor & cur, bool up) const;
221 /// Move one cell backwards
222 virtual bool idxBackward(Cursor &) const { return false; }
223 /// Move one cell forward
224 virtual bool idxForward(Cursor &) const { return false; }
226 /// Move to the next cell
227 virtual bool idxNext(Cursor &) const { return false; }
228 /// Move to the previous cell
229 virtual bool idxPrev(Cursor &) const { return false; }
231 /// Target pos when we enter the inset while moving forward
232 virtual bool idxFirst(Cursor &) const { return false; }
233 /// Target pos when we enter the inset while moving backwards
234 virtual bool idxLast(Cursor &) const { return false; }
236 /// Delete a cell and move cursor
237 virtual bool idxDelete(idx_type &) { return false; }
238 /// pulls cell after pressing erase
239 virtual void idxGlue(idx_type) {}
240 /// returns list of cell indices that are "between" from and to for
241 /// selection purposes
242 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
244 /// to which column belongs a cell with a given index?
245 virtual col_type col(idx_type) const { return 0; }
246 /// to which row belongs a cell with a given index?
247 virtual row_type row(idx_type) const { return 0; }
248 /// cell index corresponding to row and column;
249 virtual idx_type index(row_type row, col_type col) const;
250 /// any additional x-offset when drawing a cell?
251 virtual int cellXOffset(idx_type) const { return 0; }
252 /// any additional y-offset when drawing a cell?
253 virtual int cellYOffset(idx_type) const { return 0; }
254 /// number of embedded cells
255 virtual size_t nargs() const { return 0; }
256 /// number of rows in gridlike structures
257 virtual size_t nrows() const { return 0; }
258 /// number of columns in gridlike structures
259 virtual size_t ncols() const { return 0; }
260 /// Is called when the cursor leaves this inset.
261 /// Returns true if cursor is now invalid, e.g. if former
262 /// insets in higher cursor slices of \c old do not exist
264 /// \c old is the old cursor, the last slice points to this.
265 /// \c cur is the new cursor. Use the update flags to cause a redraw.
266 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
268 /// Is called when the cursor enters this inset.
269 /// Returns true if cursor is now invalid, e.g. if former
270 /// insets in higher cursor slices of \c old do not exist
272 /// \c cur is the new cursor, some slice points to this. Use the update flags to cause a redraw.
273 virtual bool notifyCursorEnters(Cursor & /*cur*/)
275 /// is called when the mouse enter or leave this inset
276 /// return true if this inset needs repaint
277 virtual bool setMouseHover(bool) { return false; }
278 /// return true if this inset is hovered (under mouse)
279 /// This is by now only used by mathed to draw corners
280 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
281 /// Other insets do not have to redefine this function to
282 /// return the correct status of mouseHovered.
283 virtual bool mouseHovered() const { return false; }
285 /// request "external features"
286 virtual void validate(LaTeXFeatures &) const {}
288 /// describe content if cursor inside
289 virtual void infoize(odocstream &) const {}
290 /// describe content if cursor behind
291 virtual void infoize2(odocstream &) const {}
293 enum { PLAINTEXT_NEWLINE = 10000 };
295 /// plain text output in ucs4 encoding
296 /// return the number of characters; in case of multiple lines of
297 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
298 virtual int plaintext(odocstream &, OutputParams const &) const = 0;
300 virtual int docbook(odocstream & os, OutputParams const &) const;
301 /// the string that is passed to the TOC
302 virtual void tocString(odocstream &) const {}
304 /** This enum indicates by which means the inset can be modified:
305 - NOT_EDITABLE: the inset's content cannot be modified at all
306 (e.g. printindex, insetspecialchar)
307 - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, href)
308 - HIGHLY_EDITABLE: content can be edited on screen (normally means that
309 insettext is contained, e.g. collapsables, tabular) */
310 // FIXME: This has not yet been fully implemented to math insets
319 /// what appears in the minibuffer when opening
320 virtual docstring editMessage() const;
322 virtual EDITABLE editable() const;
323 /// can we go further down on mouse click?
324 virtual bool descendable() const { return false; }
325 /// does this contain text that can be change track marked in DVI?
326 virtual bool canTrackChanges() const { return false; }
327 /// return true if the inset should be removed automatically
328 virtual bool autoDelete() const;
330 /// Returns true if the inset supports completions.
331 virtual bool completionSupported(Cursor const &) const { return false; }
332 /// Returns true if the inset supports inline completions at the
333 /// cursor position. In this case the completion might be stored
334 /// in the BufferView's inlineCompletion property.
335 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
337 /// Return true if the inline completion should be automatic.
338 virtual bool automaticInlineCompletion() const { return true; }
339 /// Return true if the popup completion should be automatic.
340 virtual bool automaticPopupCompletion() const { return true; }
341 /// Return true if the cursor should indicate a completion.
342 virtual bool showCompletionCursor() const { return true; }
343 /// Returns completion suggestions at cursor position. Return an
344 /// null pointer if no completion is a available or possible.
345 /// The caller is responsible to free the returned object!
346 virtual CompletionList const * createCompletionList(Cursor const &) const
348 /// Returns the completion prefix to filter the suggestions for completion.
349 /// This is only called if completionList returned a non-null list.
350 virtual docstring completionPrefix(Cursor const &) const;
351 /// Do a completion at the cursor position. Return true on success.
352 /// The completion does not contain the prefix. If finished is true, the
353 /// completion is final. If finished is false, completion might only be
354 /// a partial completion.
355 virtual bool insertCompletion(Cursor & /*cur*/,
356 docstring const & /*completion*/, bool /*finished*/)
358 /// Get the completion inset position and size
359 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
360 Dimension & /*dim*/) const {}
362 /// returns true if the inset can hold an inset of given type
363 virtual bool insetAllowed(InsetCode) const { return false; }
364 /// should this inset use the empty layout by default rather than
365 /// the standard layout? (default: only if that is forced.)
366 virtual bool usePlainLayout() const { return forcePlainLayout(); }
367 /// if this inset has paragraphs should they be forced to use the
369 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
370 /// if this inset has paragraphs should the user be allowed to
371 /// customize alignment, etc?
372 virtual bool allowParagraphCustomization(idx_type = 0) const { return true; }
373 /// Is the width forced to some value?
374 virtual bool hasFixedWidth() const { return false; }
376 /// Is the content of this inset part of the output document?
377 virtual bool producesOutput() const { return true; }
379 /// \return Tool tip for this inset.
380 /// This default implementation returns an empty string.
381 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
383 /// \return Context menu identifier for this inset.
384 /// This default implementation returns an empty string.
385 virtual docstring contextMenu(BufferView const & bv, int x, int y) const;
387 // FIXME This should really disappear in favor of
388 // docstring name() const { return from_ascii(insetName(lyxCode()))); }
389 // There's no reason to be using different names in different places.
390 // But to do this we would need to change the file format, since the names
391 // used there don't correspond to what is used here.
393 virtual docstring name() const;
395 virtual InsetLayout const & getLayout(BufferParams const & bp) const;
396 /// used to toggle insets
397 /// is the inset open?
398 /// should this inset be handled like a normal charater
399 virtual bool isChar() const { return false; }
400 /// is this equivalent to a letter?
401 virtual bool isLetter() const { return false; }
402 /// is this equivalent to a space (which is BTW different from
403 /// a line separator)?
404 virtual bool isSpace() const { return false; }
405 /// is this an expandible space (rubber length)?
406 virtual bool isStretchableSpace() const { return false; }
415 /// should we have a non-filled line before this inset?
416 virtual DisplayType display() const { return Inline; }
418 virtual LyXAlignment contentAlignment() const { return LYX_ALIGN_NONE; }
419 /// should we break lines after this inset?
420 virtual bool isLineSeparator() const { return false; }
421 /// should paragraph indendation be ommitted in any case?
422 virtual bool neverIndent() const { return false; }
423 /// dumps content to lyxerr
424 virtual void dump() const;
425 /// write inset in .lyx format
426 virtual void write(std::ostream &) const {}
427 /// read inset in .lyx format
428 virtual void read(Lexer &) {}
429 /** Export the inset to LaTeX.
430 * Don't use a temporary stringstream if the final output is
431 * supposed to go to a file.
432 * \sa Buffer::writeLaTeXSource for the reason.
433 * \return the number of rows (\n's) of generated LaTeX code.
435 virtual int latex(odocstream &, OutputParams const &) const { return 0; }
436 /// returns true to override begin and end inset in file
437 virtual bool directWrite() const;
439 virtual bool allowSpellCheck() const { return false; }
441 /// if this insets owns text cells (e.g. InsetText) return cell num
442 virtual Text * getText(int /*num*/) const { return 0; }
444 /** Adds a LaTeX snippet to the Preview Loader for transformation
445 * into a bitmap image. Does not start the laoding process.
447 * Most insets have no interest in this capability, so the method
450 virtual void addPreview(graphics::PreviewLoader &) const {}
452 /** Classifies the unicode characters appearing in a math inset
453 * depending on whether they are to be translated as latex
454 * math/text commands or used as math symbols without translation.
456 * Only math insets have interest in this classification, so the
457 * method defaults to empty.
459 virtual void initUnicodeMath() const {}
461 /// Add an entry to the TocList
462 /// pit is the ParConstIterator of the paragraph containing the inset
463 virtual void addToToc(DocIterator const &) {}
464 /// Fill keys with BibTeX information
465 virtual void fillWithBibKeys(BiblioInfo &, InsetIterator const &) const {}
466 /// Update the counters of this inset and of its contents
467 virtual void updateLabels(ParIterator const &) {}
469 /// Updates the inset's dialog
470 virtual Buffer const * updateFrontend() const;
473 /// returns LyX code associated with the inset. Used for TOC, ...)
474 virtual InsetCode lyxCode() const { return NO_CODE; }
476 /// -1: text mode, 1: math mode, 0 undecided
477 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
478 /// return text or mathmode if that is possible to determine
479 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
480 /// returns whether this inset is allowed in other insets of given mode
481 virtual bool allowedIn(mode_type) const { return true; }
483 * Is this inset allowed within a font change?
485 * FIXME: noFontChange means currently that the font change is closed
486 * in LaTeX before the inset, and that the contents of the inset
487 * will be in default font. This should be changed so that the inset
488 * changes the font again.
490 virtual bool noFontChange() const { return false; }
492 /// set the change for the entire inset
493 virtual void setChange(Change const &) {}
494 /// accept the changes within the inset
495 virtual void acceptChanges(BufferParams const &) {};
496 /// reject the changes within the inset
497 virtual void rejectChanges(BufferParams const &) {};
500 virtual Dimension const dimension(BufferView const &) const;
502 int scroll() const { return 0; }
504 virtual ColorCode backgroundColor() const;
506 enum CollapseStatus {
511 virtual void setStatus(Cursor &, CollapseStatus) {}
513 enum { TEXT_TO_INSET_OFFSET = 4 };
517 Inset() : buffer_(0) {}
518 Inset(Inset const &) : buffer_(0) {}
520 /// replicate ourselves
521 friend class InsetList;
522 friend class MathAtom;
523 virtual Inset * clone() const = 0;
525 /** The real dispatcher.
526 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
527 * assumes the common case of 'LFUN handled, need update'.
528 * This has to be overriden by calling Cursor::undispatched() or
529 * Cursor::noUpdate() if appropriate.
530 * If you need to call the dispatch method of some inset directly
531 * you may have to explicitly request an update at that place. Don't
532 * do it in doDispatch(), since that causes nested updates when
533 * called from Cursor::dispatch(), and these can lead to crashes.
536 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);