]> git.lyx.org Git - lyx.git/blob - src/mathed/math_iterator.C
fix Herbert's up/down problem
[lyx.git] / src / mathed / math_iterator.C
1
2 #include <config.h>
3
4 #include "math_iterator.h"
5 #include "debug.h"
6 #include "support/LAssert.h"
7
8
9 //MathIterator::MathIterator()
10 //{}
11
12
13 MathIterator::MathIterator(MathInset * p)
14 {
15         push(p);
16 }
17
18
19 //MathIterator::MathIterator(MathCursor::cursor_type const & c)
20 //      : cursor_(c)
21 //{}
22
23
24 MathCursor::cursor_type const & MathIterator::cursor() const
25 {
26         return cursor_;
27 }
28
29
30 MathInset const * MathIterator::par() const
31 {
32         return position().par_;
33 }
34
35
36 MathInset * MathIterator::par()
37 {
38         return position().par_;
39 }
40
41
42 MathArray const & MathIterator::cell() const
43 {
44         MathCursorPos const & top = position();
45         return top.par_->cell(top.idx_);
46 }
47
48
49
50 void MathIterator::push(MathInset * p)
51 {
52         //lyxerr << "push: " << p << endl;
53         cursor_.push_back(MathCursorPos(p));
54 }
55
56
57 void MathIterator::pop()
58 {
59         //lyxerr << "pop: " << endl;
60         lyx::Assert(cursor_.size());
61         cursor_.pop_back();
62 }
63
64
65 MathCursorPos const & MathIterator::operator*() const
66 {
67         return position();
68 }
69
70
71 MathCursorPos const & MathIterator::operator->() const
72 {
73         return position();
74 }
75
76
77 void MathIterator::goEnd()
78 {
79         MathCursorPos & top = position();
80         top.idx_ = top.par_->nargs() - 1;
81         top.pos_ = cell().size();
82 }
83
84
85 void MathIterator::operator++()
86 {
87         MathCursorPos   & top = position();
88         MathArray const & ar  = top.par_->cell(top.idx_);
89
90         // move into the current inset if possible
91         // it is impossible for pos() == size()!
92         MathInset * n = 0;
93         if (top.pos_ != ar.size())
94                 n = (ar.begin() + top.pos_)->nucleus();
95         if (n && n->isActive()) {
96                 push(n);
97                 return;
98         }
99
100         // otherwise move on one cell position if possible
101         if (top.pos_ < ar.size()) {
102                 // pos() == size() is valid!
103                 ++top.pos_;
104                 return;
105         }
106
107         // otherwise try to move on one cell if possible
108         while (top.idx_ + 1 < top.par_->nargs()) {
109                 // idx() == nargs() is _not_ valid!
110                 ++top.idx_;
111                 if (top.par_->validCell(top.idx_)) {
112                         top.pos_ = 0;
113                         return;
114                 }
115         }
116
117         // otherwise leave array, move on one position
118         // this might yield pos() == size(), but that's a ok.
119         pop(); 
120         // it certainly invalidates top
121         ++position().pos_;
122 }
123
124
125 void MathIterator::jump(MathInset::difference_type i)
126 {
127         position().pos_ += i;
128         //lyx::Assert(position().pos_ >= 0);
129         lyx::Assert(position().pos_ <= cell().size());
130 }
131
132
133 bool operator==(MathIterator const & it, MathIterator const & jt)
134 {
135         //lyxerr << "==: " << it.cursor().size() << " " << jt.cursor().size() << endl;
136         if (it.cursor().size() != jt.cursor().size())
137                 return false;
138         return it.cursor() == jt.cursor();      
139 }
140
141
142 bool operator!=(MathIterator const & it, MathIterator const & jt)
143 {
144         //lyxerr << "!=: " << it.cursor().size() << " " << jt.cursor().size() << endl;
145         if (it.cursor().size() != jt.cursor().size())
146                 return true;
147         return it.cursor() != jt.cursor();      
148 }
149
150
151 MathIterator ibegin(MathInset * p)
152 {
153         return MathIterator(p);
154 }
155
156
157 MathIterator iend(MathInset * p)
158 {
159         MathIterator it(p);
160         it.goEnd();
161         return it;
162 }