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.name(), LM_OT_MACRO, t.numargs()), tmplate_(&t)
41 MathInset * MathMacro::clone() const
43 return new MathMacro(*this);
47 void MathMacro::Metrics(MathStyles st)
49 if (mathcursor && mathcursor->isInside(this)) {
50 expanded_ = tmplate_->xcell(0);
51 expanded_.Metrics(st);
53 width_ = expanded_.width() + 4;
54 ascent_ = expanded_.ascent() + 2;
55 descent_ = expanded_.descent() + 2;
57 width_ += mathed_string_width(LM_TC_TEXTRM, size_, name_) + 10;
59 for (int i = 0; i < nargs(); ++i) {
60 MathXArray & c = xcell(i);
62 width_ = std::max(width_, c.width() + 30);
63 descent_ += c.height() + 10;
66 expanded_ = tmplate_->xcell(0);
67 expanded_.data_.substitute(*this);
68 expanded_.Metrics(st);
70 width_ = expanded_.width() + 6;
71 ascent_ = expanded_.ascent() + 3;
72 descent_ = expanded_.descent() + 3;
77 void MathMacro::draw(Painter & pain, int x, int y)
84 if (mathcursor && mathcursor->isInside(this)) {
86 int h = y - ascent() + 2 + expanded_.ascent();
87 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
89 int w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
90 expanded_.draw(pain, x + w + 12, h);
91 h += expanded_.descent();
93 for (int i = 0; i < nargs(); ++i) {
94 MathXArray & c = xcell(i);
96 c.draw(pain, x + 30, h);
99 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
100 h += c.descent() + 5;
104 expanded_.draw(pain, x + 3, y);
108 pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
112 void MathMacro::dump(std::ostream & os) const
114 MathMacroTable::dump();
115 os << "\n macro: '" << this << "'\n";
116 os << " name: '" << name_ << "'\n";
117 os << " template: '" << tmplate_ << "'\n";
118 os << " template: '" << *tmplate_ << "'\n";
122 void MathMacro::Write(std::ostream & os, bool fragile) const
125 for (int i = 0; i < nargs(); ++i) {
127 cell(i).Write(os, fragile);
135 void MathMacro::WriteNormal(std::ostream & os) const
137 os << "[macro " << name_ << " ";
138 for (int i = 0; i < nargs(); ++i) {
139 cell(i).WriteNormal(os);
146 bool MathMacro::idxUp(int & idx, int & pos) const
148 return MathInset::idxLeft(idx, pos);
152 bool MathMacro::idxDown(int & idx, int & pos) const
154 return MathInset::idxRight(idx, pos);
158 bool MathMacro::idxLeft(int &, int &) const
164 bool MathMacro::idxRight(int &, int &) const