]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.h
Maxima
[lyx.git] / src / mathed / math_inset.h
1 // -*- C++ -*-
2 /**
3  *  File:        math_inset.h
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.
8  *
9  *  Dependencies: Xlib, XForms
10  *
11  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *   Version: 0.8beta, Math & Lyx project.
14  *
15  *   You are free to use and modify this code under the terms of
16  *   the GNU General Public Licence version 2 or later.
17  */
18
19 //  Note: These math insets are internal to Math and are not derived
20 //        from lyx inset.
21
22 #ifndef MATH_INSET_H
23 #define MATH_INSET_H
24
25 #ifdef __GNUG__
26 #pragma interface
27 #endif
28
29 #include <config.h>
30
31 #include "LString.h"
32 #include "frontends/mouse_state.h"
33 #include "math_data.h"
34
35 /**
36
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
39 the math objects.
40
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.
46
47 */
48
49
50 class MathArrayInset;
51 class MathAMSArrayInset;
52 class MathCharInset;
53 class MathDelimInset;
54 class MathGridInset;
55 class MathFracInset;
56 class MathHullInset;
57 class MathMatrixInset;
58 class MathNestInset;
59 class MathParboxInset;
60 class MathScriptInset;
61 class MathStringInset;
62 class MathSpaceInset;
63 class MathSymbolInset;
64 class MathUnknownInset;
65
66 class InsetRef;
67
68 class NormalStream;
69 class OctaveStream;
70 class MapleStream;
71 class MaximaStream;
72 class MathematicaStream;
73 class MathMLStream;
74 class WriteStream;
75 class InfoStream;
76 class MathArray;
77
78 class LaTeXFeatures;
79 class BufferView;
80 class UpdatableInset;
81 class MathMacroTemplate;
82 class MathPosFinder;
83 class Dimension;
84 class FuncRequest;
85
86
87 class MathInset {
88 public:
89         /// short of anything else reasonable
90         typedef MathArray::size_type        size_type;
91         /// type for cursor positions differences within a cell
92         typedef MathArray::difference_type  difference_type;
93         /// type for cursor positions within a cell
94         typedef MathArray::size_type        pos_type;
95         /// type for cell indices
96         typedef size_type                   idx_type;
97         /// type for row numbers
98         typedef size_type                   row_type;
99         /// type for column numbers
100         typedef size_type                   col_type;
101
102         /// our members behave nicely...
103         MathInset() {}
104         /// the virtual base destructor
105         virtual ~MathInset() {}
106
107         /// reproduce itself
108         virtual MathInset * clone() const = 0;
109         /// substitutes macro arguments if necessary
110         virtual void substitute(MathMacro const & macro);
111         /// compute the size of the object, sets ascend_, descend_ and width_
112         // updates the (xo,yo)-caches of all contained cells
113         virtual void metrics(MathMetricsInfo & mi) const;
114         /// draw the object
115         virtual void draw(MathPainterInfo & pi, int x, int y) const;
116         /// draw selection between two positions
117         virtual void drawSelection(MathPainterInfo & pi,
118                 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const;
119         /// the ascent of the inset above the baseline
120         /// compute the size of the object for text based drawing
121         virtual void metricsT(TextMetricsInfo const & st) const;
122         /// draw the object as text
123         virtual void drawT(TextPainter &, int x, int y) const;
124         /// the ascent of the inset above the baseline
125         virtual int ascent() const { return 1; }
126         /// the descent of the inset below the baseline
127         virtual int descent() const { return 1; }
128         /// total width
129         virtual int width() const { return 2; }
130         /// all in one batch
131         virtual Dimension dimensions() const;
132         /// total height (== ascent + descent)
133         virtual int height() const;
134
135         /// Where should we go when we press the up or down cursor key?
136         virtual bool idxUpDown(idx_type & idx, pos_type & pos, bool up,
137                 int targetx) const;
138         /// The left key
139         virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
140         /// The right key
141         virtual bool idxRight(idx_type & idx, pos_type & pos) const;
142
143         /// Move one physical cell up
144         virtual bool idxNext(idx_type & idx, pos_type & pos) const;
145         /// Move one physical cell down
146         virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
147
148         /// Target pos when we enter the inset from the left by pressing "Right"
149         virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
150         /// Target pos when we enter the inset from the right by pressing "Left"
151         virtual bool idxLast(idx_type & idx, pos_type & pos) const;
152
153         /// Where should we go if we press home?
154         virtual bool idxHome(idx_type & idx, pos_type & pos) const;
155         /// Where should we go if we press end?
156         virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
157
158         /// Delete a cell and move cursor
159         virtual bool idxDelete(idx_type &) { return false; }
160         /// pulls cell after pressing erase
161         virtual void idxGlue(idx_type) {}
162         // returns list of cell indices that are "between" from and to for
163         // selection purposes
164         virtual bool idxBetween(idx_type idx, idx_type from, idx_type to) const;
165
166         /// the number of nested cells this inset owns
167         virtual idx_type nargs() const;
168
169         /// return cell given its number
170         virtual MathArray & cell(idx_type);
171         /// return cell given its number
172         virtual MathArray const & cell(idx_type) const;
173
174         /// the number of columns of this inset if it is grid-like
175         virtual col_type ncols() const { return 1; }
176         /// the number of rows of this inset if it is grid-like
177         virtual row_type nrows() const { return 1; }
178         /// to which column belongs a cell with a given index?
179         virtual col_type col(idx_type) const { return 0; }
180         /// to which row belongs a cell with a given index?
181         virtual row_type row(idx_type) const { return 0; }
182         /// cell idex corresponding to row and column;
183         virtual idx_type index(row_type row, col_type col) const;
184         /// any additional x-offset when drawing a cell?
185         virtual int cellXOffset(idx_type) const { return 0; }
186         /// any additional y-offset when drawing a cell?
187         virtual int cellYOffset(idx_type) const { return 0; }
188         /// can we enter this cell?
189         virtual bool validCell(idx_type) const { return true; }
190         /// get coordinates
191         virtual void getPos(idx_type idx, pos_type pos, int & x, int & y) const;
192
193         /// identifies certain types of insets
194         virtual MathAMSArrayInset       * asAMSArrayInset()       { return 0; }
195         virtual MathAMSArrayInset const * asAMSArrayInset() const { return 0; }
196         virtual MathArrayInset          * asArrayInset()          { return 0; }
197         virtual MathArrayInset const    * asArrayInset() const    { return 0; }
198         virtual MathCharInset const     * asCharInset() const     { return 0; }
199         virtual MathDelimInset          * asDelimInset()          { return 0; }
200         virtual MathDelimInset const    * asDelimInset() const    { return 0; }
201         virtual MathFracInset           * asFracInset()           { return 0; }
202         virtual MathFracInset const     * asFracInset() const     { return 0; }
203         virtual MathGridInset           * asGridInset()           { return 0; }
204         virtual MathGridInset const     * asGridInset() const     { return 0; }
205         virtual MathHullInset           * asHullInset()           { return 0; }
206         virtual MathHullInset const     * asHullInset() const     { return 0; }
207         virtual MathMacroTemplate       * asMacroTemplate()       { return 0; }
208         virtual MathMacroTemplate const * asMacroTemplate() const { return 0; }
209         virtual MathMatrixInset const   * asMatrixInset() const   { return 0; }
210         virtual MathNestInset           * asNestInset()           { return 0; }
211         virtual MathNestInset const     * asNestInset() const     { return 0; }
212         virtual MathParboxInset         * asParboxInset()         { return 0; }
213         virtual MathScriptInset         * asScriptInset()         { return 0; }
214         virtual MathScriptInset const   * asScriptInset() const   { return 0; }
215         virtual MathSpaceInset          * asSpaceInset()          { return 0; }
216         virtual MathSpaceInset const    * asSpaceInset() const    { return 0; }
217         virtual MathStringInset         * asStringInset()         { return 0; }
218         virtual MathStringInset const   * asStringInset() const   { return 0; }
219         virtual MathSymbolInset const   * asSymbolInset() const   { return 0; }
220         virtual MathUnknownInset        * asUnknownInset()        { return 0; }
221         virtual MathUnknownInset const  * asUnknownInset() const  { return 0; }
222         virtual InsetRef                * asInsetRef()            { return 0; }
223
224         /// identifies things that can get scripts
225         virtual bool isScriptable() const { return false; }
226         /// thing that can be moved into
227         virtual bool isActive() const { return nargs() > 0; }
228         /// is the a relational operator (used for splitting equations)
229         virtual bool isRelOp() const { return false; }
230         /// -1: text mode, 1: math mode, 0 undecided
231         enum mode_type {UNDECIDED_MODE, TEXT_MODE, MATH_MODE, VERBATIM_MODE};
232         /// Dispatch result codes, see inset/inset.h
233         enum result_type {
234                 UNDISPATCHED = 0, DISPATCHED, DISPATCHED_NOUPDATE,
235                 FINISHED, FINISHED_RIGHT, FINISHED_UP, FINISHED_DOWN,
236                 DISPATCHED_POP
237         };
238
239         virtual mode_type currentMode() const { return UNDECIDED_MODE; }
240         /// will this get written as a single block in {..}
241         virtual bool extraBraces() const { return false; }
242
243         /// return the content as char if the inset is able to do so
244         virtual char getChar() const { return 0; }
245         /// identifies things that can get \limits or \nolimits
246         virtual bool takesLimits() const { return false; }
247
248         ///
249         virtual void edit(BufferView *, int, int, mouse_button::state) {}
250
251         /// request "external features"
252         virtual void validate(LaTeXFeatures &) const {}
253         /// char char code if possible
254         virtual void handleFont(string const &) {}
255         /// is this inset equal to a given other inset?
256         virtual bool match(MathAtom const &) const { return false; }
257         /// replace things by other things
258         virtual void replace(ReplaceData &) {}
259         /// do we contain a given subsequence?
260         virtual bool contains(MathArray const &) const { return false; }
261         /// access to the lock (only nest array have one)
262         virtual bool lock() const { return false; }
263         /// access to the lock (only nest array have one)
264         virtual void lock(bool) {}
265         /// get notification when the cursor leaves this inset
266         virtual void notifyCursorLeaves() {}
267
268         /// write LaTeX and Lyx code
269         virtual void write(WriteStream & os) const;
270         /// write normalized content
271         virtual void normalize(NormalStream &) const;
272         /// write content as something readable by Maple
273         virtual void maplize(MapleStream &) const;
274         /// write content as something readable by Maxima
275         virtual void maximize(MaximaStream &) const;
276         /// write content as something readable by Mathematica
277         virtual void mathematicize(MathematicaStream &) const;
278         /// write content as something resembling MathML
279         virtual void mathmlize(MathMLStream &) const;
280         /// write content as something readable by Octave
281         virtual void octavize(OctaveStream &) const;
282         /// describe content
283         virtual void infoize(std::ostream &) const {}
284         /// plain ascii output
285         virtual int ascii(std::ostream & os, int) const;
286         /// linuxdoc output
287         virtual int linuxdoc(std::ostream & os) const;
288         /// docbook output
289         virtual int docbook(std::ostream & os, bool) const;
290
291         /// dump content to stderr for debugging
292         virtual void dump() const;
293         /// local dispatcher
294         virtual result_type dispatch
295                 (FuncRequest const & cmd, idx_type & idx, pos_type & pos);
296
297         /// LyXInset stuff
298         /// write labels into a list
299         virtual void getLabelList(std::vector<string> &) const {}
300         /// LyXInset stuff
301         virtual bool numberedType() const { return false; }
302         /// hull type
303         virtual string const & getType() const;
304         /// change type
305         virtual void mutate(string const &) {}
306         /// how is the inset called in the .lyx file?
307         virtual string fileInsetLabel() const { return "Formula"; }
308         /// usually the latex name
309         virtual string name() const;
310 };
311
312 std::ostream & operator<<(std::ostream &, MathAtom const &);
313
314 // converts single cell to string
315 string asString(MathArray const & ar);
316 // converts string to single cell
317 MathArray asArray(string const & str);
318
319 // initialize math
320 void initMath();
321
322 /// here to ssave a few includes in the insets
323 class Dialogs;
324 Dialogs & getDialogs();
325
326 #endif