X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_macrotemplate.C;h=22186a52ee52fff2d81898f46f026ba39f9957a9;hb=46880e2b9b49632c56bab2377ce9a3c826cf8d1d;hp=7f0589e666ab60fc4c32af0dabfbaf577751a0fa;hpb=04a2d2ddb034ebc2eaff92997d078eee5d5805fe;p=lyx.git diff --git a/src/mathed/math_macrotemplate.C b/src/mathed/math_macrotemplate.C index 7f0589e666..22186a52ee 100644 --- a/src/mathed/math_macrotemplate.C +++ b/src/mathed/math_macrotemplate.C @@ -1,180 +1,90 @@ -#include +#ifdef __GNUG__ +#pragma implementation +#endif #include "math_macrotemplate.h" -#include "math_macro.h" -#include "macro_support.h" -#include "support/LOstream.h" +#include "math_mathmlstream.h" +#include "frontends/Painter.h" +#include "debug.h" -using std::ostream; - -void MathMacroTemplate::setTCode(MathedTextCodes t) -{ - tcode_ = t; -} - - -MathedTextCodes MathMacroTemplate::getTCode() const -{ - return tcode_; -} - - -int MathMacroTemplate::getNoArgs() const -{ - return nargs_; -} - - -MathMacroTemplate::MathMacroTemplate(string const & nm, int na, int flg): - MathParInset(LM_ST_TEXT, nm, LM_OT_MACRO), - flags_(flg), nargs_(na) -{ - if (nargs_ > 0) { - tcode_ = LM_TC_ACTIVE_INSET; - args_.resize(nargs_); - for (int i = 0; i < nargs_; ++i) { - args_[i].setNumber(i + 1); - } - } else { - tcode_ = LM_TC_INSET; - // Here is nargs != args_.size() - //args = 0; - } -} - - -MathMacroTemplate::~MathMacroTemplate() +MathMacroTemplate::MathMacroTemplate() + : MathNestInset(2), numargs_(0), name_() {} -void MathMacroTemplate::setEditMode(bool ed) +MathMacroTemplate::MathMacroTemplate(string const & nm, int numargs) + : MathNestInset(2), numargs_(numargs), name_(nm) { - if (ed) { - flags_ |= MMF_Edit; - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(false); - } - } - else { - flags_ &= ~MMF_Edit; - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(true); - } - } -} - - -void MathMacroTemplate::draw(Painter & pain, int x, int y) -{ - int x2; - int y2; - bool expnd = (nargs_ > 0) ? args_[0].getExpand(): false; - if (flags_ & MMF_Edit) { - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(false); - } - x2 = x; - y2 = y; - } else { - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(true); - } - x2 = xo(); - y2 = yo(); - } - MathParInset::draw(pain, x, y); - xo(x2); - yo(y2); - - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(expnd); - } + if (numargs_ > 9) + lyxerr << "MathMacroTemplate::MathMacroTemplate: wrong # of arguments: " + << numargs_ << std::endl; } -void MathMacroTemplate::Metrics() +MathInset * MathMacroTemplate::clone() const { - bool expnd = (nargs_ > 0) ? args_[0].getExpand(): false; - - if (flags_ & MMF_Edit) { - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(false); - } - } else { - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(true); - } - } - MathParInset::Metrics(); - - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(expnd); - } + //lyxerr << "cloning MacroTemplate!\n"; + return new MathMacroTemplate(*this); } -void MathMacroTemplate::update(MathMacro * macro) +int MathMacroTemplate::numargs() const { - int idx = (macro) ? macro->getArgumentIdx() : 0; - for (int i = 0; i < nargs_; ++i) { - if (macro) { - macro->setArgumentIdx(i); - args_[i].setData(macro->GetData()); - MathedRowSt * row = macro->getRowSt(); - args_[i].setRowSt(row); - } - } - if (macro) - macro->setArgumentIdx(idx); + return numargs_; } -void MathMacroTemplate::WriteDef(ostream & os, bool fragile) +void MathMacroTemplate::numargs(int numargs) { - os << "\n\\newcommand{\\" << name << "}"; - - if (nargs_ > 0 ) - os << "[" << nargs_ << "]"; - - os << "{"; - - for (int i = 0; i < nargs_; ++i) { - args_[i].setExpand(false); - } - Write(os, fragile); - os << "}\n"; + numargs_ = numargs; } -void MathMacroTemplate::setArgument(MathedArray * a, int i) +string const & MathMacroTemplate::name() const { - args_[i].setData(*a); + return name_; } -void MathMacroTemplate::GetMacroXY(int i, int & x, int & y) const +void MathMacroTemplate::metrics(MathMetricsInfo & mi) const { - args_[i].GetXY(x, y); + xcell(0).metrics(mi); + xcell(1).metrics(mi); + width_ = xcell(0).width() + xcell(1).width() + 10; + ascent_ = std::max(xcell(0).ascent(), xcell(1).ascent()) + 2; + descent_ = std::max(xcell(0).descent(), xcell(1).descent()) + 2; } -MathParInset * MathMacroTemplate::getMacroPar(int i) const +void MathMacroTemplate::draw(MathPainterInfo & pi, int x, int y) const { - if (i >= 0 && i < nargs_) - return const_cast - (static_cast(&args_[i])); - else - return 0; + int const w0 = xcell(0).width(); + int const w1 = xcell(1).width(); + xcell(0).draw(pi, x + 2, y + 1); + pi.pain.rectangle(x, y - ascent() + 1, w0 + 4, height(), + LColor::blue); + xcell(1).draw(pi, x + 8 + w0, y + 1); + pi.pain.rectangle(x + w0 + 6 , y - ascent() + 1, w1 + 4, + height(), LColor::blue); } -void MathMacroTemplate::SetMacroFocus(int &idx, int x, int y) +void MathMacroTemplate::write(WriteStream & os) const { - for (int i = 0; i < nargs_; ++i) { - if (args_[i].Inside(x, y)) { - idx = i; - break; - } + if (os.latex()) { + os << "\n\\newcommand{\\" << name_.c_str() << '}'; + if (numargs_ > 0) + os << '[' << numargs_ << ']'; + os << '{' << cell(0) << "}\n"; + } else { + // writing .lyx + os << "\n\\newcommand{\\" << name_.c_str() << '}'; + if (numargs_ > 0) + os << '[' << numargs_ << ']'; + os << '{' << cell(0) << '}'; + // write special .tex export only if necessary + if (!cell(1).empty()) + os << "\n{" << cell(1) << '}'; } }