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 #include "math_macro.h"
19 #include "math_support.h"
20 #include "math_extern.h"
21 #include "math_macrotable.h"
22 #include "math_macrotemplate.h"
23 #include "math_mathmlstream.h"
24 #include "math_streamstr.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
28 #include "LaTeXFeatures.h"
35 MathMacro::MathMacro(string const & name)
36 : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
37 tmplate_(MathMacroTable::provide(name))
41 MathMacro::MathMacro(MathMacro const & m)
43 tmplate_(m.tmplate_) // don't copy 'expanded_'!
48 auto_ptr<InsetBase> MathMacro::clone() const
50 return auto_ptr<InsetBase>(new MathMacro(*this));
54 string MathMacro::name() const
56 return tmplate_->asMacroTemplate()->name();
60 bool MathMacro::defining() const
63 //return mathcursor && mathcursor->formula()->getInsetName() == name();
67 void MathMacro::expand() const
69 expanded_ = tmplate_->cell(tmplate_->cell(1).empty() ? 0 : 1);
73 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
75 augmentFont(font_, "lyxtex");
80 mathed_string_dim(font_, name(), dim_);
82 } else if (editing()) {
85 expanded_.metrics(mi_, dim_);
88 dim_.wid += mathed_string_width(font_, name()) + 10;
90 int ww = mathed_string_width(font_, "#1: ");
92 for (idx_type i = 0; i < nargs(); ++i) {
93 MathArray const & c = cell(i);
95 dim_.wid = max(dim_.wid, c.width() + ww);
96 dim_.des += max(c.ascent(), dim_.asc) + 5;
97 dim_.des += max(c.descent(), dim_.des) + 5;
103 expanded_.substitute(*this);
104 expanded_.metrics(mi_, dim_);
112 void MathMacro::draw(PainterInfo & pi, int x, int y) const
117 augmentFont(texfont, "lyxtex");
120 drawStr(pi, texfont, x, y, name());
125 int h = y - dim_.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 MathArray const & c = cell(i);
137 h += max(c.ascent(), ldim.asc) + 5;
138 c.draw(pi, x + ldim.wid, h);
140 str[1] += static_cast<char>(i);
141 drawStr(pi, texfont, x + 3, h, str);
142 h += max(c.descent(), ldim.des) + 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, pos_type &, bool up, int x) const
167 if (!MathNestInset::idxLeft(idx, pos))
169 pos = cell(idx).x2pos(x);
172 if (!MathNestInset::idxRight(idx, pos))
174 pos = cell(idx).x2pos(x);
180 bool MathMacro::idxLeft(idx_type &, pos_type &) const
186 bool MathMacro::idxRight(idx_type &, pos_type &) const
192 void MathMacro::validate(LaTeXFeatures & features) const
194 if (name() == "binom" || name() == "mathcircumflex")
195 features.require(name());
196 //MathInset::validate(features);
200 void MathMacro::maple(MapleStream & os) const
203 ::maple(expanded_, os);
207 void MathMacro::mathmlize(MathMLStream & os) const
210 ::mathmlize(expanded_, os);
214 void MathMacro::octave(OctaveStream & os) const
217 ::octave(expanded_, os);
221 void MathMacro::updateExpansion() const
224 expanded_.substitute(*this);
228 void MathMacro::infoize(std::ostream & os) const
230 os << "Macro: " << name();
234 void MathMacro::infoize2(std::ostream & os) const
236 os << "Macro: " << name();