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, Mathed & 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 "math_iter.h"
26 #include "math_inset.h"
27 #include "math_accentinset.h"
28 #include "math_deliminset.h"
29 #include "math_fracinset.h"
30 #include "math_rowst.h"
31 #include "support/lstrings.h"
33 #include "mathed/support.h"
34 #include "math_macrotemplate.h"
35 #include "macro_support.h"
40 ostream & operator<<(ostream & o, MathedTextCodes mtc)
46 MathMacro::MathMacro(MathMacroTemplate const & t)
47 : MathParInset(LM_ST_TEXT, t.GetName(), LM_OT_MACRO),
48 tmplate_(const_cast<MathMacroTemplate *>(&t)),
50 idx_(t.nargs() ? 0 : -1)
52 array = tmplate_->GetData();
53 for (int i = 0; i < nargs(); ++i)
54 args_[i].reset(new MathParInset);
58 MathedInset * MathMacro::Clone()
60 return new MathMacro(*this);
64 void MathMacro::expand()
66 expanded_.reset(static_cast<MathParInset *>(tmplate_->Clone()));
67 expanded_->substitute(this);
72 MathParInset const * MathMacro::arg(int i) const
74 if (i < 0 || i >= nargs()) {
75 lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
79 return i >= 0 ? args_[i].get() : static_cast<MathParInset const *>(this);
83 MathParInset * MathMacro::arg(int i)
85 if (i < 0 || i >= nargs()) {
86 lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
90 return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
94 MathMacroTemplate * MathMacro::tmplate() const
96 return const_cast<MathMacroTemplate *>(tmplate_);
100 extern bool is_mathcursor_inside(MathParInset *);
102 void MathMacro::Metrics()
104 if (is_mathcursor_inside(this)) {
106 width = tmplate_->Width() + 4;
107 ascent = tmplate_->Ascent() + 2;
108 descent = tmplate_->Descent() + 2;
110 width += mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
112 for (int i = 0; i < nargs(); ++i) {
113 MathParInset * p = arg(i);
115 if (p->Width() + 30 > width)
116 width = p->Width() + 30;
117 descent += p->Height() + 10;
121 expanded_->Metrics();
122 width = expanded_->Width() + 6;
123 ascent = expanded_->Ascent() + 3;
124 descent = expanded_->Descent() + 3;
129 void MathMacro::draw(Painter & pain, int x, int y)
133 if (is_mathcursor_inside(this)) {
134 int h = y + Descent() - 2;
135 for (int i = nargs() - 1; i >= 0; --i) {
136 MathParInset * p = arg(i);
137 h -= p->Descent() + 5;
138 p->draw(pain, x + 30, h);
141 drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
142 h -= p->Ascent() + 5;
145 h -= tmplate_->Descent();
146 int w = mathed_string_width(LM_TC_TEXTRM, size(), GetName());
147 drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, GetName());
148 tmplate_->draw(pain, x + w + 12, h);
152 expanded_->draw(pain, x + 3, y);
159 pain.rectangle(x + 1, y - a + 1, w - 2, h - 2, col);
163 bool MathMacro::setArgumentIdx(int i)
165 if (i >= 0 && 0 < (nargs() - i)) {
175 int MathMacro::getArgumentIdx() const
177 //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
182 int MathMacro::getMaxArgumentIdx() const
189 int MathMacro::nargs() const
195 MathedArray & MathMacro::GetData()
197 //lyxerr << "MathMacro::GetData: " << *this << endl;
198 return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
202 MathedArray const & MathMacro::GetData() const
204 //lyxerr << "MathMacro::GetData: " << *this << endl;
205 return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData();
209 int MathMacro::GetColumns() const
211 return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
215 void MathMacro::GetXY(int & x, int & y) const
218 arg(idx_)->GetXY(x, y);
220 MathParInset::GetXY(x, y);
224 bool MathMacro::Permit(short f) const
226 return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
230 void MathMacro::SetFocus(int x, int y)
233 for (int i = 0; i < nargs(); ++i) {
234 if (arg(i)->Inside(x, y)) {
241 void MathMacro::setData(MathedArray const & a, int i)
247 void MathMacro::setData(MathedArray const & a)
250 arg(idx_)->setData(a);
256 MathedTextCodes MathMacro::getTCode() const
258 return nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
259 //return LM_TC_INSET;
262 void MathMacro::dump(ostream & os) const
264 os << "\n macro: '" << this << "'\n";
265 os << " name: '" << name << "'\n";
266 os << " idx: '" << idx_ << "'\n";
267 os << " data: '" << array << "'\n";
268 os << " nargs: '" << nargs() << "'\n";
269 for (int i = 0; i < nargs(); ++i)
270 os << " " << arg(i) << ": " << arg(i)->GetData() << endl;
274 void MathMacro::Write(ostream & os, bool fragile)
277 for (int i = 0; i < nargs(); ++i) {
279 arg(i)->Write(os, fragile);
287 void MathMacro::WriteNormal(ostream & os)
289 os << "[macro " << name << " ";
290 for (int i = 0; i < nargs(); ++i) {
291 arg(i)->WriteNormal(os);