]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.C
aee6d232c9afb2a7539d99b2f7ad38ca71f4b0ac
[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), code_(LM_TC_MIN), 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 string const & MathInset::name() const
60 {
61         return name_;
62 }
63
64
65 void MathInset::setName(string const & n)
66 {
67         name_ = n;
68 }
69
70
71 MathStyles MathInset::size() const
72 {
73         return size_;
74 }
75
76
77 void MathInset::size(MathStyles s)
78 {
79         size_ = s;
80 }
81
82
83 std::ostream & operator<<(std::ostream & os, MathInset const & inset)
84 {
85         inset.write(os, false);
86         return os;
87 }
88
89
90 int MathInset::xo() const
91 {
92         return xo_;
93 }
94
95
96 int MathInset::yo() const
97 {
98         return yo_;
99 }
100
101
102 void MathInset::xo(int x)
103 {
104         xo_ = x;
105 }
106
107
108 void MathInset::yo(int y)
109 {
110         yo_ = y;
111 }
112
113
114 int MathInset::nargs() const
115 {
116         return cells_.size();
117 }
118
119
120 MathXArray & MathInset::xcell(int i)
121 {
122         return cells_[i];
123 }
124
125
126 MathXArray const & MathInset::xcell(int i) const
127 {
128         return cells_[i];
129 }
130
131
132 MathArray & MathInset::cell(int i)
133 {
134         return cells_[i].data_;
135 }
136
137
138 MathArray const & MathInset::cell(int i) const
139 {
140         return cells_[i].data_;
141 }
142
143
144 void MathInset::substitute(MathArray & array, MathMacro const & m) const
145 {
146         MathInset * p = clone();
147         for (int i = 0; i < nargs(); ++i)
148                 p->cell(i).substitute(m);
149         array.push_back(p);
150 }
151
152
153 void MathInset::metrics(MathStyles st)
154 {
155         size_ = st;
156         for (int i = 0; i < nargs(); ++i)
157                 xcell(i).metrics(st);
158 }
159
160
161 void MathInset::draw(Painter & pain, int x, int y)
162 {
163         xo_ = x;
164         yo_ = y;
165         for (int i = 0; i < nargs(); ++i)
166                 xcell(i).draw(pain, x + xcell(i).xo(), y + xcell(i).yo());
167 }
168
169
170 bool MathInset::idxNext(int & idx, int & pos) const
171 {
172         if (idx + 1 >= nargs())
173                 return false;
174         ++idx;
175         pos = 0;
176         return true;
177 }
178
179
180 bool MathInset::idxRight(int & idx, int & pos) const
181 {
182         return idxNext(idx, pos);
183 }
184
185
186 bool MathInset::idxPrev(int & idx, int & pos) const
187 {
188         if (idx == 0)
189                 return false;
190         --idx;
191         pos = cell(idx).size();
192         return true;
193 }
194
195
196 bool MathInset::idxLeft(int & idx, int & pos) const
197 {
198         return idxPrev(idx, pos);
199 }
200
201
202 bool MathInset::idxUp(int &, int &) const
203 {
204         return false;
205 }
206
207
208 bool MathInset::idxDown(int &, int &) const
209 {
210         return false;
211 }
212
213
214 bool MathInset::idxFirst(int & i, int & pos) const
215 {
216         if (nargs() == 0)
217                 return false;
218         i = 0;
219         pos = 0;
220         return true;
221 }
222
223
224 bool MathInset::idxLast(int & i, int & pos) const
225 {
226         if (nargs() == 0)
227                 return false;
228         i = nargs() - 1;
229         pos = cell(i).size();
230         return true;
231 }
232
233
234 bool MathInset::idxHome(int & /* idx */, int & pos) const
235 {
236         if (pos == 0)
237                 return false;
238         pos = 0;
239         return true;
240 }
241
242
243 bool MathInset::idxEnd(int & idx, int & pos) const
244 {
245         if (pos == cell(idx).size())
246                 return false;
247
248         pos = cell(idx).size();
249         return true;
250 }
251
252
253 bool MathInset::idxFirstUp(int &, int &) const
254 {
255         return false;
256 }
257
258
259 bool MathInset::idxFirstDown(int &, int &) const
260 {
261         return false;
262 }
263
264
265 void MathInset::idxDelete(int &, bool & popit, bool & deleteit)
266 {
267         popit    = false;
268         deleteit = false;
269 }
270
271
272 void MathInset::idxDeleteRange(int, int)
273 {}
274
275
276 bool MathInset::idxLastUp(int &, int &) const
277 {
278         return false;
279 }
280
281
282 bool MathInset::idxLastDown(int &, int &) const
283 {
284         return false;
285 }
286
287
288 void MathInset::getXY(int & x, int & y) const
289 {
290    x = xo();
291    y = yo();
292 }
293
294
295 /*
296 void MathInset::userSetSize(MathStyles sz)
297 {
298         if (sz >= 0) {
299                 size_ = sz;      
300                 flag = flag & ~LMPF_FIXED_SIZE;
301         }
302 }
303 */
304
305 void MathInset::writeNormal(std::ostream & os) const
306 {
307         os << "[" << name_ << "] ";
308 }
309
310
311 void MathInset::dump() const
312 {
313         lyxerr << "---------------------------------------------\n";
314         write(lyxerr, false);
315         lyxerr << "\n";
316         for (int i = 0; i < nargs(); ++i)
317                 lyxerr << cell(i) << "\n";
318         lyxerr << "---------------------------------------------\n";
319 }
320
321
322 void MathInset::push_back(unsigned char ch, MathTextCodes fcode)
323 {
324         if (nargs())
325                 cells_.back().data_.push_back(ch, fcode);
326         else
327                 lyxerr << "can't push without a cell\n";
328 }
329
330
331 void MathInset::push_back(MathInset * p)
332 {
333         if (nargs())
334                 cells_.back().data_.push_back(p);
335         else
336                 lyxerr << "can't push without a cell\n";
337 }
338
339
340 bool MathInset::covers(int x, int y) const
341 {
342         return
343                 x >= xo_ &&
344                 x <= xo_ + width_ &&
345                 y >= yo_ - ascent_ &&
346                 y <= yo_ + descent_;
347 }
348
349
350 void MathInset::validate(LaTeXFeatures & features) const
351 {
352         for (int i = 0; i < nargs(); ++i)
353                 cell(i).validate(features);
354 }
355
356
357 std::vector<int> MathInset::idxBetween(int from, int to) const
358 {
359         std::vector<int> res;
360         for (int i = from; i <= to; ++i)
361                 res.push_back(i);
362         return res;
363 }
364
365
366 MathTextCodes MathInset::code() const
367 {
368         return code_;
369 }
370
371
372 void MathInset::code(MathTextCodes t)
373 {
374         code_ = t;
375 }