]> 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 d91fd3deb93b2927cdf213631937c95d7b234685..5085e783bf864957aff6bea102d2e7df514b0865 100644 (file)
@@ -14,8 +14,6 @@
  *  This code is under the GNU General Public Licence version 2 or later.
  */
 
-#include <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 #include "math_macrotable.h"
 #include "math_macrotemplate.h"
 #include "Painter.h"
+#include "LaTeXFeatures.h"
 
-using std::ostream;
 using std::endl;
 
 MathMacro::MathMacro(MathMacroTemplate const & t)
-       : MathInset(t.name(), LM_OT_MACRO, t.numargs()), tmplate_(&t)
+       : MathInset(t.numargs(), t.name(), LM_OT_MACRO), tmplate_(&t)
+{}
+
+
+MathMacro::MathMacro(MathMacro const & t)
+       : MathInset(t), tmplate_(t.tmplate_) // don't copy 'expanded_'!
 {}
 
 
-MathInset * MathMacro::Clone() const
+
+MathInset * MathMacro::clone() const
 {
        return new MathMacro(*this);
 }
 
 
-void MathMacro::Metrics(MathStyles st)
+void MathMacro::Metrics(MathStyles st, int, int)
 {
        if (mathcursor && mathcursor->isInside(this)) {
                expanded_ = tmplate_->xcell(0);
@@ -57,11 +61,17 @@ void MathMacro::Metrics(MathStyles st)
 
                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() + 30);
-                       descent_ += c.height() + 10;
+                       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);
@@ -80,6 +90,8 @@ void MathMacro::draw(Painter & pain, int x, int y)
        xo(x);
        yo(y);
 
+       Metrics(size());
+
        LColor::color col;
 
        if (mathcursor && mathcursor->isInside(this)) {
@@ -87,18 +99,23 @@ void MathMacro::draw(Painter & pain, int x, int y)
                int h = y - ascent() + 2 + expanded_.ascent();
                drawStr(pain, LM_TC_TEXTRM, size(), x + 3, h, name_);
 
-               int w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
+               int const w = mathed_string_width(LM_TC_TEXTRM, size(), name_);
                expanded_.draw(pain, x + w + 12, h);
                h += expanded_.descent();
 
+               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 += c.ascent() + 5;
-                       c.draw(pain, x + 30, h);
+                       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 += c.descent() + 5;
+                       h += std::max(c.descent(), ldes) + 5;
                }
                col = LColor::red;
        } else {
@@ -106,11 +123,12 @@ void MathMacro::draw(Painter & pain, int x, int y)
                col = LColor::black;
        }
 
-       pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
+       if (nargs() > 0)
+               pain.rectangle(x + 1, y - ascent() + 1, width() - 2, height() - 2, col);
 }
 
 
-void MathMacro::dump(ostream & os) const
+void MathMacro::dump(std::ostream & os) const
 {
        MathMacroTable::dump();
        os << "\n macro: '" << this << "'\n";
@@ -120,7 +138,7 @@ void MathMacro::dump(ostream & os) const
        os << endl;
 }
 
-void MathMacro::Write(ostream & os, bool fragile) const
+void MathMacro::Write(std::ostream & os, bool fragile) const
 {
        os << '\\' << name_;
        for (int i = 0; i < nargs(); ++i) {
@@ -133,7 +151,7 @@ void MathMacro::Write(ostream & os, bool fragile) const
 }
 
 
-void MathMacro::WriteNormal(ostream & os) const
+void MathMacro::WriteNormal(std::ostream & os) const
 {
        os << "[macro " << name_ << " ";
        for (int i = 0; i < nargs(); ++i) {
@@ -166,3 +184,11 @@ bool MathMacro::idxRight(int &, int &) const
 {
        return false;
 }
+
+
+void MathMacro::Validate(LaTeXFeatures & features) const
+{
+       if (name_ == "binom")
+               features.binom = true;
+       MathInset::Validate(features);
+}