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
211 int des = down()->ascent();
215 des = std::max(des, ndes());
220 int MathAtom::dy1() const
224 int asc = up()->descent();
228 asc = std::max(asc, nasc());
229 asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I'));
234 int MathAtom::dx0() const
237 return hasLimits() ? (width() - down()->width()) / 2 : nwid();
241 int MathAtom::dx1() const
244 return hasLimits() ? (width() - up()->width()) / 2 : nwid();
248 int MathAtom::dxx() const
250 lyx::Assert(nucleus());
251 return hasLimits() ? (width() - nwid()) / 2 : 0;
255 int MathAtom::ascent() const
257 return dy1() + (up() ? up()->ascent() : 0);
261 int MathAtom::descent() const
263 return dy0() + (down() ? down()->descent() : 0);
267 int MathAtom::width() const
273 wid = std::max(wid, up()->width());
275 wid = std::max(wid, down()->width());
278 wid = std::max(wid, up()->width());
280 wid = std::max(wid, down()->width());
287 int MathAtom::nwid() const
289 return nucleus() ? nucleus()->width() : 0;
293 int MathAtom::nasc() const
295 return nucleus() ? nucleus()->ascent()
296 : mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I');
300 int MathAtom::ndes() const
302 return nucleus() ? nucleus()->descent()
303 : mathed_char_descent(LM_TC_VAR, LM_ST_TEXT, 'I');
307 void MathAtom::draw(Painter & pain, int x, int y) const
312 nucleus()->draw(pain, x + dxx(), y);
314 up()->draw(pain, x + dx1(), y - dy1());
316 down()->draw(pain, x + dx0(), y + dy0());
320 void MathAtom::write(std::ostream & os, bool fragile) const
323 nucleus()->write(os, fragile);
324 if (nucleus()->takesLimits()) {
335 up()->write(os, fragile);
341 down()->write(os, fragile);
347 bool MathAtom::hasLimits() const
350 limits_ == 1 || (limits_ == 0 && nucleus() && nucleus()->isScriptable());
354 void MathAtom::substitute(MathMacro const & m)
357 nucleus()->substitute(m);
361 down()->substitute(m);
365 void MathAtom::removeEmptyScripts()
367 for (int i = 0; i <= 1; ++i)
368 if (script_[i] && !script_[i]->cell(0).size()) {