]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iter.h
use the new sstream return non-pods as const, use string instead of char * in a lot...
[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     byte * GetString(int & len) const;
72     ///
73     string const GetString() const;
74     ///
75     MathedInset * GetInset() const;
76     ///
77     MathParInset * GetActiveInset() const;
78     ///
79     bool IsInset() const;
80     ///
81     bool IsActive() const;
82     ///
83     bool IsFont() const;
84     ///
85     bool IsScript() const;
86     ///
87     bool IsTab() const;
88     ///
89     bool IsCR() const;
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() const { 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     mutable short fcode;
134     ///
135     mutable 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     };
149     ///
150     MIState stck;
151     /// Saves the current state of the iterator
152     virtual void ipush();
153     /// Recover previous state
154     virtual void ipop();
155 };
156
157 ///
158 #define MX_WAS_SUB   1
159 ///
160 #define MX_WAS_SUPER 2
161
162
163 /**
164  A graphic iterator (updates position.) Used for
165  metrics and updating cursor position
166  */
167 class MathedXIter: public MathedIter {
168  public:
169     ///
170     MathedXIter()
171             : MathedIter(), sx(0), sw(0) {
172             x = y = size = 0;  p = 0; crow = 0;
173     }
174     //
175     MathedXIter(MathParInset *);
176     ///
177     void SetData(MathParInset *);
178     ///
179     MathParInset * getPar() const { return p; }
180     ///
181     bool Next();
182     ///
183     bool Prev();
184     ///
185     bool Up();
186     ///
187     bool Down();
188     ///
189     bool goNextColumn();
190     ///
191     void GoLast();
192     ///
193     void GoBegin();
194     ///
195     void Adjust();
196     ///
197     inline
198     void GetPos(int &, int &) const;
199     ///
200     inline
201     void GetIncPos(int &, int &) const;
202     ///
203     byte * GetString(int &) const ;
204     ///
205     string const GetString() const;
206     ///
207     int GetX() const;
208     ///
209     int GetY() const;
210     ///
211     void subMetrics(int, int);
212     ///
213     void fitCoord(int, int);
214     /// 
215     void getAD(int & a, int & d) const;
216     
217     /// Create a new row and insert #ncols# tabs.
218     void addRow();
219     ///
220     void delRow();
221     
222     ///
223     bool setLabel(string const & label);
224     ///
225     bool setNumbered(bool);
226         
227     ///
228     void setTab(int, int);
229     /// Merge the array at current position
230     void Merge(LyxArrayBase *);
231     /// Delete every object from current position to pos2
232     void Clean(int pos2);
233     ///
234     MathedRowSt * adjustVerticalSt();
235     
236 private:
237     /// This function is not recursive, as MathPar::Metrics is
238     void IMetrics(int, int &, int &, int &);
239     /// Font size (display, text, script, script2) 
240     int size;
241     /// current position
242     mutable int x;
243     ///
244     int y;
245     ///
246     MathParInset * p;
247     
248     // Limits auxiliary variables
249     /// Position and max width of a script
250     int sx, sw;
251     /// true= center, false= left align (default)
252     bool limits;
253     /// Type of previous script
254     short s_type;  
255     ///
256     void ipush();
257     ///
258     void ipop();
259
260 protected:
261     /// 
262     MathedRowSt * crow;
263     
264 private:
265     ///
266     friend class MathedCursor;
267 };
268
269
270 //--------------------   Inline functions   --------------------------//
271
272
273 inline
274 bool MathedIter::IsInset() const
275 {
276     return MathIsInset((*array)[pos]);
277 }
278  
279 inline
280 bool MathedIter::IsActive() const
281 {
282     return MathIsActive((*array)[pos]);
283 }
284
285 inline
286 bool MathedIter::IsFont() const
287 {
288     return MathIsFont((*array)[pos]);
289 }
290
291
292 inline
293 bool MathedIter::IsScript() const
294 {
295     return MathIsScript((*array)[pos]);
296 }   
297
298 inline
299 bool MathedIter::IsTab() const
300 {
301     return ((*array)[pos] == LM_TC_TAB);
302 }  
303
304
305 inline
306 bool MathedIter::IsCR() const
307 {
308     return ((*array)[pos] == LM_TC_CR);
309 }  
310
311
312 inline
313 MathedIter::MathedIter(LyxArrayBase * d)
314         : array(d)
315 {
316     pos = 0;
317     row = col = 0;
318     fcode = (array && IsFont()) ? (*array)[0]: 0;
319 }
320      
321
322 inline
323 void MathedIter::ipush()
324
325     stck.fcode = fcode;
326     stck.pos = pos;
327     stck.row = row;
328     stck.col = col; 
329 }
330
331
332 inline
333 void MathedIter::ipop()
334
335     fcode = stck.fcode;
336     pos = stck.pos;
337     row = stck.row;
338     col = stck.col;  
339 }
340
341
342 inline
343 void MathedXIter::GetPos(int & xx, int & yy) const
344
345     if (p) 
346       p->GetXY(xx, yy);
347     else
348       { xx = 0;  yy = 0; }        
349     xx += x;  yy += y;
350 }
351
352 inline 
353 int MathedXIter::GetX() const
354
355     int xx, yy;
356     GetPos(xx, yy);
357     return xx; 
358 }
359
360 inline 
361 int MathedXIter::GetY() const
362
363     int xx, yy;
364     GetPos(xx, yy);
365     return yy; 
366 }
367
368
369 inline
370 void MathedXIter::GetIncPos(int & xx, int & yy) const
371
372     xx = x;  yy = y; 
373 }
374
375
376 inline
377 void MathedXIter::getAD(int & a, int & d) const
378
379     if (crow) {
380         a = crow->asc;
381         d = crow->desc;
382     } else 
383       if (p) {
384           a = p->Ascent();
385           d = p->Descent();
386       } else {
387           a = d = 0;
388       }
389 }
390
391
392 #endif
393