]> git.lyx.org Git - lyx.git/blob - src/mathed/math_inset.C
mathed108.diff
[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)
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 void MathInset::idxDeleteRange(int, int)
288 {}
289
290
291 bool MathInset::idxLastUp(int &, int &) const
292 {
293         return false;
294 }
295
296
297 bool MathInset::idxLastDown(int &, int &) const
298 {
299         return false;
300 }
301
302
303 void MathInset::GetXY(int & x, int & y) const
304 {
305    x = xo();
306    y = yo();
307 }
308
309
310 /*
311 void MathInset::UserSetSize(MathStyles sz)
312 {
313         if (sz >= 0) {
314                 size_ = sz;      
315                 flag = flag & ~LMPF_FIXED_SIZE;
316         }
317 }
318 */
319
320 void MathInset::WriteNormal(std::ostream & os) const
321 {
322         os << "[" << name_ << "] ";
323 }
324
325
326 void MathInset::dump() const
327 {
328         lyxerr << "---------------------------------------------\n";
329         Write(lyxerr, false);
330         lyxerr << "\n";
331         for (int i = 0; i < nargs(); ++i)
332                 lyxerr << cell(i) << "\n";
333         lyxerr << "---------------------------------------------\n";
334 }
335
336
337 void MathInset::push_back(unsigned char ch, MathTextCodes fcode)
338 {
339         if (nargs())
340                 cells_.back().data_.push_back(ch, fcode);
341         else
342                 lyxerr << "can't push without a cell\n";
343 }
344
345
346 void MathInset::push_back(MathInset * p)
347 {
348         if (nargs())
349                 cells_.back().data_.push_back(p);
350         else
351                 lyxerr << "can't push without a cell\n";
352 }
353
354
355 bool MathInset::covers(int x, int y) const
356 {
357         return
358                 x >= xo_ &&
359                 x <= xo_ + width_ &&
360                 y >= yo_ - ascent_ &&
361                 y <= yo_ + descent_;
362 }
363
364 void MathInset::Validate(LaTeXFeatures & features) const
365 {
366         for (int i = 0; i < nargs(); ++i)
367                 cell(i).Validate(features);
368 }
369
370 std::vector<int> MathInset::idxBetween(int from, int to) const
371 {
372         std::vector<int> res;
373         for (int i = from; i <= to; ++i)
374                 res.push_back(i);
375         return res;
376 }