]> git.lyx.org Git - features.git/blob - src/mathed/array.C
new hierarchy for frac-like things
[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 int MathArray::last() const
50 {
51         return size() - 1;
52 }
53
54
55 void MathArray::substitute(MathMacro const & m)
56 {
57         MathArray tmp;
58         for (int pos = 0; pos < size(); ++pos) 
59                 bf_[pos]->substitute(tmp, m);
60         swap(tmp);
61 }
62
63
64 MathArray & MathArray::operator=(MathArray const & array)
65 {
66         MathArray tmp(array);
67         swap(tmp);
68         return *this;
69 }
70
71
72 MathInset * MathArray::nextInset(int pos)
73 {
74         return (pos == size()) ? 0 : bf_[pos];
75 }
76
77
78 MathInset const * MathArray::nextInset(int pos) const
79 {
80         return (pos == size()) ? 0 : bf_[pos];
81 }
82
83
84 unsigned char MathArray::getChar(int pos) const
85 {
86         return (pos == size()) ? 0 : (bf_[pos]->getChar());
87 }
88
89
90 MathTextCodes MathArray::getCode(int pos) const
91 {
92         return pos < size() ? (bf_[pos]->code()) : LM_TC_MIN;
93 }
94
95
96 void MathArray::setCode(int pos, MathTextCodes t)
97 {
98         bf_[pos]->code(t);
99 }
100
101
102 void MathArray::insert(int pos, MathInset * p)
103 {
104         bf_.insert(bf_.begin() + pos, p);
105 }
106
107
108 void MathArray::insert(int pos, unsigned char b, MathTextCodes t)
109 {
110         bf_.insert(bf_.begin() + pos, new MathCharInset(b, t));
111 }
112
113
114 void MathArray::insert(int pos, MathArray const & array)
115 {
116         bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
117         deep_copy(pos, pos + array.size());
118 }
119
120
121 void MathArray::push_back(MathInset * p)
122 {       
123         insert(size(), p);
124 }
125
126
127 void MathArray::push_back(unsigned char b, MathTextCodes c)
128 {
129         insert(size(), b, c);
130 }
131
132
133 void MathArray::push_back(MathArray const & array)
134 {
135         insert(size(), array);
136 }
137
138
139 void MathArray::clear()
140 {
141         erase();
142 }
143
144
145 void MathArray::swap(MathArray & array)
146 {
147         if (this != &array) 
148                 bf_.swap(array.bf_);
149 }
150
151
152 bool MathArray::empty() const
153 {
154         return bf_.empty();
155 }
156    
157
158 int MathArray::size() const
159 {
160         return bf_.size();
161 }
162
163
164 void MathArray::erase()
165 {
166         erase(0, size());
167 }
168
169
170 void MathArray::erase(int pos)
171 {
172         if (pos < size())
173                 erase(pos, pos + 1);
174 }
175
176
177 void MathArray::erase(int pos1, int pos2)
178 {
179         for (int pos = pos1; pos < pos2; ++pos)
180                 delete bf_[pos];
181         bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
182 }
183
184
185 MathInset * MathArray::back() const
186 {
187         return size() ? bf_.back() : 0;
188 }
189
190
191 void MathArray::dump2(ostream & os) const
192 {
193         for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
194                 os << *it << ' ';
195 }
196
197
198 void MathArray::dump(ostream & os) const
199 {
200         for (int pos = 0; pos < size(); ++pos)
201                 os << "<" << nextInset(pos) << ">";
202 }
203
204
205 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
206 {
207         ar.dump2(os);
208         return os;
209 }
210
211
212 void MathArray::write(ostream & os, bool fragile) const
213 {
214         for (int pos = 0; pos < size(); ++pos)
215                 nextInset(pos)->write(os, fragile);
216 }
217
218
219 void MathArray::writeNormal(ostream & os) const
220 {
221         if (empty()) {
222                 os << "[par] ";
223                 return;
224         }
225
226         write(os, true);
227 }
228
229
230 void MathArray::validate(LaTeXFeatures & features) const
231 {
232         for (int pos = 0; pos < size(); ++pos)
233                 nextInset(pos)->validate(features);
234 }
235
236
237 void MathArray::pop_back()
238 {       
239         if (!size()) {
240                 lyxerr << "pop_back from empty array!\n";
241                 return;
242         }
243         delete back();
244         bf_.pop_back();
245 }
246