]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.C
remove unneeded member
[lyx.git] / src / mathed / math_macro.C
index 22ba0d5365c3b1783b1c2bad8344665e85871d71..9983cf5f78b523af9c7425a1a17867bf75460d45 100644 (file)
-// -*- C++ -*-
 /*
  *  File:        math_macro.C
- *  Purpose:     Implementation of macro class for mathed 
- *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
+ *  Purpose:     Implementation of macro class for mathed
+ *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx>
  *  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 <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
 #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 "Painter.h"
+#include "LaTeXFeatures.h"
 
-using namespace std;
 
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
-       return o << int(mtc);
-}
+using std::max;
 
 
-MathMacro::MathMacro(MathMacroTemplate const & t)
-       : MathParInset(LM_ST_TEXT, t.GetName(), LM_OT_MACRO),
-         tmplate_(const_cast<MathMacroTemplate *>(&t)),
-               args_(t.nargs()),
-               idx_(-1)
-{
-       array = tmplate_->GetData();
-       for (int i = 0; i < nargs(); ++i) 
-               args_[i].reset(new MathParInset);
-}
+MathMacro::MathMacro(string const & name)
+       : MathNestInset(MathMacroTable::provide(name)->asMacroTemplate()->numargs()),
+               tmplate_(MathMacroTable::provide(name))
+{}
 
 
-MathedInset * MathMacro::Clone()
+MathMacro::MathMacro(MathMacro const & m)
+       : MathNestInset(m),
+               tmplate_(m.tmplate_) // don't copy 'expanded_'!
+{}
+
+
+
+MathInset * MathMacro::clone() const
 {
        return new MathMacro(*this);
 }
 
 
-void MathMacro::expand()
+string const & MathMacro::name() const
 {
-       expanded_.reset(static_cast<MathParInset *>(tmplate_->Clone()));
-       expanded_->substitute(this);
+       return tmplate_->asMacroTemplate()->name();
 }
 
 
-
-MathParInset const * MathMacro::arg(int i) const
+bool MathMacro::defining() const
 {
-       if (i < 0 || i >= nargs()) {
-               lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
-               lyx::Assert(0);
-               return 0;
-       }
-
-       return i >= 0 ? args_[i].get() : static_cast<MathParInset const *>(this);
+       return 0;
+       //return mathcursor && mathcursor->formula()->getInsetName() == name();
 }
 
-MathParInset * MathMacro::arg(int i) 
-{
-       if (i < 0 || i >= nargs()) {
-               lyxerr << "Illegal index " << i << " max: " << nargs() << endl;
-               lyx::Assert(0);
-               return 0;
-       }
 
-       return i >= 0 ? args_[i].get() : static_cast<MathParInset *>(this);
+void MathMacro::expand() const
+{
+       expanded_ = tmplate_->xcell(tmplate_->cell(1).empty() ? 0 : 1);
 }
 
 
-MathMacroTemplate * MathMacro::tmplate() const
+void MathMacro::metrics(MathMetricsInfo & mi) const
 {
-       return const_cast<MathMacroTemplate *>(tmplate_);
-}
+       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;
+       }
 
-extern bool is_mathcursor_inside(MathParInset *);
+       expand();
+       expanded_.data().substitute(*this);
+       expanded_.metrics(mi_);
+       width_   = expanded_.width();
+       ascent_  = expanded_.ascent();
+       descent_ = expanded_.descent();
+}
 
 
-void MathMacro::Metrics()
+void MathMacro::draw(MathPainterInfo & pi, int x, int y) const
 {
+       metrics(mi_);
 
-       if (is_mathcursor_inside(this)) {
-
-               tmplate_->Metrics();
-               width   = tmplate_->Width()   + 4;
-               ascent  = tmplate_->Ascent()  + 2;
-               descent = tmplate_->Descent() + 2;
+       LyXFont texfont;
+       augmentFont(texfont, "lyxtex");
 
-               width +=  mathed_string_width(LM_TC_TEXTRM, size(), GetName()) + 10;
+       if (defining()) {
+               drawStr(pi, texfont, x, y, name());
+               return;
+       }
 
-               for (int i = 0; i < nargs(); ++i) {
-                       MathParInset * p = arg(i);
-                       p->Metrics();
-                       if (p->Width() + 30 > width) 
-                               width = p->Width() + 30;
-                       descent += p->Height() + 10;
-               }
-       } else {
-               expand();
-               expanded_->Metrics();
-               width   = expanded_->Width()   + 4;
-               ascent  = expanded_->Ascent()  + 2;
-               descent = expanded_->Descent() + 2;
+       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);
 
-void MathMacro::draw(Painter & pain, int x, int y)
-{
-       LColor::color col;
-
-       if (is_mathcursor_inside(this)) {
-               int h = y + Descent() - 2;
-               for (int i = nargs() - 1; i >= 0; --i) {
-                       MathParInset * p = arg(i);
-                       h -= p->Descent() + 5;
-                       p->draw(pain, x + 30, h);
+               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] += i;
-                       drawStr(pain, LM_TC_TEX, size(), x + 1, h, str);
-                       h -= p->Ascent() + 5;
+                       str[1] += static_cast<char>(i);
+                       drawStr(pi, texfont, x + 3, h, str);
+                       h += max(c.descent(), ldes) + 5;
                }
-
-               h -= tmplate_->Descent();
-               int w =  mathed_string_width(LM_TC_TEXTRM, size(), GetName());
-               drawStr(pain, LM_TC_TEXTRM, size(), x + 2, h, GetName());
-               tmplate_->draw(pain, x + w + 12, h);
-
-               col = LColor::red;
-       } else {
-               expanded_->draw(pain, x + 2, y - 1);
-               col = LColor::black;
+               return;
        }
 
-       int w = Width();
-       int a = Ascent();
-       int h = Height();
-       pain.rectangle(x, y - a, w, h, col);
+       expanded_.draw(pi, x, y);
 }
 
 
-bool MathMacro::setArgumentIdx(int i)
+void MathMacro::dump() const
 {
-       if (i >= 0 && 0 < (nargs() - i)) {
-               idx_ = i;
-               return true;
-       } else
-               return false;
-       idx_ = i;
-       return true;
+       MathMacroTable::dump();
+       lyxerr << "\n macro: '" << this << "'\n";
+       lyxerr << " name: '" << name() << "'\n";
+       lyxerr << " template: '";
+       WriteStream wi(lyxerr);
+       tmplate_->write(wi);
+       lyxerr << "'\n";
 }
 
 
-int MathMacro::getArgumentIdx() const 
-{ 
-       //lyxerr << "MathMacro::getArgumentIdx: res: " << idx_ << endl;
-       return idx_;
+bool MathMacro::idxUpDown(idx_type & idx, bool up) const
+{
+       pos_type pos;
+       return
+               up ? MathNestInset::idxLeft(idx, pos) : MathNestInset::idxRight(idx, pos);
 }
 
 
-int MathMacro::getMaxArgumentIdx() const 
-{ 
-       return nargs() - 1;
+bool MathMacro::idxLeft(idx_type &, pos_type &) const
+{
+       return false;
 }
 
 
-
-int MathMacro::nargs() const 
-{ 
-       return args_.size();
-} 
-
-
-MathedArray & MathMacro::GetData() 
-{ 
-       //lyxerr << "MathMacro::GetData: " << *this << endl;
-       return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData(); 
-} 
-
-
-MathedArray const & MathMacro::GetData() const
-{ 
-       //lyxerr << "MathMacro::GetData: " << *this << endl;
-       return idx_ >= 0 ? arg(idx_)->GetData() : MathParInset::GetData(); 
-} 
-
-
-int MathMacro::GetColumns() const
+bool MathMacro::idxRight(idx_type &, pos_type &) const
 {
-       return idx_ >= 0 ? arg(idx_)->GetColumns() : MathParInset::GetColumns();
+       return false;
 }
 
 
-void MathMacro::GetXY(int & x, int & y) const
+void MathMacro::validate(LaTeXFeatures & features) const
 {
-       if (idx_ >= 0)
-               arg(idx_)->GetXY(x, y);
-       else
-               MathParInset::GetXY(x, y);
+       if (name() == "binom" || name() == "mathcircumflex")
+               features.require(name());
+       //MathInset::validate(features);
 }
 
 
-bool MathMacro::Permit(short f) const
+void MathMacro::maplize(MapleStream & os) const
 {
-       return idx_ >= 0 ? arg(idx_)->Permit(f) : MathParInset::Permit(f);
+       updateExpansion();
+       ::maplize(expanded_.data(), os);
 }
 
 
-void MathMacro::SetFocus(int x, int y)
+void MathMacro::mathmlize(MathMLStream & os) const
 {
-       idx_ = -1;
-       for (int i = 0; i < nargs(); ++i) {
-               if (arg(i)->Inside(x, y)) {
-                       idx_ = i;
-                       break;
-               }
-       }
+       updateExpansion();
+       ::mathmlize(expanded_.data(), os);
 }
 
-void MathMacro::setData(MathedArray const & a, int i)
+
+void MathMacro::octavize(OctaveStream & os) const
 {
-       arg(i)->setData(a);
+       updateExpansion();
+       ::octavize(expanded_.data(), os);
 }
 
 
-void MathMacro::setData(MathedArray const & a)
+void MathMacro::normalize(NormalStream & os) const
 {
-       if (idx_ >= 0)
-               arg(idx_)->setData(a);
-       else
-               array = a;
+       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 nargs() ? LM_TC_ACTIVE_INSET : LM_TC_INSET;
-       //return LM_TC_INSET;
-}
-       
-void MathMacro::dump(ostream & os) const
-{
-       os << "\n macro: '" << this << "'\n";
-       os << " name: '" << name << "'\n";
-       os << " idx: '" << idx_ << "'\n";
-       os << " data: '" << array << "'\n";
-       os << " nargs: '" << nargs() << "'\n";
-       for (int i = 0; i < nargs(); ++i)
-               os << "  " << arg(i) << ": " << arg(i)->GetData() << endl;
-       os << endl;
+       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
 {
-       os << '\\' << name;
-       for (int i = 0; i < nargs(); ++i) {
-               os << '{';
-               arg(i)->Write(os, fragile);
-               os << '}';
-       }
-       if (nargs() == 0) 
-               os << ' ';
+       expand();
+       expanded_.data().substitute(*this);
 }