4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS.
15 #include <boost/assert.hpp>
39 namespace lyx { namespace graphics { class PreviewLoader; } }
43 /// Common base class to all insets
45 // Do not add _any_ (non-static) data members as this would inflate
46 // everything storing large quantities of insets. Mathed e.g. would
52 typedef ptrdiff_t difference_type;
53 /// short of anything else reasonable
54 typedef size_t size_type;
55 /// type for cell indices
56 typedef size_t idx_type;
57 /// type for cursor positions
58 typedef ptrdiff_t pos_type;
59 /// type for row numbers
60 typedef size_t row_type;
61 /// type for column numbers
62 typedef size_t col_type;
64 /// virtual base class destructor
65 virtual ~InsetBase() {}
66 /// replicate ourselves
67 std::auto_ptr<InsetBase> clone() const
69 std::auto_ptr<InsetBase> b = doClone();
70 BOOST_ASSERT(typeid(*b) == typeid(*this));
75 /// identification as math inset
76 virtual MathInset * asMathInset() { return 0; }
77 /// identification as non-math inset
78 virtual UpdatableInset * asUpdatableInset() { return 0; }
79 /// true for 'math' math inset, but not for e.g. mbox
80 virtual bool inMathed() const { return false; }
82 /// the real dispatcher
83 void dispatch(LCursor & cur, FuncRequest & cmd);
85 * \returns true if this function made a definitive decision on
86 * whether the inset wants to handle the request \p cmd or not.
87 * The result of this decision is put into \p status.
89 virtual bool getStatus(LCursor & cur, FuncRequest const & cmd,
90 FuncStatus & status) const;
93 virtual void edit(LCursor & cur, bool left);
95 virtual InsetBase * editXY(LCursor & cur, int x, int y) const;
97 /// compute the size of the object returned in dim
98 virtual void metrics(MetricsInfo & mi, Dimension & dim) const = 0;
99 /// draw inset and update (xo, yo)-cache
100 virtual void draw(PainterInfo & pi, int x, int y) const = 0;
101 /// draw inset selection if necessary
102 virtual void drawSelection(PainterInfo &, int, int) const {}
104 virtual bool editing(BufferView * bv) const;
105 /// draw four angular markers
106 void drawMarkers(PainterInfo & pi, int x, int y) const;
107 /// draw two angular markers
108 void drawMarkers2(PainterInfo & pi, int x, int y) const;
109 /// add space for markers
110 void metricsMarkers(Dimension & dim, int framesize = 1) const;
111 /// add space for markers
112 void metricsMarkers2(Dimension & dim, int framesize = 1) const;
113 /// last drawn position for 'important' insets
115 /// last drawn position for 'important' insets
117 /// set x/y drawing position cache if available
118 virtual void setPosCache(PainterInfo const &, int, int) const {}
119 /// do we cover screen position x/y?
120 virtual bool covers(int x, int y) const;
121 /// get the screen positions of the cursor (see note in cursor.C)
122 virtual void getCursorPos(CursorSlice const & sl, int & x, int & y) const;
124 /// is this an inset that can be moved into?
125 virtual bool isActive() const { return nargs() > 0; }
126 /// Where should we go when we press the up or down cursor key?
127 virtual bool idxUpDown(LCursor & cur, bool up) const;
128 /// Where should we go when we press the up or down cursor key?
129 virtual bool idxUpDown2(LCursor & cur, bool up) const;
130 /// Move one cell to the left
131 virtual bool idxLeft(LCursor &) const { return false; }
132 /// Move one cell to the right
133 virtual bool idxRight(LCursor &) const { return false; }
135 /// Move one physical cell up
136 virtual bool idxNext(LCursor &) const { return false; }
137 /// Move one physical cell down
138 virtual bool idxPrev(LCursor &) const { return false; }
140 /// Target pos when we enter the inset from the left by pressing "Right"
141 virtual bool idxFirst(LCursor &) const { return false; }
142 /// Target pos when we enter the inset from the right by pressing "Left"
143 virtual bool idxLast(LCursor &) const { return false; }
145 /// Delete a cell and move cursor
146 virtual bool idxDelete(idx_type &) { return false; }
147 /// pulls cell after pressing erase
148 virtual void idxGlue(idx_type) {}
149 // returns list of cell indices that are "between" from and to for
150 // selection purposes
151 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
153 /// to which column belongs a cell with a given index?
154 virtual col_type col(idx_type) const { return 0; }
155 /// to which row belongs a cell with a given index?
156 virtual row_type row(idx_type) const { return 0; }
157 /// cell idex corresponding to row and column;
158 virtual idx_type index(row_type row, col_type col) const;
159 /// any additional x-offset when drawing a cell?
160 virtual int cellXOffset(idx_type) const { return 0; }
161 /// any additional y-offset when drawing a cell?
162 virtual int cellYOffset(idx_type) const { return 0; }
163 /// number of embedded cells
164 virtual size_t nargs() const { return 0; }
165 /// number of rows in gridlike structures
166 virtual size_t nrows() const { return 0; }
167 /// number of columns in gridlike structures
168 virtual size_t ncols() const { return 0; }
169 /// is called when the cursor leaves this inset
170 virtual void notifyCursorLeaves(LCursor &) {}
172 /// request "external features"
173 virtual void validate(LaTeXFeatures &) const {}
174 /// Appends \c list with all labels found within this inset.
175 virtual void getLabelList(Buffer const &,
176 std::vector<std::string> & /* list */) const {}
178 /// describe content if cursor inside
179 virtual void infoize(std::ostream &) const {}
180 /// describe content if cursor behind
181 virtual void infoize2(std::ostream &) const {}
183 /// plain ascii output
184 virtual int plaintext(Buffer const &, std::ostream & os,
185 OutputParams const &) const;
187 virtual int linuxdoc(Buffer const &, std::ostream & os,
188 OutputParams const &) const;
190 virtual int docbook(Buffer const &, std::ostream & os,
191 OutputParams const &) const;
202 /// what appears in the minibuffer when opening
203 virtual std::string const editMessage() const;
205 virtual EDITABLE editable() const;
206 /// can we go further down on mouse click?
207 virtual bool descendable() const { return false; }
209 virtual bool isTextInset() const { return false; }
210 /// return true if the inset should be removed automatically
211 virtual bool autoDelete() const;
213 /** This is not quite the correct place for this enum. I think
214 the correct would be to let each subclass of Inset declare
215 its own enum code. Actually the notion of an InsetBase::Code
216 should be avoided, but I am not sure how this could be done
222 TOC_CODE, // do these insets really need a code? (ale)
282 MATHMACRO_CODE, // 30
313 /** returns the Code corresponding to the \c name.
314 * Eg, translate("branch") == BRANCH_CODE
316 static Code translate(std::string const & name);
318 /// returns true the inset can hold an inset of given type
319 virtual bool insetAllowed(Code) const { return false; }
320 // if this inset has paragraphs should they be output all as default
321 // paragraphs with "Standard" layout?
322 virtual bool forceDefaultParagraphs(InsetBase const *) const { return false; }
324 virtual std::string const & getInsetName() const;
325 /// used to toggle insets
326 // is the inset open?
327 virtual bool isOpen() const { return false; }
329 virtual void open() {}
331 virtual void close() {}
332 // should this inset be handled like a normal charater
333 virtual bool isChar() const { return false; }
334 // is this equivalent to a letter?
335 virtual bool isLetter() const { return false; }
336 // is this equivalent to a space (which is BTW different from
337 // a line separator)?
338 virtual bool isSpace() const { return false; }
339 // should we have a non-filled line before this inset?
340 virtual bool display() const { return false; }
341 // should we break lines after this inset?
342 virtual bool isLineSeparator() const { return false; }
343 /// dumps content to lyxerr
344 virtual void dump() const;
346 virtual void write(Buffer const &, std::ostream &) const {}
348 virtual void read(Buffer const &, LyXLex &) {}
349 /// returns the number of rows (\n's) of generated tex code.
350 virtual int latex(Buffer const &, std::ostream &,
351 OutputParams const &) const { return 0; }
352 /// returns true to override begin and end inset in file
353 virtual bool directWrite() const;
355 virtual bool allowSpellCheck() const { return false; }
357 /// if this insets owns text cells (e.g. InsetText) return cell num
358 virtual LyXText * getText(int /*num*/) const { return 0; }
360 /** Adds a LaTeX snippet to the Preview Loader for transformation
361 * into a bitmap image. Does not start the laoding process.
363 * Most insets have no interest in this capability, so the method
366 virtual void addPreview(lyx::graphics::PreviewLoader &) const {}
368 /// returns LyX code associated with the inset. Used for TOC, ...)
369 virtual Code lyxCode() const { return NO_CODE; }
371 /// -1: text mode, 1: math mode, 0 undecided
372 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
373 /// return text or mathmode if that is possible to determine
374 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
375 /// returns whether this inset is allowed in other insets of given mode
376 virtual bool allowedIn(mode_type) const { return true; }
378 /// is this inset allowed within a font change?
379 virtual bool noFontChange() const { return false; }
382 virtual void markErased();
384 virtual int width() const { return 10; }
386 virtual int ascent() const { return 10; }
388 virtual int descent() const { return 10; }
391 InsetBase(InsetBase const &);
392 // the real dispatcher
393 virtual void doDispatch(LCursor & cur, FuncRequest & cmd);
395 virtual std::auto_ptr<InsetBase> doClone() const = 0;
400 * returns true if pointer argument is valid
401 * and points to an editable inset
403 bool isEditableInset(InsetBase const * inset);
407 * returns true if pointer argument is valid
408 * and points to a highly editable inset
410 bool isHighlyEditableInset(InsetBase const * inset);