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"
29 MathMacro::MathMacro(string const & name)
30 : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
31 tmplate_(MathMacroTable::provide(name))
35 MathMacro::MathMacro(MathMacro const & m)
37 tmplate_(m.tmplate_) // don't copy 'expanded_'!
42 auto_ptr<InsetBase> MathMacro::clone() const
44 return auto_ptr<InsetBase>(new MathMacro(*this));
48 string MathMacro::name() const
50 return tmplate_->asMacroTemplate()->name();
54 bool MathMacro::defining() const
57 //return mathcursor && mathcursor->formula()->getInsetName() == name();
61 void MathMacro::expand() const
63 expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
67 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
69 augmentFont(font_, "lyxtex");
74 mathed_string_dim(font_, name(), dim_);
76 } else if (editing()) {
79 expanded_.metrics(mi_, dim_);
82 dim_.wid += mathed_string_width(font_, name()) + 10;
84 int ww = mathed_string_width(font_, "#1: ");
86 for (idx_type i = 0; i < nargs(); ++i) {
87 MathArray const & c = cell(i);
89 dim_.wid = max(dim_.wid, c.width() + ww);
90 dim_.des += max(c.ascent(), dim_.asc) + 5;
91 dim_.des += max(c.descent(), dim_.des) + 5;
97 expanded_.substitute(*this);
98 expanded_.metrics(mi_, dim_);
106 void MathMacro::draw(PainterInfo & pi, int x, int y) const
111 augmentFont(texfont, "lyxtex");
114 drawStr(pi, texfont, x, y, name());
119 int h = y - dim_.ascent() + 2 + expanded_.ascent();
120 drawStr(pi, font_, x + 3, h, name());
122 int const w = mathed_string_width(font_, name());
123 expanded_.draw(pi, x + w + 12, h);
124 h += expanded_.descent();
127 mathed_string_dim(font_, "#1: ", ldim);
129 for (idx_type i = 0; i < nargs(); ++i) {
130 MathArray const & c = cell(i);
131 h += max(c.ascent(), ldim.asc) + 5;
132 c.draw(pi, x + ldim.wid, h);
134 str[1] += static_cast<char>(i);
135 drawStr(pi, texfont, x + 3, h, str);
136 h += max(c.descent(), ldim.des) + 5;
141 expanded_.draw(pi, x, y);
145 void MathMacro::dump() const
147 MathMacroTable::dump();
148 lyxerr << "\n macro: '" << this << "'\n"
149 << " name: '" << name() << "'\n"
151 WriteStream wi(lyxerr);
153 lyxerr << "'" << endl;
157 bool MathMacro::idxUpDown(idx_type & idx, pos_type &, bool up, int x) const
161 if (!MathNestInset::idxLeft(idx, pos))
163 pos = cell(idx).x2pos(x);
166 if (!MathNestInset::idxRight(idx, pos))
168 pos = cell(idx).x2pos(x);
174 bool MathMacro::idxLeft(idx_type &, pos_type &) const
180 bool MathMacro::idxRight(idx_type &, pos_type &) const
186 void MathMacro::validate(LaTeXFeatures & features) const
188 if (name() == "binom" || name() == "mathcircumflex")
189 features.require(name());
190 //MathInset::validate(features);
194 void MathMacro::maple(MapleStream & os) const
197 ::maple(expanded_, os);
201 void MathMacro::mathmlize(MathMLStream & os) const
204 ::mathmlize(expanded_, os);
208 void MathMacro::octave(OctaveStream & os) const
211 ::octave(expanded_, os);
215 void MathMacro::updateExpansion() const
218 expanded_.substitute(*this);
222 void MathMacro::infoize(std::ostream & os) const
224 os << "Macro: " << name();
228 void MathMacro::infoize2(std::ostream & os) const
230 os << "Macro: " << name();