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"
37 MathMacro::MathMacro(MathMacroTemplate const & t)
38 : MathInset(t.name(), LM_OT_MACRO, t.numargs()), tmplate_(&t)
42 MathInset * MathMacro::Clone() const
44 return new MathMacro(*this);
48 void MathMacro::Metrics(MathStyles st)
50 if (mathcursor && mathcursor->isInside(this)) {
51 expanded_ = tmplate_->xcell(0);
52 expanded_.Metrics(st);
54 width_ = expanded_.width() + 4;
55 ascent_ = expanded_.ascent() + 2;
56 descent_ = expanded_.descent() + 2;
58 width_ += mathed_string_width(LM_TC_TEXTRM, size_, name_) + 10;
60 for (int i = 0; i < nargs(); ++i) {
61 MathXArray & c = xcell(i);
63 width_ = std::max(width_, c.width() + 30);
64 descent_ += c.height() + 10;
67 expanded_ = tmplate_->xcell(0);
68 expanded_.data_.substitute(*this);
69 expanded_.Metrics(st);
71 width_ = expanded_.width() + 6;
72 ascent_ = expanded_.ascent() + 3;
73 descent_ = expanded_.descent() + 3;
78 void MathMacro::draw(Painter & pain, int x, int y)
85 if (mathcursor && mathcursor->isInside(this)) {
87 int h = y - ascent() + 2 + expanded_.ascent();
88 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
90 int w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
91 expanded_.draw(pain, x + w + 12, h);
92 h += expanded_.descent();
94 for (int i = 0; i < nargs(); ++i) {
95 MathXArray & c = xcell(i);
97 c.draw(pain, x + 30, h);
100 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
101 h += c.descent() + 5;
105 expanded_.draw(pain, x + 3, y);
109 pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
113 void MathMacro::dump(ostream & os) const
115 MathMacroTable::dump();
116 os << "\n macro: '" << this << "'\n";
117 os << " name: '" << name_ << "'\n";
118 os << " template: '" << tmplate_ << "'\n";
119 os << " template: '" << *tmplate_ << "'\n";
123 void MathMacro::Write(ostream & os, bool fragile) const
126 for (int i = 0; i < nargs(); ++i) {
128 cell(i).Write(os, fragile);
136 void MathMacro::WriteNormal(ostream & os) const
138 os << "[macro " << name_ << " ";
139 for (int i = 0; i < nargs(); ++i) {
140 cell(i).WriteNormal(os);
147 bool MathMacro::idxUp(int & idx, int & pos) const
149 return MathInset::idxLeft(idx, pos);
153 bool MathMacro::idxDown(int & idx, int & pos) const
155 return MathInset::idxRight(idx, pos);
159 bool MathMacro::idxLeft(int &, int &) const
165 bool MathMacro::idxRight(int &, int &) const