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"
31 #include "LaTeXFeatures.h"
37 MathMacro::MathMacro(string const & name)
38 : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
39 tmplate_(MathMacroTable::provide(name))
43 MathMacro::MathMacro(MathMacro const & m)
45 tmplate_(m.tmplate_) // don't copy 'expanded_'!
50 MathInset * MathMacro::clone() const
52 return new MathMacro(*this);
56 string const & MathMacro::name() const
58 return tmplate_->asMacroTemplate()->name();
62 bool MathMacro::defining() const
65 //return mathcursor && mathcursor->formula()->getInsetName() == name();
69 void MathMacro::expand() const
71 expanded_ = tmplate_->xcell(tmplate_->cell(1).empty() ? 0 : 1);
75 void MathMacro::metrics(MathMetricsInfo & mi) const
77 augmentFont(font_, "lyxtex");
81 mathed_string_dim(font_, name(), dim_);
87 dim_ = expanded_.metrics(mi_);
90 dim_.w += mathed_string_width(font_, name()) + 10;
93 mathed_string_dim(font_, "#1: ", ldim);
95 for (idx_type i = 0; i < nargs(); ++i) {
96 MathXArray const & c = xcell(i);
98 dim_.w = max(dim_.w, c.width() + ldim.w);
99 dim_.d += max(c.ascent(), ldim.a) + 5;
100 dim_.d += max(c.descent(), ldim.d) + 5;
106 expanded_.data().substitute(*this);
107 expanded_.metrics(mi_);
108 dim_ = expanded_.dim();
112 void MathMacro::draw(MathPainterInfo & pi, int x, int y) const
117 augmentFont(texfont, "lyxtex");
120 drawStr(pi, texfont, x, y, name());
125 int h = y - ascent() + 2 + expanded_.ascent();
126 drawStr(pi, font_, x + 3, h, name());
128 int const w = mathed_string_width(font_, name());
129 expanded_.draw(pi, x + w + 12, h);
130 h += expanded_.descent();
133 mathed_string_dim(font_, "#1: ", ldim);
135 for (idx_type i = 0; i < nargs(); ++i) {
136 MathXArray const & c = xcell(i);
137 h += max(c.ascent(), ldim.a) + 5;
138 c.draw(pi, x + ldim.w, h);
140 str[1] += static_cast<char>(i);
141 drawStr(pi, texfont, x + 3, h, str);
142 h += max(c.descent(), ldim.d) + 5;
147 expanded_.draw(pi, x, y);
151 void MathMacro::dump() const
153 MathMacroTable::dump();
154 lyxerr << "\n macro: '" << this << "'\n";
155 lyxerr << " name: '" << name() << "'\n";
156 lyxerr << " template: '";
157 WriteStream wi(lyxerr);
163 bool MathMacro::idxUpDown(idx_type & idx, bool up) const
167 up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos);
171 bool MathMacro::idxLeft(idx_type &, pos_type &) const
177 bool MathMacro::idxRight(idx_type &, pos_type &) const
183 void MathMacro::validate(LaTeXFeatures & features) const
185 if (name() == "binom" || name() == "mathcircumflex")
186 features.require(name());
187 //MathInset::validate(features);
191 void MathMacro::maplize(MapleStream & os) const
194 ::maplize(expanded_.data(), os);
198 void MathMacro::mathmlize(MathMLStream & os) const
201 ::mathmlize(expanded_.data(), os);
205 void MathMacro::octavize(OctaveStream & os) const
208 ::octavize(expanded_.data(), os);
212 void MathMacro::normalize(NormalStream & os) const
214 os << "[macro " << name() << " ";
215 for (idx_type i = 0; i < nargs(); ++i)
216 os << cell(i) << ' ';
221 void MathMacro::write(WriteStream & os) const
223 os << '\\' << name();
224 for (idx_type i = 0; i < nargs(); ++i)
225 os << '{' << cell(i) << '}';
231 void MathMacro::updateExpansion() const
234 expanded_.data().substitute(*this);