]> git.lyx.org Git - features.git/blob - src/mathed/array.C
fix cursor positioning using the mouse
[features.git] / src / mathed / array.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_inset.h"
6 #include "math_charinset.h"
7 #include "math_scriptinset.h"
8 #include "debug.h"
9 #include "array.h"
10 #include "mathed/support.h"
11
12 using std::ostream;
13 using std::endl;
14
15
16 MathArray::MathArray()
17 {}
18
19
20 MathArray::MathArray(MathArray const & array, int from, int to)
21         : bf_(array.begin() + from, array.begin() + to)
22 {}
23
24
25 int MathArray::last() const
26 {
27         return size() - 1;
28 }
29
30
31 void MathArray::substitute(MathMacro const & m)
32 {
33         MathArray tmp;
34         for (iterator it = begin(); it != end(); ++it)
35                 it->substitute(tmp, m);
36         swap(tmp);
37 }
38
39
40 MathAtom * MathArray::at(int pos)
41 {
42         return (0 <= pos && pos < size()) ? &bf_[pos] : 0;
43 }
44
45
46 MathAtom const * MathArray::at(int pos) const
47 {
48         return (0 <= pos && pos < size()) ? &bf_[pos] : 0;
49 }
50
51
52 void MathArray::insert(int pos, MathInset * p)
53 {
54         //cerr << "\n  1: "; p->write(cerr, true); cerr << p << "\n";
55         // inserting here invalidates the pointer!
56         bf_.insert(begin() + pos, MathAtom(p));
57         //cerr << "\n  2: "; p->write(cerr, true); cerr << p << "\n";
58 }
59
60
61 void MathArray::insert(int pos, MathArray const & array)
62 {
63         bf_.insert(begin() + pos, array.begin(), array.end());
64 }
65
66
67 void MathArray::push_back(MathInset * p)
68 {       
69         insert(size(), p);
70 }
71
72
73 void MathArray::push_back(MathArray const & array)
74 {
75         insert(size(), array);
76 }
77
78
79 void MathArray::clear()
80 {
81         erase();
82 }
83
84
85 void MathArray::swap(MathArray & array)
86 {
87         if (this != &array) 
88                 bf_.swap(array.bf_);
89 }
90
91
92 bool MathArray::empty() const
93 {
94         return bf_.empty();
95 }
96    
97
98 int MathArray::size() const
99 {
100         return bf_.size();
101 }
102
103
104 void MathArray::erase()
105 {
106         erase(0, size());
107 }
108
109
110 void MathArray::erase(int pos)
111 {
112         if (pos < size())
113                 erase(pos, pos + 1);
114 }
115
116
117 void MathArray::erase(int pos1, int pos2)
118 {
119         bf_.erase(begin() + pos1, begin() + pos2);
120 }
121
122
123 MathAtom & MathArray::back()
124 {
125         return bf_.back();
126 }
127
128
129 void MathArray::dump2(ostream & os) const
130 {
131         for (const_iterator it = begin(); it != end(); ++it)
132                 os << *it << ' ';
133 }
134
135
136 void MathArray::dump(ostream & os) const
137 {
138         for (const_iterator it = begin(); it != end(); ++it)
139                 os << "<" << *it << ">";
140 }
141
142
143 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
144 {
145         ar.dump2(os);
146         return os;
147 }
148
149
150 // returns sequence of char with same code starting at it up to end
151 // it might be less, though...
152 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
153 {
154         string s;
155         MathCharInset const * p = it->nucleus()->asCharInset();
156         if (!p)
157                 return s;
158
159         for (MathTextCodes c = p->code(); it != end; ++it) {
160                 p = it->nucleus()->asCharInset();
161                 if (!p || it->up() || it->down() || p->code() != c)
162                         break;
163                 s += p->getChar();
164         }
165         return s;
166 }
167
168
169 void MathArray::write(ostream & os, bool fragile) const
170 {
171         for (const_iterator it = begin(); it != end(); ++it) {
172                 MathCharInset const * p = it->nucleus()->asCharInset();
173                 if (p && !it->up() && !it->down()) {
174                         // special handling for character sequences with the same code
175                         string s = charSequence(it, end());
176                         p->writeHeader(os);
177                         os << s;
178                         p->writeTrailer(os);
179                         it += s.size() - 1;
180                 } else {
181                         it->write(os, fragile);
182                 }
183         }
184 }
185
186
187 void MathArray::writeNormal(ostream & os) const
188 {
189         if (empty()) {
190                 os << "[par] ";
191                 return;
192         }
193
194         write(os, true);
195 }
196
197
198 void MathArray::validate(LaTeXFeatures & features) const
199 {
200         for (const_iterator it = begin(); it != end(); ++it)
201                 it->validate(features);
202 }
203
204
205 void MathArray::pop_back()
206 {       
207         if (!size()) {
208                 lyxerr << "pop_back from empty array!\n";
209                 return;
210         }
211         bf_.pop_back();
212 }
213
214
215 MathArray::const_iterator MathArray::begin() const
216 {
217         return bf_.begin();
218 }
219
220
221 MathArray::const_iterator MathArray::end() const
222 {
223         return bf_.end();
224 }
225
226
227 MathArray::iterator MathArray::begin()
228 {
229         return bf_.begin();
230 }
231
232
233 MathArray::iterator MathArray::end()
234 {
235         return bf_.end();
236 }