]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.C
36362c28b31bfd33f8d4fec6a494714620f1ff24
[lyx.git] / src / mathed / math_inset.C
1 /*
2  *  File:        math_inset.C
3  *  Purpose:     Implementation of insets for mathed
4  *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
5  *  Created:     January 1996
6  *  Description: 
7  *
8  *  Dependencies: Xlib, XForms
9  *
10  *  Copyright: 1996, 1997 Alejandro Aguilar Sierra
11  *
12  *   Version: 0.8beta.
13  *
14  *   You are free to use and modify this code under the terms of
15  *   the GNU General Public Licence version 2 or later.
16  */
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "math_inset.h"
23 #include "debug.h"
24
25
26 int MathInset::workwidth;
27
28
29 MathInset::MathInset(int nargs, string const & name)
30         : name_(name), width_(0), ascent_(0), descent_(0),
31                 size_(LM_ST_DISPLAY), cells_(nargs), xo_(0), yo_(0)
32 {}
33
34
35 int MathInset::ascent() const
36 {
37         return ascent_;
38 }
39
40
41 int MathInset::descent() const
42 {
43         return descent_;
44 }
45
46
47 int MathInset::width() const
48 {
49         return width_;
50 }
51
52
53 int MathInset::height() const
54 {
55         return ascent_ + descent_;
56 }
57
58
59 int MathInset::limits() const
60 {
61         return false;
62 }
63
64
65 void MathInset::limits(int)
66 {}
67
68
69 string const & MathInset::name() const
70 {
71         return name_;
72 }
73
74
75 void MathInset::setName(string const & n)
76 {
77         name_ = n;
78 }
79
80
81 MathStyles MathInset::size() const
82 {
83         return size_;
84 }
85
86
87 void MathInset::size(MathStyles s)
88 {
89         size_ = s;
90 }
91
92 std::ostream & operator<<(std::ostream & os, MathInset const & inset)
93 {
94         inset.write(os, false);
95         return os;
96 }
97
98
99 int MathInset::xo() const
100 {
101         return xo_;
102 }
103
104
105 int MathInset::yo() const
106 {
107         return yo_;
108 }
109
110
111 void MathInset::xo(int x)
112 {
113         xo_ = x;
114 }
115
116
117 void MathInset::yo(int y)
118 {
119         yo_ = y;
120 }
121
122
123 int MathInset::nargs() const
124 {
125         return cells_.size();
126 }
127
128
129
130 MathXArray & MathInset::xcell(int i)
131 {
132         return cells_[i];
133 }
134
135 MathXArray const & MathInset::xcell(int i) const
136 {
137         return cells_[i];
138 }
139
140
141
142 MathArray & MathInset::cell(int i)
143 {
144         return cells_[i].data_;
145 }
146
147 MathArray const & MathInset::cell(int i) const
148 {
149         return cells_[i].data_;
150 }
151
152
153 void MathInset::substitute(MathArray & array, MathMacro const & m) const
154 {
155         MathInset * p = clone();
156         for (int i = 0; i < nargs(); ++i)
157                 p->cell(i).substitute(m);
158         array.push_back(p);
159 }
160
161 void MathInset::metrics(MathStyles st)
162 {
163         size_ = st;
164         for (int i = 0; i < nargs(); ++i)
165                 xcell(i).metrics(st);
166 }
167
168 void MathInset::draw(Painter & pain, int x, int y)
169 {
170         xo_ = x;
171         yo_ = y;
172         for (int i = 0; i < nargs(); ++i)
173                 xcell(i).draw(pain, x + xcell(i).xo(), y + xcell(i).yo());
174 }
175
176
177 bool MathInset::idxNext(int & idx, int & pos) const
178 {
179         if (idx + 1 >= nargs())
180                 return false;
181         ++idx;
182         pos = 0;
183         return true;
184 }
185
186
187 bool MathInset::idxRight(int & idx, int & pos) const
188 {
189         return idxNext(idx, pos);
190 }
191
192
193 bool MathInset::idxPrev(int & idx, int & pos) const
194 {
195         if (idx == 0)
196                 return false;
197         --idx;
198         pos = cell(idx).size();
199         return true;
200 }
201
202
203 bool MathInset::idxLeft(int & idx, int & pos) const
204 {
205         return idxPrev(idx, pos);
206 }
207
208 bool MathInset::idxUp(int &, int &) const
209 {
210         return false;
211 }
212
213
214 bool MathInset::idxDown(int &, int &) const
215 {
216         return false;
217 }
218
219
220 bool MathInset::idxFirst(int & i, int & pos) const
221 {
222         if (nargs() == 0)
223                 return false;
224         i = 0;
225         pos = 0;
226         return true;
227 }
228
229 bool MathInset::idxLast(int & i, int & pos) const
230 {
231         if (nargs() == 0)
232                 return false;
233         i = nargs() - 1;
234         pos = cell(i).size();
235         return true;
236 }
237
238
239 bool MathInset::idxHome(int & /* idx */, int & pos) const
240 {
241         if (pos == 0)
242                 return false;
243         pos = 0;
244         return true;
245 }
246
247
248 bool MathInset::idxEnd(int & idx, int & pos) const
249 {
250         if (pos == cell(idx).size())
251                 return false;
252
253         pos = cell(idx).size();
254         return true;
255 }
256
257
258 bool MathInset::idxFirstUp(int &, int &) const
259 {
260         return false;
261 }
262
263
264 bool MathInset::idxFirstDown(int &, int &) const
265 {
266         return false;
267 }
268
269 void MathInset::idxDelete(int &, bool & popit, bool & deleteit)
270 {
271         popit    = false;
272         deleteit = false;
273 }
274
275 void MathInset::idxDeleteRange(int, int)
276 {}
277
278
279 bool MathInset::idxLastUp(int &, int &) const
280 {
281         return false;
282 }
283
284
285 bool MathInset::idxLastDown(int &, int &) const
286 {
287         return false;
288 }
289
290
291 void MathInset::getXY(int & x, int & y) const
292 {
293    x = xo();
294    y = yo();
295 }
296
297
298 /*
299 void MathInset::userSetSize(MathStyles sz)
300 {
301         if (sz >= 0) {
302                 size_ = sz;      
303                 flag = flag & ~LMPF_FIXED_SIZE;
304         }
305 }
306 */
307
308 void MathInset::writeNormal(std::ostream & os) const
309 {
310         os << "[" << name_ << "] ";
311 }
312
313
314 void MathInset::dump() const
315 {
316         lyxerr << "---------------------------------------------\n";
317         write(lyxerr, false);
318         lyxerr << "\n";
319         for (int i = 0; i < nargs(); ++i)
320                 lyxerr << cell(i) << "\n";
321         lyxerr << "---------------------------------------------\n";
322 }
323
324
325 void MathInset::push_back(unsigned char ch, MathTextCodes fcode)
326 {
327         if (nargs())
328                 cells_.back().data_.push_back(ch, fcode);
329         else
330                 lyxerr << "can't push without a cell\n";
331 }
332
333
334 void MathInset::push_back(MathInset * p)
335 {
336         if (nargs())
337                 cells_.back().data_.push_back(p);
338         else
339                 lyxerr << "can't push without a cell\n";
340 }
341
342
343 bool MathInset::covers(int x, int y) const
344 {
345         return
346                 x >= xo_ &&
347                 x <= xo_ + width_ &&
348                 y >= yo_ - ascent_ &&
349                 y <= yo_ + descent_;
350 }
351
352 void MathInset::validate(LaTeXFeatures & features) const
353 {
354         for (int i = 0; i < nargs(); ++i)
355                 cell(i).validate(features);
356 }
357
358 std::vector<int> MathInset::idxBetween(int from, int to) const
359 {
360         std::vector<int> res;
361         for (int i = from; i <= to; ++i)
362                 res.push_back(i);
363         return res;
364 }