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"
23 #include "support/lstrings.h"
24 #include "support/LAssert.h"
27 #include "math_cursor.h"
28 #include "math_macrotable.h"
29 #include "math_macrotemplate.h"
31 #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(MathStyles st) const
77 mathed_string_dim(LM_TC_TEX, size_, name(), ascent_, descent_, width_);
82 expanded_ = tmplate_->xcell(0);
83 expanded_.metrics(st);
85 width_ = expanded_.width() + 4;
86 ascent_ = expanded_.ascent() + 2;
87 descent_ = expanded_.descent() + 2;
89 width_ += mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
94 mathed_string_dim(LM_TC_TEXTRM, size_, "#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(st);
110 width_ = expanded_.width() + 6;
111 ascent_ = expanded_.ascent() + 3;
112 descent_ = expanded_.descent() + 3;
116 void MathMacro::draw(Painter & pain, int x, int y) const
124 drawStr(pain, LM_TC_TEX, size_, x, y, name());
129 int h = y - ascent() + 2 + expanded_.ascent();
130 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name());
132 int const w = mathed_string_width(LM_TC_TEXTRM, size(), name());
133 expanded_.draw(pain, x + w + 12, h);
134 h += expanded_.descent();
139 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
141 for (idx_type i = 0; i < nargs(); ++i) {
142 MathXArray const & c = xcell(i);
143 h += std::max(c.ascent(), lasc) + 5;
144 c.draw(pain, x + lwid, h);
146 str[1] += static_cast<char>(i);
147 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
148 h += std::max(c.descent(), ldes) + 5;
153 expanded_.draw(pain, x + 3, y);
157 void MathMacro::dump() const
159 MathMacroTable::dump();
160 lyxerr << "\n macro: '" << this << "'\n";
161 lyxerr << " name: '" << name() << "'\n";
162 lyxerr << " template: '"; tmplate_->write(lyxerr, false); lyxerr << "'\n";
167 void MathMacro::write(std::ostream & os, bool fragile) const
169 os << '\\' << name();
170 for (idx_type i = 0; i < nargs(); ++i) {
172 cell(i).write(os, fragile);
180 void MathMacro::writeNormal(std::ostream & os) const
182 os << "[macro " << name() << " ";
183 for (idx_type i = 0; i < nargs(); ++i) {
184 cell(i).writeNormal(os);
191 bool MathMacro::idxUp(idx_type & idx, pos_type & pos) const
193 return MathNestInset::idxLeft(idx, pos);
197 bool MathMacro::idxDown(idx_type & idx, pos_type & pos) const
199 return MathNestInset::idxRight(idx, pos);
203 bool MathMacro::idxLeft(idx_type &, pos_type &) const
209 bool MathMacro::idxRight(idx_type &, pos_type &) const
215 void MathMacro::validate(LaTeXFeatures & features) const
217 if (name() == "binom")
218 features.binom = true;
219 //MathInset::validate(features);