]> git.lyx.org Git - lyx.git/blob - src/mathed/math_atom.C
fix 'purple box has wrong size in x_{}'
[lyx.git] / src / mathed / math_atom.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_atom.h"
23 #include "math_scriptinset.h"
24 #include "debug.h"
25 #include "support.h"
26 #include "support/LAssert.h"
27
28
29 MathAtom::MathAtom()
30         : nucleus_(0), limits_(0), xo_(0), yo_(0)
31 {
32         script_[0] = 0;
33         script_[1] = 0;
34 }
35
36
37 MathAtom::MathAtom(MathInset * p)
38         : nucleus_(p), limits_(0), xo_(0), yo_(0)
39 {
40         script_[0] = 0;
41         script_[1] = 0;
42 }
43
44
45
46 MathAtom::MathAtom(MathAtom const & p)
47 {
48         copy(p);
49 }
50
51
52 void MathAtom::operator=(MathAtom const & p)
53 {
54         if (this != &p) {
55                 done();
56                 copy(p);
57         }
58 }
59
60
61 MathAtom::~MathAtom()
62 {
63         done();
64 }
65
66
67 void MathAtom::done()
68 {
69         delete nucleus_;
70         delete script_[0];
71         delete script_[1];
72 }
73
74
75 void MathAtom::copy(MathAtom const & p)
76 {
77         //cerr << "calling MathAtom::copy\n";
78         xo_        = p.xo_;
79         yo_        = p.yo_;
80         limits_    = p.limits_;
81         nucleus_   = p.nucleus_;
82         script_[0] = p.script_[0];
83         script_[1] = p.script_[1];
84         if (nucleus_)
85                 nucleus_ = nucleus_->clone();
86         if (script_[0])
87                 script_[0]  = new MathScriptInset(*script_[0]);
88         if (script_[1])
89                 script_[1]  = new MathScriptInset(*script_[1]);
90 }
91
92
93 int MathAtom::height() const
94 {
95         return ascent() + descent();
96 }
97
98
99 std::ostream & operator<<(std::ostream & os, MathAtom const & atom)
100 {
101         atom.write(os, false);
102         return os;
103 }
104
105
106 int MathAtom::xo() const
107 {
108         return xo_;
109 }
110
111
112 int MathAtom::yo() const
113 {
114         return yo_;
115 }
116
117
118 void MathAtom::xo(int x) const
119 {
120         xo_ = x;
121 }
122
123
124 void MathAtom::yo(int y) const
125 {
126         yo_ = y;
127 }
128
129
130 void MathAtom::getXY(int & x, int & y) const
131 {
132    x = xo();
133    y = yo();
134 }
135
136
137 /*
138 void MathAtom::userSetSize(MathStyles sz)
139 {
140         if (sz >= 0) {
141                 size_ = sz;      
142                 flag = flag & ~LMPF_FIXED_SIZE;
143         }
144 }
145 */
146
147 void MathAtom::writeNormal(std::ostream & os) const
148 {
149         os << "[unknown] ";
150 }
151
152
153 void MathAtom::dump() const
154 {
155         lyxerr << "---------------------------------------------\n";
156         write(lyxerr, false);
157         lyxerr << "\n---------------------------------------------\n";
158 }
159
160
161 bool MathAtom::covers(int x, int y) const
162 {
163         return
164                 x >= xo_ &&
165                 x <= xo_ + width() &&
166                 y >= yo_ - ascent() &&
167                 y <= yo_ + descent();
168 }
169
170
171 MathScriptInset * MathAtom::ensure(bool up)
172 {
173         if (!script_[up])
174                 script_[up] = new MathScriptInset(up);
175         return script_[up];
176 }
177
178
179 void MathAtom::validate(LaTeXFeatures &) const
180 {}
181
182
183 void MathAtom::metrics(MathStyles st) const
184 {
185         MathStyles tt = smallerStyleScript(st);
186         if (nucleus())
187                 nucleus()->metrics(st);
188         if (up())
189                 up()->metrics(tt);
190         if (down())
191                 down()->metrics(tt);
192 }
193
194
195 MathScriptInset * MathAtom::up() const
196 {
197         return script_[1];
198 }
199
200
201 MathScriptInset * MathAtom::down() const
202 {
203         return script_[0];
204 }
205
206
207 int MathAtom::dy0() const
208 {
209         if (!down())
210                 return ndes();
211         int des = down()->ascent();
212         if (hasLimits())
213                 des += ndes() + 2;
214         else 
215                 des = std::max(des, ndes());
216         return des;
217 }
218
219
220 int MathAtom::dy1() const
221 {
222         if (!up())
223                 return nasc();
224         int asc = up()->descent();
225         if (hasLimits())
226                 asc += nasc() + 2;
227         else 
228                 asc = std::max(asc, nasc());
229         asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I'));
230         return asc;
231 }
232
233
234 int MathAtom::dx0() const
235 {
236         lyx::Assert(down());
237         return hasLimits() ? (width() - down()->width()) / 2 : nwid();
238 }
239
240
241 int MathAtom::dx1() const
242 {
243         lyx::Assert(up());
244         return hasLimits() ? (width() - up()->width()) / 2 : nwid();
245 }
246
247
248 int MathAtom::dxx() const
249 {
250         lyx::Assert(nucleus());
251         return hasLimits() ? (width() - nwid()) / 2 : 0;
252 }
253
254
255 int MathAtom::ascent() const
256 {
257         return dy1() + (up() ? up()->ascent() : 0);
258 }
259
260
261 int MathAtom::descent() const
262 {
263         return dy0() + (down() ? down()->descent() : 0);
264 }
265
266
267 int MathAtom::width() const
268 {
269         int wid = 0;
270         if (hasLimits()) {
271                 wid = nwid();
272                 if (up())
273                         wid = std::max(wid, up()->width());
274                 if (down())
275                         wid = std::max(wid, down()->width());
276         } else {
277                 if (up())
278                         wid = std::max(wid, up()->width());
279                 if (down())
280                         wid = std::max(wid, down()->width());
281                 wid += nwid();
282         }
283         return wid;
284 }
285
286
287 int MathAtom::nwid() const
288 {
289         return nucleus() ? nucleus()->width() : 0;
290 }
291
292
293 int MathAtom::nasc() const
294 {
295         return nucleus() ? nucleus()->ascent()
296                 : mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I');
297 }
298
299
300 int MathAtom::ndes() const
301 {
302         return nucleus() ? nucleus()->descent()
303                 : mathed_char_descent(LM_TC_VAR, LM_ST_TEXT, 'I');
304 }
305
306
307 void MathAtom::draw(Painter & pain, int x, int y) const
308 {  
309         xo(x);
310         yo(y);
311         if (nucleus())
312                 nucleus()->draw(pain, x + dxx(), y);
313         if (up())
314                 up()->draw(pain, x + dx1(), y - dy1());
315         if (down())
316                 down()->draw(pain, x + dx0(), y + dy0());
317 }
318
319
320 void MathAtom::write(std::ostream & os, bool fragile) const
321 {
322         if (nucleus()) {
323                 nucleus()->write(os, fragile);
324                 if (nucleus()->takesLimits()) {
325                         if (limits_ == -1)
326                                 os << "\\nolimits ";
327                         if (limits_ == 1)
328                                 os << "\\limits ";
329                 }
330         } else
331                 os << "{}";
332
333         if (up()) {
334                 os << "^{";
335                 up()->write(os, fragile);
336                 os << "}";
337         }
338
339         if (down()) {
340                 os << "_{";
341                 down()->write(os, fragile);
342                 os << "}";
343         }
344 }
345
346
347 bool MathAtom::hasLimits() const
348 {
349         return
350                 limits_ == 1 || (limits_ == 0 && nucleus() && nucleus()->isScriptable());
351 }
352
353
354 void MathAtom::substitute(MathArray & array, MathMacro const & m) const
355 {
356         if (nucleus())
357                 nucleus()->substitute(array, m);
358         if (up())
359                 up()->substitute(array, m);
360         if (down())
361                 down()->substitute(array, m);
362 }
363
364
365 void MathAtom::removeEmptyScripts()
366 {
367         for (int i = 0; i <= 1; ++i)
368                 if (script_[i] && !script_[i]->cell(0).size()) {
369                         delete script_[i];
370                         script_[i] = 0;
371                 }
372 }