]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_scriptinset.C
use stream-like syntax for LaTeX output
[lyx.git] / src / mathed / math_scriptinset.C
index 26d58f5f6dbe4755b4b34c1db5bbca0836bcb2f9..cc9c481bdbb4f0c80b97e341d1da11b59efee07a 100644 (file)
 #include <config.h>
+#include "debug.h"
+#include "support.h"
+#include "support/LOstream.h"
+#include "support/LAssert.h"
 
 #ifdef __GNUG__
 #pragma implementation
 #endif
 
-#include <functional>
-
 #include "math_scriptinset.h"
-#include "LColor.h"
-#include "Painter.h"
-#include "debug.h"
-#include "mathed/support.h"
-#include "support/LOstream.h"
 
 
 MathScriptInset::MathScriptInset()
-       : MathInset("script", LM_OT_SCRIPT, 2), up_(false), down_(false)
-{}
+       : MathNestInset(2), limits_(0)
+{
+       script_[0] = false;
+       script_[1] = false;
+}
+
+
+MathScriptInset::MathScriptInset(bool up)
+       : MathNestInset(2), limits_(0)
+{
+       script_[0] = !up;
+       script_[1] = up;
+}
 
-MathScriptInset::MathScriptInset(bool up, bool down)
-       : MathInset("script", LM_OT_SCRIPT, 2), up_(up), down_(down)
-{}
 
 
-MathInset * MathScriptInset::Clone() const
-{   
+MathInset * MathScriptInset::clone() const
+{
        return new MathScriptInset(*this);
 }
 
 
-void MathScriptInset::Metrics(MathStyles st)
+MathScriptInset const * MathScriptInset::asScriptInset() const
 {
-       MathInset::Metrics(st);
-       size_    = st;
-       width_   = std::max(xcell(0).width(), xcell(1).width()) + 2; 
-       if (up())
-               ascent_  = std::max(ascent_, xcell(0).height() + 9);
-       if (down())
-               descent_ = std::max(descent_, xcell(1).height());
+       return this;
 }
 
 
-bool MathScriptInset::up() const
+MathScriptInset * MathScriptInset::asScriptInset()
 {
-       return up_;
+       return this;
 }
 
-bool MathScriptInset::down() const
+
+MathXArray const & MathScriptInset::up() const
 {
-       return down_;
+       return xcell(1);
 }
 
-void MathScriptInset::up(bool b)
+
+MathXArray const & MathScriptInset::down() const
 {
-       up_ = b;
+       return xcell(0);
 }
 
-void MathScriptInset::down(bool b)
+
+MathXArray & MathScriptInset::up()
 {
-       down_ = b;
+       return xcell(1);
 }
 
 
-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());
+MathXArray & MathScriptInset::down()
+{
+       return xcell(0);
 }
 
 
-void MathScriptInset::Write(std::ostream & os, bool fragile) const
+void MathScriptInset::ensure(bool up)
 {
-       if (up()) {
-               os << "^{";
-               cell(0).Write(os, fragile);
-               os << "}";
-       }
-       if (down()) {
-               os << "_{";
-               cell(1).Write(os, fragile);
-               os << "}";
-       }
+       script_[up] = true;
 }
 
 
-void MathScriptInset::WriteNormal(std::ostream & os) const
+int MathScriptInset::dy0(MathInset const * nuc) const
 {
-       if (up()) {
-               os << "[superscript ";
-               cell(0).WriteNormal(os);
-               os << "] ";
-       }
-       if (down()) {
-               os << "[subscript ";
-               cell(1).WriteNormal(os);
-               os << "] ";
-       }
+       int nd = ndes(nuc);
+       if (!hasDown())
+               return nd;
+       int des = down().ascent();
+       if (hasLimits(nuc))
+               des += nd + 2;
+       else 
+               des = std::max(des, nd);
+       return des;
 }
 
