]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_scriptinset.C
move things around
[lyx.git] / src / mathed / math_scriptinset.C
index 7be748d1e277cc6027291a4004348cb5587fe376..158c2b91c42e838f4ce635f3de9b2b38ab038668 100644 (file)
@@ -1,41 +1,37 @@
-#include <config.h>
-
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
 #include "math_scriptinset.h"
-#include "LColor.h"
-#include "Painter.h"
-#include "debug.h"
-#include "mathed/support.h"
 #include "support/LOstream.h"
+#include "support.h"
 
 
 MathScriptInset::MathScriptInset()
-       : MathInset("script", LM_OT_SCRIPT, 2), up_(false), down_(false)
+       : MathNestInset(2), up_(false), down_(false), limits_(0), symbol_(0)
 {}
 
-MathScriptInset::MathScriptInset(bool up, bool down)
-       : MathInset("script", LM_OT_SCRIPT, 2), up_(up), down_(down)
+
+MathScriptInset::MathScriptInset(bool up, bool down, MathInset * symbol)
+       : MathNestInset(2), up_(up), down_(down), limits_(0), symbol_(symbol)
 {}
 
 
-MathInset * MathScriptInset::Clone() const
-{   
-       return new MathScriptInset(*this);
-}
+MathScriptInset::MathScriptInset(MathScriptInset const & p)
+       : MathNestInset(p), up_(p.up_), down_(p.down_),
+               limits_(p.limits_), symbol_(p.symbol_ ? p.symbol_->clone() : 0)
+{}
 
 
-void MathScriptInset::Metrics(MathStyles st)
+MathScriptInset::~MathScriptInset()
 {
-       MathInset::Metrics(st);
-       size_    = st;
-       width_   = max(xcell(0).width(), xcell(1).width()) + 2; 
-       if (up())
-               ascent_  = max(ascent_, xcell(0).height() + 9);
-       if (down())
-               descent_ = max(descent_, xcell(1).height());
+       delete symbol_;
+}
+
+
+MathInset * MathScriptInset::clone() const
+{   
+       return new MathScriptInset(*this);
 }
 
 
@@ -44,67 +40,31 @@ bool MathScriptInset::up() const
        return up_;
 }
 
+
 bool MathScriptInset::down() const
 {
        return down_;
 }
 
+
 void MathScriptInset::up(bool b)
 {
        up_ = b;
 }
 
+
 void MathScriptInset::down(bool b)
 {
        down_ = b;
 }
 
 
-void MathScriptInset::draw(Painter & pain, int x, int y)
-{ 
-       xo(x);
-       yo(y);
-       if (up())
-               xcell(0).draw(pain, x, y - xcell(0).descent() - 9);
-       if (down())
-               xcell(1).draw(pain, x, y + xcell(1).ascent());
-}
-
-
-void MathScriptInset::Write(std::ostream & os, bool fragile) const
-{
-       if (up()) {
-               os << "^{";
-               cell(0).Write(os, fragile);
-               os << "}";
-       }
-       if (down()) {
-               os << "_{";
-               cell(1).Write(os, fragile);
-               os << "}";
-       }
-}
-
-
-void MathScriptInset::WriteNormal(std::ostream & os) const
-{
-       if (up()) {
-               os << "[superscript ";
-               cell(0).WriteNormal(os);
-               os << "] ";
-       }
-       if (down()) {
-               os << "[subscript ";
-               cell(1).WriteNormal(os);
-               os << "] ";
-       }
-}
-
 bool MathScriptInset::idxRight(int &, int &) const
 {
        return false;
 }
 
+
 bool MathScriptInset::idxLeft(int &, int &) const
 {
        return false;
@@ -153,6 +113,7 @@ bool MathScriptInset::idxFirstUp(int & idx, int & pos) const
        return true;
 }
 
+
 bool MathScriptInset::idxFirstDown(int & idx, int & pos) const
 {
        if (!down()) 
@@ -162,6 +123,7 @@ bool MathScriptInset::idxFirstDown(int & idx, int & pos) const
        return true;
 }
 
+
 bool MathScriptInset::idxLastUp(int & idx, int & pos) const
 {
        if (!up()) 
@@ -171,6 +133,7 @@ bool MathScriptInset::idxLastUp(int & idx, int & pos) const
        return true;
 }
 
