3 * Purpose: Implementation of insets for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: January 1996
8 * Dependencies: Xlib, XForms
10 * Copyright: 1996, 1997 Alejandro Aguilar Sierra
14 * You are free to use and modify this code under the terms of
15 * the GNU General Public Licence version 2 or later.
19 #pragma implementation
22 #include "math_atom.h"
23 #include "math_scriptinset.h"
26 #include "support/LAssert.h"
30 : nucleus_(0), limits_(0), xo_(0), yo_(0)
37 MathAtom::MathAtom(MathInset * p)
38 : nucleus_(p), limits_(0), xo_(0), yo_(0)
46 MathAtom::MathAtom(MathAtom const & p)
52 void MathAtom::operator=(MathAtom const & p)
75 void MathAtom::copy(MathAtom const & p)
77 //cerr << "calling MathAtom::copy\n";
81 nucleus_ = p.nucleus_;
82 script_[0] = p.script_[0];
83 script_[1] = p.script_[1];
85 nucleus_ = nucleus_->clone();
87 script_[0] = new MathScriptInset(*script_[0]);
89 script_[1] = new MathScriptInset(*script_[1]);
93 int MathAtom::height() const
95 return ascent() + descent();
99 std::ostream & operator<<(std::ostream & os, MathAtom const & atom)
101 atom.write(os, false);
106 int MathAtom::xo() const
112 int MathAtom::yo() const
118 void MathAtom::xo(int x) const
124 void MathAtom::yo(int y) const
130 void MathAtom::getXY(int & x, int & y) const
138 void MathAtom::userSetSize(MathStyles sz)
142 flag = flag & ~LMPF_FIXED_SIZE;
147 void MathAtom::writeNormal(std::ostream & os) const
153 void MathAtom::dump() const
155 lyxerr << "---------------------------------------------\n";
156 write(lyxerr, false);
157 lyxerr << "\n---------------------------------------------\n";
161 bool MathAtom::covers(int x, int y) const
165 x <= xo_ + width() &&
166 y >= yo_ - ascent() &&
167 y <= yo_ + descent();
171 MathScriptInset * MathAtom::ensure(bool up)
174 script_[up] = new MathScriptInset(up);
179 void MathAtom::validate(LaTeXFeatures &) const
183 void MathAtom::metrics(MathStyles st) const
185 MathStyles tt = smallerStyleScript(st);
187 nucleus()->metrics(st);
195 MathScriptInset * MathAtom::up() const
201 MathScriptInset * MathAtom::down() const
207 int MathAtom::dy0() const
210 int des = down()->ascent();
214 des = std::max(des, ndes());
219 int MathAtom::dy1() const
222 int asc = up()->descent();
226 asc = std::max(asc, nasc());
227 asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I'));
232 int MathAtom::dx0() const
235 return hasLimits() ? (width() - down()->width()) / 2 : nwid();
239 int MathAtom::dx1() const
242 return hasLimits() ? (width() - up()->width()) / 2 : nwid();
246 int MathAtom::dxx() const
248 lyx::Assert(nucleus());
249 return hasLimits() ? (width() - nwid()) / 2 : 0;
253 int MathAtom::ascent() const
257 asc += hasLimits() ? up()->height() + 2 : up()->ascent();
262 int MathAtom::descent() const
266 des += hasLimits() ? down()->height() + 2 : down()->descent();
271 int MathAtom::width() const
277 wid = std::max(wid, up()->width());
279 wid = std::max(wid, down()->width());
282 wid = std::max(wid, up()->width());
284 wid = std::max(wid, down()->width());
291 int MathAtom::nwid() const
293 return nucleus() ? nucleus()->width() : 0;
297 int MathAtom::nasc() const
299 return nucleus() ? nucleus()->ascent()
300 : mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I');
304 int MathAtom::ndes() const
306 return nucleus() ? nucleus()->descent()
307 : mathed_char_descent(LM_TC_VAR, LM_ST_TEXT, 'I');
311 void MathAtom::draw(Painter & pain, int x, int y) const
316 nucleus()->draw(pain, x + dxx(), y);
318 up()->draw(pain, x + dx1(), y - dy1());
320 down()->draw(pain, x + dx0(), y + dy0());
324 void MathAtom::write(std::ostream & os, bool fragile) const
327 nucleus()->write(os, fragile);
328 if (nucleus()->takesLimits()) {
339 up()->write(os, fragile);
345 down()->write(os, fragile);
351 bool MathAtom::hasLimits() const
354 limits_ == 1 || (limits_ == 0 && nucleus() && nucleus()->isScriptable());
358 void MathAtom::substitute(MathArray & array, MathMacro const & m) const
361 nucleus()->substitute(array, m);
363 up()->substitute(array, m);
365 down()->substitute(array, m);
369 void MathAtom::removeEmptyScripts()
371 for (int i = 0; i <= 1; ++i)
372 if (script_[i] && !script_[i]->cell(0).size()) {