]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_macro.C
fix pullArg when pressing <Delete> at the end of an cell
[lyx.git] / src / mathed / math_macro.C
index eb83d1587af13808aa786611ccfc7ed4a084b478..5085e783bf864957aff6bea102d2e7df514b0865 100644 (file)
@@ -1,4 +1,3 @@
-// -*- C++ -*-
 /*
  *  File:        math_macro.C
  *  Purpose:     Implementation of macro class for mathed 
@@ -6,81 +5,83 @@
  *  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 "support/lstrings.h"
+#include "support/LAssert.h"
 #include "debug.h"
 #include "mathed/support.h"
+#include "mathed/math_cursor.h"
+#include "math_macrotable.h"
 #include "math_macrotemplate.h"
-#include "macro_support.h"
+#include "Painter.h"
+#include "LaTeXFeatures.h"
 
-using std::ostream;
 using std::endl;
 
-ostream & operator<<(ostream & o, MathedTextCodes mtc)
-{
-       return o << int(mtc);
-}
+MathMacro::MathMacro(MathMacroTemplate const & t)
+       : MathInset(t.numargs(), t.name(), LM_OT_MACRO), tmplate_(&t)
+{}
 
 
-MathMacro::MathMacro(boost::shared_ptr<MathMacroTemplate> const & t)
-       : MathParInset(LM_ST_TEXT, "", LM_OT_MACRO),
-         tmplate_(t)
-{
-       //nargs_ = tmplate_->getNoArgs();
-       int const n = tmplate_->getNoArgs();
-       
-       tcode_ = tmplate_->getTCode();
+MathMacro::MathMacro(MathMacro const & t)
+       : MathInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
+{}
 
-       for (int i = 0; i < n; ++i) {
-               args_.push_back(MathMacroArgument(t->args_[i]));
-       }
-       //for (int i = 0; i < nargs_; ++i) {
-       //      MathMacroArgument * ma = new MathMacroArgument(*t->args_[i]);
-       //      args_.push_back(boost::shared_ptr<MathMacroArgument>(ma));
-       //}
-       
-       idx_ = 0;
-       SetName(tmplate_->GetName());
-}
 
 
-MathedInset * MathMacro::Clone()
+MathInset * MathMacro::clone() const
 {
        return new MathMacro(*this);
 }
 
 
-void MathMacro::Metrics()
+void MathMacro::Metrics(MathStyles st, int, int)
 {
-       for (unsigned int i = 0; i < args_.size(); ++i) 
-               tmplate_->args_[i] = getArg(i);
-       tmplate_->SetStyle(size());
-       tmplate_->Metrics();
-       width = tmplate_->Width();
-       ascent = tmplate_->Ascent();
-       descent = tmplate_->Descent();
+       if (mathcursor && mathcursor->isInside(this)) {
+               expanded_ = tmplate_->xcell(0);
+               expanded_.Metrics(st);
+               size_    = st;
+               width_   = expanded_.width()   + 4;
+               ascent_  = expanded_.ascent()  + 2;
+               descent_ = expanded_.descent() + 2;
+
+               width_ +=  mathed_string_width(LM_TC_TEXTRM, size_, name_) + 10;
+
+               int lasc;
+               int ldes;
+               int lwid;
+               mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
+
+               for (int i = 0; i < nargs(); ++i) {
+                       MathXArray & c = xcell(i);
+                       c.Metrics(st);
+                       width_    = std::max(width_, c.width() + lwid);
+                       descent_ += std::max(c.ascent(),  lasc) + 5;
+                       descent_ += std::max(c.descent(), ldes) + 5;
+               }
+       } else {
+               expanded_ = tmplate_->xcell(0);
+               expanded_.data_.substitute(*this);
+               expanded_.Metrics(st);
+               size_    = st;
+               width_   = expanded_.width()   + 6;
+               ascent_  = expanded_.ascent()  + 3;
+               descent_ = expanded_.descent() + 3;
+       }
 }
 
 
@@ -88,113 +89,106 @@ void MathMacro::draw(Painter & pain, int x, int y)
 {
        xo(x);
        yo(y);
-       Metrics();
-       tmplate_->SetStyle(size());
-       tmplate_->draw(pain, x, y);
-}
-
-
-bool MathMacro::setArgumentIdx(int i)
-{
-       if (i >= 0 && 0 < (args_.size() - i)) {
-               idx_ = i;
-               return true;
-       } else
-               return false;
-}
 
+       Metrics(size());
 
-int MathMacro::getArgumentIdx() const 
-{ 
-       return idx_;
-}
+       LColor::color col;
 
+       if (mathcursor && mathcursor->isInside(this)) {
 
-int MathMacro::getMaxArgumentIdx() const 
-{ 
-       return args_.size() - 1;
-} 
+               int h = y - ascent() + 2 + expanded_.ascent();
+               drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
 
+               int const w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
+               expanded_.draw(pain, x + w + 12, h);
+               h += expanded_.descent();
 
-MathedArray & MathMacro::GetData() 
-{ 
-       return args_[idx_].GetData(); 
-} 
+               int lasc;
+               int ldes;
+               int lwid;
+               mathed_string_dim(LM_TC_TEXTRM, size_, "#1: ", lasc, ldes, lwid);
 
+               for (int i = 0; i < nargs(); ++i) {
+                       MathXArray & c = xcell(i);
+                       h += std::max(c.ascent(), lasc) + 5;
+                       c.draw(pain, x + lwid, h);
+                       char str[] = "#1:";
+                       str[1] += i;
+                       drawStr(pain, LM_TC_TEX, size(), x + 3, h, str);
+                       h += std::max(c.descent(), ldes) + 5;
+               }
+               col = LColor::red;
+       } else {
+               expanded_.draw(pain, x + 3, y);
+               col = LColor::black;
+       }
 
-MathedArray const & MathMacro::GetData() const
-{ 
-       return args_[idx_].GetData(); 
-} 
+       if (nargs() > 0)
+               pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
+}
 
 
-int MathMacro::GetColumns() const
+void MathMacro::dump(std::ostream & os) const
 {
-       return tmplate_->getMacroPar(idx_)->GetColumns();
+       MathMacroTable::dump();
+       os << "\n macro: '" << this << "'\n";
+       os << " name: '" << name_ << "'\n";
+       os << " template: '" << tmplate_ << "'\n";
+       os << " template: '" << *tmplate_ << "'\n";
+       os << endl;
 }
 
-
-void MathMacro::GetXY(int & x, int & y) const
+void MathMacro::Write(std::ostream & os, bool fragile) const
 {
-       const_cast<MathMacro*>(this)->Metrics();
-       tmplate_->GetMacroXY(idx_, x, y);
+       os << '\\' << name_;
+       for (int i = 0; i < nargs(); ++i) {
+               os << '{';
+               cell(i).Write(os, fragile);
+               os << '}';
+       }
+       if (nargs() == 0) 
+               os << ' ';
 }
 
 
-bool MathMacro::Permit(short f) const
+void MathMacro::WriteNormal(std::ostream & os) const
 {
-       return (args_.size() > 0) ?
-               tmplate_->getMacroPar(idx_)->Permit(f) :
-               MathParInset::Permit(f);
+       os << "[macro " << name_ << " ";
+       for (int i = 0; i < nargs(); ++i) {
+               cell(i).WriteNormal(os);
+               os << ' ';
+       }
+       os << "] ";
 }
 
 
-void MathMacro::SetFocus(int x, int y)
+bool MathMacro::idxUp(int & idx, int & pos) const
 {
-       Metrics();
-       tmplate_->SetMacroFocus(idx_, x, y);
+       return MathInset::idxLeft(idx, pos);
 }
 
 
-void MathMacro::setData(MathedArray const & a)
+bool MathMacro::idxDown(int & idx, int & pos) const
 {
-       args_[idx_].setData(a);
+       return MathInset::idxRight(idx, pos);
 }
 
 
-MathedTextCodes MathMacro::getTCode() const
+bool MathMacro::idxLeft(int &, int &) const
 {
-       return tcode_;
+       return false;
 }
-       
 
-void MathMacro::Write(ostream & os, bool fragile)
-{
-       os << '\\' << name;
 
-       int const n = args_.size();
-       
-       if (n > 0) {
-               os << '{';
-               
-               for (int i = 0; i < n; ++i) {
-                       array = args_[i].GetData();
-                       MathParInset::Write(os, fragile);
-                       if (i < n - 1)  
-                               os << "}{";
-               }
-               os << '}';
-       } else
-               os << ' ';
+bool MathMacro::idxRight(int &, int &) const
+{
+       return false;
 }
 
 
-MathMacroArgument const & MathMacro::getArg(int i) const
+void MathMacro::Validate(LaTeXFeatures & features) const
 {
-       return args_[i];
+       if (name_ == "binom")
+               features.binom = true;
+       MathInset::Validate(features);
 }
-//boost::shared_ptr<MathMacroArgument> MathMacro::getArg(int i)
-//{
-//     return args_[i];
-//}
-