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 "LayoutEnums.h"
21 #include "OutputEnums.h"
23 #include "support/strfwd.h"
24 #include "support/types.h"
40 class InsetCollapsable;
54 class ParConstIterator;
61 namespace graphics { class PreviewLoader; }
64 /// returns the InsetCode corresponding to the \c name.
65 /// Eg, insetCode("branch") == BRANCH_CODE
66 InsetCode insetCode(std::string const & name);
67 /// returns the Inset name corresponding to the \c InsetCode.
68 /// Eg, insetName(BRANCH_CODE) == "branch"
69 std::string insetName(InsetCode);
70 /// returns the Inset name corresponding to the \c InsetCode.
71 /// Eg, insetDisplayName(BRANCH_CODE) == _("Branch")
72 docstring insetDisplayName(InsetCode);
74 static int const TOC_ENTRY_LENGTH = 40;
76 /// Common base class to all insets
78 // Do not add _any_ (non-static) data members as this would inflate
79 // everything storing large quantities of insets. Mathed e.g. would
86 ENTRY_DIRECTION_IGNORE,
87 ENTRY_DIRECTION_RIGHT,
91 typedef ptrdiff_t difference_type;
92 /// short of anything else reasonable
93 typedef size_t size_type;
94 /// type for cell indices
95 typedef size_t idx_type;
96 /// type for cursor positions
97 typedef ptrdiff_t pos_type;
98 /// type for row numbers
99 typedef size_t row_type;
100 /// type for column numbers
101 typedef size_t col_type;
103 /// virtual base class destructor
106 /// change associated Buffer
107 virtual void setBuffer(Buffer & buffer);
108 /// remove the buffer reference
109 void resetBuffer() { setBuffer( *static_cast<Buffer *>(0)); }
110 /// retrieve associated Buffer
111 virtual Buffer & buffer();
112 virtual Buffer const & buffer() const;
113 /// Returns true if buffer_ actually points to a Buffer that has
114 /// been loaded into LyX and is still open. Note that this will
115 /// always return false for cloned Buffers. If you want to allow
116 /// for the case of cloned Buffers, use isBufferValid().
117 bool isBufferLoaded() const;
118 /// Returns true if this is a loaded buffer or a cloned buffer.
119 bool isBufferValid() const;
121 /// initialize view for this inset.
123 * This is typically used after this inset is created interactively.
124 * Intented purpose is to sanitize internal state with regard to current
127 virtual void initView() {};
128 /// \return true if this inset is labeled.
129 virtual bool isLabeled() const { return false; }
131 /// identification as math inset
132 virtual InsetMath * asInsetMath() { return 0; }
133 /// identification as math inset
134 virtual InsetMath const * asInsetMath() const { return 0; }
135 /// true for 'math' math inset, but not for e.g. mbox
136 virtual bool inMathed() const { return false; }
137 /// is this inset based on the InsetText class?
138 virtual InsetText * asInsetText() { return 0; }
139 /// is this inset based on the InsetText class?
140 virtual InsetText const * asInsetText() const { return 0; }
141 /// is this inset based on the InsetCollapsable class?
142 virtual InsetCollapsable * asInsetCollapsable() { return 0; }
143 /// is this inset based on the InsetCollapsable class?
144 virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
145 /// is this inset based on the InsetTabular class?
146 virtual InsetTabular * asInsetTabular() { return 0; }
147 /// is this inset based on the InsetTabular class?
148 virtual InsetTabular const * asInsetTabular() const { return 0; }
149 /// is this inset based on the InsetCommand class?
150 virtual InsetCommand * asInsetCommand() { return 0; }
151 /// is this inset based on the InsetCommand class?
152 virtual InsetCommand const * asInsetCommand() const { return 0; }
154 /// the real dispatcher
155 void dispatch(Cursor & cur, FuncRequest & cmd);
157 * \returns true if this function made a definitive decision on
158 * whether the inset wants to handle the request \p cmd or not.
159 * The result of this decision is put into \p status.
161 * Every request that is enabled in this method needs to be handled
162 * in doDispatch(). Normally we have a 1:1 relationship between the
163 * requests handled in getStatus() and doDispatch(), but there are
165 * - A request that is disabled in getStatus() does not need to
166 * appear in doDispatch(). It is guaranteed that doDispatch()
167 * is never called with this request.
168 * - A few requests are en- or disabled in Inset::getStatus().
169 * These need to be handled in the doDispatch() methods of the
170 * derived insets, since Inset::doDispatch() has not enough
171 * information to handle them.
172 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
173 * are dispatched directly
175 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
176 FuncStatus & status) const;
179 virtual void edit(Cursor & cur, bool front,
180 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
182 virtual Inset * editXY(Cursor & cur, int x, int y);
184 /// compute the size of the object returned in dim
185 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
186 /// draw inset and update (xo, yo)-cache
187 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
188 /// draw inset selection if necessary
189 virtual void drawSelection(PainterInfo &, int, int) const {}
190 /// draw inset background if the inset has an own background and a
191 /// selection is drawn by drawSelection.
192 virtual void drawBackground(PainterInfo &, int, int) const {}
194 virtual bool editing(BufferView const * bv) const;
196 virtual bool showInsetDialog(BufferView *) const;
198 /// draw inset decoration if necessary.
199 /// This can use \c drawMarkers() for example.
200 virtual void drawDecoration(PainterInfo &, int, int) const {}
201 /// draw four angular markers
202 void drawMarkers(PainterInfo & pi, int x, int y) const;
203 /// draw two angular markers
204 void drawMarkers2(PainterInfo & pi, int x, int y) const;
205 /// add space for markers
206 void metricsMarkers(Dimension & dim, int framesize = 1) const;
207 /// add space for markers
208 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
209 /// last drawn position for 'important' insets
210 int xo(BufferView const & bv) const;
211 /// last drawn position for 'important' insets
212 int yo(BufferView const & bv) const;
213 /// set x/y drawing position cache if available
214 virtual void setPosCache(PainterInfo const &, int, int) const;
216 void setDimCache(MetricsInfo const &, Dimension const &) const;
217 /// do we cover screen position x/y?
218 virtual bool covers(BufferView const & bv, int x, int y) const;
219 /// get the screen positions of the cursor (see note in Cursor.cpp)
220 virtual void cursorPos(BufferView const & bv,
221 CursorSlice const & sl, bool boundary, int & x, int & y) const;
223 /// Allow multiple blanks
224 virtual bool isFreeSpacing() const;
225 /// Don't eliminate empty paragraphs
226 virtual bool allowEmpty() const;
227 /// Force inset into LTR environment if surroundings are RTL
228 virtual bool forceLTR() const;
229 /// whether to include this inset in the strings generated for the TOC
230 virtual bool isInToc() const;
232 /// Where should we go when we press the up or down cursor key?
233 virtual bool idxUpDown(Cursor & cur, bool up) const;
234 /// Move one cell backwards
235 virtual bool idxBackward(Cursor &) const { return false; }
236 /// Move one cell forward
237 virtual bool idxForward(Cursor &) const { return false; }
239 /// Move to the next cell
240 virtual bool idxNext(Cursor &) const { return false; }
241 /// Move to the previous cell
242 virtual bool idxPrev(Cursor &) const { return false; }
244 /// Target pos when we enter the inset while moving forward
245 virtual bool idxFirst(Cursor &) const { return false; }
246 /// Target pos when we enter the inset while moving backwards
247 virtual bool idxLast(Cursor &) const { return false; }
249 /// Delete a cell and move cursor
250 virtual bool idxDelete(idx_type &) { return false; }
251 /// pulls cell after pressing erase
252 virtual void idxGlue(idx_type) {}
253 /// returns list of cell indices that are "between" from and to for
254 /// selection purposes
255 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
257 /// to which column belongs a cell with a given index?
258 virtual col_type col(idx_type) const { return 0; }
259 /// to which row belongs a cell with a given index?
260 virtual row_type row(idx_type) const { return 0; }
261 /// cell index corresponding to row and column;
262 virtual idx_type index(row_type row, col_type col) const;
263 /// any additional x-offset when drawing a cell?
264 virtual int cellXOffset(idx_type) const { return 0; }
265 /// any additional y-offset when drawing a cell?
266 virtual int cellYOffset(idx_type) const { return 0; }
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(odocstream &, OutputParams const &) 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 forToc(docstring & str,
333 size_t maxlen = TOC_ENTRY_LENGTH) const;
335 /// can the contents of the inset be edited on screen ?
336 // true for InsetCollapsables (not ButtonOnly) (not InsetInfo), InsetText
337 virtual bool editable() const;
338 /// has the Inset settings that can be modified in a dialog ?
339 virtual bool hasSettings() const;
340 /// can we go further down on mouse click?
341 // true for InsetCaption, InsetCollapsables (not ButtonOnly), InsetTabular
342 virtual bool descendable(BufferView const &) const { return false; }
343 /// is this an inset that can be moved into?
344 /// FIXME: merge with editable()
345 // true for InsetTabular & InsetText
346 virtual bool isActive() const { return nargs() > 0; }
347 /// can we click at the specified position ?
348 virtual bool clickable(int, int) const { return false; }
350 /// does this contain text that can be change track marked in DVI?
351 virtual bool canTrackChanges() const { return false; }
352 /// return true if the inset should be removed automatically
353 virtual bool autoDelete() const;
355 /// Returns true if the inset supports completions.
356 virtual bool completionSupported(Cursor const &) const { return false; }
357 /// Returns true if the inset supports inline completions at the
358 /// cursor position. In this case the completion might be stored
359 /// in the BufferView's inlineCompletion property.
360 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
362 /// Return true if the inline completion should be automatic.
363 virtual bool automaticInlineCompletion() const { return true; }
364 /// Return true if the popup completion should be automatic.
365 virtual bool automaticPopupCompletion() const { return true; }
366 /// Return true if the cursor should indicate a completion.
367 virtual bool showCompletionCursor() const { return true; }
368 /// Returns completion suggestions at cursor position. Return an
369 /// null pointer if no completion is a available or possible.
370 /// The caller is responsible to free the returned object!
371 virtual CompletionList const * createCompletionList(Cursor const &) const
373 /// Returns the completion prefix to filter the suggestions for completion.
374 /// This is only called if completionList returned a non-null list.
375 virtual docstring completionPrefix(Cursor const &) const;
376 /// Do a completion at the cursor position. Return true on success.
377 /// The completion does not contain the prefix. If finished is true, the
378 /// completion is final. If finished is false, completion might only be
379 /// a partial completion.
380 virtual bool insertCompletion(Cursor & /*cur*/,
381 docstring const & /*completion*/, bool /*finished*/)
383 /// Get the completion inset position and size
384 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
385 Dimension & /*dim*/) const {}
387 /// returns true if the inset can hold an inset of given type
388 virtual bool insetAllowed(InsetCode) const { return false; }
389 /// should this inset use the empty layout by default rather than
390 /// the standard layout? (default: only if that is forced.)
391 virtual bool usePlainLayout() const { return forcePlainLayout(); }
392 /// if this inset has paragraphs should they be forced to use the
394 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
395 /// if this inset has paragraphs should the user be allowed to
396 /// customize alignment, etc?
397 virtual bool allowParagraphCustomization(idx_type = 0) const
399 /// Is the width forced to some value?
400 virtual bool hasFixedWidth() const { return false; }
402 /// Is the content of this inset part of the output document?
403 virtual bool producesOutput() const { return true; }
405 /// \return Tool tip for this inset.
406 /// This default implementation returns an empty string.
407 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
409 /// \return Context menu identifier. This function determines
410 /// whose Inset's menu should be shown for the given position.
411 virtual docstring contextMenu(BufferView const & bv, int x, int y) const;
413 /// \return Context menu identifier for this inset.
414 /// This default implementation returns an empty string.
415 virtual docstring contextMenuName() const;
418 // FIXME This should really disappear in favor of
419 // docstring name() const { return from_ascii(insetName(lyxCode()))); }
420 // There's no reason to be using different names in different places.
421 // But to do this we would need to change the file format, since the names
422 // used there don't correspond to what is used here.
424 virtual docstring name() const;
426 virtual InsetLayout const & getLayout() const;
427 /// Is this inset's layout defined in the document's textclass?
428 bool undefined() const;
429 /// used to toggle insets
430 /// is the inset open?
431 /// should this inset be handled like a normal charater
432 virtual bool isChar() const { return false; }
433 /// is this equivalent to a letter?
434 virtual bool isLetter() const { return false; }
435 /// is this equivalent to a space (which is BTW different from
436 /// a line separator)?
437 virtual bool isSpace() const { return false; }
438 /// is this an expandible space (rubber length)?
439 virtual bool isStretchableSpace() const { return false; }
448 /// should we have a non-filled line before this inset?
449 virtual DisplayType display() const { return Inline; }
451 virtual LyXAlignment contentAlignment() const { return LYX_ALIGN_NONE; }
452 /// should we break lines after this inset?
453 virtual bool isLineSeparator() const { return false; }
454 /// should paragraph indendation be ommitted in any case?
455 virtual bool neverIndent() const { return false; }
456 /// dumps content to lyxerr
457 virtual void dump() const;
458 /// write inset in .lyx format
459 virtual void write(std::ostream &) const {}
460 /// read inset in .lyx format
461 virtual void read(Lexer &) {}
462 /** Export the inset to LaTeX.
463 * Don't use a temporary stringstream if the final output is
464 * supposed to go to a file.
465 * \sa Buffer::writeLaTeXSource for the reason.
467 virtual void latex(otexstream &, OutputParams const &) const {}
468 /// returns true to override begin and end inset in file
469 virtual bool directWrite() const;
471 virtual bool allowSpellCheck() const { return false; }
473 /// if this insets owns text cells (e.g. InsetText) return cell num
474 virtual Text * getText(int /*num*/) const { return 0; }
476 /** Adds a LaTeX snippet to the Preview Loader for transformation
477 * into a bitmap image. Does not start the laoding process.
479 * Most insets have no interest in this capability, so the method
482 virtual void addPreview(DocIterator const &,
483 graphics::PreviewLoader &) const {}
485 /** Classifies the unicode characters appearing in a math inset
486 * depending on whether they are to be translated as latex
487 * math/text commands or used as math symbols without translation.
489 * Only math insets have interest in this classification, so the
490 * method defaults to empty.
492 virtual void initUnicodeMath() const {}
494 /// Add an entry to the TocList
495 /// Pass a DocIterator that points at the paragraph containing
497 virtual void addToToc(DocIterator const &) const {}
498 /// Collect BibTeX information
499 virtual void collectBibKeys(InsetIterator const &) const {}
500 /// Update the counters of this inset and of its contents.
501 /// The boolean indicates whether we are preparing for output, e.g.,
503 virtual void updateBuffer(ParIterator const &, UpdateType) {}
505 /// Updates the inset's dialog
506 virtual Buffer const * updateFrontend() const;
509 /// returns LyX code associated with the inset. Used for TOC, ...)
510 virtual InsetCode lyxCode() const { return NO_CODE; }
512 /// -1: text mode, 1: math mode, 0 undecided
513 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
514 /// return text or mathmode if that is possible to determine
515 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
516 /// returns whether changing mode during latex export is forbidden
517 virtual bool lockedMode() const { return false; }
518 /// returns whether only ascii chars are allowed during latex export
519 virtual bool asciiOnly() const { return false; }
520 /// returns whether this inset is allowed in other insets of given mode
521 virtual bool allowedIn(mode_type) const { return true; }
523 * Is this inset allowed within a font change?
525 * FIXME: noFontChange means currently that the font change is closed
526 * in LaTeX before the inset, and that the contents of the inset
527 * will be in default font. This should be changed so that the inset
528 * changes the font again.
530 virtual bool noFontChange() const { return false; }
532 /// set the change for the entire inset
533 virtual void setChange(Change const &) {}
534 /// accept the changes within the inset
535 virtual void acceptChanges() {}
536 /// reject the changes within the inset
537 virtual void rejectChanges() {}
540 virtual Dimension const dimension(BufferView const &) const;
542 virtual ColorCode backgroundColor(PainterInfo const &) const;
544 virtual ColorCode labelColor() const;
546 enum { TEXT_TO_INSET_OFFSET = 4 };
550 Inset(Buffer * buf) : buffer_(buf) {}
551 Inset(Inset const &) : buffer_(0) {}
553 /// replicate ourselves
554 friend class InsetList;
555 friend class MathAtom;
556 virtual Inset * clone() const = 0;
558 /** The real dispatcher.
559 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
560 * assumes the common case of 'LFUN handled, need update'.
561 * This has to be overriden by calling Cursor::undispatched() or
562 * Cursor::noScreenUpdate() if appropriate.
563 * If you need to call the dispatch method of some inset directly
564 * you may have to explicitly request an update at that place. Don't
565 * do it in doDispatch(), since that causes nested updates when
566 * called from Cursor::dispatch(), and these can lead to crashes.
569 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);