4 * Purpose: Implementation of macro class for mathed
5 * Author: Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
6 * Created: November 1996
7 * Description: WYSIWYG math macros
11 * Copyright: 1996, 1997 Alejandro Aguilar Sierra
13 * Version: 0.2, Mathed & Lyx project.
15 * This code is under the GNU General Public Licence version 2 or later.
21 #pragma implementation
24 #include "math_macro.h"
26 #include "math_iter.h"
27 #include "math_inset.h"
28 #include "math_accentinset.h"
29 #include "math_deliminset.h"
30 #include "math_fracinset.h"
31 #include "math_rowst.h"
32 #include "support/lstrings.h"
34 #include "mathed/support.h"
35 #include "math_macrotemplate.h"
36 #include "macro_support.h"
41 ostream & operator<<(ostream & o, MathedTextCodes mtc)
47 MathMacro::MathMacro(MathMacroTemplate const & t)
48 : MathParInset(LM_ST_TEXT, t.GetName(), LM_OT_MACRO),
49 tmplate_(const_cast<MathMacroTemplate *>(&t)),
51 idx_(t.nargs() ? 0 : -1)
53 array = tmplate_->GetData();
54 for (int i = 0; i < nargs(); ++i)
55 args_[i].reset(new MathParInset);
59 MathedInset * MathMacro::Clone()
61 return new MathMacro(*this);
65 void MathMacro::expand()
67 expanded_.reset(static_cast<MathParInset *>(tmplate_->Clone()));
68 expanded_->substitute(this);
73 MathParInset const * MathMacro::arg(int i) const
75 if (i < 0 || i >= nargs()) {
76 lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
80 return i >= 0 ? args_[i].get() : static_cast<MathParInset const *>(this);
84 MathParInset * MathMacro::arg(int i)
86 if (i < 0 || i >= nargs()) {
87 lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
91 return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
95 MathMacroTemplate * MathMacro::tmplate() const
97 return const_cast<MathMacroTemplate *>(tmplate_);
101 extern bool is_mathcursor_inside(MathParInset *);
103 void MathMacro::Metrics()
105 if (is_mathcursor_inside(this)) {
107 width = tmplate_->Width() + 4;
108 ascent = tmplate_->Ascent() + 2;
109 descent = tmplate_->Descent() + 2;
111 width += mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
113 for (int i = 0; i < nargs(); ++i) {
114 MathParInset * p = arg(i);
116 if (p->Width() + 30 > width)
117 width = p->Width() + 30;
118 descent += p->Height() + 10;
122 expanded_->Metrics();
123 width = expanded_->Width() + 6;
124 ascent = expanded_->Ascent() + 3;
125 descent = expanded_->Descent() + 3;
130 void MathMacro::draw(Painter & pain, int x, int y)
134 if (is_mathcursor_inside(this)) {
135 int h = y + Descent() - 2;
136 for (int i = nargs() - 1; i >= 0; --i) {
137 MathParInset * p = arg(i);
138 h -= p->Descent() + 5;
139 p->draw(pain, x + 30, h);
142 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
143 h -= p->Ascent() + 5;
146 h -= tmplate_->Descent();
147 int w = mathed_string_width(LM_TC_TEXTRM, size(), GetName());
148 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, GetName());
149 tmplate_->draw(pain, x + w + 12, h);
153 expanded_->draw(pain, x + 3, y);
160 pain.rectangle(x + 1, y - a + 1, w - 2, h - 2, col);
164 bool MathMacro::setArgumentIdx(int i)
166 if (i >= 0 && 0 < (nargs() - i)) {
176 int MathMacro::getArgumentIdx() const
178 //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
183 int MathMacro::getMaxArgumentIdx() const
190 int MathMacro::nargs() const
196 MathedArray & MathMacro::GetData()
198 //lyxerr << "MathMacro::GetData: " << *this << endl;
199 return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
203 MathedArray const & MathMacro::GetData() const
205 //lyxerr << "MathMacro::GetData: " << *this << endl;
206 return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
210 int MathMacro::GetColumns() const
212 return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
216 void MathMacro::GetXY(int & x, int & y) const
219 arg(idx_)->GetXY(x, y);
221 MathParInset::GetXY(x, y);
225 bool MathMacro::Permit(short f) const
227 return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
231 void MathMacro::SetFocus(int x, int y)
234 for (int i = 0; i < nargs(); ++i) {
235 if (arg(i)->Inside(x, y)) {
242 void MathMacro::setData(MathedArray const & a, int i)
248 void MathMacro::setData(MathedArray const & a)
251 arg(idx_)->setData(a);
257 MathedTextCodes MathMacro::getTCode() const
259 return nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
260 //return LM_TC_INSET;
263 void MathMacro::dump(ostream & os) const
265 os << "\n macro: '" << this << "'\n";
266 os << " name: '" << name << "'\n";
267 os << " idx: '" << idx_ << "'\n";
268 os << " data: '" << array << "'\n";
269 os << " nargs: '" << nargs() << "'\n";
270 for (int i = 0; i < nargs(); ++i)
271 os << " " << arg(i) << ": " << arg(i)->GetData() << endl;
275 void MathMacro::Write(ostream & os, bool fragile)
278 for (int i = 0; i < nargs(); ++i) {
280 arg(i)->Write(os, fragile);
288 void MathMacro::WriteNormal(ostream & os)
290 os << "{macro " << name << " ";
291 for (int i = 0; i < nargs(); ++i)
292 arg(i)->WriteNormal(os);