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 MathInset * MathMacro::clone() const
43 return new MathMacro(*this);
47 void MathMacro::Metrics(MathStyles st, int, int)
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;
62 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
64 for (int i = 0; i < nargs(); ++i) {
65 MathXArray & c = xcell(i);
67 width_ = std::max(width_, c.width() + lwid);
68 descent_ += std::max(c.ascent(), lasc) + 5;
69 descent_ += std::max(c.descent(), ldes) + 5;
72 expanded_ = tmplate_->xcell(0);
73 expanded_.data_.substitute(*this);
74 expanded_.Metrics(st);
76 width_ = expanded_.width() + 6;
77 ascent_ = expanded_.ascent() + 3;
78 descent_ = expanded_.descent() + 3;
83 void MathMacro::draw(Painter & pain, int x, int y)
90 if (mathcursor && mathcursor->isInside(this)) {
92 int h = y - ascent() + 2 + expanded_.ascent();
93 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
95 int const w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
96 expanded_.draw(pain, x + w + 12, h);
97 h += expanded_.descent();
102 mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
104 for (int i = 0; i < nargs(); ++i) {
105 MathXArray & c = xcell(i);
106 h += std::max(c.ascent(), lasc) + 5;
107 c.draw(pain, x + lwid, h);
110 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
111 h += std::max(c.descent(), ldes) + 5;
115 expanded_.draw(pain, x + 3, y);
119 pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
123 void MathMacro::dump(std::ostream & os) const
125 MathMacroTable::dump();
126 os << "\n macro: '" << this << "'\n";
127 os << " name: '" << name_ << "'\n";
128 os << " template: '" << tmplate_ << "'\n";
129 os << " template: '" << *tmplate_ << "'\n";
133 void MathMacro::Write(std::ostream & os, bool fragile) const
136 for (int i = 0; i < nargs(); ++i) {
138 cell(i).Write(os, fragile);
146 void MathMacro::WriteNormal(std::ostream & os) const
148 os << "[macro " << name_ << " ";
149 for (int i = 0; i < nargs(); ++i) {
150 cell(i).WriteNormal(os);
157 bool MathMacro::idxUp(int & idx, int & pos) const
159 return MathInset::idxLeft(idx, pos);
163 bool MathMacro::idxDown(int & idx, int & pos) const
165 return MathInset::idxRight(idx, pos);
169 bool MathMacro::idxLeft(int &, int &) const
175 bool MathMacro::idxRight(int &, int &) const