+
 bool MathScriptInset::idxLastDown(int & idx, int & pos) const
 {
        if (!down()) 
@@ -181,13 +144,133 @@ bool MathScriptInset::idxLastDown(int & idx, int & pos) const
 }
 
 
-bool MathScriptInset::idxDelete(int idx)
+void MathScriptInset::write(std::ostream & os, bool fragile) const
 {
-       if (idx == 0) {
+       if (symbol_) {
+               symbol_->write(os, fragile);
+               if (limits())
+                       os << (limits() == 1 ? "\\limits" : "\\nolimits");
+       }
+       if (up()) {
+               os << "^{";
+               cell(0).write(os, fragile);
+               os << "}";
+       }
+       if (down()) {
+               os << "_{";
+               cell(1).write(os, fragile);
+               os << "}";
+       }
+       os << " ";
+}
+
+
+void MathScriptInset::idxDelete(int & idx, bool & popit, bool & deleteit)
+{
+       if (idx == 0) 
                up(false);
-               return !down();
-       } else {
+       else
                down(false);
-               return !up();
+       popit = true;
+       deleteit = !(up() || down());
+}
+
+
+int MathScriptInset::limits() const
+{  
+       return limits_;
+}
+
+
+void MathScriptInset::limits(int limits) 
+{  
+       limits_ = limits;
+}
+
+
+bool MathScriptInset::hasLimits() const
+{
+       return
+               symbol_ && (limits_ == 1 || (limits_ == 0 && size() == LM_ST_DISPLAY));
+}
+
+
+void MathScriptInset::writeNormal(std::ostream & os) const
+{
+       if (limits() && symbol_) 
+               os << "[" << (limits() ? "limits" : "nolimits") << "]";
+       if (up()) {
+               os << "[superscript ";
+               cell(0).writeNormal(os);
+               os << "] ";
+       }
+       if (down()) {
+               os << "[subscript ";
+               cell(1).writeNormal(os);
+               os << "] ";
+       }
+}
+
+
+void MathScriptInset::metrics(MathStyles st) const
+{
+       size_ = st;
+       MathStyles tt = smallerStyleScript(st);
+       
+       xcell(0).metrics(tt);
+       xcell(1).metrics(tt);
+
+       width_   = std::max(xcell(0).width(), xcell(1).width());
+
+       if (hasLimits()) {
+               symbol_->metrics(st);
+               int wid  = symbol_->width();
+               ascent_  = symbol_->ascent();
+               descent_ = symbol_->descent();
+               width_   = std::max(width_, wid);
+               if (up()) {
+                       ascent_  += xcell(0).height() + 2;
+                       dy0_     = - (ascent_ - xcell(0).ascent());
+               }
+               if (down()) {
+                       descent_ += xcell(1).height() + 2;
+                       dy1_     = descent_ - xcell(1).descent();
+               }
+               dxx_   = (width_ - wid) / 2;
+               dx0_   = (width_ - xcell(0).width()) / 2;
+               dx1_   = (width_ - xcell(1).width()) / 2;
+       } else {
+               int asc = 0;
+               int des = 0;
+               int wid = 0;
+               mathed_char_height(LM_TC_VAR, st, 'I', asc, des);
+               if (symbol_) {
+                       symbol_->metrics(st);
+                       wid  = symbol_->width();
+                       asc  = symbol_->ascent();
+                       des  = symbol_->descent();
+               }
+               ascent_  = up()   ? xcell(0).height() + asc : 0;
+               descent_ = down() ? xcell(1).height() + des : 0;
+               width_  += wid;
+               dy0_     = - asc - xcell(0).descent();
+               dy1_     =   des + xcell(1).ascent();
+               dx0_     = wid;
+               dx1_     = wid;
+               dxx_     = 0;
        }
 }
+
+
+void MathScriptInset::draw(Painter & pain, int x, int y) const
+{  
+       xo(x);
+       yo(y);
+
+       if (symbol_)
+               symbol_->draw(pain, x + dxx_, y);
+       if (up())
+               xcell(0).draw(pain, x + dx0_, y + dy0_);
+       if (down())
+               xcell(1).draw(pain, x + dx1_, y + dy1_);
+}