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 = 120;
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 /// reset associated Buffer to null value
112 virtual void resetBuffer();
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 // The possible marker types for insets
202 enum marker_type { NO_MARKER, MARKER2, MARKER };
203 /// draw two angular markers
204 void drawMarkers(PainterInfo & pi, int x, int y) const;
205 /// draw four angular markers
206 void drawMarkers2(PainterInfo & pi, int x, int y) const;
207 /// add space for markers
208 void metricsMarkers(Dimension & dim, int framesize = 1) const;
209 /// add space for markers
210 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
211 /// draw inset decoration if necessary.
212 /// This can use \c drawMarkers() for example.
213 virtual void drawDecoration(PainterInfo &, int, int) const {}
215 /// last metrics computed for the inset
216 Dimension const dimension(BufferView const &) const;
217 /// last drawn position for 'important' insets
218 int xo(BufferView const & bv) const;
219 /// last drawn position for 'important' insets
220 int yo(BufferView const & bv) const;
221 /// do we cover screen position x/y?
222 bool covers(BufferView const & bv, int x, int y) const;
223 /// get the screen positions of the cursor (see note in Cursor.cpp)
224 virtual void cursorPos(BufferView const & bv,
225 CursorSlice const & sl, bool boundary, int & x, int & y) const;
227 /// Allow multiple blanks
228 virtual bool isFreeSpacing() const;
229 /// Don't eliminate empty paragraphs
230 virtual bool allowEmpty() const;
231 /// Force inset into LTR environment if surroundings are RTL
232 virtual bool forceLTR() const;
233 /// whether to include this inset in the strings generated for the TOC
234 virtual bool isInToc() const;
236 /// Where should we go when we press the up or down cursor key?
237 virtual bool idxUpDown(Cursor & cur, bool up) const;
238 /// Move one cell backwards
239 virtual bool idxBackward(Cursor &) const { return false; }
240 /// Move one cell forward
241 virtual bool idxForward(Cursor &) const { return false; }
243 /// Move to the next cell
244 virtual bool idxNext(Cursor &) const { return false; }
245 /// Move to the previous cell
246 virtual bool idxPrev(Cursor &) const { return false; }
248 /// Target pos when we enter the inset while moving forward
249 virtual bool idxFirst(Cursor &) const { return false; }
250 /// Target pos when we enter the inset while moving backwards
251 virtual bool idxLast(Cursor &) const { return false; }
253 /// Delete a cell and move cursor
254 virtual bool idxDelete(idx_type &) { return false; }
255 /// pulls cell after pressing erase
256 virtual void idxGlue(idx_type) {}
257 /// returns list of cell indices that are "between" from and to for
258 /// selection purposes
259 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
261 /// to which column belongs a cell with a given index?
262 virtual col_type col(idx_type) const { return 0; }
263 /// to which row belongs a cell with a given index?
264 virtual row_type row(idx_type) const { return 0; }
265 /// cell index corresponding to row and column;
266 virtual idx_type index(row_type row, col_type col) const;
267 /// number of embedded cells
268 virtual size_t nargs() const { return 0; }
269 /// number of rows in gridlike structures
270 virtual size_t nrows() const { return 0; }
271 /// number of columns in gridlike structures
272 virtual size_t ncols() const { return 0; }
273 /// Is called when the cursor leaves this inset.
274 /// Returns true if cursor is now invalid, e.g. if former
275 /// insets in higher cursor slices of \c old do not exist
277 /// \c old is the old cursor, the last slice points to this.
278 /// \c cur is the new cursor. Use the update flags to cause a redraw.
279 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
281 /// Is called when the cursor enters this inset.
282 /// Returns true if cursor is now invalid, e.g. if former
283 /// insets in higher cursor slices of \c old do not exist
285 /// \c cur is the new cursor, some slice points to this. Use the update
286 /// flags to cause a redraw.
287 virtual bool notifyCursorEnters(Cursor & /*cur*/)
289 /// is called when the mouse enters or leaves this inset
290 /// return true if this inset needs a repaint
291 virtual bool setMouseHover(BufferView const *, bool) const
293 /// return true if this inset is hovered (under mouse)
294 /// This is by now only used by mathed to draw corners
295 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
296 /// Other insets do not have to redefine this function to
297 /// return the correct status of mouseHovered.
298 virtual bool mouseHovered(BufferView const *) const { return false; }
300 /// request "external features"
301 virtual void validate(LaTeXFeatures &) const {}
303 /// Validate LFUN_INSET_MODIFY argument.
304 virtual bool validateModifyArgument(docstring const &) const { return true; }
306 /// describe content if cursor inside
307 virtual void infoize(odocstream &) const {}
308 /// describe content if cursor behind
309 virtual void infoize2(odocstream &) const {}
311 enum { PLAINTEXT_NEWLINE = 10000 };
313 /// plain text output in ucs4 encoding
314 /// return the number of characters; in case of multiple lines of
315 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
316 virtual int plaintext(odocstringstream &, OutputParams const &,
317 size_t max_length = INT_MAX) const = 0;
319 virtual int docbook(odocstream & os, OutputParams const &) const;
321 /// the inset is expected to write XHTML to the XHTMLStream
322 /// \return any "deferred" material that should be written outside the
323 /// normal stream, and which will in fact be written after the current
324 /// paragraph closes. this is appropriate e.g. for floats.
325 virtual docstring xhtml(XHTMLStream & xs, OutputParams const &) const;
327 /// Writes a string representation of the inset to the odocstream.
328 /// This one should be called when you want the whole contents of
330 virtual void toString(odocstream &) const {}
331 /// Appends a potentially abbreviated version of the inset to
332 /// \param str. Intended for use by the TOC.
333 virtual void forOutliner(docstring & str,
334 size_t const maxlen = TOC_ENTRY_LENGTH,
335 bool const shorten = true) const;
337 /// can the contents of the inset be edited on screen ?
338 // true for InsetCollapsables (not ButtonOnly) (not InsetInfo), InsetText
339 virtual bool editable() const;
340 /// has the Inset settings that can be modified in a dialog ?
341 virtual bool hasSettings() const;
342 /// can we go further down on mouse click?
343 // true for InsetCaption, InsetCollapsables (not ButtonOnly), InsetTabular
344 virtual bool descendable(BufferView const &) const { return false; }
345 /// is this an inset that can be moved into?
346 /// FIXME: merge with editable()
347 // true for InsetTabular & InsetText
348 virtual bool isActive() const { return nargs() > 0; }
349 /// can we click at the specified position ?
350 virtual bool clickable(BufferView const &, int, int) const { return false; }
351 /// Move one cell backwards
352 virtual bool allowsCaptionVariation(std::string const &) const { return false; }
353 // true for insets that have a table structure (InsetMathGrid, InsetTabular)
354 virtual bool isTable() const { return false; }
356 /// does this contain text that can be change track marked in DVI?
357 virtual bool canTrackChanges() const { return false; }
358 /// Will this inset paint its own change tracking status (in the parent
359 /// paragraph) or will it let RowPainter handle it?
360 virtual bool canPaintChange(BufferView const &) const { return false; }
361 /// return true if the inset should be removed automatically
362 virtual bool autoDelete() const;
364 /// Returns true if the inset supports completions.
365 virtual bool completionSupported(Cursor const &) const { return false; }
366 /// Returns true if the inset supports inline completions at the
367 /// cursor position. In this case the completion might be stored
368 /// in the BufferView's inlineCompletion property.
369 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
371 /// Return true if the inline completion should be automatic.
372 virtual bool automaticInlineCompletion() const { return true; }
373 /// Return true if the popup completion should be automatic.
374 virtual bool automaticPopupCompletion() const { return true; }
375 /// Return true if the cursor should indicate a completion.
376 virtual bool showCompletionCursor() const { return true; }
377 /// Returns completion suggestions at cursor position. Return an
378 /// null pointer if no completion is a available or possible.
379 /// The caller is responsible to free the returned object!
380 virtual CompletionList const * createCompletionList(Cursor const &) const
382 /// Returns the completion prefix to filter the suggestions for completion.
383 /// This is only called if completionList returned a non-null list.
384 virtual docstring completionPrefix(Cursor const &) const;
385 /// Do a completion at the cursor position. Return true on success.
386 /// The completion does not contain the prefix. If finished is true, the
387 /// completion is final. If finished is false, completion might only be
388 /// a partial completion.
389 virtual bool insertCompletion(Cursor & /*cur*/,
390 docstring const & /*completion*/, bool /*finished*/)
392 /// Get the completion inset position and size
393 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
394 Dimension & /*dim*/) const {}
396 /// returns true if the inset can hold an inset of given type
397 virtual bool insetAllowed(InsetCode) const { return false; }
398 /// should this inset use the empty layout by default rather than
399 /// the standard layout? (default: only if that is forced.)
400 virtual bool usePlainLayout() const { return forcePlainLayout(); }
401 /// if this inset has paragraphs should they be forced to use the
403 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
404 /// if this inset has paragraphs should the user be allowed to
405 /// customize alignment, etc?
406 virtual bool allowParagraphCustomization(idx_type = 0) const
408 /// Is the width forced to some value?
409 virtual bool hasFixedWidth() const { return false; }
410 /// if this inset has paragraphs should they be forced to use a
411 /// local font language switch?
412 virtual bool forceLocalFontSwitch() const { return false; }
414 /// Is the content of this inset part of the output document?
415 virtual bool producesOutput() const { return true; }
416 /// Is the content of this inset part of the immediate (visible) text sequence?
417 virtual bool isPartOfTextSequence() const { return producesOutput(); }
419 /// \return Tool tip for this inset.
420 /// This default implementation returns an empty string. This can be
421 /// either plain text or Qt html, and formatToolTip will be called
422 /// on it before display in both cases.
423 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
425 /// \return Context menu identifier. This function determines
426 /// whose Inset's menu should be shown for the given position.
427 virtual std::string contextMenu(BufferView const & bv, int x, int y) const;
429 /// \return Context menu identifier for this inset.
430 /// This default implementation returns an empty string.
431 virtual std::string contextMenuName() const;
434 virtual docstring layoutName() const;
436 virtual InsetLayout const & getLayout() const;
438 virtual bool isPassThru() const { return getLayout().isPassThru(); }
439 /// Is this inset's layout defined in the document's textclass?
440 bool undefined() const;
441 /// should this inset be handled like a normal character?
442 /// (a character can be a letter or punctuation)
443 virtual bool isChar() const { return false; }
444 /// is this equivalent to a letter?
445 /// (a letter is a character that is considered part of a word)
446 virtual bool isLetter() const { return false; }
447 /// is this equivalent to a space (which is BTW different from
448 /// a line separator)?
449 virtual bool isSpace() const { return false; }
450 /// does this inset try to use all available space (like \\hfill does)?
451 virtual bool isHfill() const { return false; }
460 /// should we have a non-filled line before this inset?
461 virtual DisplayType display() const { return Inline; }
463 virtual LyXAlignment contentAlignment() const { return LYX_ALIGN_NONE; }
464 /// should we break lines after this inset?
465 virtual bool isLineSeparator() const { return false; }
466 /// should paragraph indendation be ommitted in any case?
467 virtual bool neverIndent() const { return false; }
468 /// dumps content to lyxerr
469 virtual void dump() const;
470 /// write inset in .lyx format
471 virtual void write(std::ostream &) const {}
472 /// read inset in .lyx format
473 virtual void read(Lexer &) {}
474 /** Export the inset to LaTeX.
475 * Don't use a temporary stringstream if the final output is
476 * supposed to go to a file.
477 * \sa Buffer::writeLaTeXSource for the reason.
479 virtual void latex(otexstream &, OutputParams const &) const {}
480 /// returns true to override begin and end inset in file
481 virtual bool directWrite() const;
483 virtual bool allowSpellCheck() const { return false; }
485 /// if this insets owns text cells (e.g. InsetText) return cell num
486 virtual Text * getText(int /*num*/) const { return 0; }
488 /** Adds a LaTeX snippet to the Preview Loader for transformation
489 * into a bitmap image. Does not start the laoding process.
491 * Most insets have no interest in this capability, so the method
494 virtual void addPreview(DocIterator const &,
495 graphics::PreviewLoader &) const {}
497 /** Classifies the unicode characters appearing in a math inset
498 * depending on whether they are to be translated as latex
499 * math/text commands or used as math symbols without translation.
501 * Only math insets have interest in this classification, so the
502 * method defaults to empty.
504 virtual void initUnicodeMath() const {}
506 /// Add an entry to the TocList
507 /// Pass a DocIterator that points at the paragraph containing
510 /// \param output_active : is the inset active or is it in an inactive
511 /// branch or a note?
513 /// \param utype : is the toc being generated for use by the output
515 virtual void addToToc(DocIterator const & /* di */,
516 bool /* output_active */,
517 UpdateType /* utype*/) const {}
518 /// Collect BibTeX information
519 virtual void collectBibKeys(InsetIterator const &) const {}
520 /// Update the counters of this inset and of its contents.
521 /// The boolean indicates whether we are preparing for output, e.g.,
523 virtual void updateBuffer(ParIterator const &, UpdateType) {}
525 /// Updates the inset's dialog
526 virtual Buffer const * updateFrontend() const;
529 /// returns LyX code associated with the inset. Used for TOC, ...)
530 virtual InsetCode lyxCode() const { return NO_CODE; }
533 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
534 /// return text or mathmode if that is possible to determine
535 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
536 /// returns whether changing mode during latex export is forbidden
537 virtual bool lockedMode() const { return false; }
538 /// returns whether only ascii chars are allowed during latex export
539 virtual bool asciiOnly() const { return false; }
540 /// returns whether this inset is allowed in other insets of given mode
541 virtual bool allowedIn(mode_type) const { return true; }
543 * The font is inherited from the parent for LaTeX export if this
544 * method returns true. No open font changes are closed in front of
545 * the inset for LaTeX export, and the font is inherited for all other
546 * exports as well as on screen.
547 * If this method returns false all open font changes are closed in
548 * front of the inset for LaTeX export. The default font is used
549 * inside the inset for all exports and on screen.
551 virtual bool inheritFont() const { return true; }
553 * If this method returns true all explicitly set font attributes
554 * are reset during editing operations.
555 * For copy/paste operations the language is never changed, since
556 * the language of a given text never changes if the text is
557 * formatted differently, while other font attribues like size may
558 * need to change if the text is copied from one environment to
560 * If this method returns false no font attribute is reset.
561 * The default implementation returns true if the resetFont layout
562 * tag is set and otherwise the negation of inheritFont(),
563 * since inherited inset font attributes do not need to be changed,
564 * and non-inherited ones need to be set explicitly.
566 virtual bool resetFontEdit() const;
568 /// set the change for the entire inset
569 virtual void setChange(Change const &) {}
570 /// accept the changes within the inset
571 virtual void acceptChanges() {}
572 /// reject the changes within the inset
573 virtual void rejectChanges() {}
576 virtual ColorCode backgroundColor(PainterInfo const &) const;
578 virtual ColorCode labelColor() const;
580 enum { TEXT_TO_INSET_OFFSET = 4 };
584 Inset(Buffer * buf) : buffer_(buf) {}
585 Inset(Inset const &) : buffer_(0) {}
587 /// replicate ourselves
588 friend class InsetList;
589 friend class MathAtom;
590 virtual Inset * clone() const = 0;
592 /** The real dispatcher.
593 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
594 * assumes the common case of 'LFUN handled, need update'.
595 * This has to be overriden by calling Cursor::undispatched() or
596 * Cursor::noScreenUpdate() if appropriate.
597 * If you need to call the dispatch method of some inset directly
598 * you may have to explicitly request an update at that place. Don't
599 * do it in doDispatch(), since that causes nested updates when
600 * called from Cursor::dispatch(), and these can lead to crashes.
603 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);