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