]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.h
halfway through fixing size of math in non-standard sizesd paragraohs like
[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
54
55 struct MathWriteInfo {
56         ///
57         MathWriteInfo(Buffer const * buffer_, std::ostream & os_, bool fragile_)
58                 : buffer(buffer_), os(os_), fragile(fragile_)
59         {}
60         ///
61         explicit MathWriteInfo(std::ostream & os_)
62                 : buffer(0), os(os_), fragile(false)
63         {}
64
65         ///
66         template <class T>
67         MathWriteInfo & operator<<(T const & t)
68         {
69                 os << t;
70                 return *this;
71         }
72         ///
73         MathWriteInfo & operator<<(MathArray const & ar)
74         {
75                 ar.write(*this);
76                 return *this;
77         }
78
79
80         ///
81         Buffer const * buffer;
82         ///
83         std::ostream & os;
84         ///
85         bool fragile;
86 };
87
88
89 class MathInset {
90 public: 
91         /// short of anything else reasonable
92         typedef MathArray::size_type     size_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         ///
103         MathInset();
104         /// the virtual base destructor
105         virtual ~MathInset(); 
106
107         /// draw the object, sets xo_ and yo_ cached values 
108         virtual void draw(Painter &, int x, int y) const;
109         /// write LaTeX and Lyx code
110         virtual void write(MathWriteInfo & os) const;
111         /// write normalized content
112         virtual void writeNormal(std::ostream &) const;
113         /// reproduce itself
114         virtual MathInset * clone() const = 0;
115         ///substitutes macro arguments if necessary
116         virtual void substitute(MathMacro const & macro);
117         /// compute the size of the object, sets ascend_, descend_ and width_
118         virtual void metrics(MathMetricsInfo const & st) const;
119         /// 
120         virtual int ascent() const { return 1; }
121         ///
122         virtual int descent() const { return 1; }
123         ///
124         virtual int width() const { return 2; }
125         ///
126         virtual int height() const;
127
128         /// Where should we go when we press the up cursor key?
129         virtual bool idxUp(idx_type & idx, pos_type & pos) const;
130         /// The down key
131         virtual bool idxDown(idx_type & idx, pos_type & pos) const;
132         /// The left key
133         virtual bool idxLeft(idx_type & idx, pos_type & pos) const;
134         /// The right key
135         virtual bool idxRight(idx_type & idx, pos_type & pos) const;
136
137         /// Move one physical cell up
138         virtual bool idxNext(idx_type & idx, pos_type & pos) const;
139         /// Move one physical cell down
140         virtual bool idxPrev(idx_type & idx, pos_type & pos) const;
141
142         /// Target pos when we enter the inset from the left by pressing "Right"
143         virtual bool idxFirst(idx_type & idx, pos_type & pos) const;
144         /// Target pos when we enter the inset from the right by pressing "Left"
145         virtual bool idxLast(idx_type & idx, pos_type & pos) const;
146
147         /// Where should we go if we press home?
148         virtual bool idxHome(idx_type & idx, pos_type & pos) const;
149         /// Where should we go if we press end?
150         virtual bool idxEnd(idx_type & idx, pos_type & pos) const;
151
152         /// Delete a cell and move cursor
153         // the return value indicates whether the cursor should leave the inset
154         // and/or the whole inset should be deleted
155         virtual void idxDelete(idx_type & idx, bool & popit, bool & deleteit);
156         // deletes a cell range and moves the cursor 
157         virtual void idxDeleteRange(idx_type from, idx_type to);
158         // returns list of cell indices that are "between" from and to for
159         // selection purposes
160         virtual std::vector<idx_type> idxBetween(idx_type from, idx_type to) const;
161
162         ///
163         virtual idx_type nargs() const;
164
165         ///
166         virtual MathArray & cell(idx_type);
167         ///
168         virtual MathArray const & cell(idx_type) const;
169         ///
170         virtual MathXArray & xcell(idx_type);
171         ///
172         virtual MathXArray const & xcell(idx_type) const;
173                         
174         ///
175         virtual int xo() const;
176         ///
177         virtual int yo() const;
178         ///
179         virtual void xo(int tx) const;
180         ///
181         virtual void yo(int ty) const;
182         ///
183
184         ///
185         virtual col_type ncols() const { return 1; }
186         ///
187         virtual row_type nrows() const { return 1; }
188         ///
189         virtual col_type col(row_type) const { return 0; }
190         ///
191         virtual row_type row(row_type) const { return 0; }
192         ///
193         virtual int cellXOffset(row_type) const { return 0; }
194         ///
195         virtual int cellYOffset(row_type) const { return 0; }
196         ///
197         virtual void addRow(row_type) {}
198         ///
199         virtual void delRow(row_type) {}
200         ///
201         virtual void addCol(col_type) {}
202         ///
203         virtual void delCol(col_type) {}
204
205         ///
206         virtual void userSetSize(MathStyles &) {}
207
208         ///
209         virtual void getXY(int & x, int & y) const;
210         ///
211         virtual bool covers(int x, int y) const;
212
213         /// identifies NestInsets
214         virtual MathNestInset * asNestInset() { return 0; }
215         /// identifies CharInsets
216         virtual MathCharInset const * asCharInset() const { return 0; }
217         /// identifies ScriptInsets
218         virtual MathScriptInset const * asScriptInset() const { return 0; }
219         /// identifies ScriptInsets
220         virtual MathScriptInset * asScriptInset() { return 0; }
221         /// identifies MatrixInsets
222         virtual MathMatrixInset const * asMatrixInset() const { return 0; }
223         /// identifies MatrixInsets
224         virtual MathMatrixInset * asMatrixInset() { return 0; }
225         /// identifies SpaceInset
226         virtual MathSpaceInset * asSpaceInset() { return 0; }
227         /// identifies GridInset
228         virtual MathGridInset * asGridInset() { return 0; }
229         /// identifies ArrayInsets
230         virtual MathArrayInset * asArrayInset() { return 0; }
231         /// identifies BoxInsets
232         virtual MathBoxInset * asBoxInset() { return 0; }
233         /// identifies macro templates
234         virtual MathMacroTemplate * asMacroTemplate() { return 0; }
235
236         /// identifies things that can get scripts
237         virtual bool isScriptable() const { return false; }
238         ///
239         virtual bool isActive() const { return nargs() > 0; }
240         ///
241         virtual bool isRelOp() const { return false; }
242         ///
243         virtual bool isMacro() const { return false; }
244
245         ///
246         virtual char getChar() const { return 0; }
247         ///
248         virtual MathTextCodes code() const { return LM_TC_MIN; }
249         /// identifies things that can get \limits or \nolimits
250         virtual bool takesLimits() const { return false; }
251
252         ///
253         virtual void dump() const;
254
255         ///
256         virtual void validate(LaTeXFeatures & features) const;
257         ///
258         virtual void handleFont(MathTextCodes) {}
259
260         ///
261         static int workwidth;
262
263 protected:
264         /// the used font size
265         mutable MathMetricsInfo size_;
266
267 private:
268         /// the following are used for positioning the cursor with the mouse
269         /// cached cursor start position in pixels from the document left
270         mutable int xo_;
271         /// cached cursor start position in pixels from the document top
272         mutable int yo_;
273 };
274
275 std::ostream & operator<<(std::ostream &, MathInset const &);
276
277 #endif