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"
22 #include "support/strfwd.h"
23 #include "support/types.h"
39 class InsetCollapsable;
53 class ParConstIterator;
59 namespace graphics { class PreviewLoader; }
62 /** returns the InsetCode corresponding to the \c name.
63 * Eg, insetCode("branch") == BRANCH_CODE
64 * Implemented in 'Inset.cpp'.
66 InsetCode insetCode(std::string const & name);
68 std::string insetName(InsetCode);
70 /// Common base class to all insets
72 // Do not add _any_ (non-static) data members as this would inflate
73 // everything storing large quantities of insets. Mathed e.g. would
80 ENTRY_DIRECTION_IGNORE,
81 ENTRY_DIRECTION_RIGHT,
85 typedef ptrdiff_t difference_type;
86 /// short of anything else reasonable
87 typedef size_t size_type;
88 /// type for cell indices
89 typedef size_t idx_type;
90 /// type for cursor positions
91 typedef ptrdiff_t pos_type;
92 /// type for row numbers
93 typedef size_t row_type;
94 /// type for column numbers
95 typedef size_t col_type;
97 /// virtual base class destructor
100 /// change associated Buffer
101 virtual void setBuffer(Buffer & buffer);
102 /// remove the buffer reference
103 void resetBuffer() { setBuffer( *static_cast<Buffer *>(0)); }
104 /// retrieve associated Buffer
105 virtual Buffer & buffer();
106 virtual Buffer const & buffer() const;
107 /// This checks whether the Buffer * actually points to an open
108 /// Buffer. It might not if that Buffer has been closed.
109 bool isBufferValid() const;
111 /// initialize view for this inset.
113 * This is typically used after this inset is created interactively.
114 * Intented purpose is to sanitize internal state with regard to current
115 * Buffer. The default implementation calls buffer().updateLabels() if
116 * the inset is labeled.
120 virtual void initView();
121 /// \return true if this inset is labeled.
122 virtual bool isLabeled() const { return false; }
124 /// identification as math inset
125 virtual InsetMath * asInsetMath() { return 0; }
126 /// identification as math inset
127 virtual InsetMath const * asInsetMath() const { return 0; }
128 /// true for 'math' math inset, but not for e.g. mbox
129 virtual bool inMathed() const { return false; }
130 /// is this inset based on the InsetText class?
131 virtual InsetText * asInsetText() { return 0; }
132 /// is this inset based on the InsetText class?
133 virtual InsetText const * asInsetText() const { return 0; }
134 /// is this inset based on the InsetCollapsable class?
135 virtual InsetCollapsable * asInsetCollapsable() { return 0; }
136 /// is this inset based on the InsetCollapsable class?
137 virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
138 /// is this inset based on the InsetTabular class?
139 virtual InsetTabular * asInsetTabular() { return 0; }
140 /// is this inset based on the InsetTabular class?
141 virtual InsetTabular const * asInsetTabular() const { return 0; }
142 /// is this inset based on the InsetCommand class?
143 virtual InsetCommand * asInsetCommand() { return 0; }
144 /// is this inset based on the InsetCommand class?
145 virtual InsetCommand const * asInsetCommand() const { return 0; }
147 /// the real dispatcher
148 void dispatch(Cursor & cur, FuncRequest & cmd);
150 * \returns true if this function made a definitive decision on
151 * whether the inset wants to handle the request \p cmd or not.
152 * The result of this decision is put into \p status.
154 * Every request that is enabled in this method needs to be handled
155 * in doDispatch(). Normally we have a 1:1 relationship between the
156 * requests handled in getStatus() and doDispatch(), but there are
158 * - A request that is disabled in getStatus() does not need to
159 * appear in doDispatch(). It is guaranteed that doDispatch()
160 * is never called with this request.
161 * - A few requests are en- or disabled in Inset::getStatus().
162 * These need to be handled in the doDispatch() methods of the
163 * derived insets, since Inset::doDispatch() has not enough
164 * information to handle them.
165 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
166 * are dispatched directly
168 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
169 FuncStatus & status) const;
172 virtual void edit(Cursor & cur, bool front,
173 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
175 virtual Inset * editXY(Cursor & cur, int x, int y);
177 /// compute the size of the object returned in dim
178 /// \retval true if metrics changed.
179 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
180 /// draw inset and update (xo, yo)-cache
181 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
182 /// draw inset selection if necessary
183 virtual void drawSelection(PainterInfo &, int, int) const {}
184 /// draw inset background if the inset has an own background and a
185 /// selection is drawn by drawSelection.
186 virtual void drawBackground(PainterInfo &, int, int) const {}
188 virtual bool editing(BufferView const * bv) const;
190 virtual bool showInsetDialog(BufferView *) const { return false; }
192 /// draw inset decoration if necessary.
193 /// This can use \c drawMarkers() for example.
194 virtual void drawDecoration(PainterInfo &, int, int) const {}
195 /// draw four angular markers
196 void drawMarkers(PainterInfo & pi, int x, int y) const;
197 /// draw two angular markers
198 void drawMarkers2(PainterInfo & pi, int x, int y) const;
199 /// add space for markers
200 void metricsMarkers(Dimension & dim, int framesize = 1) const;
201 /// add space for markers
202 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
203 /// last drawn position for 'important' insets
204 int xo(BufferView const & bv) const;
205 /// last drawn position for 'important' insets
206 int yo(BufferView const & bv) const;
207 /// set x/y drawing position cache if available
208 virtual void setPosCache(PainterInfo const &, int, int) const;
210 void setDimCache(MetricsInfo const &, Dimension const &) const;
211 /// do we cover screen position x/y?
212 virtual bool covers(BufferView const & bv, int x, int y) const;
213 /// get the screen positions of the cursor (see note in Cursor.cpp)
214 virtual void cursorPos(BufferView const & bv,
215 CursorSlice const & sl, bool boundary, int & x, int & y) const;
217 /// Allow multiple blanks
218 virtual bool isFreeSpacing() const;
219 /// Don't eliminate empty paragraphs
220 virtual bool allowEmpty() const;
221 /// Force inset into LTR environment if surroundings are RTL?
222 virtual bool forceLTR() const;
224 /// Where should we go when we press the up or down cursor key?
225 virtual bool idxUpDown(Cursor & cur, bool up) const;
226 /// Move one cell backwards
227 virtual bool idxBackward(Cursor &) const { return false; }
228 /// Move one cell forward
229 virtual bool idxForward(Cursor &) const { return false; }
231 /// Move to the next cell
232 virtual bool idxNext(Cursor &) const { return false; }
233 /// Move to the previous cell
234 virtual bool idxPrev(Cursor &) const { return false; }
236 /// Target pos when we enter the inset while moving forward
237 virtual bool idxFirst(Cursor &) const { return false; }
238 /// Target pos when we enter the inset while moving backwards
239 virtual bool idxLast(Cursor &) const { return false; }
241 /// Delete a cell and move cursor
242 virtual bool idxDelete(idx_type &) { return false; }
243 /// pulls cell after pressing erase
244 virtual void idxGlue(idx_type) {}
245 /// returns list of cell indices that are "between" from and to for
246 /// selection purposes
247 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
249 /// to which column belongs a cell with a given index?
250 virtual col_type col(idx_type) const { return 0; }
251 /// to which row belongs a cell with a given index?
252 virtual row_type row(idx_type) const { return 0; }
253 /// cell index corresponding to row and column;
254 virtual idx_type index(row_type row, col_type col) const;
255 /// any additional x-offset when drawing a cell?
256 virtual int cellXOffset(idx_type) const { return 0; }
257 /// any additional y-offset when drawing a cell?
258 virtual int cellYOffset(idx_type) const { return 0; }
259 /// number of embedded cells
260 virtual size_t nargs() const { return 0; }
261 /// number of rows in gridlike structures
262 virtual size_t nrows() const { return 0; }
263 /// number of columns in gridlike structures
264 virtual size_t ncols() const { return 0; }
265 /// Is called when the cursor leaves this inset.
266 /// Returns true if cursor is now invalid, e.g. if former
267 /// insets in higher cursor slices of \c old do not exist
269 /// \c old is the old cursor, the last slice points to this.
270 /// \c cur is the new cursor. Use the update flags to cause a redraw.
271 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
273 /// Is called when the cursor enters 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 cur is the new cursor, some slice points to this. Use the update flags to cause a redraw.
278 virtual bool notifyCursorEnters(Cursor & /*cur*/)
280 /// is called when the mouse enter or leave this inset
281 /// return true if this inset needs repaint
282 virtual bool setMouseHover(bool) { return false; }
283 /// return true if this inset is hovered (under mouse)
284 /// This is by now only used by mathed to draw corners
285 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
286 /// Other insets do not have to redefine this function to
287 /// return the correct status of mouseHovered.
288 virtual bool mouseHovered() const { return false; }
290 /// request "external features"
291 virtual void validate(LaTeXFeatures &) const {}
293 /// describe content if cursor inside
294 virtual void infoize(odocstream &) const {}
295 /// describe content if cursor behind
296 virtual void infoize2(odocstream &) const {}
298 enum { PLAINTEXT_NEWLINE = 10000 };
300 /// plain text output in ucs4 encoding
301 /// return the number of characters; in case of multiple lines of
302 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
303 virtual int plaintext(odocstream &, OutputParams const &) const = 0;
305 virtual int docbook(odocstream & os, OutputParams const &) const;
307 /// the inset is expected to write XHTML to the XHTMLStream
308 /// \return any "deferred" material that should be written outside the
309 /// normal stream, and which will in fact be written after the current
310 /// paragraph closes. this is appropriate e.g. for floats.
311 virtual docstring xhtml(XHTMLStream & xs, OutputParams const &) const;
312 // the old one, to be removed
313 virtual docstring xhtml(odocstream & os, OutputParams const &) const;
314 /// the string that is passed to the TOC
315 virtual void tocString(odocstream &) const {}
317 /// can the contents of the inset be edited on screen ?
318 // true for InsetCollapsables (not ButtonOnly) (not InsetInfo), InsetText
319 virtual bool editable() const;
320 /// has the Inset settings that can be modified in a dialog ?
321 virtual bool hasSettings() const;
322 /// can we go further down on mouse click?
323 // true for InsetCaption, InsetCollapsables (not ButtonOnly), InsetTabular
324 virtual bool descendable(BufferView const &) const { return false; }
325 /// is this an inset that can be moved into?
326 /// FIXME: merge with editable()
327 // true for InsetTabular & InsetText
328 virtual bool isActive() const { return nargs() > 0; }
330 /// does this contain text that can be change track marked in DVI?
331 virtual bool canTrackChanges() const { return false; }
332 /// return true if the inset should be removed automatically
333 virtual bool autoDelete() const;
335 /// Returns true if the inset supports completions.
336 virtual bool completionSupported(Cursor const &) const { return false; }
337 /// Returns true if the inset supports inline completions at the
338 /// cursor position. In this case the completion might be stored
339 /// in the BufferView's inlineCompletion property.
340 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
342 /// Return true if the inline completion should be automatic.
343 virtual bool automaticInlineCompletion() const { return true; }
344 /// Return true if the popup completion should be automatic.
345 virtual bool automaticPopupCompletion() const { return true; }
346 /// Return true if the cursor should indicate a completion.
347 virtual bool showCompletionCursor() const { return true; }
348 /// Returns completion suggestions at cursor position. Return an
349 /// null pointer if no completion is a available or possible.
350 /// The caller is responsible to free the returned object!
351 virtual CompletionList const * createCompletionList(Cursor const &) const
353 /// Returns the completion prefix to filter the suggestions for completion.
354 /// This is only called if completionList returned a non-null list.
355 virtual docstring completionPrefix(Cursor const &) const;
356 /// Do a completion at the cursor position. Return true on success.
357 /// The completion does not contain the prefix. If finished is true, the
358 /// completion is final. If finished is false, completion might only be
359 /// a partial completion.
360 virtual bool insertCompletion(Cursor & /*cur*/,
361 docstring const & /*completion*/, bool /*finished*/)
363 /// Get the completion inset position and size
364 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
365 Dimension & /*dim*/) const {}
367 /// returns true if the inset can hold an inset of given type
368 virtual bool insetAllowed(InsetCode) const { return false; }
369 /// should this inset use the empty layout by default rather than
370 /// the standard layout? (default: only if that is forced.)
371 virtual bool usePlainLayout() const { return forcePlainLayout(); }
372 /// if this inset has paragraphs should they be forced to use the
374 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
375 /// if this inset has paragraphs should the user be allowed to
376 /// customize alignment, etc?
377 virtual bool allowParagraphCustomization(idx_type = 0) const { return true; }
378 /// Is the width forced to some value?
379 virtual bool hasFixedWidth() const { return false; }
381 /// Is the content of this inset part of the output document?
382 virtual bool producesOutput() const { return true; }
384 /// \return Tool tip for this inset.
385 /// This default implementation returns an empty string.
386 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
388 /// \return Context menu identifier for this inset.
389 /// This default implementation returns an empty string.
390 virtual docstring contextMenu(BufferView const & bv, int x, int y) const;
392 // FIXME This should really disappear in favor of
393 // docstring name() const { return from_ascii(insetName(lyxCode()))); }
394 // There's no reason to be using different names in different places.
395 // But to do this we would need to change the file format, since the names
396 // used there don't correspond to what is used here.
398 virtual docstring name() const;
400 virtual InsetLayout const & getLayout() const;
401 /// Is this inset's layout defined in the document's textclass?
402 bool undefined() const;
403 /// used to toggle insets
404 /// is the inset open?
405 /// should this inset be handled like a normal charater
406 virtual bool isChar() const { return false; }
407 /// is this equivalent to a letter?
408 virtual bool isLetter() const { return false; }
409 /// is this equivalent to a space (which is BTW different from
410 /// a line separator)?
411 virtual bool isSpace() const { return false; }
412 /// is this an expandible space (rubber length)?
413 virtual bool isStretchableSpace() const { return false; }
422 /// should we have a non-filled line before this inset?
423 virtual DisplayType display() const { return Inline; }
425 virtual LyXAlignment contentAlignment() const { return LYX_ALIGN_NONE; }
426 /// should we break lines after this inset?
427 virtual bool isLineSeparator() const { return false; }
428 /// should paragraph indendation be ommitted in any case?
429 virtual bool neverIndent() const { return false; }
430 /// dumps content to lyxerr
431 virtual void dump() const;
432 /// write inset in .lyx format
433 virtual void write(std::ostream &) const {}
434 /// read inset in .lyx format
435 virtual void read(Lexer &) {}
436 /** Export the inset to LaTeX.
437 * Don't use a temporary stringstream if the final output is
438 * supposed to go to a file.
439 * \sa Buffer::writeLaTeXSource for the reason.
440 * \return the number of rows (\n's) of generated LaTeX code.
442 virtual int latex(odocstream &, OutputParams const &) const { return 0; }
443 /// returns true to override begin and end inset in file
444 virtual bool directWrite() const;
446 virtual bool allowSpellCheck() const { return false; }
448 /// if this insets owns text cells (e.g. InsetText) return cell num
449 virtual Text * getText(int /*num*/) const { return 0; }
451 /** Adds a LaTeX snippet to the Preview Loader for transformation
452 * into a bitmap image. Does not start the laoding process.
454 * Most insets have no interest in this capability, so the method
457 virtual void addPreview(DocIterator const &,
458 graphics::PreviewLoader &) const {}
460 /** Classifies the unicode characters appearing in a math inset
461 * depending on whether they are to be translated as latex
462 * math/text commands or used as math symbols without translation.
464 * Only math insets have interest in this classification, so the
465 * method defaults to empty.
467 virtual void initUnicodeMath() const {}
469 /// Add an entry to the TocList
470 /// pit is the ParConstIterator of the paragraph containing the inset
471 virtual void addToToc(DocIterator const &) {}
472 /// Fill keys with BibTeX information
473 virtual void fillWithBibKeys(BiblioInfo &, InsetIterator const &) const {}
474 /// Update the counters of this inset and of its contents.
475 /// The boolean indicates whether we are preparing for output, e.g.,
477 virtual void updateLabels(ParIterator const &, bool) {}
479 /// Updates the inset's dialog
480 virtual Buffer const * updateFrontend() const;
483 /// returns LyX code associated with the inset. Used for TOC, ...)
484 virtual InsetCode lyxCode() const { return NO_CODE; }
486 /// -1: text mode, 1: math mode, 0 undecided
487 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
488 /// return text or mathmode if that is possible to determine
489 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
490 /// returns whether changing mode during latex export is forbidden
491 virtual bool lockedMode() const { return false; }
492 /// returns whether this inset is allowed in other insets of given mode
493 virtual bool allowedIn(mode_type) const { return true; }
495 * Is this inset allowed within a font change?
497 * FIXME: noFontChange means currently that the font change is closed
498 * in LaTeX before the inset, and that the contents of the inset
499 * will be in default font. This should be changed so that the inset
500 * changes the font again.
502 virtual bool noFontChange() const { return false; }
504 /// set the change for the entire inset
505 virtual void setChange(Change const &) {}
506 /// accept the changes within the inset
507 virtual void acceptChanges() {};
508 /// reject the changes within the inset
509 virtual void rejectChanges() {};
512 virtual Dimension const dimension(BufferView const &) const;
514 int scroll() const { return 0; }
516 virtual ColorCode backgroundColor() const;
518 virtual ColorCode labelColor() const;
520 enum { TEXT_TO_INSET_OFFSET = 4 };
524 Inset(Buffer * buf) : buffer_(buf) {}
525 Inset(Inset const &) : buffer_(0) {}
527 /// replicate ourselves
528 friend class InsetList;
529 friend class MathAtom;
530 virtual Inset * clone() const = 0;
532 /** The real dispatcher.
533 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
534 * assumes the common case of 'LFUN handled, need update'.
535 * This has to be overriden by calling Cursor::undispatched() or
536 * Cursor::noUpdate() if appropriate.
537 * If you need to call the dispatch method of some inset directly
538 * you may have to explicitly request an update at that place. Don't
539 * do it in doDispatch(), since that causes nested updates when
540 * called from Cursor::dispatch(), and these can lead to crashes.
543 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);