3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "math_macro.h"
15 #include "math_support.h"
16 #include "math_extern.h"
17 #include "math_macrotable.h"
18 #include "math_macrotemplate.h"
19 #include "math_mathmlstream.h"
21 #include "LaTeXFeatures.h"
30 MathMacro::MathMacro(string const & name)
31 : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
32 tmplate_(MathMacroTable::provide(name))
36 MathMacro::MathMacro(MathMacro const & m)
38 tmplate_(m.tmplate_) // don't copy 'expanded_'!
43 auto_ptr<InsetBase> MathMacro::clone() const
45 return auto_ptr<InsetBase>(new MathMacro(*this));
49 string MathMacro::name() const
51 return tmplate_->asMacroTemplate()->name();
55 bool MathMacro::defining() const
58 //return mathcursor && mathcursor->formula()->getInsetName() == name();
62 void MathMacro::expand() const
64 expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
68 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
70 augmentFont(font_, "lyxtex");
75 mathed_string_dim(font_, name(), dim_);
77 } else if (editing()) {
80 expanded_.metrics(mi_, dim_);
83 dim_.wid += mathed_string_width(font_, name()) + 10;
85 int ww = mathed_string_width(font_, "#1: ");
87 for (idx_type i = 0; i < nargs(); ++i) {
88 MathArray const & c = cell(i);
90 dim_.wid = max(dim_.wid, c.width() + ww);
91 dim_.des += max(c.ascent(), dim_.asc) + 5;
92 dim_.des += max(c.descent(), dim_.des) + 5;
98 expanded_.substitute(*this);
99 expanded_.metrics(mi_, dim_);
107 void MathMacro::draw(PainterInfo & pi, int x, int y) const
112 augmentFont(texfont, "lyxtex");
115 drawStr(pi, texfont, x, y, name());
120 int h = y - dim_.ascent() + 2 + expanded_.ascent();
121 drawStr(pi, font_, x + 3, h, name());
123 int const w = mathed_string_width(font_, name());
124 expanded_.draw(pi, x + w + 12, h);
125 h += expanded_.descent();
128 mathed_string_dim(font_, "#1: ", ldim);
130 for (idx_type i = 0; i < nargs(); ++i) {
131 MathArray const & c = cell(i);
132 h += max(c.ascent(), ldim.asc) + 5;
133 c.draw(pi, x + ldim.wid, h);
135 str[1] += static_cast<char>(i);
136 drawStr(pi, texfont, x + 3, h, str);
137 h += max(c.descent(), ldim.des) + 5;
142 expanded_.draw(pi, x, y);
146 void MathMacro::dump() const
148 MathMacroTable::dump();
149 lyxerr << "\n macro: '" << this << "'\n"
150 << " name: '" << name() << "'\n"
152 WriteStream wi(lyxerr);
154 lyxerr << "'" << endl;
158 bool MathMacro::idxUpDown(LCursor & cur, bool up, int x) const
161 if (!MathNestInset::idxLeft(cur))
164 if (!MathNestInset::idxRight(cur))
167 cur.pos() = cur.cell().x2pos(x);
172 bool MathMacro::idxLeft(LCursor &) const
178 bool MathMacro::idxRight(LCursor &) const
184 void MathMacro::validate(LaTeXFeatures & features) const
186 if (name() == "binom" || name() == "mathcircumflex")
187 features.require(name());
188 //MathInset::validate(features);
192 void MathMacro::maple(MapleStream & os) const
195 ::maple(expanded_, os);
199 void MathMacro::mathmlize(MathMLStream & os) const
202 ::mathmlize(expanded_, os);
206 void MathMacro::octave(OctaveStream & os) const
209 ::octave(expanded_, os);
213 void MathMacro::updateExpansion() const
216 expanded_.substitute(*this);
220 void MathMacro::infoize(std::ostream & os) const
222 os << "Macro: " << name();
226 void MathMacro::infoize2(std::ostream & os) const
228 os << "Macro: " << name();