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