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"
20 #include "InsetLayout.h"
21 #include "LayoutEnums.h"
22 #include "OutputEnums.h"
24 #include "support/strfwd.h"
25 #include "support/types.h"
43 class InsetCollapsable;
57 class ParConstIterator;
64 namespace graphics { class PreviewLoader; }
67 /// returns the InsetCode corresponding to the \c name.
68 /// Eg, insetCode("branch") == BRANCH_CODE
69 InsetCode insetCode(std::string const & name);
70 /// returns the Inset name corresponding to the \c InsetCode.
71 /// Eg, insetName(BRANCH_CODE) == "branch"
72 std::string insetName(InsetCode);
73 /// returns the Inset name corresponding to the \c InsetCode.
74 /// Eg, insetDisplayName(BRANCH_CODE) == _("Branch")
75 docstring insetDisplayName(InsetCode);
77 static int const TOC_ENTRY_LENGTH = 40;
79 /// Common base class to all insets
81 // Do not add _any_ (non-static) data members as this would inflate
82 // everything storing large quantities of insets. Mathed e.g. would
89 ENTRY_DIRECTION_IGNORE,
90 ENTRY_DIRECTION_RIGHT,
94 typedef ptrdiff_t difference_type;
95 /// short of anything else reasonable
96 typedef size_t size_type;
97 /// type for cell indices
98 typedef size_t idx_type;
99 /// type for cursor positions
100 typedef ptrdiff_t pos_type;
101 /// type for row numbers
102 typedef size_t row_type;
103 /// type for column numbers
104 typedef size_t col_type;
106 /// virtual base class destructor
109 /// change associated Buffer
110 virtual void setBuffer(Buffer & buffer);
111 /// remove the buffer reference
112 void resetBuffer() { setBuffer( *static_cast<Buffer *>(0)); }
113 /// retrieve associated Buffer
114 virtual Buffer & buffer();
115 virtual Buffer const & buffer() const;
116 /// Returns true if buffer_ actually points to a Buffer that has
117 /// been loaded into LyX and is still open. Note that this will
118 /// always return false for cloned Buffers. If you want to allow
119 /// for the case of cloned Buffers, use isBufferValid().
120 bool isBufferLoaded() const;
121 /// Returns true if this is a loaded buffer or a cloned buffer.
122 bool isBufferValid() const;
124 /// initialize view for this inset.
126 * This is typically used after this inset is created interactively.
127 * Intented purpose is to sanitize internal state with regard to current
130 virtual void initView() {};
131 /// \return true if this inset is labeled.
132 virtual bool isLabeled() const { return false; }
134 /// identification as math inset
135 virtual InsetMath * asInsetMath() { return 0; }
136 /// identification as math inset
137 virtual InsetMath const * asInsetMath() const { return 0; }
138 /// true for 'math' math inset, but not for e.g. mbox
139 virtual bool inMathed() const { return false; }
140 /// is this inset based on the InsetText class?
141 virtual InsetText * asInsetText() { return 0; }
142 /// is this inset based on the InsetText class?
143 virtual InsetText const * asInsetText() const { return 0; }
144 /// is this inset based on the InsetCollapsable class?
145 virtual InsetCollapsable * asInsetCollapsable() { return 0; }
146 /// is this inset based on the InsetCollapsable class?
147 virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
148 /// is this inset based on the InsetTabular class?
149 virtual InsetTabular * asInsetTabular() { return 0; }
150 /// is this inset based on the InsetTabular class?
151 virtual InsetTabular const * asInsetTabular() const { return 0; }
152 /// is this inset based on the InsetCommand class?
153 virtual InsetCommand * asInsetCommand() { return 0; }
154 /// is this inset based on the InsetCommand class?
155 virtual InsetCommand const * asInsetCommand() const { return 0; }
157 /// the real dispatcher
158 void dispatch(Cursor & cur, FuncRequest & cmd);
160 * \returns true if this function made a definitive decision on
161 * whether the inset wants to handle the request \p cmd or not.
162 * The result of this decision is put into \p status.
164 * Every request that is enabled in this method needs to be handled
165 * in doDispatch(). Normally we have a 1:1 relationship between the
166 * requests handled in getStatus() and doDispatch(), but there are
168 * - A request that is disabled in getStatus() does not need to
169 * appear in doDispatch(). It is guaranteed that doDispatch()
170 * is never called with this request.
171 * - A few requests are en- or disabled in Inset::getStatus().
172 * These need to be handled in the doDispatch() methods of the
173 * derived insets, since Inset::doDispatch() has not enough
174 * information to handle them.
175 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
176 * are dispatched directly
178 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
179 FuncStatus & status) const;
182 virtual void edit(Cursor & cur, bool front,
183 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
185 virtual Inset * editXY(Cursor & cur, int x, int y);
187 /// compute the size of the object returned in dim
188 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
189 /// draw inset and update (xo, yo)-cache
190 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
191 /// draw inset selection if necessary
192 virtual void drawSelection(PainterInfo &, int, int) const {}
193 /// draw inset background if the inset has an own background and a
194 /// selection is drawn by drawSelection.
195 virtual void drawBackground(PainterInfo &, int, int) const {}
197 virtual bool editing(BufferView const * bv) const;
199 virtual bool showInsetDialog(BufferView *) const;
201 /// draw inset decoration if necessary.
202 /// This can use \c drawMarkers() for example.
203 virtual void drawDecoration(PainterInfo &, int, int) const {}
204 /// draw four angular markers
205 void drawMarkers(PainterInfo & pi, int x, int y) const;
206 /// draw two angular markers
207 void drawMarkers2(PainterInfo & pi, int x, int y) const;
208 /// add space for markers
209 void metricsMarkers(Dimension & dim, int framesize = 1) const;
210 /// add space for markers
211 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
212 /// last drawn position for 'important' insets
213 int xo(BufferView const & bv) const;
214 /// last drawn position for 'important' insets
215 int yo(BufferView const & bv) const;
216 /// set x/y drawing position cache if available
217 virtual void setPosCache(PainterInfo const &, int, int) const;
219 void setDimCache(MetricsInfo const &, Dimension const &) const;
220 /// do we cover screen position x/y?
221 virtual bool covers(BufferView const & bv, int x, int y) const;
222 /// get the screen positions of the cursor (see note in Cursor.cpp)
223 virtual void cursorPos(BufferView const & bv,
224 CursorSlice const & sl, bool boundary, int & x, int & y) const;
226 /// Allow multiple blanks
227 virtual bool isFreeSpacing() const;
228 /// Don't eliminate empty paragraphs
229 virtual bool allowEmpty() const;
230 /// Force inset into LTR environment if surroundings are RTL
231 virtual bool forceLTR() const;
232 /// whether to include this inset in the strings generated for the TOC
233 virtual bool isInToc() const;
235 /// Where should we go when we press the up or down cursor key?
236 virtual bool idxUpDown(Cursor & cur, bool up) const;
237 /// Move one cell backwards
238 virtual bool idxBackward(Cursor &) const { return false; }
239 /// Move one cell forward
240 virtual bool idxForward(Cursor &) const { return false; }
242 /// Move to the next cell
243 virtual bool idxNext(Cursor &) const { return false; }
244 /// Move to the previous cell
245 virtual bool idxPrev(Cursor &) const { return false; }
247 /// Target pos when we enter the inset while moving forward
248 virtual bool idxFirst(Cursor &) const { return false; }
249 /// Target pos when we enter the inset while moving backwards
250 virtual bool idxLast(Cursor &) const { return false; }
252 /// Delete a cell and move cursor
253 virtual bool idxDelete(idx_type &) { return false; }
254 /// pulls cell after pressing erase
255 virtual void idxGlue(idx_type) {}
256 /// returns list of cell indices that are "between" from and to for
257 /// selection purposes
258 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
260 /// to which column belongs a cell with a given index?
261 virtual col_type col(idx_type) const { return 0; }
262 /// to which row belongs a cell with a given index?
263 virtual row_type row(idx_type) const { return 0; }
264 /// cell index corresponding to row and column;
265 virtual idx_type index(row_type row, col_type col) const;
266 /// any additional x-offset when drawing a cell?
267 virtual int cellXOffset(idx_type) const { return 0; }
268 /// any additional y-offset when drawing a cell?
269 virtual int cellYOffset(idx_type) const { return 0; }
270 /// number of embedded cells
271 virtual size_t nargs() const { return 0; }
272 /// number of rows in gridlike structures
273 virtual size_t nrows() const { return 0; }
274 /// number of columns in gridlike structures
275 virtual size_t ncols() const { return 0; }
276 /// Is called when the cursor leaves this inset.
277 /// Returns true if cursor is now invalid, e.g. if former
278 /// insets in higher cursor slices of \c old do not exist
280 /// \c old is the old cursor, the last slice points to this.
281 /// \c cur is the new cursor. Use the update flags to cause a redraw.
282 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
284 /// Is called when the cursor enters this inset.
285 /// Returns true if cursor is now invalid, e.g. if former
286 /// insets in higher cursor slices of \c old do not exist
288 /// \c cur is the new cursor, some slice points to this. Use the update
289 /// flags to cause a redraw.
290 virtual bool notifyCursorEnters(Cursor & /*cur*/)
292 /// is called when the mouse enters or leaves this inset
293 /// return true if this inset needs a repaint
294 virtual bool setMouseHover(BufferView const *, bool) const
296 /// return true if this inset is hovered (under mouse)
297 /// This is by now only used by mathed to draw corners
298 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
299 /// Other insets do not have to redefine this function to
300 /// return the correct status of mouseHovered.
301 virtual bool mouseHovered(BufferView const *) const { return false; }
303 /// request "external features"
304 virtual void validate(LaTeXFeatures &) const {}
306 /// Validate LFUN_INSET_MODIFY argument.
307 virtual bool validateModifyArgument(docstring const &) const { return true; }
309 /// describe content if cursor inside
310 virtual void infoize(odocstream &) const {}
311 /// describe content if cursor behind
312 virtual void infoize2(odocstream &) const {}
314 enum { PLAINTEXT_NEWLINE = 10000 };
316 /// plain text output in ucs4 encoding
317 /// return the number of characters; in case of multiple lines of
318 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
319 virtual int plaintext(odocstringstream &, OutputParams const &,
320 size_t max_length = INT_MAX) const = 0;
322 virtual int docbook(odocstream & os, OutputParams const &) const;
324 /// the inset is expected to write XHTML to the XHTMLStream
325 /// \return any "deferred" material that should be written outside the
326 /// normal stream, and which will in fact be written after the current
327 /// paragraph closes. this is appropriate e.g. for floats.
328 virtual docstring xhtml(XHTMLStream & xs, OutputParams const &) const;
330 /// Writes a string representation of the inset to the odocstream.
331 /// This one should be called when you want the whole contents of
333 virtual void toString(odocstream &) const {}
334 /// Appends a potentially abbreviated version of the inset to
335 /// \param str. Intended for use by the TOC.
336 virtual void forToc(docstring & str,
337 size_t maxlen = TOC_ENTRY_LENGTH) const;
339 /// can the contents of the inset be edited on screen ?
340 // true for InsetCollapsables (not ButtonOnly) (not InsetInfo), InsetText
341 virtual bool editable() const;
342 /// has the Inset settings that can be modified in a dialog ?
343 virtual bool hasSettings() const;
344 /// can we go further down on mouse click?
345 // true for InsetCaption, InsetCollapsables (not ButtonOnly), InsetTabular
346 virtual bool descendable(BufferView const &) const { return false; }
347 /// is this an inset that can be moved into?
348 /// FIXME: merge with editable()
349 // true for InsetTabular & InsetText
350 virtual bool isActive() const { return nargs() > 0; }
351 /// can we click at the specified position ?
352 virtual bool clickable(int, int) const { return false; }
353 /// Move one cell backwards
354 virtual bool allowsCaptionVariation(std::string const &) const { return false; }
356 /// does this contain text that can be change track marked in DVI?
357 virtual bool canTrackChanges() const { return false; }
358 /// return true if the inset should be removed automatically
359 virtual bool autoDelete() const;
361 /// Returns true if the inset supports completions.
362 virtual bool completionSupported(Cursor const &) const { return false; }
363 /// Returns true if the inset supports inline completions at the
364 /// cursor position. In this case the completion might be stored
365 /// in the BufferView's inlineCompletion property.
366 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
368 /// Return true if the inline completion should be automatic.
369 virtual bool automaticInlineCompletion() const { return true; }
370 /// Return true if the popup completion should be automatic.
371 virtual bool automaticPopupCompletion() const { return true; }
372 /// Return true if the cursor should indicate a completion.
373 virtual bool showCompletionCursor() const { return true; }
374 /// Returns completion suggestions at cursor position. Return an
375 /// null pointer if no completion is a available or possible.
376 /// The caller is responsible to free the returned object!
377 virtual CompletionList const * createCompletionList(Cursor const &) const
379 /// Returns the completion prefix to filter the suggestions for completion.
380 /// This is only called if completionList returned a non-null list.
381 virtual docstring completionPrefix(Cursor const &) const;
382 /// Do a completion at the cursor position. Return true on success.
383 /// The completion does not contain the prefix. If finished is true, the
384 /// completion is final. If finished is false, completion might only be
385 /// a partial completion.
386 virtual bool insertCompletion(Cursor & /*cur*/,
387 docstring const & /*completion*/, bool /*finished*/)
389 /// Get the completion inset position and size
390 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
391 Dimension & /*dim*/) const {}
393 /// returns true if the inset can hold an inset of given type
394 virtual bool insetAllowed(InsetCode) const { return false; }
395 /// should this inset use the empty layout by default rather than
396 /// the standard layout? (default: only if that is forced.)
397 virtual bool usePlainLayout() const { return forcePlainLayout(); }
398 /// if this inset has paragraphs should they be forced to use the
400 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
401 /// if this inset has paragraphs should the user be allowed to
402 /// customize alignment, etc?
403 virtual bool allowParagraphCustomization(idx_type = 0) const
405 /// Is the width forced to some value?
406 virtual bool hasFixedWidth() const { return false; }
408 /// Is the content of this inset part of the output document?
409 virtual bool producesOutput() const { return true; }
411 /// \return Tool tip for this inset.
412 /// This default implementation returns an empty string.
413 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
415 /// \return Context menu identifier. This function determines
416 /// whose Inset's menu should be shown for the given position.
417 virtual std::string contextMenu(BufferView const & bv, int x, int y) const;
419 /// \return Context menu identifier for this inset.
420 /// This default implementation returns an empty string.
421 virtual std::string contextMenuName() const;
424 virtual docstring layoutName() const;
426 virtual InsetLayout const & getLayout() const;
428 virtual bool isPassThru() const { return getLayout().isPassThru(); }
429 /// Is this inset's layout defined in the document's textclass?
430 bool undefined() const;
431 /// should this inset be handled like a normal character?
432 /// (a character can be a letter or punctuation)
433 virtual bool isChar() const { return false; }
434 /// is this equivalent to a letter?
435 /// (a letter is a character that is considered part of a word)
436 virtual bool isLetter() const { return false; }
437 /// is this equivalent to a space (which is BTW different from
438 /// a line separator)?
439 virtual bool isSpace() const { return false; }
440 /// is this an expandible space (rubber length)?
441 virtual bool isStretchableSpace() const { return false; }
450 /// should we have a non-filled line before this inset?
451 virtual DisplayType display() const { return Inline; }
453 virtual LyXAlignment contentAlignment() const { return LYX_ALIGN_NONE; }
454 /// should we break lines after this inset?
455 virtual bool isLineSeparator() const { return false; }
456 /// should paragraph indendation be ommitted in any case?
457 virtual bool neverIndent() const { return false; }
458 /// dumps content to lyxerr
459 virtual void dump() const;
460 /// write inset in .lyx format
461 virtual void write(std::ostream &) const {}
462 /// read inset in .lyx format
463 virtual void read(Lexer &) {}
464 /** Export the inset to LaTeX.
465 * Don't use a temporary stringstream if the final output is
466 * supposed to go to a file.
467 * \sa Buffer::writeLaTeXSource for the reason.
469 virtual void latex(otexstream &, OutputParams const &) const {}
470 /// returns true to override begin and end inset in file
471 virtual bool directWrite() const;
473 virtual bool allowSpellCheck() const { return false; }
475 /// if this insets owns text cells (e.g. InsetText) return cell num
476 virtual Text * getText(int /*num*/) const { return 0; }
478 /** Adds a LaTeX snippet to the Preview Loader for transformation
479 * into a bitmap image. Does not start the laoding process.
481 * Most insets have no interest in this capability, so the method
484 virtual void addPreview(DocIterator const &,
485 graphics::PreviewLoader &) const {}
487 /** Classifies the unicode characters appearing in a math inset
488 * depending on whether they are to be translated as latex
489 * math/text commands or used as math symbols without translation.
491 * Only math insets have interest in this classification, so the
492 * method defaults to empty.
494 virtual void initUnicodeMath() const {}
496 /// Add an entry to the TocList
497 /// Pass a DocIterator that points at the paragraph containing
499 virtual void addToToc(DocIterator const & /* di */, bool /* output_active */) const {}
500 /// Collect BibTeX information
501 virtual void collectBibKeys(InsetIterator const &) const {}
502 /// Update the counters of this inset and of its contents.
503 /// The boolean indicates whether we are preparing for output, e.g.,
505 virtual void updateBuffer(ParIterator const &, UpdateType) {}
507 /// Updates the inset's dialog
508 virtual Buffer const * updateFrontend() const;
511 /// returns LyX code associated with the inset. Used for TOC, ...)
512 virtual InsetCode lyxCode() const { return NO_CODE; }
514 /// -1: text mode, 1: math mode, 0 undecided
515 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
516 /// return text or mathmode if that is possible to determine
517 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
518 /// returns whether changing mode during latex export is forbidden
519 virtual bool lockedMode() const { return false; }
520 /// returns whether only ascii chars are allowed during latex export
521 virtual bool asciiOnly() const { return false; }
522 /// returns whether this inset is allowed in other insets of given mode
523 virtual bool allowedIn(mode_type) const { return true; }
525 * The font is inherited from the parent for LaTeX export if this
526 * method returns true. No open font changes are closed in front of
527 * the inset for LaTeX export, and the font is inherited for all other
528 * exports as well as on screen.
529 * If this method returns false all open font changes are closed in
530 * front of the inset for LaTeX export. The default font is used
531 * inside the inset for all exports and on screen.
533 virtual bool inheritFont() const { return true; }
535 * If this method returns true all explicitly set font attributes
536 * are reset during editing operations.
537 * For copy/paste the operations the language is never changed, since
538 * the language of a given text never changes if the text is
539 * formatted differently, while other font attribues like size may
540 * need to change if the text is copied from one environment to
542 * If it returns false no font attribute is reset.
543 * The default implementation returns the negation of inheritFont(),
544 * since inherited inset font attributes do not need to be changed,
545 * and non-inherited ones need to be set explicitly.
547 virtual bool resetFontEdit() const { return !inheritFont(); }
549 /// set the change for the entire inset
550 virtual void setChange(Change const &) {}
551 /// accept the changes within the inset
552 virtual void acceptChanges() {}
553 /// reject the changes within the inset
554 virtual void rejectChanges() {}
557 virtual Dimension const dimension(BufferView const &) const;
559 virtual ColorCode backgroundColor(PainterInfo const &) const;
561 virtual ColorCode labelColor() const;
563 enum { TEXT_TO_INSET_OFFSET = 4 };
567 Inset(Buffer * buf) : buffer_(buf) {}
568 Inset(Inset const &) : buffer_(0) {}
570 /// replicate ourselves
571 friend class InsetList;
572 friend class MathAtom;
573 virtual Inset * clone() const = 0;
575 /** The real dispatcher.
576 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
577 * assumes the common case of 'LFUN handled, need update'.
578 * This has to be overriden by calling Cursor::undispatched() or
579 * Cursor::noScreenUpdate() if appropriate.
580 * If you need to call the dispatch method of some inset directly
581 * you may have to explicitly request an update at that place. Don't
582 * do it in doDispatch(), since that causes nested updates when
583 * called from Cursor::dispatch(), and these can lead to crashes.
586 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);