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
28 #include "insets/insetbase.h"
29 #include "math_data.h"
33 Abstract base class for all math objects. A math insets is for use of the
34 math editor only, it isn't a general LyX inset. It's used to represent all
37 Math insets do not know there parents, a cursor position or things
38 like that. The are dumb object that are contained in other math insets
39 (mathNestInsets, in fact) thus forming a tree. The root of this tree is
40 always a mathHullInset, which provides an interface to the Outer World by
41 inclusion in the "real LyX insets" FormulaInset and FormulaMacroInset.
47 class MathAMSArrayInset;
54 class MathMatrixInset;
56 class MathParboxInset;
57 class MathScriptInset;
58 class MathStringInset;
60 class MathSymbolInset;
61 class MathUnknownInset;
69 class MathematicaStream;
78 class MathMacroTemplate;
84 class MathInset : public InsetBase {
86 /// short of anything else reasonable
87 typedef MathArray::size_type size_type;
88 /// type for cursor positions differences within a cell
89 typedef MathArray::difference_type difference_type;
90 /// type for cursor positions within a cell
91 typedef MathArray::size_type pos_type;
92 /// type for cell indices
93 typedef size_type idx_type;
94 /// type for row numbers
95 typedef size_type row_type;
96 /// type for column numbers
97 typedef size_type col_type;
99 /// our members behave nicely...
103 virtual MathInset * clone() const = 0;
104 /// substitutes macro arguments if necessary
105 virtual void substitute(MathMacro const & macro);
106 /// compute the size of the object returned in dim
107 virtual Dimension metrics(MetricsInfo & mi) const = 0;
109 // updates the (xo,yo)-caches of all contained cells
110 virtual void draw(PainterInfo & pi, int x, int y) const;
111 /// draw selection between two positions
112 virtual void drawSelection(PainterInfo & pi,
113 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const;
114 /// the ascent of the inset above the baseline
115 /// compute the size of the object for text based drawing
116 virtual void metricsT(TextMetricsInfo const & mi, Dimension & dim) const;
117 /// draw the object as text
118 virtual void drawT(TextPainter &, int x, int y) const;
120 /// Where should we go when we press the up or down cursor key?
121 virtual bool idxUpDown(idx_type & idx, pos_type & pos, bool up,
123 /// Where should we go when we press the up or down cursor key?
124 virtual bool idxUpDown2(idx_type & idx, pos_type & pos, bool up,
127 virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
129 virtual bool idxRight(idx_type & idx, pos_type & pos) const;
131 /// Move one physical cell up
132 virtual bool idxNext(idx_type & idx, pos_type & pos) const;
133 /// Move one physical cell down
134 virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
136 /// Target pos when we enter the inset from the left by pressing "Right"
137 virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
138 /// Target pos when we enter the inset from the right by pressing "Left"
139 virtual bool idxLast(idx_type & idx, pos_type & pos) const;
141 /// Where should we go if we press home?
142 virtual bool idxHome(idx_type & idx, pos_type & pos) const;
143 /// Where should we go if we press end?
144 virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
146 /// Delete a cell and move cursor
147 virtual bool idxDelete(idx_type &) { return false; }
148 /// pulls cell after pressing erase
149 virtual void idxGlue(idx_type) {}
150 // returns list of cell indices that are "between" from and to for
151 // selection purposes
152 virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
154 /// the number of nested cells this inset owns
155 virtual idx_type nargs() const;
157 /// return cell given its number
158 virtual MathArray & cell(idx_type);
159 /// return cell given its number
160 virtual MathArray const & cell(idx_type) const;
162 /// the number of columns of this inset if it is grid-like
163 virtual col_type ncols() const { return 1; }
164 /// the number of rows of this inset if it is grid-like
165 virtual row_type nrows() const { return 1; }
166 /// to which column belongs a cell with a given index?
167 virtual col_type col(idx_type) const { return 0; }
168 /// to which row belongs a cell with a given index?
169 virtual row_type row(idx_type) const { return 0; }
170 /// cell idex corresponding to row and column;
171 virtual idx_type index(row_type row, col_type col) const;
172 /// any additional x-offset when drawing a cell?
173 virtual int cellXOffset(idx_type) const { return 0; }
174 /// any additional y-offset when drawing a cell?
175 virtual int cellYOffset(idx_type) const { return 0; }
176 /// can we enter this cell?
177 virtual bool validCell(idx_type) const { return true; }
179 virtual void getPos(idx_type idx, pos_type pos, int & x, int & y) const;
181 /// identifies certain types of insets
182 virtual MathAMSArrayInset * asAMSArrayInset() { return 0; }
183 virtual MathAMSArrayInset const * asAMSArrayInset() const { return 0; }
184 virtual MathArrayInset * asArrayInset() { return 0; }
185 virtual MathArrayInset const * asArrayInset() const { return 0; }
186 virtual MathCharInset const * asCharInset() const { return 0; }
187 virtual MathDelimInset * asDelimInset() { return 0; }
188 virtual MathDelimInset const * asDelimInset() const { return 0; }
189 virtual MathFracInset * asFracInset() { return 0; }
190 virtual MathFracInset const * asFracInset() const { return 0; }
191 virtual MathFontInset * asFontInset() { return 0; }
192 virtual MathFontInset const * asFontInset() const { return 0; }
193 virtual MathGridInset * asGridInset() { return 0; }
194 virtual MathGridInset const * asGridInset() const { return 0; }
195 virtual MathHullInset * asHullInset() { return 0; }
196 virtual MathHullInset const * asHullInset() const { return 0; }
197 virtual MathMacroTemplate * asMacroTemplate() { return 0; }
198 virtual MathMacroTemplate const * asMacroTemplate() const { return 0; }
199 virtual MathMatrixInset const * asMatrixInset() const { return 0; }
200 virtual MathNestInset * asNestInset() { return 0; }
201 virtual MathNestInset const * asNestInset() const { return 0; }
202 virtual MathParboxInset * asParboxInset() { return 0; }
203 virtual MathScriptInset * asScriptInset() { return 0; }
204 virtual MathScriptInset const * asScriptInset() const { return 0; }
205 virtual MathSpaceInset * asSpaceInset() { return 0; }
206 virtual MathSpaceInset const * asSpaceInset() const { return 0; }
207 virtual MathStringInset * asStringInset() { return 0; }
208 virtual MathStringInset const * asStringInset() const { return 0; }
209 virtual MathSymbolInset const * asSymbolInset() const { return 0; }
210 virtual MathUnknownInset * asUnknownInset() { return 0; }
211 virtual MathUnknownInset const * asUnknownInset() const { return 0; }
212 virtual RefInset * asRefInset() { return 0; }
214 /// identifies things that can get scripts
215 virtual bool isScriptable() const { return false; }
216 /// thing that can be moved into
217 virtual bool isActive() const { return nargs() > 0; }
218 /// is the a relational operator (used for splitting equations)
219 virtual bool isRelOp() const { return false; }
220 /// -1: text mode, 1: math mode, 0 undecided
221 enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE};
223 virtual mode_type currentMode() const { return UNDECIDED_MODE; }
224 /// will this get written as a single block in {..}
225 virtual bool extraBraces() const { return false; }
227 /// return the content as char if the inset is able to do so
228 virtual char getChar() const { return 0; }
229 /// identifies things that can get \limits or \nolimits
230 virtual bool takesLimits() const { return false; }
232 /// request "external features"
233 virtual void validate(LaTeXFeatures &) const {}
234 /// char char code if possible
235 virtual void handleFont(string const &) {}
236 /// is this inset equal to a given other inset?
237 virtual bool match(MathAtom const &) const { return false; }
238 /// replace things by other things
239 virtual void replace(ReplaceData &) {}
240 /// do we contain a given subsequence?
241 virtual bool contains(MathArray const &) const { return false; }
242 /// access to the lock (only nest array have one)
243 virtual bool lock() const { return false; }
244 /// access to the lock (only nest array have one)
245 virtual void lock(bool) {}
246 /// get notification when the cursor leaves this inset
247 virtual void notifyCursorLeaves(idx_type) {}
249 /// write LaTeX and Lyx code
250 virtual void write(WriteStream & os) const;
251 /// write normalized content
252 virtual void normalize(NormalStream &) const;
253 /// write content as something readable by Maple
254 virtual void maple(MapleStream &) const;
255 /// write content as something readable by Maxima
256 virtual void maxima(MaximaStream &) const;
257 /// write content as something readable by Mathematica
258 virtual void mathematica(MathematicaStream &) const;
259 /// write content as something resembling MathML
260 virtual void mathmlize(MathMLStream &) const;
261 /// write content as something readable by Octave
262 virtual void octave(OctaveStream &) const;
263 /// describe content if cursor inside
264 virtual void infoize(std::ostream &) const {}
265 /// describe content if cursor behind
266 virtual void infoize2(std::ostream &) const {}
267 /// plain ascii output
268 virtual int ascii(std::ostream & os, int) const;
270 virtual int linuxdoc(std::ostream & os) const;
272 virtual int docbook(std::ostream & os, bool) const;
274 /// dump content to stderr for debugging
275 virtual void dump() const;
278 /// write labels into a list
279 virtual void getLabelList(std::vector<string> &) const {}
281 virtual bool numberedType() const { return false; }
283 virtual string const & getType() const;
285 virtual void mutate(string const &) {}
286 /// how is the inset called in the .lyx file?
287 virtual string fileInsetLabel() const;
288 /// usually the latex name
289 virtual string name() const;
293 BufferView * view() const;
296 std::ostream & operator<<(std::ostream &, MathAtom const &);
298 // converts single cell to string
299 string asString(MathArray const & ar);
300 // converts string to single cell
301 MathArray asArray(string const & str);