]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iter.h
Fix mathed bugs.
[lyx.git] / src / mathed / math_iter.h
1 // -*- C++ -*-
2 /*
3  *  File:        math_iter.h
4  *  Purpose:     Iterator for Math paragraphs
5  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
6  *  Created:     January 1997
7  *  Description: Using iterators is the only way to handle math paragraphs 
8  *
9  *  Dependencies: Xlib
10  *
11  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
12  *
13  *   Version: 0.8beta, Mathed & 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
20 #ifndef MATH_ITER
21 #define MATH_ITER
22
23 #ifdef __GNUG__
24 #pragma interface
25 #endif
26
27 #include "math_defs.h"
28
29 ///
30 enum mathIterFlags {
31     /// Allow newlines
32     MthIF_CR = 1,
33     /// Allow tabs
34     MthIF_Tabs = 2
35 };
36
37
38 /** Specialized array iterator for math paragraph.
39     Used for storing and querying data operations
40 */
41 class MathedIter {
42  public:
43     ///
44     MathedIter() {
45         pos = 0;
46         fcode = 0;
47         array = 0;
48         flags = 0;
49         ncols = row = col = 0;
50     }
51     ///
52     explicit
53     MathedIter(LyxArrayBase *);
54     ///
55     virtual ~MathedIter() {}
56     ///
57     bool goNextCode(MathedTextCodes);
58     ///
59     void goPosRel(int);
60     ///
61     void goPosAbs(int);
62     ///
63     int Empty() const { return array->Last()<= 1; }
64     ///
65     int OK() const { return array && (pos < array->Last()); }
66     ///
67     int IsFirst() const { return (pos == 0); }
68     ///
69     byte GetChar() const;
70     ///
71     byte * GetString(int & len) const;
72     ///
73     string const GetString() const;
74     ///
75     MathedInset * GetInset() const;
76     ///
77     MathParInset * GetActiveInset() const;
78     ///
79     bool IsInset() const;
80     ///
81     bool IsActive() const;
82     ///
83     bool IsFont() const;
84     ///
85     bool IsScript() const;
86     ///
87     bool IsTab() const;
88     ///
89     bool IsCR() const;
90     ///
91     virtual void Reset();
92     ///
93     virtual void Insert(byte, MathedTextCodes c = LM_TC_CONST);
94     ///
95     virtual void Insert(MathedInset *, int t = LM_TC_INSET);
96     ///
97     virtual bool Delete();
98     ///
99     virtual bool Next();
100     /// Check consistency of tabs and newlines
101     void checkTabs();
102     /// Try to adjust tabs in the expected place, as in eqnarrays
103     void adjustTabs();
104     ///
105     short FCode() const { return fcode; }
106     ///
107     int getPos() const { return pos; }
108     ///
109     int getRow() const { return row; }
110     ///
111     int getCol() const { return col; }
112     ///
113     void setNumCols(int n) { ncols = n; }
114     ///
115     void SetData(LyxArrayBase * a) { array = a; Reset(); }
116     ///
117     LyxArrayBase * GetData() const { return array; }
118     
119     /// Copy every object from position p1 to p2
120     LyxArrayBase * Copy(int p1 = 0, int p2 = 10000);
121    
122     /// Delete every object from position p1 to p2
123     void Clear();
124     
125  protected:
126     ///
127     void split(int);
128     ///
129     void join(int);
130     ///
131     int flags;
132     ///
133     mutable short fcode;
134     ///
135     mutable int pos;
136     ///
137     int row, col, ncols;
138     ///
139     LyxArrayBase * array;
140     // one element stack
141     struct MIState {
142         ///
143         short fcode;
144         ///
145         int x, y;
146         ///
147         int pos, row, col;
148     };
149     ///
150     MIState stck;
151     /// Saves the current state of the iterator
152     virtual void ipush();
153     /// Recover previous state
154     virtual void ipop();
155 };
156
157 ///
158 #define MX_WAS_SUB   1
159 ///
160 #define MX_WAS_SUPER 2
161
162
163 /**
164  A graphic iterator (updates position.) Used for
165  metrics and updating cursor position
166  */
167 class MathedXIter: public MathedIter {
168  public:
169     ///
170     MathedXIter()
171             : MathedIter(), sx(0), sw(0) {
172             x = y = size = 0;  p = 0; crow = 0;
173     }
174     //
175     MathedXIter(MathParInset *);
176     ///
177     void SetData(MathParInset *);
178     ///
179     MathParInset * getPar() const { return p; }
180     ///
181     bool Next();
182     ///
183     bool Prev();
184     ///
185     bool Up();
186     ///
187     bool Down();
188     ///
189     bool goNextColumn();
190     ///
191     void GoLast();
192     ///
193     void GoBegin();
194     ///
195     void Adjust();
196     ///
197     inline
198     void GetPos(int &, int &) const;
199     ///
200     inline
201     void GetIncPos(int &, int &) const;
202     ///
203     byte * GetString(int &) const ;
204     ///
205     string const GetString() const;
206     ///
207     int GetX() const;
208     ///
209     int GetY() const;
210     ///
211     void subMetrics(int, int);
212     ///
213     void fitCoord(int, int);
214     /// 
215     void getAD(int & a, int & d) const;
216     
217     /// Create a new row and insert #ncols# tabs.
218     void addRow();
219     ///
220     void delRow();
221     
222     ///
223     bool setLabel(string const & label);
224     ///
225     static string error_label;
226     ///
227     string const & getLabel() const {
228             return crow ? crow->getLabel() : error_label;
229     }
230     ///
231     bool setNumbered(bool);
232         
233     ///
234     void setTab(int, int);
235     /// Merge the array at current position
236     void Merge(LyxArrayBase *);
237     /// Delete every object from current position to pos2
238     void Clean(int pos2);
239     ///
240     MathedRowSt * adjustVerticalSt();
241     
242 private:
243     /// This function is not recursive, as MathPar::Metrics is
244     void IMetrics(int, int &, int &, int &);
245     /// Font size (display, text, script, script2) 
246     int size;
247     /// current position
248     mutable int x;
249     ///
250     int y;
251     ///
252     MathParInset * p;
253     
254     // Limits auxiliary variables
255     /// Position and max width of a script
256     int sx, sw;
257     /// true= center, false= left align (default)
258     bool limits;
259     /// Type of previous script
260     short s_type;  
261     ///
262     void ipush();
263     ///
264     void ipop();
265
266 protected:
267     /// 
268     MathedRowSt * crow;
269     
270 private:
271     ///
272     friend class MathedCursor;
273 };
274
275
276 //--------------------   Inline functions   --------------------------//
277
278
279 inline
280 bool MathedIter::IsInset() const
281 {
282     return MathIsInset((*array)[pos]);
283 }
284  
285 inline
286 bool MathedIter::IsActive() const
287 {
288     return MathIsActive((*array)[pos]);
289 }
290
291 inline
292 bool MathedIter::IsFont() const
293 {
294     return MathIsFont((*array)[pos]);
295 }
296
297
298 inline
299 bool MathedIter::IsScript() const
300 {
301     return MathIsScript((*array)[pos]);
302 }   
303
304 inline
305 bool MathedIter::IsTab() const
306 {
307     return ((*array)[pos] == LM_TC_TAB);
308 }  
309
310
311 inline
312 bool MathedIter::IsCR() const
313 {
314     return ((*array)[pos] == LM_TC_CR);
315 }  
316
317
318 inline
319 MathedIter::MathedIter(LyxArrayBase * d)
320         : array(d)
321 {
322     pos = 0;
323     row = col = 0;
324     fcode = (array && IsFont()) ? (*array)[0]: 0;
325 }
326      
327
328 inline
329 void MathedIter::ipush()
330
331     stck.fcode = fcode;
332     stck.pos = pos;
333     stck.row = row;
334     stck.col = col; 
335 }
336
337
338 inline
339 void MathedIter::ipop()
340
341     fcode = stck.fcode;
342     pos = stck.pos;
343     row = stck.row;
344     col = stck.col;  
345 }
346
347
348 inline
349 void MathedXIter::GetPos(int & xx, int & yy) const
350
351     if (p) 
352       p->GetXY(xx, yy);
353     else
354       { xx = 0;  yy = 0; }        
355     xx += x;  yy += y;
356 }
357
358 inline 
359 int MathedXIter::GetX() const
360
361     int xx;
362     int dummy_y;
363     GetPos(xx, dummy_y);
364     return xx; 
365 }
366
367 inline 
368 int MathedXIter::GetY() const
369
370     int dummy_x;
371     int yy;
372     GetPos(dummy_x, yy);
373     return yy; 
374 }
375
376
377 inline
378 void MathedXIter::GetIncPos(int & xx, int & yy) const
379
380     xx = x;  yy = y; 
381 }
382
383
384 inline
385 void MathedXIter::getAD(int & a, int & d) const
386
387     if (crow) {
388         a = crow->ascent();
389         d = crow->descent();
390     } else 
391       if (p) {
392           a = p->Ascent();
393           d = p->Descent();
394       } else {
395           a = d = 0;
396       }
397 }
398
399
400 #endif
401