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