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.
19 #include "math_macro.h"
20 #include "math_support.h"
21 #include "math_extern.h"
22 #include "math_macrotable.h"
23 #include "math_macrotemplate.h"
24 #include "math_mathmlstream.h"
25 #include "math_streamstr.h"
26 #include "support/lstrings.h"
27 #include "support/LAssert.h"
29 #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 auto_ptr<InsetBase> MathMacro::clone() const
52 return auto_ptr<InsetBase>(new MathMacro(*this));
56 string 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_->cell(tmplate_->cell(1).empty() ? 0 : 1);
75 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
77 augmentFont(font_, "lyxtex");
82 mathed_string_dim(font_, name(), dim_);
84 } else if (editing()) {
87 expanded_.metrics(mi_, dim_);
90 dim_.wid += mathed_string_width(font_, name()) + 10;
92 int ww = mathed_string_width(font_, "#1: ");
94 for (idx_type i = 0; i < nargs(); ++i) {
95 MathArray const & c = cell(i);
97 dim_.wid = max(dim_.wid, c.width() + ww);
98 dim_.des += max(c.ascent(), dim_.asc) + 5;
99 dim_.des += max(c.descent(), dim_.des) + 5;
105 expanded_.substitute(*this);
106 expanded_.metrics(mi_, dim_);
114 void MathMacro::draw(PainterInfo & pi, int x, int y) const
119 augmentFont(texfont, "lyxtex");
122 drawStr(pi, texfont, x, y, name());
127 int h = y - dim_.ascent() + 2 + expanded_.ascent();
128 drawStr(pi, font_, x + 3, h, name());
130 int const w = mathed_string_width(font_, name());
131 expanded_.draw(pi, x + w + 12, h);
132 h += expanded_.descent();
135 mathed_string_dim(font_, "#1: ", ldim);
137 for (idx_type i = 0; i < nargs(); ++i) {
138 MathArray const & c = cell(i);
139 h += max(c.ascent(), ldim.asc) + 5;
140 c.draw(pi, x + ldim.wid, h);
142 str[1] += static_cast<char>(i);
143 drawStr(pi, texfont, x + 3, h, str);
144 h += max(c.descent(), ldim.des) + 5;
149 expanded_.draw(pi, x, y);
153 void MathMacro::dump() const
155 MathMacroTable::dump();
156 lyxerr << "\n macro: '" << this << "'\n"
157 << " name: '" << name() << "'\n"
159 WriteStream wi(lyxerr);
161 lyxerr << "'" << endl;
165 bool MathMacro::idxUpDown(idx_type & idx, pos_type &, bool up, int x) const
169 if (!MathNestInset::idxLeft(idx, pos))
171 pos = cell(idx).x2pos(x);
174 if (!MathNestInset::idxRight(idx, pos))
176 pos = cell(idx).x2pos(x);
182 bool MathMacro::idxLeft(idx_type &, pos_type &) const
188 bool MathMacro::idxRight(idx_type &, pos_type &) const
194 void MathMacro::validate(LaTeXFeatures & features) const
196 if (name() == "binom" || name() == "mathcircumflex")
197 features.require(name());
198 //MathInset::validate(features);
202 void MathMacro::maple(MapleStream & os) const
205 ::maple(expanded_, os);
209 void MathMacro::mathmlize(MathMLStream & os) const
212 ::mathmlize(expanded_, os);
216 void MathMacro::octave(OctaveStream & os) const
219 ::octave(expanded_, os);
223 void MathMacro::updateExpansion() const
226 expanded_.substitute(*this);
230 void MathMacro::infoize(std::ostream & os) const
232 os << "Macro: " << name();
236 void MathMacro::infoize2(std::ostream & os) const
238 os << "Macro: " << name();