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 virtual Inset * editXY(Cursor & cur, int x, int y);
188 /// compute the size of the object returned in dim
189 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
190 /// draw inset and update (xo, yo)-cache
191 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
192 /// draw inset selection if necessary
193 virtual void drawSelection(PainterInfo &, int, int) const {}
194 /// draw inset background if the inset has an own background and a
195 /// selection is drawn by drawSelection.
196 virtual void drawBackground(PainterInfo &, int, int) const;
198 virtual bool editing(BufferView const * bv) const;
200 virtual bool showInsetDialog(BufferView *) const;
202 /// draw two angular markers
203 void drawMarkers(PainterInfo & pi, int x, int y) const;
204 /// draw four angular markers
205 void drawMarkers2(PainterInfo & pi, int x, int y) const;
206 /// add space for markers
207 void metricsMarkers(Dimension & dim, int framesize = 1) const;
208 /// add space for markers
209 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
210 /// draw inset decoration if necessary.
211 /// This can use \c drawMarkers() for example.
212 virtual void drawDecoration(PainterInfo &, int, int) const {}
214 /// last metrics computed for the inset
215 Dimension const dimension(BufferView const &) const;
216 /// last drawn position for 'important' insets
217 int xo(BufferView const & bv) const;
218 /// last drawn position for 'important' insets
219 int yo(BufferView const & bv) const;
220 /// do we cover screen position x/y?
221 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 /// number of embedded cells
267 virtual size_t nargs() const { return 0; }
268 /// number of rows in gridlike structures
269 virtual size_t nrows() const { return 0; }
270 /// number of columns in gridlike structures
271 virtual size_t ncols() const { return 0; }
272 /// Is called when the cursor leaves this inset.
273 /// Returns true if cursor is now invalid, e.g. if former
274 /// insets in higher cursor slices of \c old do not exist
276 /// \c old is the old cursor, the last slice points to this.
277 /// \c cur is the new cursor. Use the update flags to cause a redraw.
278 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
280 /// Is called when the cursor enters this inset.
281 /// Returns true if cursor is now invalid, e.g. if former
282 /// insets in higher cursor slices of \c old do not exist
284 /// \c cur is the new cursor, some slice points to this. Use the update
285 /// flags to cause a redraw.
286 virtual bool notifyCursorEnters(Cursor & /*cur*/)
288 /// is called when the mouse enters or leaves this inset
289 /// return true if this inset needs a repaint
290 virtual bool setMouseHover(BufferView const *, bool) const
292 /// return true if this inset is hovered (under mouse)
293 /// This is by now only used by mathed to draw corners
294 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
295 /// Other insets do not have to redefine this function to
296 /// return the correct status of mouseHovered.
297 virtual bool mouseHovered(BufferView const *) const { return false; }
299 /// request "external features"
300 virtual void validate(LaTeXFeatures &) const {}
302 /// Validate LFUN_INSET_MODIFY argument.
303 virtual bool validateModifyArgument(docstring const &) const { return true; }
305 /// describe content if cursor inside
306 virtual void infoize(odocstream &) const {}
307 /// describe content if cursor behind
308 virtual void infoize2(odocstream &) const {}
310 enum { PLAINTEXT_NEWLINE = 10000 };
312 /// plain text output in ucs4 encoding
313 /// return the number of characters; in case of multiple lines of
314 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
315 virtual int plaintext(odocstringstream &, OutputParams const &,
316 size_t max_length = INT_MAX) const = 0;
318 virtual int docbook(odocstream & os, OutputParams const &) const;
320 /// the inset is expected to write XHTML to the XHTMLStream
321 /// \return any "deferred" material that should be written outside the
322 /// normal stream, and which will in fact be written after the current
323 /// paragraph closes. this is appropriate e.g. for floats.
324 virtual docstring xhtml(XHTMLStream & xs, OutputParams const &) const;
326 /// Writes a string representation of the inset to the odocstream.
327 /// This one should be called when you want the whole contents of
329 virtual void toString(odocstream &) const {}
330 /// Appends a potentially abbreviated version of the inset to
331 /// \param str. Intended for use by the TOC.
332 virtual void forOutliner(docstring & str,
333 size_t const maxlen = TOC_ENTRY_LENGTH,
334 bool const shorten = true) const;
336 /// can the contents of the inset be edited on screen ?
337 // true for InsetCollapsables (not ButtonOnly) (not InsetInfo), InsetText
338 virtual bool editable() const;
339 /// has the Inset settings that can be modified in a dialog ?
340 virtual bool hasSettings() const;
341 /// can we go further down on mouse click?
342 // true for InsetCaption, InsetCollapsables (not ButtonOnly), InsetTabular
343 virtual bool descendable(BufferView const &) const { return false; }
344 /// is this an inset that can be moved into?
345 /// FIXME: merge with editable()
346 // true for InsetTabular & InsetText
347 virtual bool isActive() const { return nargs() > 0; }
348 /// can we click at the specified position ?
349 virtual bool clickable(BufferView const &, int, int) const { return false; }
350 /// Move one cell backwards
351 virtual bool allowsCaptionVariation(std::string const &) const { return false; }
352 // true for insets that have a table structure (InsetMathGrid, InsetTabular)
353 virtual bool isTable() const { return false; }
355 /// does this contain text that can be change track marked in DVI?
356 virtual bool canTrackChanges() const { return false; }
357 /// Will this inset paint its own change tracking status (in the parent
358 /// paragraph) or will it let RowPainter handle it?
359 virtual bool canPaintChange(BufferView const &) const { return false; }
360 /// return true if the inset should be removed automatically
361 virtual bool autoDelete() const;
363 /// Returns true if the inset supports completions.
364 virtual bool completionSupported(Cursor const &) const { return false; }
365 /// Returns true if the inset supports inline completions at the
366 /// cursor position. In this case the completion might be stored
367 /// in the BufferView's inlineCompletion property.
368 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
370 /// Return true if the inline completion should be automatic.
371 virtual bool automaticInlineCompletion() const { return true; }
372 /// Return true if the popup completion should be automatic.
373 virtual bool automaticPopupCompletion() const { return true; }
374 /// Return true if the cursor should indicate a completion.
375 virtual bool showCompletionCursor() const { return true; }
376 /// Returns completion suggestions at cursor position. Return an
377 /// null pointer if no completion is a available or possible.
378 /// The caller is responsible to free the returned object!
379 virtual CompletionList const * createCompletionList(Cursor const &) const
381 /// Returns the completion prefix to filter the suggestions for completion.
382 /// This is only called if completionList returned a non-null list.
383 virtual docstring completionPrefix(Cursor const &) const;
384 /// Do a completion at the cursor position. Return true on success.
385 /// The completion does not contain the prefix. If finished is true, the
386 /// completion is final. If finished is false, completion might only be
387 /// a partial completion.
388 virtual bool insertCompletion(Cursor & /*cur*/,
389 docstring const & /*completion*/, bool /*finished*/)
391 /// Get the completion inset position and size
392 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
393 Dimension & /*dim*/) const {}
395 /// returns true if the inset can hold an inset of given type
396 virtual bool insetAllowed(InsetCode) const { return false; }
397 /// should this inset use the empty layout by default rather than
398 /// the standard layout? (default: only if that is forced.)
399 virtual bool usePlainLayout() const { return forcePlainLayout(); }
400 /// if this inset has paragraphs should they be forced to use the
402 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
403 /// if this inset has paragraphs should the user be allowed to
404 /// customize alignment, etc?
405 virtual bool allowParagraphCustomization(idx_type = 0) const
407 /// Is the width forced to some value?
408 virtual bool hasFixedWidth() const { return false; }
409 /// if this inset has paragraphs should they be forced to use a
410 /// local font language switch?
411 virtual bool forceLocalFontSwitch() const { return false; }
413 /// Is the content of this inset part of the output document?
414 virtual bool producesOutput() const { return true; }
415 /// Is the content of this inset part of the immediate (visible) text sequence?
416 virtual bool isPartOfTextSequence() const { return producesOutput(); }
418 /// \return Tool tip for this inset.
419 /// This default implementation returns an empty string. This can be
420 /// either plain text or Qt html, and formatToolTip will be called
421 /// on it before display in both cases.
422 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
424 /// \return Context menu identifier. This function determines
425 /// whose Inset's menu should be shown for the given position.
426 virtual std::string contextMenu(BufferView const & bv, int x, int y) const;
428 /// \return Context menu identifier for this inset.
429 /// This default implementation returns an empty string.
430 virtual std::string contextMenuName() const;
433 virtual docstring layoutName() const;
435 virtual InsetLayout const & getLayout() const;
437 virtual bool isPassThru() const { return getLayout().isPassThru(); }
438 /// Is this inset's layout defined in the document's textclass?
439 bool undefined() const;
440 /// should this inset be handled like a normal character?
441 /// (a character can be a letter or punctuation)
442 virtual bool isChar() const { return false; }
443 /// is this equivalent to a letter?
444 /// (a letter is a character that is considered part of a word)
445 virtual bool isLetter() const { return false; }
446 /// is this equivalent to a space (which is BTW different from
447 /// a line separator)?
448 virtual bool isSpace() const { return false; }
449 /// does this inset try to use all available space (like \\hfill does)?
450 virtual bool isHfill() const { return false; }
459 /// should we have a non-filled line before this inset?
460 virtual DisplayType display() const { return Inline; }
462 virtual LyXAlignment contentAlignment() const { return LYX_ALIGN_NONE; }
463 /// should we break lines after this inset?
464 virtual bool isLineSeparator() const { return false; }
465 /// should paragraph indendation be ommitted in any case?
466 virtual bool neverIndent() const { return false; }
467 /// dumps content to lyxerr
468 virtual void dump() const;
469 /// write inset in .lyx format
470 virtual void write(std::ostream &) const {}
471 /// read inset in .lyx format
472 virtual void read(Lexer &) {}
473 /** Export the inset to LaTeX.
474 * Don't use a temporary stringstream if the final output is
475 * supposed to go to a file.
476 * \sa Buffer::writeLaTeXSource for the reason.
478 virtual void latex(otexstream &, OutputParams const &) const {}
479 /// returns true to override begin and end inset in file
480 virtual bool directWrite() const;
482 virtual bool allowSpellCheck() const { return false; }
484 /// if this insets owns text cells (e.g. InsetText) return cell num
485 virtual Text * getText(int /*num*/) const { return 0; }
487 /** Adds a LaTeX snippet to the Preview Loader for transformation
488 * into a bitmap image. Does not start the laoding process.
490 * Most insets have no interest in this capability, so the method
493 virtual void addPreview(DocIterator const &,
494 graphics::PreviewLoader &) const {}
496 /** Classifies the unicode characters appearing in a math inset
497 * depending on whether they are to be translated as latex
498 * math/text commands or used as math symbols without translation.
500 * Only math insets have interest in this classification, so the
501 * method defaults to empty.
503 virtual void initUnicodeMath() const {}
505 /// Add an entry to the TocList
506 /// Pass a DocIterator that points at the paragraph containing
509 /// \param output_active : is the inset active or is it in an inactive
510 /// branch or a note?
512 /// \param utype : is the toc being generated for use by the output
515 /// \param tocbackend : where to add the toc information.
516 virtual void addToToc(DocIterator const & /* di */,
517 bool /* output_active */,
518 UpdateType /* utype*/,
519 TocBackend & /* tocbackend */) const {}
520 /// Collect BibTeX information
521 virtual void collectBibKeys(InsetIterator const &) const {}
522 /// Update the counters of this inset and of its contents.
523 /// The boolean indicates whether we are preparing for output, e.g.,
525 virtual void updateBuffer(ParIterator const &, UpdateType) {}
527 /// Updates the inset's dialog
528 virtual Buffer const * updateFrontend() const;
531 /// returns LyX code associated with the inset. Used for TOC, ...)
532 virtual InsetCode lyxCode() const { return NO_CODE; }
535 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
536 /// return text or mathmode if that is possible to determine
537 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
538 /// returns whether changing mode during latex export is forbidden
539 virtual bool lockedMode() const { return false; }
540 /// returns whether only ascii chars are allowed during latex export
541 virtual bool asciiOnly() const { return false; }
542 /// returns whether this inset is allowed in other insets of given mode
543 virtual bool allowedIn(mode_type) const { return true; }
545 * The font is inherited from the parent for LaTeX export if this
546 * method returns true. No open font changes are closed in front of
547 * the inset for LaTeX export, and the font is inherited for all other
548 * exports as well as on screen.
549 * If this method returns false all open font changes are closed in
550 * front of the inset for LaTeX export. The default font is used
551 * inside the inset for all exports and on screen.
553 virtual bool inheritFont() const { return true; }
555 * If this method returns true all explicitly set font attributes
556 * are reset during editing operations.
557 * For copy/paste operations the language is never changed, since
558 * the language of a given text never changes if the text is
559 * formatted differently, while other font attribues like size may
560 * need to change if the text is copied from one environment to
562 * If this method returns false no font attribute is reset.
563 * The default implementation returns true if the resetFont layout
564 * tag is set and otherwise the negation of inheritFont(),
565 * since inherited inset font attributes do not need to be changed,
566 * and non-inherited ones need to be set explicitly.
568 virtual bool resetFontEdit() const;
570 /// set the change for the entire inset
571 virtual void setChange(Change const &) {}
572 /// accept the changes within the inset
573 virtual void acceptChanges() {}
574 /// reject the changes within the inset
575 virtual void rejectChanges() {}
578 virtual ColorCode backgroundColor(PainterInfo const &) const;
580 virtual ColorCode labelColor() const;
582 enum { TEXT_TO_INSET_OFFSET = 4 };
586 Inset(Buffer * buf) : buffer_(buf) {}
587 Inset(Inset const &) : buffer_(0) {}
589 /// replicate ourselves
590 friend class InsetList;
591 friend class MathAtom;
592 virtual Inset * clone() const = 0;
594 /** The real dispatcher.
595 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
596 * assumes the common case of 'LFUN handled, need update'.
597 * This has to be overriden by calling Cursor::undispatched() or
598 * Cursor::noScreenUpdate() if appropriate.
599 * If you need to call the dispatch method of some inset directly
600 * you may have to explicitly request an update at that place. Don't
601 * do it in doDispatch(), since that causes nested updates when
602 * called from Cursor::dispatch(), and these can lead to crashes.
605 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);