]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.C
LyX Drinkers Union: patch 1
[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, short ot, int nargs)
34         : name_(name), objtype(ot), width_(0), ascent_(0), descent_(0),
35                 size_(LM_ST_TEXT), 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 bool MathInset::GetLimits() const
64 {
65         return false;
66 }
67
68
69 void MathInset::SetLimits(bool) {}   
70
71
72 string const & MathInset::name() const
73 {
74         return name_;
75 }
76
77
78 short MathInset::GetType() const
79 {
80         return objtype;
81 }
82
83
84 void  MathInset::SetType(short t)
85 {
86         objtype = t;
87 }
88
89
90 void MathInset::SetName(string const & n)
91 {
92         name_ = n;
93 }
94
95
96 MathStyles MathInset::size() const
97 {
98         return size_;
99 }
100
101
102 void MathInset::size(MathStyles s)
103 {
104         size_ = s;
105 }
106
107 std::ostream & operator<<(std::ostream & os, MathInset const & inset)
108 {
109         inset.Write(os, false);
110         return os;
111 }
112
113
114 int MathInset::xo() const
115 {
116         return xo_;
117 }
118
119
120 int MathInset::yo() const
121 {
122         return yo_;
123 }
124
125
126 void MathInset::xo(int x)
127 {
128         xo_ = x;
129 }
130
131
132 void MathInset::yo(int y)
133 {
134         yo_ = y;
135 }
136
137
138 int MathInset::nargs() const
139 {
140         return cells_.size();
141 }
142
143
144
145 MathXArray & MathInset::xcell(int i)
146 {
147         return cells_[i];
148 }
149
150 MathXArray const & MathInset::xcell(int i) const
151 {
152         return cells_[i];
153 }
154
155
156
157 MathArray & MathInset::cell(int i)
158 {
159         return cells_[i].data_;
160 }
161
162 MathArray const & MathInset::cell(int i) const
163 {
164         return cells_[i].data_;
165 }
166
167
168 void MathInset::setData(MathArray const & a, int idx)
169 {
170         cells_[idx].data_ = a;
171 }
172
173
174 void MathInset::substitute(MathArray & array, MathMacro const & m) const
175 {
176         MathInset * p = Clone();
177         for (int i = 0; i < nargs(); ++i)
178                 p->cell(i).substitute(m);
179         array.push_back(p);
180 }
181
182 void MathInset::Metrics(MathStyles st)
183 {
184         size_ = st;
185         for (int i = 0; i < nargs(); ++i)
186                 xcell(i).Metrics(st);
187 }
188
189 void MathInset::draw(Painter & pain, int x, int y)
190 {
191         xo_ = x;
192         yo_ = y;
193         for (int i = 0; i < nargs(); ++i)
194                 xcell(i).draw(pain, x + xcell(i).xo(), y + xcell(i).yo());
195 }
196
197
198 bool MathInset::idxRight(int & idx, int & pos) const
199 {
200         if (idx + 1 >= nargs())
201                 return false;
202         ++idx;
203         pos = 0;
204         return true;
205 }
206
207
208 bool MathInset::idxLeft(int & idx, int & pos) const
209 {
210         if (idx == 0)
211                 return false;
212         --idx;
213         pos = cell(idx).size();
214         return true;
215 }
216
217
218 bool MathInset::idxUp(int &, int &) const
219 {
220         return false;
221 }
222
223
224 bool MathInset::idxDown(int &, int &) const
225 {
226         return false;
227 }
228
229
230 bool MathInset::idxFirst(int & i, int & pos) const
231 {
232         if (nargs() == 0)
233                 return false;
234         i = 0;
235         pos = 0;
236         return true;
237 }
238
239 bool MathInset::idxLast(int & i, int & pos) const
240 {
241         if (nargs() == 0)
242                 return false;
243         i = nargs() - 1;
244         pos = cell(i).size();
245         return true;
246 }
247
248
249 bool MathInset::idxHome(int & i, int & pos) const
250 {
251         if (pos == 0)
252                 return false;
253         pos = 0;
254         return true;
255 }
256
257
258 bool MathInset::idxEnd(int & idx, int & pos) const
259 {
260         if (pos == cell(idx).size())
261                 return false;
262
263         pos = cell(idx).size();
264         return true;
265 }
266
267
268 void MathInset::GetXY(int & x, int & y) const
269 {
270    x = xo();
271    y = yo();
272 }
273
274
275 /*
276 void MathInset::UserSetSize(MathStyles sz)
277 {
278         if (sz >= 0) {
279                 size_ = sz;      
280                 flag = flag & ~LMPF_FIXED_SIZE;
281         }
282 }
283 */
284
285 void MathInset::WriteNormal(std::ostream & os) const
286 {
287         os << "[" << name_ << "] ";
288 }
289
290
291 void MathInset::dump() const
292 {
293         lyxerr << "---------------------------------------------\n";
294         Write(lyxerr, false);
295         lyxerr << "\n";
296         for (int i = 0; i < nargs(); ++i)
297                 lyxerr << cell(i) << "\n";
298         lyxerr << "---------------------------------------------\n";
299 }
300
301
302 void MathInset::push_back(byte ch, MathTextCodes fcode)
303 {
304         if (nargs())
305                 cells_.back().data_.push_back(ch, fcode);
306         else
307                 lyxerr << "can't push without a cell\n";
308 }
309
310
311 void MathInset::push_back(MathInset * p)
312 {
313         if (nargs())
314                 cells_.back().data_.push_back(p);
315         else
316                 lyxerr << "can't push without a cell\n";
317 }
318
319
320 bool MathInset::covers(int x, int y) const
321 {
322         return
323                 x >= xo_ &&
324                 x <= xo_ + width_ &&
325                 y >= yo_ - ascent_ &&
326                 y <= yo_ + descent_;
327 }
328