]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iter.h
Hopefully fix the problem with stateText() in lyxfont.C
[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 /**
39  Specialized array iterator for amth paragraph.  Used for
40  storing and querying data operations
41  */
42 class MathedIter {
43  public:
44     ///
45     MathedIter()
46     {
47         pos = 0;
48         fcode = 0;
49         array = 0;
50         flags = 0;
51         ncols = row = col = 0;
52     }
53     ///
54         explicit
55     MathedIter(LyxArrayBase *);
56     ///
57     virtual ~MathedIter() { }
58     ///
59     bool goNextCode(MathedTextCodes);
60     ///
61    void goPosRel(int);
62     ///
63     void goPosAbs(int);
64     ///
65     int Empty() { return array->Last()<= 1; }
66     ///
67     int OK() { return array && (pos < array->Last()); }
68     ///
69     int IsFirst() { return (pos == 0); }
70     ///
71     byte GetChar();
72     ///
73     byte * GetString(int& len);
74     ///
75     MathedInset * GetInset();
76     ///
77     MathParInset * GetActiveInset();
78     ///
79     bool IsInset();
80     ///
81     bool IsActive();
82     ///
83     bool IsFont();
84     ///
85     bool IsScript();
86     ///
87     bool IsTab();
88     ///
89     bool IsCR();
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() { 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     short fcode;
134     ///
135     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     } stck;
149     
150     /// Saves the current state of the iterator
151     virtual void ipush();
152     /// Recover previous state
153     virtual void ipop();
154 };
155
156 ///
157 #define MX_WAS_SUB   1
158 ///
159 #define MX_WAS_SUPER 2
160
161
162 /**
163  A graphic iterator (updates position.) Used for
164  metrics and updating cursor position
165  */
166 class MathedXIter: public MathedIter {
167  public:
168     ///
169     MathedXIter(): MathedIter() { x = y = size = 0;  p = 0; crow = 0; }
170     ///
171     MathedXIter(MathParInset*);
172     ///
173     ~MathedXIter() { };
174     ///
175     void SetData(MathParInset *);
176     ///
177     MathParInset *getPar() { 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&);
197     ///
198     inline
199     void GetIncPos(int&, int&);
200     ///
201     byte* GetString(int&);
202     ///
203     int GetX();
204     ///
205     int GetY();
206     ///
207     void subMetrics(int, int);
208     ///
209     void fitCoord(int, int);
210     /// 
211     void getAD(int& a, int& d);
212     
213     /// Create a new row and insert #ncols# tabs.
214     void addRow();
215         ///
216         void delRow();
217     
218         /**$ These two functions will be moved from here */
219           //@{
220         ///
221     bool setLabel(char* label);
222         ///
223     bool setNumbered(bool);
224         //@}
225         
226         ///
227     void setTab(int, int);
228     /// Merge the array at current position
229     void Merge(LyxArrayBase*);
230     /// Delete every object from current position to pos2
231     void Clean(int pos2);
232     MathedRowSt *adjustVerticalSt();
233     
234  private:
235     /// This function is not recursive, as MathPar::Metrics is
236     void IMetrics(int, int&, int&, int&);
237     /// Font size (display, text, script, script2) 
238     int size;
239     /// current position
240     int x, y;
241 //    /// max ascent and descent
242 //    int asc, des;
243         ///
244     MathParInset *p;
245     
246     // Limits auxiliary variables
247     /// Position and max width of a script
248     int sx, sw;
249     /// true= center, false= left align (default)
250     bool limits;
251     /// Type of previous script
252     short s_type;  
253
254     void ipush();
255
256     void ipop();
257
258  protected:
259     /// 
260     MathedRowSt *crow;
261     
262  private:
263     ///
264     friend class MathedCursor;
265 };
266
267
268 //--------------------   Inline functions   --------------------------//
269
270
271 inline
272 bool MathedIter::IsInset()
273 {
274     return MathIsInset((*array)[pos]);
275 }
276  
277 inline
278 bool MathedIter::IsActive()
279 {
280     return MathIsActive((*array)[pos]);
281 }
282
283 inline
284 bool MathedIter::IsFont()
285 {
286     return MathIsFont((*array)[pos]);
287 }
288
289
290 inline
291 bool MathedIter::IsScript()
292 {
293     return MathIsScript((*array)[pos]);
294 }   
295
296 inline
297 bool MathedIter::IsTab()
298 {
299     return ((*array)[pos] == LM_TC_TAB);
300 }  
301
302
303 inline
304 bool MathedIter::IsCR()
305 {
306     return ((*array)[pos] == LM_TC_CR);
307 }  
308
309
310 inline
311 MathedIter::MathedIter(LyxArrayBase * d): array(d)
312 {
313     pos = 0;
314     row = col = 0;
315     fcode = (array && IsFont()) ? (*array)[0]: 0;
316 }
317      
318
319 inline
320 void MathedIter::ipush()
321
322     stck.fcode = fcode;
323     stck.pos = pos;
324     stck.row = row;
325     stck.col = col; 
326 }
327
328
329 inline
330 void MathedIter::ipop()
331
332     fcode = stck.fcode;
333     pos = stck.pos;
334     row = stck.row;
335     col = stck.col;  
336 }
337
338
339 inline
340 void MathedXIter::GetPos(int&xx, int& yy)
341
342     if (p) 
343       p->GetXY(xx, yy);
344     else
345       { xx = 0;  yy = 0; }        
346     xx += x;  yy += y;
347 }
348
349 inline 
350 int MathedXIter::GetX() 
351
352     int xx, yy;
353     GetPos(xx, yy);
354     return xx; 
355 }
356
357 inline 
358 int MathedXIter::GetY() 
359
360     int xx, yy;
361     GetPos(xx, yy);
362     return yy; 
363 }
364
365
366 inline
367 void MathedXIter::GetIncPos(int& xx, int& yy) 
368
369     xx = x;  yy = y; 
370 }
371
372
373 inline
374 void MathedXIter::getAD(int& a, int& d) 
375
376     if (crow) {
377         a = crow->asc;
378         d = crow->desc;
379     } else 
380       if (p) {
381           a = p->Ascent();
382           d = p->Descent();
383       } else {
384           a = d = 0;
385       }
386 }
387
388
389 #endif
390