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