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