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"
21 #include "support/strfwd.h"
22 #include "support/types.h"
39 class InsetCollapsable;
52 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 /// FIXME this should go.
102 virtual void setBuffer(Buffer & buffer);
103 /// retrieve associated Buffer
104 virtual Buffer & buffer();
105 virtual Buffer const & buffer() const;
106 /// This checks whether the Buffer * actually points to an open
107 /// Buffer. It might not if that Buffer has been closed.
108 bool isBufferValid() const;
110 /// initialize view for this inset.
112 * This is typically used after this inset is created interactively.
113 * Intented purpose is to sanitize internal state with regard to current
114 * Buffer. The default implementation calls updateLabels(buffer()) is
115 * the inset is labeled.
119 virtual void initView();
120 /// \return true if this inset is labeled.
121 virtual bool isLabeled() const { return false; }
123 /// identification as math inset
124 virtual InsetMath * asInsetMath() { return 0; }
125 /// true for 'math' math inset, but not for e.g. mbox
126 virtual bool inMathed() const { return false; }
127 /// is this inset based on the InsetText class?
128 virtual InsetText * asInsetText() { return 0; }
129 /// is this inset based on the InsetText class?
130 virtual InsetText const * asInsetText() const { return 0; }
131 /// is this inset based on the InsetCollapsable class?
132 virtual InsetCollapsable * asInsetCollapsable() { return 0; }
133 /// is this inset based on the InsetCollapsable class?
134 virtual InsetCollapsable const * asInsetCollapsable() const { return 0; }
135 /// is this inset based on the InsetTabular class?
136 virtual InsetTabular * asInsetTabular() { return 0; }
137 /// is this inset based on the InsetTabular class?
138 virtual InsetTabular const * asInsetTabular() const { return 0; }
140 /// the real dispatcher
141 void dispatch(Cursor & cur, FuncRequest & cmd);
143 * \returns true if this function made a definitive decision on
144 * whether the inset wants to handle the request \p cmd or not.
145 * The result of this decision is put into \p status.
147 * Every request that is enabled in this method needs to be handled
148 * in doDispatch(). Normally we have a 1:1 relationship between the
149 * requests handled in getStatus() and doDispatch(), but there are
151 * - A request that is disabled in getStatus() does not need to
152 * appear in doDispatch(). It is guaranteed that doDispatch()
153 * is never called with this request.
154 * - A few requests are en- or disabled in Inset::getStatus().
155 * These need to be handled in the doDispatch() methods of the
156 * derived insets, since Inset::doDispatch() has not enough
157 * information to handle them.
158 * - LFUN_MOUSE_* need not to be handled in getStatus(), because these
159 * are dispatched directly
161 virtual bool getStatus(Cursor & cur, FuncRequest const & cmd,
162 FuncStatus & status) const;
165 virtual void edit(Cursor & cur, bool front,
166 EntryDirection entry_from = ENTRY_DIRECTION_IGNORE);
168 virtual Inset * editXY(Cursor & cur, int x, int y);
170 /// compute the size of the object returned in dim
171 /// \retval true if metrics changed.
172 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
173 /// draw inset and update (xo, yo)-cache
174 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
175 /// draw inset selection if necessary
176 virtual void drawSelection(PainterInfo &, int, int) const {}
178 virtual bool editing(BufferView const * bv) const;
180 virtual bool showInsetDialog(BufferView *) const { return false; }
182 /// draw inset decoration if necessary.
183 /// This can use \c drawMarkers() for example.
184 virtual void drawDecoration(PainterInfo &, int, int) const {}
185 /// draw four angular markers
186 void drawMarkers(PainterInfo & pi, int x, int y) const;
187 /// draw two angular markers
188 void drawMarkers2(PainterInfo & pi, int x, int y) const;
189 /// add space for markers
190 void metricsMarkers(Dimension & dim, int framesize = 1) const;
191 /// add space for markers
192 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
193 /// last drawn position for 'important' insets
194 int xo(BufferView const & bv) const;
195 /// last drawn position for 'important' insets
196 int yo(BufferView const & bv) const;
197 /// set x/y drawing position cache if available
198 virtual void setPosCache(PainterInfo const &, int, int) const;
200 void setDimCache(MetricsInfo const &, Dimension const &) const;
201 /// do we cover screen position x/y?
202 virtual bool covers(BufferView const & bv, int x, int y) const;
203 /// get the screen positions of the cursor (see note in Cursor.cpp)
204 virtual void cursorPos(BufferView const & bv,
205 CursorSlice const & sl, bool boundary, int & x, int & y) const;
208 virtual bool isFreeSpacing() const { return false; }
210 virtual bool allowEmpty() const { return false; }
211 /// Force inset into LTR environment if surroundings are RTL?
212 virtual bool forceLTR() const { return false; }
214 /// is this an inset that can be moved into?
215 /// FIXME: merge with editable()
216 virtual bool isActive() const { return nargs() > 0; }
217 /// Where should we go when we press the up or down cursor key?
218 virtual bool idxUpDown(Cursor & cur, bool up) const;
219 /// Move one cell backwards
220 virtual bool idxBackward(Cursor &) const { return false; }
221 /// Move one cell forward
222 virtual bool idxForward(Cursor &) const { return false; }
224 /// Move to the next cell
225 virtual bool idxNext(Cursor &) const { return false; }
226 /// Move to the previous cell
227 virtual bool idxPrev(Cursor &) const { return false; }
229 /// Target pos when we enter the inset while moving forward
230 virtual bool idxFirst(Cursor &) const { return false; }
231 /// Target pos when we enter the inset while moving backwards
232 virtual bool idxLast(Cursor &) const { return false; }
234 /// Delete a cell and move cursor
235 virtual bool idxDelete(idx_type &) { return false; }
236 /// pulls cell after pressing erase
237 virtual void idxGlue(idx_type) {}
238 /// returns list of cell indices that are "between" from and to for
239 /// selection purposes
240 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
242 /// to which column belongs a cell with a given index?
243 virtual col_type col(idx_type) const { return 0; }
244 /// to which row belongs a cell with a given index?
245 virtual row_type row(idx_type) const { return 0; }
246 /// cell index corresponding to row and column;
247 virtual idx_type index(row_type row, col_type col) const;
248 /// any additional x-offset when drawing a cell?
249 virtual int cellXOffset(idx_type) const { return 0; }
250 /// any additional y-offset when drawing a cell?
251 virtual int cellYOffset(idx_type) const { return 0; }
252 /// number of embedded cells
253 virtual size_t nargs() const { return 0; }
254 /// number of rows in gridlike structures
255 virtual size_t nrows() const { return 0; }
256 /// number of columns in gridlike structures
257 virtual size_t ncols() const { return 0; }
258 /// Is called when the cursor leaves this inset.
259 /// Returns true if cursor is now invalid, e.g. if former
260 /// insets in higher cursor slices of \c old do not exist
262 /// \c old is the old cursor, i.e. there is a slice pointing to this.
263 /// \c cur is the new cursor. Use the update flags to cause a redraw.
264 virtual bool notifyCursorLeaves(Cursor const & /*old*/, Cursor & /*cur*/)
266 /// is called when the mouse enter or leave this inset
267 /// return true if this inset needs repaint
268 virtual bool setMouseHover(bool) { return false; }
269 /// return true if this inset is hovered (under mouse)
270 /// This is by now only used by mathed to draw corners
271 /// (Inset::drawMarkers() and Inset::drawMarkers2()).
272 /// Other insets do not have to redefine this function to
273 /// return the correct status of mouseHovered.
274 virtual bool mouseHovered() const { return false; }
276 /// request "external features"
277 virtual void validate(LaTeXFeatures &) const {}
279 /// describe content if cursor inside
280 virtual void infoize(odocstream &) const {}
281 /// describe content if cursor behind
282 virtual void infoize2(odocstream &) const {}
284 enum { PLAINTEXT_NEWLINE = 10000 };
286 /// plain text output in ucs4 encoding
287 /// return the number of characters; in case of multiple lines of
288 /// output, add PLAINTEXT_NEWLINE to the number of chars in the last line
289 virtual int plaintext(odocstream &, OutputParams const &) const = 0;
291 virtual int docbook(odocstream & os, OutputParams const &) const;
292 /// the string that is passed to the TOC
293 virtual void textString(odocstream &) const {}
295 /** This enum indicates by which means the inset can be modified:
296 - NOT_EDITABLE: the inset's content cannot be modified at all
297 (e.g. printindex, insetspecialchar)
298 - IS_EDITABLE: content can be edited via dialog (e.g. bibtex, index, href)
299 - HIGHLY_EDITABLE: content can be edited on screen (normally means that
300 insettext is contained, e.g. collapsables, tabular) */
301 // FIXME: This has not yet been fully implemented to math insets
310 /// what appears in the minibuffer when opening
311 virtual docstring editMessage() const;
313 virtual EDITABLE editable() const;
314 /// can we go further down on mouse click?
315 virtual bool descendable() const { return false; }
316 /// does this contain text that can be change track marked in DVI?
317 virtual bool canTrackChanges() const { return false; }
318 /// return true if the inset should be removed automatically
319 virtual bool autoDelete() const;
321 /// Returns true if the inset supports completions.
322 virtual bool completionSupported(Cursor const &) const { return false; }
323 /// Returns true if the inset supports inline completions at the
324 /// cursor position. In this case the completion might be stored
325 /// in the BufferView's inlineCompletion property.
326 virtual bool inlineCompletionSupported(Cursor const & /*cur*/) const
328 /// Return true if the inline completion should be automatic.
329 virtual bool automaticInlineCompletion() const { return true; }
330 /// Return true if the popup completion should be automatic.
331 virtual bool automaticPopupCompletion() const { return true; }
332 /// Return true if the cursor should indicate a completion.
333 virtual bool showCompletionCursor() const { return true; }
334 /// Returns completion suggestions at cursor position. Return an
335 /// null pointer if no completion is a available or possible.
336 /// The caller is responsible to free the returned object!
337 virtual CompletionList const * createCompletionList(Cursor const &) const
339 /// Returns the completion prefix to filter the suggestions for completion.
340 /// This is only called if completionList returned a non-null list.
341 virtual docstring completionPrefix(Cursor const &) const;
342 /// Do a completion at the cursor position. Return true on success.
343 /// The completion does not contain the prefix. If finished is true, the
344 /// completion is final. If finished is false, completion might only be
345 /// a partial completion.
346 virtual bool insertCompletion(Cursor & /*cur*/,
347 docstring const & /*completion*/, bool /*finished*/)
349 /// Get the completion inset position and size
350 virtual void completionPosAndDim(Cursor const &, int & /*x*/, int & /*y*/,
351 Dimension & /*dim*/) const {}
353 /// returns true if the inset can hold an inset of given type
354 virtual bool insetAllowed(InsetCode) const { return false; }
355 /// should this inset use the empty layout by default rather than
356 /// the standard layout? (default: only if that is forced.)
357 virtual bool usePlainLayout() const { return forcePlainLayout(); }
358 /// if this inset has paragraphs should they be forced to use the
360 virtual bool forcePlainLayout(idx_type = 0) const { return false; }
361 /// if this inset has paragraphs should the user be allowed to
362 /// customize alignment, etc?
363 virtual bool allowParagraphCustomization(idx_type = 0) const { return true; }
364 /// Is the width forced to some value?
365 virtual bool hasFixedWidth() const { return false; }
367 /// Is the content of this inset part of the output document?
368 virtual bool producesOutput() const { return true; }
370 /// \return Tool tip for this inset.
371 /// This default implementation returns an empty string.
372 virtual docstring toolTip(BufferView const & bv, int x, int y) const;
374 /// \return Context menu identifier for this inset.
375 /// This default implementation returns an empty string.
376 virtual docstring contextMenu(BufferView const & bv, int x, int y) const;
378 // FIXME This should really disappear in favor of
379 // docstring name() const { return from_ascii(insetName(lyxCode()))); }
380 // There's no reason to be using different names in different places.
381 // But to do this we would need to change the file format, since the names
382 // used there don't correspond to what is used here.
384 virtual docstring name() const;
386 virtual InsetLayout const & getLayout(BufferParams const & bp) const;
387 /// used to toggle insets
388 /// is the inset open?
389 /// should this inset be handled like a normal charater
390 virtual bool isChar() const { return false; }
391 /// is this equivalent to a letter?
392 virtual bool isLetter() const { return false; }
393 /// is this equivalent to a space (which is BTW different from
394 /// a line separator)?
395 virtual bool isSpace() const { return false; }
396 /// is this an expandible space (rubber length)?
397 virtual bool isStretchableSpace() const { return false; }
406 /// should we have a non-filled line before this inset?
407 virtual DisplayType display() const { return Inline; }
408 /// should we break lines after this inset?
409 virtual bool isLineSeparator() const { return false; }
410 /// should paragraph indendation be ommitted in any case?
411 virtual bool neverIndent() const { return false; }
412 /// dumps content to lyxerr
413 virtual void dump() const;
414 /// write inset in .lyx format
415 virtual void write(std::ostream &) const {}
416 /// read inset in .lyx format
417 virtual void read(Lexer &) {}
418 /** Export the inset to LaTeX.
419 * Don't use a temporary stringstream if the final output is
420 * supposed to go to a file.
421 * \sa Buffer::writeLaTeXSource for the reason.
422 * \return the number of rows (\n's) of generated LaTeX code.
424 virtual int latex(odocstream &, OutputParams const &) const { return 0; }
425 /// returns true to override begin and end inset in file
426 virtual bool directWrite() const;
428 virtual bool allowSpellCheck() const { return false; }
430 /// if this insets owns text cells (e.g. InsetText) return cell num
431 virtual Text * getText(int /*num*/) const { return 0; }
433 /** Adds a LaTeX snippet to the Preview Loader for transformation
434 * into a bitmap image. Does not start the laoding process.
436 * Most insets have no interest in this capability, so the method
439 virtual void addPreview(graphics::PreviewLoader &) const {}
440 /// Add an entry to the TocList
441 /// pit is the ParConstIterator of the paragraph containing the inset
442 virtual void addToToc(DocIterator const &) {}
443 /// Fill keys with BibTeX information
444 virtual void fillWithBibKeys(BiblioInfo &, InsetIterator const &) const {}
445 /// Update the counters of this inset and of its contents
446 virtual void updateLabels(ParIterator const &) {}
448 /// Updates the inset's dialog
449 virtual Buffer const * updateFrontend() const;
452 /// returns LyX code associated with the inset. Used for TOC, ...)
453 virtual InsetCode lyxCode() const { return NO_CODE; }
455 /// -1: text mode, 1: math mode, 0 undecided
456 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
457 /// return text or mathmode if that is possible to determine
458 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
459 /// returns whether this inset is allowed in other insets of given mode
460 virtual bool allowedIn(mode_type) const { return true; }
462 * Is this inset allowed within a font change?
464 * FIXME: noFontChange means currently that the font change is closed
465 * in LaTeX before the inset, and that the contents of the inset
466 * will be in default font. This should be changed so that the inset
467 * changes the font again.
469 virtual bool noFontChange() const { return false; }
471 /// set the change for the entire inset
472 virtual void setChange(Change const &) {}
473 /// accept the changes within the inset
474 virtual void acceptChanges(BufferParams const &) {};
475 /// reject the changes within the inset
476 virtual void rejectChanges(BufferParams const &) {};
479 virtual Dimension const dimension(BufferView const &) const;
481 int scroll() const { return 0; }
483 virtual ColorCode backgroundColor() const;
485 enum CollapseStatus {
490 virtual void setStatus(Cursor &, CollapseStatus) {}
492 enum { TEXT_TO_INSET_OFFSET = 4 };
496 explicit Inset() : buffer_(0) {}
498 /// replicate ourselves
499 friend class InsetList;
500 friend class MathAtom;
501 virtual Inset * clone() const = 0;
503 /** The real dispatcher.
504 * Gets normally called from Cursor::dispatch(). Cursor::dispatch()
505 * assumes the common case of 'LFUN handled, need update'.
506 * This has to be overriden by calling Cursor::undispatched() or
507 * Cursor::noUpdate() if appropriate.
508 * If you need to call the dispatch method of some inset directly
509 * you may have to explicitly request an update at that place. Don't
510 * do it in doDispatch(), since that causes nested updates when
511 * called from Cursor::dispatch(), and these can lead to crashes.
514 virtual void doDispatch(Cursor & cur, FuncRequest & cmd);