]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.h
fix nasty bug when inserting labels when cursor was in nested array
[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 "math_xdata.h"
32 #include "math_defs.h"
33
34 /**
35
36 Abstract base class for all math objects.  A math insets is for use of the
37 math editor only, it isn't a general LyX inset. It's used to represent all
38 the math objects. 
39
40 Math insets do not know there parents, a cursor position or things
41 like that. The are dumb object that are contained in other math insets
42 (mathNestInsets, in fact) thus forming a tree. The root of this tree is
43 always a mathHullInset, which provides an interface to the Outer World by
44 inclusion in the "real LyX insets" FormulaInset and FormulaMacroInset.
45
46 */
47
48
49 class MathArrayInset;
50 class MathBoxInset;
51 class MathCharInset;
52 class MathDelimInset;
53 class MathFuncInset;
54 class MathGridInset;
55 class MathFracInset;
56 class MathHullInset;
57 class MathMatrixInset;
58 class MathNestInset;
59 class MathScriptInset;
60 class MathStringInset;
61 class MathSpaceInset;
62 class MathSymbolInset;
63
64 class NormalStream;
65 class OctaveStream;
66 class MapleStream;
67 class MathMLStream;
68 class WriteStream;
69 class MathArray;
70
71 class LaTeXFeatures;
72 class BufferView;
73 class UpdatableInset;
74 class MathMacroTemplate;
75 class MathPosFinder;
76
77
78 class MathInset {
79 public: 
80         /// short of anything else reasonable
81         typedef MathArray::size_type     size_type;
82         /// type for cursor positions within a cell
83         typedef MathArray::size_type     pos_type;
84         /// type for cell indices
85         typedef size_type                idx_type;
86         /// type for row numbers
87         typedef size_type                row_type;
88         /// type for column numbers
89         typedef size_type                col_type;
90
91         /// our members behave nicely...
92         MathInset() {}
93         /// the virtual base destructor
94         virtual ~MathInset() {}
95
96         /// draw the object
97         virtual void draw(Painter &, int x, int y) const;
98         /// write LaTeX and Lyx code
99         virtual void write(WriteStream & os) const;
100         /// reproduce itself
101         virtual MathInset * clone() const = 0;
102         /// substitutes macro arguments if necessary
103         virtual void substitute(MathMacro const & macro);
104         /// compute the size of the object, sets ascend_, descend_ and width_
105         // updates the (xo,yo)-caches of all contained cells
106         virtual void metrics(MathMetricsInfo const & st) const;
107         /// the ascent of the inset above the baseline
108         virtual int ascent() const { return 1; }
109         /// the descent of the inset below the baseline
110         virtual int descent() const { return 1; }
111         /// total width
112         virtual int width() const { return 2; }
113         /// total height (== ascent + descent)
114         virtual int height() const;
115
116         /// Where should we go when we press the up cursor key?
117         virtual bool idxUp(idx_type & idx, pos_type & pos) const;
118         /// The down key
119         virtual bool idxDown(idx_type & idx, pos_type & pos) const;
120         /// The left key
121         virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
122         /// The right key
123         virtual bool idxRight(idx_type & idx, pos_type & pos) const;
124
125         /// Move one physical cell up
126         virtual bool idxNext(idx_type & idx, pos_type & pos) const;
127         /// Move one physical cell down
128         virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
129
130         /// Target pos when we enter the inset from the left by pressing "Right"
131         virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
132         /// Target pos when we enter the inset from the left by pressing "Up"
133         virtual bool idxFirstUp(idx_type & idx, pos_type & pos) const;
134         /// Target pos when we enter the inset from the left by pressing "Down"
135         virtual bool idxFirstDown(idx_type & idx, pos_type & pos) const;
136         /// Target pos when we enter the inset from the right by pressing "Left"
137         virtual bool idxLast(idx_type & idx, pos_type & pos) const;
138         /// Target pos when we enter the inset from the right by pressing "Up"
139         virtual bool idxLastUp(idx_type & idx, pos_type & pos) const;
140         /// Target pos when we enter the inset from the right by pressing "Down"
141         virtual bool idxLastDown(idx_type & idx, pos_type & pos) const;
142
143         /// Where should we go if we press home?
144         virtual bool idxHome(idx_type & idx, pos_type & pos) const;
145         /// Where should we go if we press end?
146         virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
147
148         /// Delete a cell and move cursor
149         // the return value indicates whether the cursor should leave the inset
150         // and/or the whole inset should be deleted
151         virtual void idxDelete(idx_type & idx, bool & popit, bool & deleteit);
152         // deletes a cell range and moves the cursor 
153         virtual void idxDeleteRange(idx_type from, idx_type to);
154         // returns list of cell indices that are "between" from and to for
155         // selection purposes
156         virtual std::vector<idx_type> idxBetween(idx_type from, idx_type to) const;
157
158         /// the number of nested cells this inset owns
159         virtual idx_type nargs() const;
160
161         /// return cell given its number
162         virtual MathArray & cell(idx_type);
163         /// return cell given its number
164         virtual MathArray const & cell(idx_type) const;
165         /// return cell plus drawing cache given its number
166         virtual MathXArray & xcell(idx_type);
167         /// return cell plus drawing cache given its number
168         virtual MathXArray const & xcell(idx_type) const;
169                         
170         /// the number of columns of this inset if it is grid-like
171         virtual col_type ncols() const { return 1; }
172         /// the number of rows of this inset if it is grid-like
173         virtual row_type nrows() const { return 1; }
174         /// to which column belongs a cell with a given index?
175         virtual col_type col(idx_type) const { return 0; }
176         /// to which row belongs a cell with a given index?
177         virtual row_type row(idx_type) const { return 0; }
178         /// any additional x-offset when drawing a cell?
179         virtual int cellXOffset(idx_type) const { return 0; }
180         /// any additional y-offset when drawing a cell?
181         virtual int cellYOffset(idx_type) const { return 0; }
182
183         /// identifies certain types of insets
184         virtual MathArrayInset        * asArrayInset()        { return 0; }
185         virtual MathBoxInset          * asBoxInset()          { 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 MathFuncInset         * asFuncInset()         { return 0; }
190         virtual MathFracInset         * asFracInset()         { return 0; }
191         virtual MathGridInset         * asGridInset()         { return 0; }
192         virtual MathHullInset         * asHullInset()         { return 0; }
193         virtual MathHullInset const   * asHullInset() const   { return 0; }
194         virtual MathMacroTemplate     * asMacroTemplate()     { return 0; }
195         virtual MathMatrixInset const * asMatrixInset() const { return 0; }
196         virtual MathNestInset         * asNestInset()         { return 0; }
197         virtual MathScriptInset       * asScriptInset()       { return 0; }
198         virtual MathScriptInset const * asScriptInset() const { return 0; }
199         virtual MathSpaceInset        * asSpaceInset()        { return 0; }
200         virtual MathStringInset       * asStringInset()       { return 0; }
201         virtual MathSymbolInset const * asSymbolInset() const { return 0; }
202         virtual UpdatableInset   * asHyperActiveInset() const { return 0; }
203
204         /// identifies things that can get scripts
205         virtual bool isScriptable() const { return false; }
206         /// thing that can be moved into
207         virtual bool isActive() const { return nargs() > 0; }
208         /// identifies insets from the outer world
209         virtual bool isHyperActive() const { return 0; }
210         /// is the a relational operator (used for splitting equations)
211         virtual bool isRelOp() const { return false; }
212
213         /// return the content as char if the inset is able to do so
214         virtual char getChar() const { return 0; }
215         /// return the content's char code if it has one
216         virtual MathTextCodes code() const { return LM_TC_MIN; }
217         /// identifies things that can get \limits or \nolimits
218         virtual bool takesLimits() const { return false; }
219
220         ///
221         virtual void edit(BufferView *, int, int, unsigned int) {}
222
223         /// request "external features"
224         virtual void validate(LaTeXFeatures & features) const;
225         /// char char code if possible
226         virtual void handleFont(MathTextCodes) {}
227         /// is this inset equal to a given other inset?
228         virtual bool match(MathInset *) const { return false; }
229         /// replace things by other things
230         virtual void replace(ReplaceData &) {}
231
232         /// write normalized content
233         virtual void normalize(NormalStream &) const;
234         /// write content as something readable by Maple
235         virtual void maplize(MapleStream &) const;
236         /// write content as something resembling MathML
237         virtual void mathmlize(MathMLStream &) const;
238         /// write content as something readable by Octave
239         virtual void octavize(OctaveStream &) const;
240
241         /// dump content to stderr for debugging
242         virtual void dump() const;
243 };
244
245 std::ostream & operator<<(std::ostream &, MathInset const &);
246
247 #endif