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_cursor.h"
24 #include "math_extern.h"
25 #include "math_macrotable.h"
26 #include "math_macrotemplate.h"
27 #include "math_mathmlstream.h"
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
32 #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 MathInset * MathMacro::clone() const
50 return new MathMacro(*this);
54 const char * MathMacro::name() const
56 return tmplate_->asMacroTemplate()->name().c_str();
60 bool MathMacro::defining() const
63 //return mathcursor && mathcursor->formula()->getInsetName() == name();
67 bool MathMacro::editing() const
69 return mathcursor && mathcursor->isInside(this);
73 void MathMacro::metrics(MathMetricsInfo const & mi) const
78 mathed_string_dim(LM_TC_TEX, mi_, name(), ascent_, descent_, width_);
83 expanded_ = tmplate_->xcell(0);
84 expanded_.metrics(mi_);
85 width_ = expanded_.width() + 4;
86 ascent_ = expanded_.ascent() + 2;
87 descent_ = expanded_.descent() + 2;
89 width_ += mathed_string_width(LM_TC_TEXTRM, mi_, name()) + 10;
94 mathed_string_dim(LM_TC_TEXTRM, mi_, "#1: ", lasc, ldes, lwid);
96 for (idx_type i = 0; i < nargs(); ++i) {
97 MathXArray const & c = xcell(i);
99 width_ = std::max(width_, c.width() + lwid);
100 descent_ += std::max(c.ascent(), lasc) + 5;
101 descent_ += std::max(c.descent(), ldes) + 5;
106 expanded_ = tmplate_->xcell(0);
107 expanded_.data_.substitute(*this);
108 expanded_.metrics(mi_);
109 width_ = expanded_.width();
110 ascent_ = expanded_.ascent();
111 descent_ = expanded_.descent();
115 void MathMacro::draw(Painter & pain, int x, int y) const
120 drawStr(pain, LM_TC_TEX, mi_, x, y, name());
125 int h = y - ascent() + 2 + expanded_.ascent();
126 drawStr(pain, LM_TC_TEXTRM, mi_, x + 3, h, name());
128 int const w = mathed_string_width(LM_TC_TEXTRM, mi_, name());
129 expanded_.draw(pain, x + w + 12, h);
130 h += expanded_.descent();
135 mathed_string_dim(LM_TC_TEXTRM, mi_, "#1: ", lasc, ldes, lwid);
137 for (idx_type i = 0; i < nargs(); ++i) {
138 MathXArray const & c = xcell(i);
139 h += std::max(c.ascent(), lasc) + 5;
140 c.draw(pain, x + lwid, h);
142 str[1] += static_cast<char>(i);
143 drawStr(pain, LM_TC_TEX, mi_, x + 3, h, str);
144 h += std::max(c.descent(), ldes) + 5;
149 expanded_.draw(pain, x, y);
153 void MathMacro::dump() const
155 MathMacroTable::dump();
156 lyxerr << "\n macro: '" << this << "'\n";
157 lyxerr << " name: '" << name() << "'\n";
158 lyxerr << " template: '";
159 WriteStream wi(lyxerr);
165 bool MathMacro::idxUp(idx_type & idx) const
168 return MathNestInset::idxLeft(idx, pos);
172 bool MathMacro::idxDown(idx_type & idx) const
175 return 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")
194 features.require("binom");
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
241 expanded_ = tmplate_->xcell(0);
242 expanded_.data_.substitute(*this);