-bool MathScriptInset::idxRight(int &, int &) const
+
+int MathScriptInset::dy1(MathInset const * nuc) const
 {
-       return false;
+       int na = nasc(nuc);
+       if (!hasUp())
+               return na;
+       int asc = up().descent();
+       if (hasLimits(nuc))
+               asc += na + 2;
+       else 
+               asc = std::max(asc, na);
+       asc = std::max(asc, mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I'));
+       return asc;
 }
 
-bool MathScriptInset::idxLeft(int &, int &) const
+
+int MathScriptInset::dx0(MathInset const * nuc) const
 {
-       return false;
+       lyx::Assert(hasDown());
+       return hasLimits(nuc) ? (width(nuc) - down().width()) / 2 : nwid(nuc);
 }
 
 
-bool MathScriptInset::idxUp(int & idx, int & pos) const
+int MathScriptInset::dx1(MathInset const * nuc) const
 {
-       if (idx == 0 || !up()) 
-               return false;
-       idx = 0;
-       pos = 0;
-       return true;
+       lyx::Assert(hasUp());
+       return hasLimits(nuc) ? (width(nuc) - up().width()) / 2 : nwid(nuc);
 }
 
-bool MathScriptInset::idxDown(int & idx, int & pos) const
+
+int MathScriptInset::dxx(MathInset const * nuc) const
 {
-       if (idx == 1 || !down()) 
-               return false;
-       idx = 1;
-       pos = 0;
-       return true;
+       //lyx::Assert(nuc());
+       return hasLimits(nuc)  ?  (width(nuc) - nwid(nuc)) / 2  :  0;
 }
 
-bool MathScriptInset::idxFirst(int & idx, int & pos) const
+
+int MathScriptInset::ascent(MathInset const * nuc) const
 {
-       idx = up() ? 0 : 1;
-       pos = 0;
-       return true;
+       return dy1(nuc) + (hasUp() ? up().ascent() : 0);
 }
 
-bool MathScriptInset::idxLast(int & idx, int & pos) const
+
+int MathScriptInset::descent(MathInset const * nuc) const
 {
-       idx = down() ? 1 : 0;
-       pos = cell(idx).size();
-       return true;
+       return dy0(nuc) + (hasDown() ? down().descent() : 0);
 }
 
 
-bool MathScriptInset::idxFirstUp(int & idx, int & pos) const
+int MathScriptInset::width(MathInset const * nuc) const
 {
-       if (!up()) 
-               return false;
-       idx = 0;
-       pos = 0;
-       return true;
+       int wid = 0;
+       if (hasLimits(nuc)) {
+               wid = nwid(nuc);
+               if (hasUp())
+                       wid = std::max(wid, up().width());
+               if (hasDown())
+                       wid = std::max(wid, down().width());
+       } else {
+               if (hasUp())
+                       wid = std::max(wid, up().width());
+               if (hasDown())
+                       wid = std::max(wid, down().width());
+               wid += nwid(nuc);
+       }
+       return wid;
 }
 
-bool MathScriptInset::idxFirstDown(int & idx, int & pos) const
+
+int MathScriptInset::nwid(MathInset const * nuc) const
 {
-       if (!down()) 
-               return false;
-       idx = 1;
-       pos = 0;
-       return true;
+       return nuc ?
+               nuc->width() :
+               mathed_char_width(LM_TC_TEX, LM_ST_TEXT, '.');
 }
 
-bool MathScriptInset::idxLastUp(int & idx, int & pos) const
+
+int MathScriptInset::nasc(MathInset const * nuc) const
 {
-       if (!up()) 
-               return false;
-       idx = 0;
-       pos = cell(idx).size();
-       return true;
+       return nuc ? nuc->ascent()
+               : mathed_char_ascent(LM_TC_VAR, LM_ST_TEXT, 'I');
 }
 
-bool MathScriptInset::idxLastDown(int & idx, int & pos) const
+
+int MathScriptInset::ndes(MathInset const * nuc) const
 {
-       if (!down()) 
-               return false;
-       idx = 1;
-       pos = cell(idx).size();
-       return true;
+       return nuc ? nuc->descent()
+               : mathed_char_descent(LM_TC_VAR, LM_ST_TEXT, 'I');
 }
 
 
-bool MathScriptInset::idxDelete(int idx)
+void MathScriptInset::metrics(MathMetricsInfo const & st) const
+{      
+       metrics(0, st);
+}
+
+
+void MathScriptInset::metrics(MathInset const * nuc,
+       MathMetricsInfo const & st) const
+{      
+       MathNestInset::metrics(st);
+       if (nuc)
+               nuc->metrics(st);
+
+       ascent_  = ascent(nuc);
+       descent_ = descent(nuc);
+       width_   = width(nuc);
+}
+
+
+void MathScriptInset::draw(Painter & pain, int x, int y) const
+{  
+       //lyxerr << "unexpected call to MathScriptInset::draw()\n";
+       draw(0, pain, x, y);
+}
+
+
+void MathScriptInset::draw(MathInset const * nuc, Painter & pain,
+       int x, int y) const
+{  
+       xo(x);
+       yo(y);
+       if (nuc)
+               nuc->draw(pain, x + dxx(nuc), y);
+       else
+               drawStr(pain, LM_TC_TEX, LM_ST_TEXT, x + dxx(nuc), y, ".");
+       if (hasUp())
+               up().draw(pain, x + dx1(nuc), y - dy1(nuc));
+       if (hasDown())
+               down().draw(pain, x + dx0(nuc), y + dy0(nuc));
+}
+
+
+void MathScriptInset::write(MathWriteInfo & os) const
+{  
+       //lyxerr << "unexpected call to MathScriptInset::write()\n";
+       write(0, os);
+}
+
+
+void MathScriptInset::write(MathInset const * nuc, MathWriteInfo & os) const
 {
-       if (idx == 0) {
-               up(false);
-               return !down();
-       } else {
-               down(false);
-               return !up();
+       if (nuc) {
+               nuc->write(os);
+               if (nuc->takesLimits()) {
+                       if (limits_ == -1)
+                               os << "\\nolimits ";
+                       if (limits_ == 1)
+                               os << "\\limits ";
+               }
        }
+       else
+               os << "{}";
+
+       if (hasDown() && down().data_.size())
+               os << "_{" << down().data_ << '}';
+
+       if (hasUp() && up().data_.size())
+               os << "^{" << up().data_ << '}';
 }
+
+
+bool MathScriptInset::hasLimits(MathInset const * nuc) const
+{
+       return limits_ == 1 || (limits_ == 0 && nuc && nuc->isScriptable());
+}
+
+
+void MathScriptInset::removeEmptyScripts()
+{
+       for (int i = 0; i <= 1; ++i)
+               if (script_[i] && !cell(i).size())
+                       script_[i] = false;
+}
+
+
+void MathScriptInset::removeScript(bool up)
+{
+       cell(up).clear();
+       script_[up] = false;
+}
+
+
+bool MathScriptInset::has(bool up) const
+{
+       return script_[up];
+}
+
+
+bool MathScriptInset::hasUp() const
+{
+       return script_[1];
+}
+
+
+bool MathScriptInset::hasDown() const
+{
+       return script_[0];
+}
+
+
+bool MathScriptInset::idxRight(MathInset::idx_type &,
+                                MathInset::pos_type &) const
+{
+       return false;
+}
+
+
+bool MathScriptInset::idxLeft(MathInset::idx_type &,
+                               MathInset::pos_type &) const
+{
+       return false;
+}
+
+