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