]> git.lyx.org Git - features.git/blob - src/mathed/array.C
- fix nasty bug due to missing InsetFormula copy c'tor
[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 "debug.h"
8 #include "array.h"
9 #include "math_scriptinset.h"
10 #include "mathed/support.h"
11
12 using std::ostream;
13 using std::endl;
14
15 MathArray::MathArray()
16 {}
17
18
19 MathArray::~MathArray()
20 {
21         erase();
22 }
23
24
25 MathArray::MathArray(MathArray const & array)
26         : bf_(array.bf_)
27 {
28         deep_copy(0, size());
29 }
30
31
32 MathArray::MathArray(MathArray const & array, int from, int to)
33         : bf_(array.bf_.begin() + from, array.bf_.begin() + to)
34 {
35         deep_copy(0, size());
36 }
37
38
39 void MathArray::deep_copy(int pos1, int pos2)
40 {
41         for (int pos = pos1; pos < pos2; ++pos) {
42                 MathInset * p = bf_[pos]->clone();
43                 //lyxerr << "cloning: '" <<  bf_[pos] << " to " << p << "'\n";
44                 bf_[pos] = p;
45         }
46 }
47
48
49 bool MathArray::next(int & pos) const
50 {
51         if (pos >= size() - 1)
52                 return false;
53
54         ++pos;
55         return true;
56 }
57
58
59 int MathArray::last() const
60 {
61         return size() - 1;
62 }
63
64
65 void MathArray::substitute(MathMacro const & m)
66 {
67         MathArray tmp;
68         for (int pos = 0; pos < size(); ++pos) 
69                 bf_[pos]->substitute(tmp, m);
70         swap(tmp);
71 }
72
73
74 MathArray & MathArray::operator=(MathArray const & array)
75 {
76         MathArray tmp(array);
77         swap(tmp);
78         return *this;
79 }
80
81
82 MathInset * MathArray::nextInset(int pos)
83 {
84         return (pos == size()) ? 0 : bf_[pos];
85 }
86
87
88 MathInset const * MathArray::nextInset(int pos) const
89 {
90         return (pos == size()) ? 0 : bf_[pos];
91 }
92
93
94 unsigned char MathArray::getChar(int pos) const
95 {
96         return (pos == size()) ? 0 : (bf_[pos]->getChar());
97 }
98
99
100 /*
101 string MathArray::getString(int & pos) const
102 {
103         string s;
104         if (isInset(pos))
105                 return s;
106
107         MathTextCodes const fcode = getCode(pos);
108         do {
109                 s += getChar(pos);
110                 next(pos);
111         } while (pos < size() && !isInset(pos) && getCode(pos) == fcode);
112
113         return s;
114 }
115 */
116
117
118 MathTextCodes MathArray::getCode(int pos) const
119 {
120         return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN;
121 }
122
123
124 void MathArray::setCode(int pos, MathTextCodes t)
125 {
126         bf_[pos]->code(t);
127 }
128
129
130 void MathArray::insert(int pos, MathInset * p)
131 {
132         bf_.insert(bf_.begin() + pos, p);
133 }
134
135
136 void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
137 {
138         bf_.insert(bf_.begin() + pos, new MathCharInset(b, t));
139 }
140
141
142 void MathArray::insert(int pos, MathArray const & array)
143 {
144         bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
145         deep_copy(pos, pos + array.size());
146 }
147
148
149 void MathArray::push_back(MathInset * p)
150 {       
151         insert(size(), p);
152 }
153
154
155 void MathArray::push_back(unsigned char b, MathTextCodes c)
156 {
157         insert(size(), b, c);
158 }
159
160
161 void MathArray::push_back(MathArray const & array)
162 {
163         insert(size(), array);
164 }
165
166
167 void MathArray::clear()
168 {
169         erase();
170 }
171
172
173 void MathArray::swap(MathArray & array)
174 {
175         if (this != &array) 
176                 bf_.swap(array.bf_);
177 }
178
179
180 bool MathArray::empty() const
181 {
182         return bf_.empty();
183 }
184    
185
186 int MathArray::size() const
187 {
188         return bf_.size();
189 }
190
191
192 void MathArray::erase()
193 {
194         erase(0, size());
195 }
196
197
198 void MathArray::erase(int pos)
199 {
200         if (pos < size())
201                 bf_.erase(bf_.begin() + pos);
202 }
203
204
205 void MathArray::erase(int pos1, int pos2)
206 {
207         for (int pos = pos1; pos < pos2; ++pos)
208                 delete nextInset(pos);
209         bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
210 }
211
212
213 MathInset * MathArray::back() const
214 {
215         return size() ? bf_.back() : 0;
216 }
217
218
219 void MathArray::dump2(ostream & os) const
220 {
221         for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
222                 os << *it << ' ';
223 }
224
225
226 void MathArray::dump(ostream & os) const
227 {
228         for (int pos = 0; pos < size(); ++pos)
229                 os << "<" << nextInset(pos) << ">";
230 }
231
232
233 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
234 {
235         ar.dump2(os);
236         return os;
237 }
238
239
240 void MathArray::write(ostream & os, bool fragile) const
241 {
242         for (int pos = 0; pos < size(); ++pos)
243                 nextInset(pos)->write(os, fragile);
244 }
245
246
247 void MathArray::writeNormal(ostream & os) const
248 {
249         if (empty()) {
250                 os << "[par] ";
251                 return;
252         }
253
254         write(os, true);
255 }
256
257
258 void MathArray::validate(LaTeXFeatures & features) const
259 {
260         for (int pos = 0; pos < size(); ++pos)
261                 nextInset(pos)->validate(features);
262 }
263
264
265 void MathArray::pop_back()
266 {       
267         if (!size()) {
268                 lyxerr << "pop_back from empty array!\n";
269                 return;
270         }
271         delete back();
272         bf_.pop_back();
273 }
274