X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fmathed%2Fmath_macro.C;h=9983cf5f78b523af9c7425a1a17867bf75460d45;hb=d359dd8fca52c4f0100f7cf4bf636113c5c4e49f;hp=632c3d76763742c3da7d4da66cc00912bf5ce817;hpb=26428cdf19cf41b4c07ed47c0058af863faacb1e;p=lyx.git diff --git a/src/mathed/math_macro.C b/src/mathed/math_macro.C index 632c3d7676..9983cf5f78 100644 --- a/src/mathed/math_macro.C +++ b/src/mathed/math_macro.C @@ -1,239 +1,243 @@ -// -*- C++ -*- /* * File: math_macro.C - * Purpose: Implementation of macro class for mathed - * Author: Alejandro Aguilar Sierra + * Purpose: Implementation of macro class for mathed + * Author: Alejandro Aguilar Sierra * Created: November 1996 * Description: WYSIWYG math macros * - * Dependencies: Mathed + * Dependencies: Math * * Copyright: 1996, 1997 Alejandro Aguilar Sierra * - * Version: 0.2, Mathed & Lyx project. + * Version: 0.2, Math & Lyx project. * * This code is under the GNU General Public Licence version 2 or later. */ -#include -#include FORMS_H_LOCATION - #ifdef __GNUG__ #pragma implementation #endif -#include "LString.h" #include "math_macro.h" -#include "array.h" -#include "math_iter.h" -#include "math_inset.h" -#include "math_accentinset.h" -#include "math_deliminset.h" -#include "math_fracinset.h" -#include "math_rowst.h" +#include "math_support.h" +#include "math_extern.h" +#include "math_macrotable.h" +#include "math_macrotemplate.h" +#include "math_mathmlstream.h" +#include "math_streamstr.h" #include "support/lstrings.h" +#include "support/LAssert.h" #include "debug.h" -#include "mathed/support.h" -#include "math_macrotemplate.h" -#include "macro_support.h" +#include "LaTeXFeatures.h" -using std::ostream; -using std::endl; -ostream & operator<<(ostream & o, MathedTextCodes mtc) -{ - return o << int(mtc); -} +using std::max; -//extern int mathed_string_width(short type, int style, string const & s); -//extern int mathed_string_height(short, int, string const &, int &, int &); +MathMacro::MathMacro(string const & name) + : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()), + tmplate_(MathMacroTable::provide(name)) +{} -MathMacro::MathMacro(MathMacroTemplate * t): - MathParInset(LM_ST_TEXT, "", LM_OT_MACRO), tmplate(t) -{ - nargs = tmplate->getNoArgs(); - tcode = tmplate->getTCode(); - args_.resize(nargs); - for (int i = 0; i < nargs; ++i) { - args_[i].row = 0; - } - idx = 0; - SetName(tmplate->GetName()); -} + +MathMacro::MathMacro(MathMacro const & m) + : MathNestInset(m), + tmplate_(m.tmplate_) // don't copy 'expanded_'! +{} -MathMacro::MathMacro(MathMacro * m): - MathParInset(LM_ST_TEXT, m->GetName(), LM_OT_MACRO) + +MathInset * MathMacro::clone() const { - tmplate = m->tmplate; - nargs = tmplate->getNoArgs(); - tcode = tmplate->getTCode(); - args_.resize(nargs); - idx = 0; - SetName(tmplate->GetName()); - for (int i = 0; i < tmplate->nargs; ++i) { - m->setArgumentIdx(i); - MathedIter it(m->GetData()); - args_[i].row = m->args_[i].row; - args_[i].array = it.Copy(); - } + return new MathMacro(*this); } -MathMacro::~MathMacro() + +string const & MathMacro::name() const { - for (idx = 0; idx < nargs; ++idx) { - MathedIter it(args_[idx].array); - it.Clear(); - delete args_[idx].row; - } + return tmplate_->asMacroTemplate()->name(); } -MathedInset * MathMacro::Clone() +bool MathMacro::defining() const { - return new MathMacro(this); + return 0; + //return mathcursor && mathcursor->formula()->getInsetName() == name(); } -void MathMacro::Metrics() +void MathMacro::expand() const { - if (nargs > 0) - tmplate->update(this); - tmplate->SetStyle(size); - tmplate->Metrics(); - width = tmplate->Width(); - ascent = tmplate->Ascent(); - descent = tmplate->Descent(); + expanded_ = tmplate_->xcell(tmplate_->cell(1).empty() ? 0 : 1); } -void MathMacro::draw(Painter & pain, int x, int y) +void MathMacro::metrics(MathMetricsInfo & mi) const { - xo = x; yo = y; - Metrics(); - tmplate->update(this); - tmplate->SetStyle(size); - tmplate->draw(pain, x, y); - for (int i = 0; i < nargs; ++i) { - tmplate->GetMacroXY(i, args_[i].x, args_[i].y); + augmentFont(font_, "lyxtex"); + mi_ = mi; + + if (defining()) { + mathed_string_dim(font_, name(), ascent_, descent_, width_); + return; + } + + if (editing()) { + expand(); + expanded_.metrics(mi_); + width_ = expanded_.width() + 4; + ascent_ = expanded_.ascent() + 2; + descent_ = expanded_.descent() + 2; + + width_ += mathed_string_width(font_, name()) + 10; + + int lasc; + int ldes; + int lwid; + mathed_string_dim(font_, "#1: ", lasc, ldes, lwid); + + for (idx_type i = 0; i < nargs(); ++i) { + MathXArray const & c = xcell(i); + c.metrics(mi_); + width_ = max(width_, c.width() + lwid); + descent_ += max(c.ascent(), lasc) + 5; + descent_ += max(c.descent(), ldes) + 5; + } + return; } + + expand(); + expanded_.data().substitute(*this); + expanded_.metrics(mi_); + width_ = expanded_.width(); + ascent_ = expanded_.ascent(); + descent_ = expanded_.descent(); } -bool MathMacro::setArgumentIdx(int i) +void MathMacro::draw(MathPainterInfo & pi, int x, int y) const { - if (i >= 0 && i < nargs) { - idx = i; - return true; - } else - return false; -} + metrics(mi_); + LyXFont texfont; + augmentFont(texfont, "lyxtex"); -int MathMacro::getArgumentIdx() const -{ - return idx; + if (defining()) { + drawStr(pi, texfont, x, y, name()); + return; + } + + if (editing()) { + int h = y - ascent() + 2 + expanded_.ascent(); + drawStr(pi, font_, x + 3, h, name()); + + int const w = mathed_string_width(font_, name()); + expanded_.draw(pi, x + w + 12, h); + h += expanded_.descent(); + + int lasc; + int ldes; + int lwid; + mathed_string_dim(font_, "#1: ", lasc, ldes, lwid); + + for (idx_type i = 0; i < nargs(); ++i) { + MathXArray const & c = xcell(i); + h += max(c.ascent(), lasc) + 5; + c.draw(pi, x + lwid, h); + char str[] = "#1:"; + str[1] += static_cast(i); + drawStr(pi, texfont, x + 3, h, str); + h += max(c.descent(), ldes) + 5; + } + return; + } + + expanded_.draw(pi, x, y); } -int MathMacro::getMaxArgumentIdx() const -{ - return nargs - 1; -} +void MathMacro::dump() const +{ + MathMacroTable::dump(); + lyxerr << "\n macro: '" << this << "'\n"; + lyxerr << " name: '" << name() << "'\n"; + lyxerr << " template: '"; + WriteStream wi(lyxerr); + tmplate_->write(wi); + lyxerr << "'\n"; +} -MathedArray * MathMacro::GetData() -{ - return args_[idx].array; -} +bool MathMacro::idxUpDown(idx_type & idx, bool up) const +{ + pos_type pos; + return + up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos); +} -int MathMacro::GetColumns() const +bool MathMacro::idxLeft(idx_type &, pos_type &) const { - return tmplate->getMacroPar(idx)->GetColumns(); + return false; } -void MathMacro::GetXY(int & x, int & y) const +bool MathMacro::idxRight(idx_type &, pos_type &) const { - x = args_[idx].x; y = args_[idx].y; + return false; } -bool MathMacro::Permit(short f) const +void MathMacro::validate(LaTeXFeatures & features) const { - return (nargs > 0) ? - tmplate->getMacroPar(idx)->Permit(f) : MathParInset::Permit(f); + if (name() == "binom" || name() == "mathcircumflex") + features.require(name()); + //MathInset::validate(features); } -void MathMacro::SetFocus(int x, int y) +void MathMacro::maplize(MapleStream & os) const { - tmplate->update(this); - tmplate->SetMacroFocus(idx, x, y); + updateExpansion(); + ::maplize(expanded_.data(), os); } -void MathMacro::SetData(MathedArray * a) +void MathMacro::mathmlize(MathMLStream & os) const { - args_[idx].array = a; + updateExpansion(); + ::mathmlize(expanded_.data(), os); } +void MathMacro::octavize(OctaveStream & os) const +{ + updateExpansion(); + ::octavize(expanded_.data(), os); +} -MathedRowSt * MathMacro::getRowSt() const +void MathMacro::normalize(NormalStream & os) const { - return args_[idx].row; + os << "[macro " << name() << " "; + for (idx_type i = 0; i < nargs(); ++i) + os << cell(i) << ' '; + os << ']'; } -MathedTextCodes MathMacro::getTCode() const +void MathMacro::write(WriteStream & os) const { - return tcode; + os << '\\' << name(); + for (idx_type i = 0; i < nargs(); ++i) + os << '{' << cell(i) << '}'; + if (nargs() == 0) + os << ' '; } - -void MathMacro::Write(ostream & os, bool fragile) + +void MathMacro::updateExpansion() const { - if (tmplate->flags & MMF_Exp) { - lyxerr[Debug::MATHED] << "Expand " << tmplate->flags - << ' ' << MMF_Exp << endl; - tmplate->update(this); - tmplate->Write(os, fragile); - } else { - if (tmplate->flags & MMF_Env) { - os << "\\begin{" - << name - << "} "; - } else { - os << '\\' << name; - } -// if (options) { -// file += '['; -// file += options; -// file += ']'; -// } - - if (!(tmplate->flags & MMF_Env) && nargs > 0) - os << '{'; - - for (int i = 0; i < nargs; ++i) { - array = args_[i].array; - MathParInset::Write(os, fragile); - if (i < nargs - 1) - os << "}{"; - } - if (tmplate->flags & MMF_Env) { - os << "\\end{" - << name - << '}'; - } else { - if (nargs > 0) - os << '}'; - else - os << ' '; - } - } + expand(); + expanded_.data().substitute(*this); }