]> git.lyx.org Git - features.git/blob - src/mathed/math_iter.h
apply the ostream changes to mathed, some other small related things
[features.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
195     void GetPos(int&, int&);
196     ///
197     inline
198     void GetIncPos(int&, int&);
199     ///
200     byte* GetString(int&);
201     ///
202     int GetX();
203     ///
204     int GetY();
205     ///
206     void subMetrics(int, int);
207     ///
208     void fitCoord(int, int);
209     /// 
210     void getAD(int& a, int& d);
211     
212     /// Create a new row and insert #ncols# tabs.
213     void addRow();
214         ///
215         void delRow();
216     
217         /**$ These two functions will be moved from here */
218           //@{
219         ///
220     bool setLabel(char* label);
221         ///
222     bool setNumbered(bool);
223         //@}
224         
225         ///
226     void setTab(int, int);
227     /// Merge the array at current position
228     void Merge(LyxArrayBase*);
229     /// Delete every object from current position to pos2
230     void Clean(int pos2);
231     MathedRowSt *adjustVerticalSt();
232     
233  private:
234     /// This function is not recursive, as MathPar::Metrics is
235     void IMetrics(int, int&, int&, int&);
236     /// Font size (display, text, script, script2) 
237     int size;
238     /// current position
239     int x, y;
240 //    /// max ascent and descent
241 //    int asc, des;
242         ///
243     MathParInset *p;
244     
245     // Limits auxiliary variables
246     /// Position and max width of a script
247     int sx, sw;
248     /// true= center, false= left align (default)
249     bool limits;
250     /// Type of previous script
251     short s_type;  
252
253     void ipush();
254
255     void ipop();
256
257  protected:
258     /// 
259     MathedRowSt *crow;
260     
261  private:
262     ///
263     friend class MathedCursor;
264 };
265
266
267 //--------------------   Inline functions   --------------------------//
268
269
270 inline
271 bool MathedIter::IsInset()
272 {
273     return MathIsInset((*array)[pos]);
274 }
275  
276 inline
277 bool MathedIter::IsActive()
278 {
279     return MathIsActive((*array)[pos]);
280 }
281
282 inline
283 bool MathedIter::IsFont()
284 {
285     return MathIsFont((*array)[pos]);
286 }
287
288
289 inline
290 bool MathedIter::IsScript()
291 {
292     return MathIsScript((*array)[pos]);
293 }   
294
295 inline
296 bool MathedIter::IsTab()
297 {
298     return ((*array)[pos] == LM_TC_TAB);
299 }  
300
301
302 inline
303 bool MathedIter::IsCR()
304 {
305     return ((*array)[pos] == LM_TC_CR);
306 }  
307
308
309 inline
310 MathedIter::MathedIter(LyxArrayBase * d): array(d)
311 {
312     pos = 0;
313     row = col = 0;
314     fcode = (array && IsFont()) ? (*array)[0]: 0;
315 }
316      
317
318 inline
319 void MathedIter::ipush()
320
321     stck.fcode = fcode;
322     stck.pos = pos;
323     stck.row = row;
324     stck.col = col; 
325 }
326
327
328 inline
329 void MathedIter::ipop()
330
331     fcode = stck.fcode;
332     pos = stck.pos;
333     row = stck.row;
334     col = stck.col;  
335 }
336
337
338 inline
339 void MathedXIter::GetPos(int&xx, int& yy)
340
341     if (p) 
342       p->GetXY(xx, yy);
343     else
344       { xx = 0;  yy = 0; }        
345     xx += x;  yy += y;
346 }
347
348 inline 
349 int MathedXIter::GetX() 
350
351     int xx, yy;
352     GetPos(xx, yy);
353     return xx; 
354 }
355
356 inline 
357 int MathedXIter::GetY() 
358
359     int xx, yy;
360     GetPos(xx, yy);
361     return yy; 
362 }
363
364
365 inline
366 void MathedXIter::GetIncPos(int& xx, int& yy) 
367
368     xx = x;  yy = y; 
369 }
370
371
372 inline
373 void MathedXIter::getAD(int& a, int& d) 
374
375     if (crow) {
376         a = crow->asc;
377         d = crow->desc;
378     } else 
379       if (p) {
380           a = p->Ascent();
381           d = p->Descent();
382       } else {
383           a = d = 0;
384       }
385 }
386
387
388 #endif
389