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