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"
26 #include "mathed/support.h"
27 #include "mathed/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 void MathMacro::metrics(MathStyles st) const
62 if (mathcursor && mathcursor->isInside(this)) {
63 expanded_ = tmplate_->xcell(0);
64 expanded_.metrics(st);
66 width_ = expanded_.width() + 4;
67 ascent_ = expanded_.ascent() + 2;
68 descent_ = expanded_.descent() + 2;
70 width_ += mathed_string_width(LM_TC_TEXTRM, size_, name()) + 10;
75 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
77 for (idx_type i = 0; i < nargs(); ++i) {
78 MathXArray const & c = xcell(i);
80 width_ = std::max(width_, c.width() + lwid);
81 descent_ += std::max(c.ascent(), lasc) + 5;
82 descent_ += std::max(c.descent(), ldes) + 5;
85 expanded_ = tmplate_->xcell(0);
86 expanded_.data_.substitute(*this);
87 expanded_.metrics(st);
89 width_ = expanded_.width() + 6;
90 ascent_ = expanded_.ascent() + 3;
91 descent_ = expanded_.descent() + 3;
96 void MathMacro::draw(Painter & pain, int x, int y) const
105 if (mathcursor && mathcursor->isInside(this)) {
107 int h = y - ascent() + 2 + expanded_.ascent();
108 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name());
110 int const w = mathed_string_width(LM_TC_TEXTRM, size(), name());
111 expanded_.draw(pain, x + w + 12, h);
112 h += expanded_.descent();
117 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
119 for (idx_type i = 0; i < nargs(); ++i) {
120 MathXArray const & c = xcell(i);
121 h += std::max(c.ascent(), lasc) + 5;
122 c.draw(pain, x + lwid, h);
124 str[1] += static_cast<char>(i);
125 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
126 h += std::max(c.descent(), ldes) + 5;
130 expanded_.draw(pain, x + 3, y);
131 //col = LColor::black;
135 // pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
139 void MathMacro::dump() const
141 MathMacroTable::dump();
142 lyxerr << "\n macro: '" << this << "'\n";
143 lyxerr << " name: '" << name() << "'\n";
144 lyxerr << " template: '"; tmplate_->write(lyxerr, false); lyxerr << "'\n";
149 void MathMacro::write(std::ostream & os, bool fragile) const
151 os << '\\' << name();
152 for (idx_type i = 0; i < nargs(); ++i) {
154 cell(i).write(os, fragile);
162 void MathMacro::writeNormal(std::ostream & os) const
164 os << "[macro " << name() << " ";
165 for (idx_type i = 0; i < nargs(); ++i) {
166 cell(i).writeNormal(os);
173 bool MathMacro::idxUp(idx_type & idx, pos_type & pos) const
175 return MathNestInset::idxLeft(idx, pos);
179 bool MathMacro::idxDown(idx_type & idx, pos_type & pos) const
181 return MathNestInset::idxRight(idx, pos);
185 bool MathMacro::idxLeft(idx_type &, pos_type &) const
191 bool MathMacro::idxRight(idx_type &, pos_type &) const
197 void MathMacro::validate(LaTeXFeatures & features) const
199 if (name() == "binom")
200 features.binom = true;
201 //MathInset::validate(features);