]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_fracinset.C
oh well
[lyx.git] / src / mathed / math_fracinset.C
index ecf2fcdaf0675412ff76738791c93ef7e8a19c2f..69f935689e62322fe7a992e045e7d08a1ead0bb1 100644 (file)
-#include <config.h>
+#ifdef __GNUG__
+#pragma implementation
+#endif
 
 #include "math_fracinset.h"
-#include "math_iter.h"
-#include "LColor.h"
+#include "math_support.h"
 #include "Painter.h"
-#include "mathed/support.h"
-#include "support/LOstream.h"
+#include "math_mathmlstream.h"
+#include "textpainter.h"
 
-using std::ostream;
 
+using std::max;
 
-MathFracInset::MathFracInset(short ot)
-       : MathParInset(LM_ST_TEXT, "frac", ot)
-{
-       
-       den_ = new MathParInset(LM_ST_TEXT); // this leaks
-       dh_ = 0;
-       idx_ = 0;
-       if (objtype == LM_OT_STACKREL) {
-               flag |= LMPF_SCRIPT;
-               SetName("stackrel");
-       }
-}
+
+MathFracInset::MathFracInset(bool atop)
+       : atop_(atop)
+{}
 
 
-MathFracInset::~MathFracInset()
+MathInset * MathFracInset::clone() const
 {
-       delete den_;
+       return new MathFracInset(*this);
 }
 
 
-MathedInset * MathFracInset::Clone()
-{   
-       MathFracInset * p = new MathFracInset(*this);
-       // this cast will go again...
-       p->den_ = (MathParInset*) (p->den_->Clone());
-       return p;
+MathFracInset * MathFracInset::asFracInset()
+{
+       return atop_ ? 0 : this;
 }
 
 
-bool MathFracInset::setArgumentIdx(int i)
+void MathFracInset::metrics(MathMetricsInfo const & mi) const
 {
-       if (i == 0 || i == 1) {
-               idx_ = i;
-               return true;
-       } else 
-               return false;
+       MathMetricsInfo m = mi;
+       smallerStyleFrac(m);
+       xcell(0).metrics(m);
+       xcell(1).metrics(m);
+       width_   = max(xcell(0).width(), xcell(1).width()) + 2;
+       ascent_  = xcell(0).height() + 2 + 5;
+       descent_ = xcell(1).height() + 2 - 5;
 }
 
 
-void MathFracInset::SetStyle(short st)
+void MathFracInset::draw(Painter & pain, int x, int y) const
 {
-       MathParInset::SetStyle(st);
-       dh_ = 0;
-       den_->SetStyle((size() == LM_ST_DISPLAY) ?
-                     static_cast<short>(LM_ST_TEXT)
-                     : size());
+       int m = x + width() / 2;
+       xcell(0).draw(pain, m - xcell(0).width() / 2, y - xcell(0).descent() - 2 - 5);
+       xcell(1).draw(pain, m - xcell(1).width() / 2, y + xcell(1).ascent()  + 2 - 5);
+       if (!atop_)
+               pain.line(x, y - 5, x + width(), y - 5, LColor::math);
 }
 
 
-void MathFracInset::SetData(MathedArray * n, MathedArray * d)
+void MathFracInset::metricsT(TextMetricsInfo const & mi) const
 {
-       den_->setData(d);
-       MathParInset::setData(n);
+       xcell(0).metricsT(mi);
+       xcell(1).metricsT(mi);
+       width_   = max(xcell(0).width(), xcell(1).width());
+       ascent_  = xcell(0).height() + 1;
+       descent_ = xcell(1).height();
 }
 
 
-void MathFracInset::setData(MathedArray * d)
+void MathFracInset::drawT(TextPainter & pain, int x, int y) const
 {
-       if (idx_ == 0)
-               MathParInset::setData(d);
-       else {
-               den_->setData(d);
-       }
+       int m = x + width() / 2;
+       xcell(0).drawT(pain, m - xcell(0).width() / 2, y - xcell(0).descent() - 1);
+       xcell(1).drawT(pain, m - xcell(1).width() / 2, y + xcell(1).ascent());
+       if (!atop_)
+               pain.horizontalLine(x, y, width());
 }
 
 
-void MathFracInset::GetXY(int & x, int & y) const
-{  
-       if (idx_ == 0)
-               MathParInset::GetXY(x, y);
+void MathFracInset::write(WriteStream & os) const
+{
+       if (atop_)
+               os << '{' << cell(0) << "\\atop " << cell(1) << '}';
        else
-               den_->GetXY(x, y);
+               os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
 }
 
 
-MathedArray * MathFracInset::GetData()
+void MathFracInset::normalize(NormalStream & os) const
 {
-       if (idx_ == 0)
-               return &array;
+       if (atop_)
+               os << "[atop ";
        else
-               return den_->GetData();
+               os << "[frac ";
+       os << cell(0) << ' ' << cell(1) << ']';
 }
 
 
-bool MathFracInset::Inside(int x, int y) 
+void MathFracInset::maplize(MapleStream & os) const
 {
-       int xx = xo() - (width - w0_) / 2;
-       
-       return x >= xx
-               && x <= xx + width
-               && y <= yo() + descent
-               && y >= yo() - ascent;
-}
-
-
-void MathFracInset::SetFocus(int /*x*/, int y)
-{  
-       // lyxerr << "y " << y << " " << yo << " " << den_->yo << " ";
-       idx_ = (y > yo()) ? 1 : 0;
-}
-
-
-void
-MathFracInset::draw(Painter & pain, int x, int y)
-{ 
-       int const idxp = idx_;
-       int const sizex = size();
-       
-       idx_ = 0;
-       if (size() == LM_ST_DISPLAY) incSize();
-       MathParInset::draw(pain, x + (width - w0_) / 2, y - des0_);
-       den_->draw(pain, x + (width - w1_) / 2, y + den_->Ascent() + 2 - dh_);
-       size(sizex);
-       if (objtype == LM_OT_FRAC)
-               pain.line(x + 2, y - dh_,
-                         x + width - 4, y - dh_, LColor::mathline);
-       idx_ = idxp;
+       os << '(' << cell(0) << ")/(" << cell(1) << ')';
 }
 
 
-void
-MathFracInset::Metrics()
+void MathFracInset::octavize(OctaveStream & os) const
 {
-       if (!dh_) {
-               int a;
-               int b;
-               dh_ = mathed_char_height(LM_TC_CONST, size(), 'I', a, b) / 2;
-       }
-       int const idxp = idx_;
-       int const sizex = size();
-       idx_ = 0;
-       if (size() == LM_ST_DISPLAY) incSize(); 
-       MathParInset::Metrics();
-       size(sizex);
-       w0_ = width;
-       int const as = Height() + 2 + dh_;
-       des0_ = Descent() + 2 + dh_;
-       den_->Metrics();  
-       w1_ = den_->Width();   
-       width = ((w0_ > w1_) ? w0_: w1_) + 12;
-       ascent = as; 
-       descent = den_->Height()+ 2 - dh_;
-       idx_ = idxp;
+       os << '(' << cell(0) << ")/(" << cell(1) << ')';
 }
 
 
-void MathFracInset::Write(ostream & os, bool fragile)
+void MathFracInset::mathmlize(MathMLStream & os) const
 {
-       os << '\\' << name << '{';
-       MathParInset::Write(os, fragile);
-       os << "}{";
-       den_->Write(os, fragile);
-       os << '}';
+       os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
 }