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;
65 namespace graphics { class PreviewLoader; }
68 /// returns the InsetCode corresponding to the \c name.
69 /// Eg, insetCode("branch") == BRANCH_CODE
70 InsetCode insetCode(std::string const & name);
71 /// returns the Inset name corresponding to the \c InsetCode.
72 /// Eg, insetName(BRANCH_CODE) == "branch"
73 std::string insetName(InsetCode);
74 /// returns the Inset name corresponding to the \c InsetCode.
75 /// Eg, insetDisplayName(BRANCH_CODE) == _("Branch")
76 docstring insetDisplayName(InsetCode);
78 static int const TOC_ENTRY_LENGTH = 120;
80 /// Common base class to all insets
82 // Do not add _any_ (non-static) data members as this would inflate
83 // everything storing large quantities of insets. Mathed e.g. would
90 ENTRY_DIRECTION_IGNORE,
91 ENTRY_DIRECTION_RIGHT,
95 typedef ptrdiff_t difference_type;
96 /// short of anything else reasonable
97 typedef size_t size_type;
98 /// type for cell indices
99 typedef size_t idx_type;
100 /// type for cursor positions
101 typedef ptrdiff_t pos_type;
102 /// type for row numbers
103 typedef size_t row_type;
104 /// type for column numbers
105 typedef size_t col_type;
107 /// virtual base class destructor
110 /// change associated Buffer
111 virtual void setBuffer(Buffer & buffer);
112 /// reset associated Buffer to null value
113 virtual void resetBuffer();
114 /// retrieve associated Buffer
115 virtual Buffer & buffer();
116 virtual Buffer const & buffer() const;
117 /// Returns true if buffer_ actually points to a Buffer that has
118 /// been loaded into LyX and is still open. Note that this will
119 /// always return false for cloned Buffers. If you want to allow
120 /// for the case of cloned Buffers, use isBufferValid().
121 bool isBufferLoaded() const;
122 /// Returns true if this is a loaded buffer or a cloned buffer.
123 bool isBufferValid() const;
125 /// initialize view for this inset.
127 * This is typically used after this inset is created interactively.
128 * Intented purpose is to sanitize internal state with regard to current
131 virtual void initView() {}
132 /// \return true if this inset is labeled.
133 virtual bool isLabeled() const { return false; }
135 /// identification as math inset
136 virtual InsetMath * asInsetMath() { return 0; }
137 /// identification as math inset
138 virtual InsetMath const * asInsetMath() const { return 0; }
139 /// true for 'math' math inset, but not for e.g. mbox
140 virtual bool inMathed() const { return false; }
141 /// is this inset based on the InsetText class?
142 virtual InsetText * asInsetText() { return 0; }
143 /// is this inset based on the InsetText class?
144 virtual InsetText const * asInsetText() const { return 0; }
145 /// is this inset based on the InsetCollapsable class?
146 virtual InsetCollapsable * asInsetCollapsable() { return 0; }
147 /// is this inset based on the InsetCollapsable class?
148 virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
149 /// is this inset based on the InsetTabular class?
150 virtual InsetTabular * asInsetTabular() { return 0; }
151 /// is this inset based on the InsetTabular class?
152 virtual InsetTabular const * asInsetTabular() const { return 0; }
153 /// is this inset based on the InsetCommand class?
154 virtual InsetCommand * asInsetCommand() { return 0; }
155 /// is this inset based on the InsetCommand class?
156 virtual InsetCommand const * asInsetCommand() const { return 0; }
158 /// the real dispatcher
159 void dispatch(Cursor & cur, FuncRequest & cmd);
161 * \returns true if this function made a definitive decision on
162 * whether the inset wants to handle the request \p cmd or not.
163 * The result of this decision is put into \p status.
165 * Every request that is enabled in this method needs to be handled
166 * in doDispatch(). Normally we have a 1:1 relationship between the
167 * requests handled in getStatus() and doDispatch(), but there are
169 * - A request that is disabled in getStatus() does not need to
170 * appear in doDispatch(). It is guaranteed that doDispatch()
171 * is never called with this request.
172 * - A few requests are en- or disabled in Inset::getStatus().
173 * These need to be handled in the doDispatch() methods of the
174 * derived insets, since Inset::doDispatch() has not enough
175 * information to handle them.
176 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
177 * are dispatched directly
179 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
180 FuncStatus & status) const;
183 virtual void edit(Cursor & cur, bool front,
184 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
186 /// Note: this method must preserve the selection status. See:
187 /// https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg199001.html
188 virtual Inset * editXY(Cursor & cur, int x, int y);
190 /// compute the size of the object returned in dim
191 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
192 /// draw inset and update (xo, yo)-cache
193 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
194 /// draw inset selection if necessary
195 virtual void drawSelection(PainterInfo &, int, int) const {}
196 /// draw inset background if the inset has an own background and a
197 /// selection is drawn by drawSelection.
198 virtual void drawBackground(PainterInfo &, int, int) const;
200 virtual bool editing(BufferView const * bv) const;
202 virtual bool showInsetDialog(BufferView *) const;
204 /// draw two angular markers
205 void drawMarkers(PainterInfo & pi, int x, int y) const;
206 /// draw four 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 /// draw inset decoration if necessary.
213 /// This can use \c drawMarkers() for example.
214 virtual void drawDecoration(PainterInfo &, int, int) const {}
216 /// last metrics computed for the inset
217 Dimension const dimension(BufferView const &) const;
218 /// last drawn position for 'important' insets
219 int xo(BufferView const & bv) const;
220 /// last drawn position for 'important' insets
221 int yo(BufferView const & bv) const;
222 /// do we cover screen position x/y?
223 bool covers(BufferView const & bv, int x, int y) const;
224 /// get the screen positions of the cursor (see note in Cursor.cpp)
225 virtual void cursorPos(BufferView const & bv,
226 CursorSlice const & sl, bool boundary, int & x, int & y) const;
228 /// Allow multiple blanks
229 virtual bool isFreeSpacing() const;
230 /// Don't eliminate empty paragraphs
231 virtual bool allowEmpty() const;
232 /// Force inset into LTR environment if surroundings are RTL
233 virtual bool forceLTR() const;
234 /// whether to include this inset in the strings generated for the TOC
235 virtual bool isInToc() const;
237 /// Where should we go when we press the up or down cursor key?
238 virtual bool idxUpDown(Cursor & cur, bool up) const;
239 /// Move one cell backwards
240 virtual bool idxBackward(Cursor &) const { return false; }
241 /// Move one cell forward
242 virtual bool idxForward(Cursor &) const { return false; }
244 /// Move to the next cell
245 virtual bool idxNext(Cursor &) const { return false; }
246 /// Move to the previous cell
247 virtual bool idxPrev(Cursor &) const { return false; }
249 /// Target pos when we enter the inset while moving forward
250 virtual bool idxFirst(Cursor &) const { return false; }
251 /// Target pos when we enter the inset while moving backwards
252 virtual bool idxLast(Cursor &) const { return false; }
254 /// Delete a cell and move cursor
255 virtual bool idxDelete(idx_type &) { return false; }
256 /// pulls cell after pressing erase
257 virtual void idxGlue(idx_type) {}
258 /// returns list of cell indices that are "between" from and to for
259 /// selection purposes
260 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
262 /// to which column belongs a cell with a given index?
263 virtual col_type col(idx_type) const { return 0; }
264 /// to which row belongs a cell with a given index?
265 virtual row_type row(idx_type) const { return 0; }
266 /// cell index corresponding to row and column;
267 virtual idx_type index(row_type row, col_type col) const;
268 /// number of embedded cells
269 virtual size_t nargs() const { return 0; }
270 /// number of rows in gridlike structures
271 virtual size_t nrows() const { return 0; }
272 /// number of columns in gridlike structures
273 virtual size_t ncols() const { return 0; }
274 /// Is called when the cursor leaves this inset.
275 /// Returns true if cursor is now invalid, e.g. if former
276 /// insets in higher cursor slices of \c old do not exist
278 /// \c old is the old cursor, the last slice points to this.
279 /// \c cur is the new cursor. Use the update flags to cause a redraw.
280 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
282 /// Is called when the cursor enters this inset.
283 /// Returns true if cursor is now invalid, e.g. if former
284 /// insets in higher cursor slices of \c old do not exist
286 /// \c cur is the new cursor, some slice points to this. Use the update
287 /// flags to cause a redraw.
288 virtual bool notifyCursorEnters(Cursor & /*cur*/)
290 /// is called when the mouse enters or leaves this inset
291 /// return true if this inset needs a repaint
292 virtual bool setMouseHover(BufferView const *, bool) const
294 /// return true if this inset is hovered (under mouse)
295 /// This is by now only used by mathed to draw corners
296 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
297 /// Other insets do not have to redefine this function to
298 /// return the correct status of mouseHovered.
299 virtual bool mouseHovered(BufferView const *) const { return false; }
301 /// request "external features"
302 virtual void validate(LaTeXFeatures &) const {}
304 /// Validate LFUN_INSET_MODIFY argument.
305 virtual bool validateModifyArgument(docstring const &) const { return true; }
307 /// describe content if cursor inside
308 virtual void infoize(odocstream &) const {}
309 /// describe content if cursor behind
310 virtual void infoize2(odocstream &) const {}
312 enum { PLAINTEXT_NEWLINE = 10000 };
314 /// plain text output in ucs4 encoding
315 /// return the number of characters; in case of multiple lines of
316 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
317 virtual int plaintext(odocstringstream &, OutputParams const &,
318 size_t max_length = INT_MAX) const = 0;
320 virtual int docbook(odocstream & os, OutputParams const &) const;
322 /// the inset is expected to write XHTML to the XHTMLStream
323 /// \return any "deferred" material that should be written outside the
324 /// normal stream, and which will in fact be written after the current
325 /// paragraph closes. this is appropriate e.g. for floats.
326 virtual docstring xhtml(XHTMLStream & xs, OutputParams const &) const;
328 /// Writes a string representation of the inset to the odocstream.
329 /// This one should be called when you want the whole contents of
331 virtual void toString(odocstream &) const {}
332 /// Appends a potentially abbreviated version of the inset to
333 /// \param str. Intended for use by the TOC.
334 virtual void forOutliner(docstring & str,
335 size_t const maxlen = TOC_ENTRY_LENGTH,
336 bool const shorten = true) const;
338 /// can the contents of the inset be edited on screen ?
339 // true for InsetCollapsables (not ButtonOnly) (not InsetInfo), InsetText
340 virtual bool editable() const;
341 /// has the Inset settings that can be modified in a dialog ?
342 virtual bool hasSettings() const;
343 /// can we go further down on mouse click?
344 // true for InsetCaption, InsetCollapsables (not ButtonOnly), InsetTabular
345 virtual bool descendable(BufferView const &) const { return false; }
346 /// is this an inset that can be moved into?
347 /// FIXME: merge with editable()
348 // true for InsetTabular & InsetText
349 virtual bool isActive() const { return nargs() > 0; }
350 /// can we click at the specified position ?
351 virtual bool clickable(BufferView const &, int, int) const { return false; }
352 /// Move one cell backwards
353 virtual bool allowsCaptionVariation(std::string const &) const { return false; }
354 // true for insets that have a table structure (InsetMathGrid, InsetTabular)
355 virtual bool isTable() const { return false; }
357 /// does this contain text that can be change track marked in DVI?
358 virtual bool canTrackChanges() const { return false; }
359 /// Will this inset paint its own change tracking status (in the parent
360 /// paragraph) or will it let RowPainter handle it?
361 virtual bool canPaintChange(BufferView const &) const { return false; }
362 /// return true if the inset should be removed automatically
363 virtual bool autoDelete() const;
365 /// Returns true if the inset supports completions.
366 virtual bool completionSupported(Cursor const &) const { return false; }
367 /// Returns true if the inset supports inline completions at the
368 /// cursor position. In this case the completion might be stored
369 /// in the BufferView's inlineCompletion property.
370 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
372 /// Return true if the inline completion should be automatic.
373 virtual bool automaticInlineCompletion() const { return true; }
374 /// Return true if the popup completion should be automatic.
375 virtual bool automaticPopupCompletion() const { return true; }
376 /// Return true if the cursor should indicate a completion.
377 virtual bool showCompletionCursor() const { return true; }
378 /// Returns completion suggestions at cursor position. Return an
379 /// null pointer if no completion is a available or possible.
380 /// The caller is responsible to free the returned object!
381 virtual CompletionList const * createCompletionList(Cursor const &) const
383 /// Returns the completion prefix to filter the suggestions for completion.
384 /// This is only called if completionList returned a non-null list.
385 virtual docstring completionPrefix(Cursor const &) const;
386 /// Do a completion at the cursor position. Return true on success.
387 /// The completion does not contain the prefix. If finished is true, the
388 /// completion is final. If finished is false, completion might only be
389 /// a partial completion.
390 virtual bool insertCompletion(Cursor & /*cur*/,
391 docstring const & /*completion*/, bool /*finished*/)
393 /// Get the completion inset position and size
394 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
395 Dimension & /*dim*/) const {}
397 /// returns true if the inset can hold an inset of given type
398 virtual bool insetAllowed(InsetCode) const { return false; }
399 /// should this inset use the empty layout by default rather than
400 /// the standard layout? (default: only if that is forced.)
401 virtual bool usePlainLayout() const { return forcePlainLayout(); }
402 /// if this inset has paragraphs should they be forced to use the
404 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
405 /// if this inset has paragraphs should the user be allowed to
406 /// customize alignment, etc?
407 virtual bool allowParagraphCustomization(idx_type = 0) const
409 /// Is the width forced to some value?
410 virtual bool hasFixedWidth() const { return false; }
411 /// if this inset has paragraphs should they be forced to use a
412 /// local font language switch?
413 virtual bool forceLocalFontSwitch() const { return false; }
415 /// Is the content of this inset part of the output document?
416 virtual bool producesOutput() const { return true; }
417 /// Is the content of this inset part of the immediate (visible) text sequence?
418 virtual bool isPartOfTextSequence() const { return producesOutput(); }
420 /// \return Tool tip for this inset.
421 /// This default implementation returns an empty string. This can be
422 /// either plain text or Qt html, and formatToolTip will be called
423 /// on it before display in both cases.
424 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
426 /// \return Context menu identifier. This function determines
427 /// whose Inset's menu should be shown for the given position.
428 virtual std::string contextMenu(BufferView const & bv, int x, int y) const;
430 /// \return Context menu identifier for this inset.
431 /// This default implementation returns an empty string.
432 virtual std::string contextMenuName() const;
435 virtual docstring layoutName() const;
437 virtual InsetLayout const & getLayout() const;
439 virtual bool isPassThru() const { return getLayout().isPassThru(); }
440 /// Is this inset's layout defined in the document's textclass?
441 bool undefined() const;
442 /// should this inset be handled like a normal character?
443 /// (a character can be a letter or punctuation)
444 virtual bool isChar() const { return false; }
445 /// is this equivalent to a letter?
446 /// (a letter is a character that is considered part of a word)
447 virtual bool isLetter() const { return false; }
448 /// is this equivalent to a space (which is BTW different from
449 /// a line separator)?
450 virtual bool isSpace() const { return false; }
451 /// does this inset try to use all available space (like \\hfill does)?
452 virtual bool isHfill() const { return false; }
461 /// should we have a non-filled line before this inset?
462 virtual DisplayType display() const { return Inline; }
464 virtual LyXAlignment contentAlignment() const { return LYX_ALIGN_NONE; }
465 /// should we break lines after this inset?
466 virtual bool isLineSeparator() const { return false; }
467 /// should paragraph indendation be ommitted in any case?
468 virtual bool neverIndent() const { return false; }
469 /// dumps content to lyxerr
470 virtual void dump() const;
471 /// write inset in .lyx format
472 virtual void write(std::ostream &) const {}
473 /// read inset in .lyx format
474 virtual void read(Lexer &) {}
475 /** Export the inset to LaTeX.
476 * Don't use a temporary stringstream if the final output is
477 * supposed to go to a file.
478 * \sa Buffer::writeLaTeXSource for the reason.
480 virtual void latex(otexstream &, OutputParams const &) const {}
481 /// returns true to override begin and end inset in file
482 virtual bool directWrite() const;
484 virtual bool allowSpellCheck() const { return false; }
486 /// if this insets owns text cells (e.g. InsetText) return cell num
487 virtual Text * getText(int /*num*/) const { return 0; }
489 /** Adds a LaTeX snippet to the Preview Loader for transformation
490 * into a bitmap image. Does not start the laoding process.
492 * Most insets have no interest in this capability, so the method
495 virtual void addPreview(DocIterator const &,
496 graphics::PreviewLoader &) const {}
498 /** Classifies the unicode characters appearing in a math inset
499 * depending on whether they are to be translated as latex
500 * math/text commands or used as math symbols without translation.
502 * Only math insets have interest in this classification, so the
503 * method defaults to empty.
505 virtual void initUnicodeMath() const {}
507 /// Add an entry to the TocList
508 /// Pass a DocIterator that points at the paragraph containing
511 /// \param output_active : is the inset active or is it in an inactive
512 /// branch or a note?
514 /// \param utype : is the toc being generated for use by the output
517 /// \param tocbackend : where to add the toc information.
518 virtual void addToToc(DocIterator const & /* di */,
519 bool /* output_active */,
520 UpdateType /* utype*/,
521 TocBackend & /* tocbackend */) const {}
522 /// Collect BibTeX information
523 virtual void collectBibKeys(InsetIterator const &) const {}
524 /// Update the counters of this inset and of its contents.
525 /// The boolean indicates whether we are preparing for output, e.g.,
527 virtual void updateBuffer(ParIterator const &, UpdateType) {}
529 /// Updates the inset's dialog
530 virtual Buffer const * updateFrontend() const;
533 /// returns LyX code associated with the inset. Used for TOC, ...)
534 virtual InsetCode lyxCode() const { return NO_CODE; }
537 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
538 /// return text or mathmode if that is possible to determine
539 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
540 /// returns whether changing mode during latex export is forbidden
541 virtual bool lockedMode() const { return false; }
542 /// returns whether only ascii chars are allowed during latex export
543 virtual bool asciiOnly() const { return false; }
544 /// returns whether this inset is allowed in other insets of given mode
545 virtual bool allowedIn(mode_type) const { return true; }
547 * The font is inherited from the parent for LaTeX export if this
548 * method returns true. No open font changes are closed in front of
549 * the inset for LaTeX export, and the font is inherited for all other
550 * exports as well as on screen.
551 * If this method returns false all open font changes are closed in
552 * front of the inset for LaTeX export. The default font is used
553 * inside the inset for all exports and on screen.
555 virtual bool inheritFont() const { return true; }
557 * If this method returns true all explicitly set font attributes
558 * are reset during editing operations.
559 * For copy/paste operations the language is never changed, since
560 * the language of a given text never changes if the text is
561 * formatted differently, while other font attribues like size may
562 * need to change if the text is copied from one environment to
564 * If this method returns false no font attribute is reset.
565 * The default implementation returns true if the resetFont layout
566 * tag is set and otherwise the negation of inheritFont(),
567 * since inherited inset font attributes do not need to be changed,
568 * and non-inherited ones need to be set explicitly.
570 virtual bool resetFontEdit() const;
572 /// set the change for the entire inset
573 virtual void setChange(Change const &) {}
574 /// accept the changes within the inset
575 virtual void acceptChanges() {}
576 /// reject the changes within the inset
577 virtual void rejectChanges() {}
580 virtual ColorCode backgroundColor(PainterInfo const &) const;
582 virtual ColorCode labelColor() const;
584 enum { TEXT_TO_INSET_OFFSET = 4 };
586 /// Determine the action of backspace and delete: do we select instead of
587 /// deleting if not already selected?
588 virtual bool confirmDeletion() const { return false; }
592 Inset(Buffer * buf) : buffer_(buf) {}
593 Inset(Inset const &) : buffer_(0) {}
595 /// replicate ourselves
596 friend class InsetList;
597 friend class MathAtom;
598 virtual Inset * clone() const = 0;
600 /** The real dispatcher.
601 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
602 * assumes the common case of 'LFUN handled, need update'.
603 * This has to be overriden by calling Cursor::undispatched() or
604 * Cursor::noScreenUpdate() if appropriate.
605 * If you need to call the dispatch method of some inset directly
606 * you may have to explicitly request an update at that place. Don't
607 * do it in doDispatch(), since that causes nested updates when
608 * called from Cursor::dispatch(), and these can lead to crashes.
611 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);