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