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"
34 MathMacro::MathMacro(string const & name)
35 : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
36 tmplate_(MathMacroTable::provide(name))
40 MathMacro::MathMacro(MathMacro const & m)
42 tmplate_(m.tmplate_) // don't copy 'expanded_'!
47 MathInset * MathMacro::clone() const
49 return new MathMacro(*this);
53 const char * MathMacro::name() const
55 return tmplate_->asMacroTemplate()->name().c_str();
59 bool MathMacro::defining() const
62 //return mathcursor && mathcursor->formula()->getInsetName() == name();
66 bool MathMacro::editing() const
68 return mathcursor && mathcursor->isInside(this);
72 void MathMacro::metrics(MathMetricsInfo const & st) const
76 mathed_string_dim(LM_TC_TEX, size_.size, name(), ascent_, descent_, width_);
81 expanded_ = tmplate_->xcell(0);
82 expanded_.metrics(st);
84 width_ = expanded_.width() + 4;
85 ascent_ = expanded_.ascent() + 2;
86 descent_ = expanded_.descent() + 2;
88 width_ += mathed_string_width(LM_TC_TEXTRM, size_.size, name()) + 10;
93 mathed_string_dim(LM_TC_TEXTRM, size_.size, "#1: ", lasc, ldes, lwid);
95 for (idx_type i = 0; i < nargs(); ++i) {
96 MathXArray const & c = xcell(i);
98 width_ = std::max(width_, c.width() + lwid);
99 descent_ += std::max(c.ascent(), lasc) + 5;
100 descent_ += std::max(c.descent(), ldes) + 5;
105 expanded_ = tmplate_->xcell(0);
106 expanded_.data_.substitute(*this);
107 expanded_.metrics(st);
109 width_ = expanded_.width() + 6;
110 ascent_ = expanded_.ascent() + 3;
111 descent_ = expanded_.descent() + 3;
115 void MathMacro::draw(Painter & pain, int x, int y) const
123 drawStr(pain, LM_TC_TEX, size_.size, x, y, name());
128 int h = y - ascent() + 2 + expanded_.ascent();
129 drawStr(pain, LM_TC_TEXTRM, size_.size, x + 3, h, name());
131 int const w = mathed_string_width(LM_TC_TEXTRM, size_.size, name());
132 expanded_.draw(pain, x + w + 12, h);
133 h += expanded_.descent();
138 mathed_string_dim(LM_TC_TEXTRM, size_.size, "#1: ", lasc, ldes, lwid);
140 for (idx_type i = 0; i < nargs(); ++i) {
141 MathXArray const & c = xcell(i);
142 h += std::max(c.ascent(), lasc) + 5;
143 c.draw(pain, x + lwid, h);
145 str[1] += static_cast<char>(i);
146 drawStr(pain, LM_TC_TEX, size_.size, x + 3, h, str);
147 h += std::max(c.descent(), ldes) + 5;
152 expanded_.draw(pain, x + 3, y);
156 void MathMacro::dump() const
158 MathMacroTable::dump();
159 lyxerr << "\n macro: '" << this << "'\n";
160 lyxerr << " name: '" << name() << "'\n";
161 lyxerr << " template: '";
162 MathWriteInfo wi(lyxerr);
168 void MathMacro::write(MathWriteInfo & os) const
170 os << '\\' << name();
171 for (idx_type i = 0; i < nargs(); ++i)
172 os << '{' << cell(i) << '}';
178 void MathMacro::writeNormal(std::ostream & os) const
180 os << "[macro " << name() << " ";
181 for (idx_type i = 0; i < nargs(); ++i) {
182 cell(i).writeNormal(os);
189 bool MathMacro::idxUp(idx_type & idx, pos_type & pos) const
191 return MathNestInset::idxLeft(idx, pos);
195 bool MathMacro::idxDown(idx_type & idx, pos_type & pos) const
197 return MathNestInset::idxRight(idx, pos);
201 bool MathMacro::idxLeft(idx_type &, pos_type &) const
207 bool MathMacro::idxRight(idx_type &, pos_type &) const
213 void MathMacro::validate(LaTeXFeatures & features) const
215 if (name() == "binom")
216 features.binom = true;
217 //MathInset::validate(features);