]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.h
next step...
[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 "xarray.h"
32 #include "math_defs.h"
33
34 /** Abstract base class for all math objects.
35     A math insets is for use of the math editor only, it isn't a
36     general LyX inset. It's used to represent all the math objects.
37     The formulaInset (a LyX inset) encapsulates a math inset.
38 */
39
40
41 class MathArrayInset;
42 class MathBoxInset;
43 class MathCharInset;
44 class MathGridInset;
45 class MathNestInset;
46 class MathMatrixInset;
47 class MathScriptInset;
48 class MathSpaceInset;
49 class MathMacroTemplate;
50
51 class LaTeXFeatures;
52 class Buffer;
53 class BufferView;
54 class UpdatableInset;
55
56
57 struct MathWriteInfo {
58         ///
59         MathWriteInfo(Buffer const * buffer_, std::ostream & os_, bool fragile_)
60                 : buffer(buffer_), os(os_), fragile(fragile_)
61         {}
62         ///
63         explicit MathWriteInfo(std::ostream & os_)
64                 : buffer(0), os(os_), fragile(false)
65         {}
66
67         ///
68         template <class T>
69         MathWriteInfo & operator<<(T const & t)
70         {
71                 os << t;
72                 return *this;
73         }
74         ///
75         MathWriteInfo & operator<<(MathArray const & ar)
76         {
77                 ar.write(*this);
78                 return *this;
79         }
80
81
82         ///
83         Buffer const * buffer;
84         ///
85         std::ostream & os;
86         ///
87         bool fragile;
88 };
89
90
91 class MathInset {
92 public: 
93         /// short of anything else reasonable
94         typedef MathArray::size_type     size_type;
95         /// type for cursor positions within a cell
96         typedef MathArray::size_type     pos_type;
97         /// type for cell indices
98         typedef size_type                idx_type;
99         /// type for row numbers
100         typedef size_type                row_type;
101         /// type for column numbers
102         typedef size_type                col_type;
103
104         ///
105         MathInset();
106         /// the virtual base destructor
107         virtual ~MathInset(); 
108
109         /// draw the object
110         virtual void draw(Painter &, int x, int y) const;
111         /// write LaTeX and Lyx code
112         virtual void write(MathWriteInfo & os) const;
113         /// write normalized content
114         virtual void writeNormal(std::ostream &) const;
115         /// reproduce itself
116         virtual MathInset * clone() const = 0;
117         ///substitutes macro arguments if necessary
118         virtual void substitute(MathMacro const & macro);
119         /// compute the size of the object, sets ascend_, descend_ and width_
120         virtual void metrics(MathMetricsInfo const & st) const;
121         /// 
122         virtual int ascent() const { return 1; }
123         ///
124         virtual int descent() const { return 1; }
125         ///
126         virtual int width() const { return 2; }
127         ///
128         virtual int height() const;
129
130         /// Where should we go when we press the up cursor key?
131         virtual bool idxUp(idx_type & idx, pos_type & pos) const;
132         /// The down key
133         virtual bool idxDown(idx_type & idx, pos_type & pos) const;
134         /// The left key
135         virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
136         /// The right key
137         virtual bool idxRight(idx_type & idx, pos_type & pos) const;
138
139         /// Move one physical cell up
140         virtual bool idxNext(idx_type & idx, pos_type & pos) const;
141         /// Move one physical cell down
142         virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
143
144         /// Target pos when we enter the inset from the left by pressing "Right"
145         virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
146         /// Target pos when we enter the inset from the right by pressing "Left"
147         virtual bool idxLast(idx_type & idx, pos_type & pos) const;
148
149         /// Where should we go if we press home?
150         virtual bool idxHome(idx_type & idx, pos_type & pos) const;
151         /// Where should we go if we press end?
152         virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
153
154         /// Delete a cell and move cursor
155         // the return value indicates whether the cursor should leave the inset
156         // and/or the whole inset should be deleted
157         virtual void idxDelete(idx_type & idx, bool & popit, bool & deleteit);
158         // deletes a cell range and moves the cursor 
159         virtual void idxDeleteRange(idx_type from, idx_type to);
160         // returns list of cell indices that are "between" from and to for
161         // selection purposes
162         virtual std::vector<idx_type> idxBetween(idx_type from, idx_type to) const;
163
164         ///
165         virtual idx_type nargs() const;
166
167         ///
168         virtual MathArray & cell(idx_type);
169         ///
170         virtual MathArray const & cell(idx_type) const;
171         ///
172         virtual MathXArray & xcell(idx_type);
173         ///
174         virtual MathXArray const & xcell(idx_type) const;
175                         
176         ///
177         virtual col_type ncols() const { return 1; }
178         ///
179         virtual row_type nrows() const { return 1; }
180         ///
181         virtual col_type col(row_type) const { return 0; }
182         ///
183         virtual row_type row(row_type) const { return 0; }
184         ///
185         virtual int cellXOffset(row_type) const { return 0; }
186         ///
187         virtual int cellYOffset(row_type) const { return 0; }
188         ///
189         virtual void addRow(row_type) {}
190         ///
191         virtual void delRow(row_type) {}
192         ///
193         virtual void addCol(col_type) {}
194         ///
195         virtual void delCol(col_type) {}
196
197         ///
198         virtual bool covers(int x, int y) const;
199
200         /// identifies NestInsets
201         virtual MathNestInset * asNestInset() { return 0; }
202         /// identifies CharInsets
203         virtual MathCharInset const * asCharInset() const { return 0; }
204         /// identifies ScriptInsets
205         virtual MathScriptInset const * asScriptInset() const { return 0; }
206         /// identifies ScriptInsets
207         virtual MathScriptInset * asScriptInset() { return 0; }
208         /// identifies MatrixInsets
209         virtual MathMatrixInset const * asMatrixInset() const { return 0; }
210         /// identifies MatrixInsets
211         virtual MathMatrixInset * asMatrixInset() { return 0; }
212         /// identifies SpaceInset
213         virtual MathSpaceInset * asSpaceInset() { return 0; }
214         /// identifies GridInset
215         virtual MathGridInset * asGridInset() { return 0; }
216         /// identifies ArrayInsets
217         virtual MathArrayInset * asArrayInset() { return 0; }
218         /// identifies BoxInsets
219         virtual MathBoxInset * asBoxInset() { return 0; }
220         /// identifies macro templates
221         virtual MathMacroTemplate * asMacroTemplate() { return 0; }
222         /// identifies hyperactive insets
223         virtual UpdatableInset * asHyperActiveInset() const { return 0; }
224
225         /// identifies things that can get scripts
226         virtual bool isScriptable() const { return false; }
227         ///
228         virtual bool isActive() const { return nargs() > 0; }
229         /// identifies insets from the outer world
230         virtual bool isHyperActive() const { return 0; }
231         ///
232         virtual bool isRelOp() const { return false; }
233         ///
234         virtual bool isMacro() const { return false; }
235
236         ///
237         virtual char getChar() const { return 0; }
238         ///
239         virtual MathTextCodes code() const { return LM_TC_MIN; }
240         /// identifies things that can get \limits or \nolimits
241         virtual bool takesLimits() const { return false; }
242
243         ///
244         virtual void dump() const;
245         ///
246         virtual void edit(BufferView *, int, int, unsigned int) {}
247
248         ///
249         virtual void validate(LaTeXFeatures & features) const;
250         ///
251         virtual void handleFont(MathTextCodes) {}
252 };
253
254 std::ostream & operator<<(std::ostream &, MathInset const &);
255
256 #endif