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.
20 #pragma implementation
23 #include "math_macro.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
28 #include "mathed/support.h"
29 #include "mathed/math_cursor.h"
30 #include "math_macrotable.h"
31 #include "math_macrotemplate.h"
36 MathMacro::MathMacro(MathMacroTemplate const & t)
37 : MathInset(t.numargs(), t.name(), LM_OT_MACRO), tmplate_(&t)
41 MathMacro::MathMacro(MathMacro const & t)
42 : MathInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
47 MathInset * MathMacro::clone() const
49 return new MathMacro(*this);
53 void MathMacro::Metrics(MathStyles st, int, int)
55 if (mathcursor && mathcursor->isInside(this)) {
56 expanded_ = tmplate_->xcell(0);
57 expanded_.Metrics(st);
59 width_ = expanded_.width() + 4;
60 ascent_ = expanded_.ascent() + 2;
61 descent_ = expanded_.descent() + 2;
63 width_ += mathed_string_width(LM_TC_TEXTRM, size_, name_) + 10;
68 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
70 for (int i = 0; i < nargs(); ++i) {
71 MathXArray & c = xcell(i);
73 width_ = std::max(width_, c.width() + lwid);
74 descent_ += std::max(c.ascent(), lasc) + 5;
75 descent_ += std::max(c.descent(), ldes) + 5;
78 expanded_ = tmplate_->xcell(0);
79 expanded_.data_.substitute(*this);
80 expanded_.Metrics(st);
82 width_ = expanded_.width() + 6;
83 ascent_ = expanded_.ascent() + 3;
84 descent_ = expanded_.descent() + 3;
89 void MathMacro::draw(Painter & pain, int x, int y)
98 if (mathcursor && mathcursor->isInside(this)) {
100 int h = y - ascent() + 2 + expanded_.ascent();
101 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
103 int const w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
104 expanded_.draw(pain, x + w + 12, h);
105 h += expanded_.descent();
110 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
112 for (int i = 0; i < nargs(); ++i) {
113 MathXArray & c = xcell(i);
114 h += std::max(c.ascent(), lasc) + 5;
115 c.draw(pain, x + lwid, h);
118 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
119 h += std::max(c.descent(), ldes) + 5;
123 expanded_.draw(pain, x + 3, y);
128 pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
132 void MathMacro::dump(std::ostream & os) const
134 MathMacroTable::dump();
135 os << "\n macro: '" << this << "'\n";
136 os << " name: '" << name_ << "'\n";
137 os << " template: '" << tmplate_ << "'\n";
138 os << " template: '" << *tmplate_ << "'\n";
142 void MathMacro::Write(std::ostream & os, bool fragile) const
145 for (int i = 0; i < nargs(); ++i) {
147 cell(i).Write(os, fragile);
155 void MathMacro::WriteNormal(std::ostream & os) const
157 os << "[macro " << name_ << " ";
158 for (int i = 0; i < nargs(); ++i) {
159 cell(i).WriteNormal(os);
166 bool MathMacro::idxUp(int & idx, int & pos) const
168 return MathInset::idxLeft(idx, pos);
172 bool MathMacro::idxDown(int & idx, int & pos) const
174 return MathInset::idxRight(idx, pos);
178 bool MathMacro::idxLeft(int &, int &) const
184 bool MathMacro::idxRight(int &, int &) const