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(), ascent_, descent_, width_);
87 expanded_.metrics(mi_);
88 width_ = expanded_.width() + 4;
89 ascent_ = expanded_.ascent() + 2;
90 descent_ = expanded_.descent() + 2;
92 width_ += mathed_string_width(font_, name()) + 10;
97 mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
99 for (idx_type i = 0; i < nargs(); ++i) {
100 MathXArray const & c = xcell(i);
102 width_ = max(width_, c.width() + lwid);
103 descent_ += max(c.ascent(), lasc) + 5;
104 descent_ += max(c.descent(), ldes) + 5;
110 expanded_.data_.substitute(*this);
111 expanded_.metrics(mi_);
112 width_ = expanded_.width();
113 ascent_ = expanded_.ascent();
114 descent_ = expanded_.descent();
118 void MathMacro::draw(MathPainterInfo & pi, int x, int y) const
123 augmentFont(texfont, "lyxtex");
126 drawStr(pi, texfont, x, y, name());
131 int h = y - ascent() + 2 + expanded_.ascent();
132 drawStr(pi, font_, x + 3, h, name());
134 int const w = mathed_string_width(font_, name());
135 expanded_.draw(pi, x + w + 12, h);
136 h += expanded_.descent();
141 mathed_string_dim(font_, "#1: ", lasc, ldes, lwid);
143 for (idx_type i = 0; i < nargs(); ++i) {
144 MathXArray const & c = xcell(i);
145 h += max(c.ascent(), lasc) + 5;
146 c.draw(pi, x + lwid, h);
148 str[1] += static_cast<char>(i);
149 drawStr(pi, texfont, x + 3, h, str);
150 h += max(c.descent(), ldes) + 5;
155 expanded_.draw(pi, x, y);
159 void MathMacro::dump() const
161 MathMacroTable::dump();
162 lyxerr << "\n macro: '" << this << "'\n";
163 lyxerr << " name: '" << name() << "'\n";
164 lyxerr << " template: '";
165 WriteStream wi(lyxerr);
171 bool MathMacro::idxUpDown(idx_type & idx, bool up) const
175 up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos);
179 bool MathMacro::idxLeft(idx_type &, pos_type &) const
185 bool MathMacro::idxRight(idx_type &, pos_type &) const
191 void MathMacro::validate(LaTeXFeatures & features) const
193 if (name() == "binom" || name() == "mathcircumflex")
194 features.require(name());
195 //MathInset::validate(features);
199 void MathMacro::maplize(MapleStream & os) const
202 ::maplize(expanded_.data_, os);
206 void MathMacro::mathmlize(MathMLStream & os) const
209 ::mathmlize(expanded_.data_, os);
213 void MathMacro::octavize(OctaveStream & os) const
216 ::octavize(expanded_.data_, os);
220 void MathMacro::normalize(NormalStream & os) const
222 os << "[macro " << name() << " ";
223 for (idx_type i = 0; i < nargs(); ++i)
224 os << cell(i) << ' ';
229 void MathMacro::write(WriteStream & os) const
231 os << '\\' << name();
232 for (idx_type i = 0; i < nargs(); ++i)
233 os << '{' << cell(i) << '}';
239 void MathMacro::updateExpansion() const
242 expanded_.data_.substitute(*this);