3 * Purpose: Implementation of macro class for mathed
4 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
5 * Created: November 1996
6 * Description: WYSIWYG math macros
10 * Copyright: 1996, 1997 Alejandro Aguilar Sierra
12 * Version: 0.2, Math & Lyx project.
14 * This code is under the GNU General Public Licence version 2 or later.
18 #pragma implementation
21 #include "math_macro.h"
22 #include "math_support.h"
23 #include "math_extern.h"
24 #include "math_macrotable.h"
25 #include "math_macrotemplate.h"
26 #include "math_mathmlstream.h"
27 #include "math_streamstr.h"
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
32 #include "LaTeXFeatures.h"
38 MathMacro::MathMacro(string const & name)
39 : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
40 tmplate_(MathMacroTable::provide(name))
44 MathMacro::MathMacro(MathMacro const & m)
46 tmplate_(m.tmplate_) // don't copy 'expanded_'!
51 MathInset * MathMacro::clone() const
53 return new MathMacro(*this);
57 string const & MathMacro::name() const
59 return tmplate_->asMacroTemplate()->name();
63 bool MathMacro::defining() const
66 //return mathcursor && mathcursor->formula()->getInsetName() == name();
70 void MathMacro::expand() const
72 expanded_ = tmplate_->xcell(tmplate_->cell(1).empty() ? 0 : 1);
76 void MathMacro::metrics(MathMetricsInfo const & mi) const
78 whichFont(font_, LM_TC_TEX, mi);
82 mathed_string_dim(font_, name(), ascent_, descent_, width_);
88 expanded_.metrics(mi_);
89 width_ = expanded_.width() + 4;
90 ascent_ = expanded_.ascent() + 2;
91 descent_ = expanded_.descent() + 2;
93 width_ += mathed_string_width(font_, name()) + 10;
98 mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
100 for (idx_type i = 0; i < nargs(); ++i) {
101 MathXArray const & c = xcell(i);
103 width_ = max(width_, c.width() + lwid);
104 descent_ += max(c.ascent(), lasc) + 5;
105 descent_ += max(c.descent(), ldes) + 5;
111 expanded_.data_.substitute(*this);
112 expanded_.metrics(mi_);
113 width_ = expanded_.width();
114 ascent_ = expanded_.ascent();
115 descent_ = expanded_.descent();
119 void MathMacro::draw(Painter & pain, int x, int y) const
124 whichFont(texfont, LM_TC_TEX, mi_);
127 drawStr(pain, texfont, x, y, name());
132 int h = y - ascent() + 2 + expanded_.ascent();
133 drawStr(pain, font_, x + 3, h, name());
135 int const w = mathed_string_width(font_, name());
136 expanded_.draw(pain, x + w + 12, h);
137 h += expanded_.descent();
142 mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
144 for (idx_type i = 0; i < nargs(); ++i) {
145 MathXArray const & c = xcell(i);
146 h += max(c.ascent(), lasc) + 5;
147 c.draw(pain, x + lwid, h);
149 str[1] += static_cast<char>(i);
150 drawStr(pain, texfont, x + 3, h, str);
151 h += max(c.descent(), ldes) + 5;
156 expanded_.draw(pain, x, y);
160 void MathMacro::dump() const
162 MathMacroTable::dump();
163 lyxerr << "\n macro: '" << this << "'\n";
164 lyxerr << " name: '" << name() << "'\n";
165 lyxerr << " template: '";
166 WriteStream wi(lyxerr);
172 bool MathMacro::idxUpDown(idx_type & idx, bool up) const
176 up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos);
180 bool MathMacro::idxLeft(idx_type &, pos_type &) const
186 bool MathMacro::idxRight(idx_type &, pos_type &) const
192 void MathMacro::validate(LaTeXFeatures & features) const
194 if (name() == "binom" || name() == "mathcircumflex")
195 features.require(name());
196 //MathInset::validate(features);
200 void MathMacro::maplize(MapleStream & os) const
203 ::maplize(expanded_.data_, os);
207 void MathMacro::mathmlize(MathMLStream & os) const
210 ::mathmlize(expanded_.data_, os);
214 void MathMacro::octavize(OctaveStream & os) const
217 ::octavize(expanded_.data_, os);
221 void MathMacro::normalize(NormalStream & os) const
223 os << "[macro " << name() << " ";
224 for (idx_type i = 0; i < nargs(); ++i)
225 os << cell(i) << ' ';
230 void MathMacro::write(WriteStream & os) const
232 os << '\\' << name();
233 for (idx_type i = 0; i < nargs(); ++i)
234 os << '{' << cell(i) << '}';
240 void MathMacro::updateExpansion() const
243 expanded_.data_.substitute(*this);