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)),
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;
81 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;
92 return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
96 MathMacroTemplate * MathMacro::tmplate() const
98 return const_cast<MathMacroTemplate *>(tmplate_);
102 extern bool is_mathcursor_inside(MathParInset *);
105 void MathMacro::Metrics()
108 if (is_mathcursor_inside(this)) {
111 width = tmplate_->Width() + 4;
112 ascent = tmplate_->Ascent() + 2;
113 descent = tmplate_->Descent() + 2;
115 width += mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
117 for (int i = 0; i < nargs(); ++i) {
118 MathParInset * p = arg(i);
120 if (p->Width() + 30 > width)
121 width = p->Width() + 30;
122 descent += p->Height() + 10;
126 expanded_->Metrics();
127 width = expanded_->Width() + 4;
128 ascent = expanded_->Ascent() + 2;
129 descent = expanded_->Descent() + 2;
135 void MathMacro::draw(Painter & pain, int x, int y)
139 if (is_mathcursor_inside(this)) {
140 int h = y + Descent() - 2;
141 for (int i = nargs() - 1; i >= 0; --i) {
142 MathParInset * p = arg(i);
143 h -= p->Descent() + 5;
144 p->draw(pain, x + 30, h);
147 drawStr(pain, LM_TC_TEX, size(), x + 1, h, str);
148 h -= p->Ascent() + 5;
151 h -= tmplate_->Descent();
152 int w = mathed_string_width(LM_TC_TEXTRM, size(), GetName());
153 drawStr(pain, LM_TC_TEXTRM, size(), x + 2, h, GetName());
154 tmplate_->draw(pain, x + w + 12, h);
158 expanded_->draw(pain, x + 2, y - 1);
165 pain.rectangle(x, y - a, w, h, col);
169 bool MathMacro::setArgumentIdx(int i)
171 if (i >= 0 && 0 < (nargs() - i)) {
181 int MathMacro::getArgumentIdx() const
183 //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
188 int MathMacro::getMaxArgumentIdx() const
195 int MathMacro::nargs() const
201 MathedArray & MathMacro::GetData()
203 //lyxerr << "MathMacro::GetData: " << *this << endl;
204 return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
208 MathedArray const & MathMacro::GetData() const
210 //lyxerr << "MathMacro::GetData: " << *this << endl;
211 return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
215 int MathMacro::GetColumns() const
217 return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
221 void MathMacro::GetXY(int & x, int & y) const
224 arg(idx_)->GetXY(x, y);
226 MathParInset::GetXY(x, y);
230 bool MathMacro::Permit(short f) const
232 return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
236 void MathMacro::SetFocus(int x, int y)
239 for (int i = 0; i < nargs(); ++i) {
240 if (arg(i)->Inside(x, y)) {
247 void MathMacro::setData(MathedArray const & a, int i)
253 void MathMacro::setData(MathedArray const & a)
256 arg(idx_)->setData(a);
262 MathedTextCodes MathMacro::getTCode() const
264 return nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
265 //return LM_TC_INSET;
268 void MathMacro::dump(ostream & os) const
270 os << "\n macro: '" << this << "'\n";
271 os << " name: '" << name << "'\n";
272 os << " idx: '" << idx_ << "'\n";
273 os << " data: '" << array << "'\n";
274 os << " nargs: '" << nargs() << "'\n";
275 for (int i = 0; i < nargs(); ++i)
276 os << " " << arg(i) << ": " << arg(i)->GetData() << endl;
280 void MathMacro::Write(ostream & os, bool fragile)
283 for (int i = 0; i < nargs(); ++i) {
285 arg(i)->Write(os, fragile);