4 * Purpose: Declaration of insets for mathed
5 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
6 * Created: January 1996
7 * Description: Math paragraph and objects for a WYSIWYG math editor.
9 * Dependencies: Xlib, XForms
11 * Copyright: 1996, 1997 Alejandro Aguilar Sierra
13 * Version: 0.8beta, Math & Lyx project.
15 * You are free to use and modify this code under the terms of
16 * the GNU General Public Licence version 2 or later.
19 // Note: These math insets are internal to Math and are not derived
32 #include "frontends/mouse_state.h"
33 #include "math_data.h"
37 Abstract base class for all math objects. A math insets is for use of the
38 math editor only, it isn't a general LyX inset. It's used to represent all
41 Math insets do not know there parents, a cursor position or things
42 like that. The are dumb object that are contained in other math insets
43 (mathNestInsets, in fact) thus forming a tree. The root of this tree is
44 always a mathHullInset, which provides an interface to the Outer World by
45 inclusion in the "real LyX insets" FormulaInset and FormulaMacroInset.
51 class MathAMSArrayInset;
58 class MathMatrixInset;
60 class MathParboxInset;
61 class MathScriptInset;
62 class MathStringInset;
64 class MathSymbolInset;
65 class MathUnknownInset;
73 class MathematicaStream;
82 class MathMacroTemplate;
90 /// short of anything else reasonable
91 typedef MathArray::size_type size_type;
92 /// type for cursor positions differences within a cell
93 typedef MathArray::difference_type difference_type;
94 /// type for cursor positions within a cell
95 typedef MathArray::size_type pos_type;
96 /// type for cell indices
97 typedef size_type idx_type;
98 /// type for row numbers
99 typedef size_type row_type;
100 /// type for column numbers
101 typedef size_type col_type;
103 /// our members behave nicely...
105 /// the virtual base destructor
106 virtual ~MathInset() {}
109 virtual MathInset * clone() const = 0;
110 /// substitutes macro arguments if necessary
111 virtual void substitute(MathMacro const & macro);
112 /// compute the size of the object, sets ascend_, descend_ and width_
113 // updates the (xo,yo)-caches of all contained cells
114 virtual void metrics(MathMetricsInfo & mi) const;
116 virtual void draw(MathPainterInfo & pi, int x, int y) const;
117 /// draw selection between two positions
118 virtual void drawSelection(MathPainterInfo & pi,
119 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const;
120 /// the ascent of the inset above the baseline
121 /// compute the size of the object for text based drawing
122 virtual void metricsT(TextMetricsInfo const & st) const;
123 /// draw the object as text
124 virtual void drawT(TextPainter &, int x, int y) const;
125 /// the ascent of the inset above the baseline
126 virtual int ascent() const { return 1; }
127 /// the descent of the inset below the baseline
128 virtual int descent() const { return 1; }
130 virtual int width() const { return 2; }
132 virtual Dimension dimensions() const;
133 /// total height (== ascent + descent)
134 virtual int height() const;
136 /// Where should we go when we press the up or down cursor key?
137 virtual bool idxUpDown(idx_type & idx, pos_type & pos, bool up,
140 virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
142 virtual bool idxRight(idx_type & idx, pos_type & pos) const;
144 /// Move one physical cell up
145 virtual bool idxNext(idx_type & idx, pos_type & pos) const;
146 /// Move one physical cell down
147 virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
149 /// Target pos when we enter the inset from the left by pressing "Right"
150 virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
151 /// Target pos when we enter the inset from the right by pressing "Left"
152 virtual bool idxLast(idx_type & idx, pos_type & pos) const;
154 /// Where should we go if we press home?
155 virtual bool idxHome(idx_type & idx, pos_type & pos) const;
156 /// Where should we go if we press end?
157 virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
159 /// Delete a cell and move cursor
160 virtual bool idxDelete(idx_type &) { return false; }
161 /// pulls cell after pressing erase
162 virtual void idxGlue(idx_type) {}
163 // returns list of cell indices that are "between" from and to for
164 // selection purposes
165 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
167 /// the number of nested cells this inset owns
168 virtual idx_type nargs() const;
170 /// return cell given its number
171 virtual MathArray & cell(idx_type);
172 /// return cell given its number
173 virtual MathArray const & cell(idx_type) const;
175 /// the number of columns of this inset if it is grid-like
176 virtual col_type ncols() const { return 1; }
177 /// the number of rows of this inset if it is grid-like
178 virtual row_type nrows() const { return 1; }
179 /// to which column belongs a cell with a given index?
180 virtual col_type col(idx_type) const { return 0; }
181 /// to which row belongs a cell with a given index?
182 virtual row_type row(idx_type) const { return 0; }
183 /// cell idex corresponding to row and column;
184 virtual idx_type index(row_type row, col_type col) const;
185 /// any additional x-offset when drawing a cell?
186 virtual int cellXOffset(idx_type) const { return 0; }
187 /// any additional y-offset when drawing a cell?
188 virtual int cellYOffset(idx_type) const { return 0; }
189 /// can we enter this cell?
190 virtual bool validCell(idx_type) const { return true; }
192 virtual void getPos(idx_type idx, pos_type pos, int & x, int & y) const;
194 /// identifies certain types of insets
195 virtual MathAMSArrayInset * asAMSArrayInset() { return 0; }
196 virtual MathAMSArrayInset const * asAMSArrayInset() const { return 0; }
197 virtual MathArrayInset * asArrayInset() { return 0; }
198 virtual MathArrayInset const * asArrayInset() const { return 0; }
199 virtual MathCharInset const * asCharInset() const { return 0; }
200 virtual MathDelimInset * asDelimInset() { return 0; }
201 virtual MathDelimInset const * asDelimInset() const { return 0; }
202 virtual MathFracInset * asFracInset() { return 0; }
203 virtual MathFracInset const * asFracInset() const { return 0; }
204 virtual MathFontInset * asFontInset() { return 0; }
205 virtual MathFontInset const * asFontInset() const { return 0; }
206 virtual MathGridInset * asGridInset() { return 0; }
207 virtual MathGridInset const * asGridInset() const { return 0; }
208 virtual MathHullInset * asHullInset() { return 0; }
209 virtual MathHullInset const * asHullInset() const { return 0; }
210 virtual MathMacroTemplate * asMacroTemplate() { return 0; }
211 virtual MathMacroTemplate const * asMacroTemplate() const { return 0; }
212 virtual MathMatrixInset const * asMatrixInset() const { return 0; }
213 virtual MathNestInset * asNestInset() { return 0; }
214 virtual MathNestInset const * asNestInset() const { return 0; }
215 virtual MathParboxInset * asParboxInset() { return 0; }
216 virtual MathScriptInset * asScriptInset() { return 0; }
217 virtual MathScriptInset const * asScriptInset() const { return 0; }
218 virtual MathSpaceInset * asSpaceInset() { return 0; }
219 virtual MathSpaceInset const * asSpaceInset() const { return 0; }
220 virtual MathStringInset * asStringInset() { return 0; }
221 virtual MathStringInset const * asStringInset() const { return 0; }
222 virtual MathSymbolInset const * asSymbolInset() const { return 0; }
223 virtual MathUnknownInset * asUnknownInset() { return 0; }
224 virtual MathUnknownInset const * asUnknownInset() const { return 0; }
225 virtual InsetRef * asInsetRef() { return 0; }
227 /// identifies things that can get scripts
228 virtual bool isScriptable() const { return false; }
229 /// thing that can be moved into
230 virtual bool isActive() const { return nargs() > 0; }
231 /// is the a relational operator (used for splitting equations)
232 virtual bool isRelOp() const { return false; }
233 /// -1: text mode, 1: math mode, 0 undecided
234 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
235 /// Dispatch result codes, see inset/inset.h
237 UNDISPATCHED = 0, DISPATCHED, DISPATCHED_NOUPDATE,
238 FINISHED, FINISHED_RIGHT, FINISHED_UP, FINISHED_DOWN,
242 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
243 /// will this get written as a single block in {..}
244 virtual bool extraBraces() const { return false; }
246 /// return the content as char if the inset is able to do so
247 virtual char getChar() const { return 0; }
248 /// identifies things that can get \limits or \nolimits
249 virtual bool takesLimits() const { return false; }
252 virtual void edit(BufferView *, int, int, mouse_button::state) {}
254 /// request "external features"
255 virtual void validate(LaTeXFeatures &) const {}
256 /// char char code if possible
257 virtual void handleFont(string const &) {}
258 /// is this inset equal to a given other inset?
259 virtual bool match(MathAtom const &) const { return false; }
260 /// replace things by other things
261 virtual void replace(ReplaceData &) {}
262 /// do we contain a given subsequence?
263 virtual bool contains(MathArray const &) const { return false; }
264 /// access to the lock (only nest array have one)
265 virtual bool lock() const { return false; }
266 /// access to the lock (only nest array have one)
267 virtual void lock(bool) {}
268 /// get notification when the cursor leaves this inset
269 virtual void notifyCursorLeaves(idx_type) {}
271 /// write LaTeX and Lyx code
272 virtual void write(WriteStream & os) const;
273 /// write normalized content
274 virtual void normalize(NormalStream &) const;
275 /// write content as something readable by Maple
276 virtual void maplize(MapleStream &) const;
277 /// write content as something readable by Maxima
278 virtual void maximize(MaximaStream &) const;
279 /// write content as something readable by Mathematica
280 virtual void mathematicize(MathematicaStream &) const;
281 /// write content as something resembling MathML
282 virtual void mathmlize(MathMLStream &) const;
283 /// write content as something readable by Octave
284 virtual void octavize(OctaveStream &) const;
285 /// describe content if cursor inside
286 virtual void infoize(std::ostream &) const {}
287 /// describe content if cursor behind
288 virtual void infoize2(std::ostream &) const {}
289 /// plain ascii output
290 virtual int ascii(std::ostream & os, int) const;
292 virtual int linuxdoc(std::ostream & os) const;
294 virtual int docbook(std::ostream & os, bool) const;
296 /// dump content to stderr for debugging
297 virtual void dump() const;
299 virtual result_type dispatch
300 (FuncRequest const & cmd, idx_type & idx, pos_type & pos);
303 /// write labels into a list
304 virtual void getLabelList(std::vector<string> &) const {}
306 virtual bool numberedType() const { return false; }
308 virtual string const & getType() const;
310 virtual void mutate(string const &) {}
311 /// how is the inset called in the .lyx file?
312 virtual string fileInsetLabel() const { return "Formula"; }
313 /// usually the latex name
314 virtual string name() const;
317 std::ostream & operator<<(std::ostream &, MathAtom const &);
319 // converts single cell to string
320 string asString(MathArray const & ar);
321 // converts string to single cell
322 MathArray asArray(string const & str);
327 /// here to ssave a few includes in the insets
329 Dialogs & getDialogs();