]> git.lyx.org Git - features.git/blob - src/mathed/math_iter.h
make doc++ able to generate the source documentation for lyx
[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: 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() { return array->Last()<= 1; }
64     ///
65     int OK() { return array && (pos < array->Last()); }
66     ///
67     int IsFirst() { return (pos == 0); }
68     ///
69     byte GetChar();
70     ///
71     byte * GetString(int& len);
72     ///
73     MathedInset * GetInset();
74     ///
75     MathParInset * GetActiveInset();
76     ///
77     bool IsInset();
78     ///
79     bool IsActive();
80     ///
81     bool IsFont();
82     ///
83     bool IsScript();
84     ///
85     bool IsTab();
86     ///
87     bool IsCR();
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() { 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     short fcode;
132     ///
133     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() { 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     ///
219     bool setLabel(char* label);
220     ///
221     bool setNumbered(bool);
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)[pos]);
272 }
273  
274 inline
275 bool MathedIter::IsActive()
276 {
277     return MathIsActive((*array)[pos]);
278 }
279
280 inline
281 bool MathedIter::IsFont()
282 {
283     return MathIsFont((*array)[pos]);
284 }
285
286
287 inline
288 bool MathedIter::IsScript()
289 {
290     return MathIsScript((*array)[pos]);
291 }   
292
293 inline
294 bool MathedIter::IsTab()
295 {
296     return ((*array)[pos] == LM_TC_TAB);
297 }  
298
299
300 inline
301 bool MathedIter::IsCR()
302 {
303     return ((*array)[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)[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