]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_fracinset.C
mathed95.diff
[lyx.git] / src / mathed / math_fracinset.C
index 3e5d5e23b55bf053b283ade290c2d81219faa1d8..856e3b664b5b333379481908975005f091ecdeae 100644 (file)
 #include <config.h>
 
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
+#include <functional>
+
 #include "math_fracinset.h"
-#include "math_iter.h"
 #include "LColor.h"
 #include "Painter.h"
 #include "mathed/support.h"
 #include "support/LOstream.h"
 
-using std::ostream;
-
 
-MathFracInset::MathFracInset(short ot)
-       : MathParInset(LM_ST_TEXT, "frac", ot)
+MathFracInset::MathFracInset(MathInsetTypes ot)
+       : MathInset("frac", ot, 2)
 {
-       
-       den = new MathParInset(LM_ST_TEXT); // this leaks
-       dh = 0;
-       idx = 0;
-       if (objtype == LM_OT_STACKREL) {
-               flag |= LMPF_SCRIPT;
+       if (objtype == LM_OT_STACKREL) 
                SetName("stackrel");
-       }
-}
-
-
-MathFracInset::~MathFracInset()
-{
-       delete den;
 }
 
 
-MathedInset * MathFracInset::Clone()
+MathInset * MathFracInset::clone() const
 {   
-       MathFracInset * p = new MathFracInset(GetType());
-       MathedIter itn(array);
-       MathedIter itd(den->GetData());
-       p->SetData(itn.Copy(), itd.Copy());
-       p->idx = idx;
-       p->dh = dh;
-       return p;
+       return new MathFracInset(*this);
 }
 
 
-bool MathFracInset::setArgumentIdx(int i)
+void MathFracInset::Metrics(MathStyles st)
 {
-       if (i == 0 || i == 1) {
-               idx = i;
-               return true;
-       } else 
-               return false;
+       size_    = smallerStyleFrac(st);
+       xcell(0).Metrics(size_);
+       xcell(1).Metrics(size_);
+       width_   = std::max(xcell(0).width(), xcell(1).width()) + 4; 
+       ascent_  = xcell(0).height() + 4 + 5;
+       descent_ = xcell(1).height() + 4 - 5; 
 }
 
 
-void MathFracInset::SetStyle(short st)
+void MathFracInset::draw(Painter & pain, int x, int y)
 {
-       MathParInset::SetStyle(st);
-       dh = 0;
-       den->SetStyle((size == LM_ST_DISPLAY) ?
-                     static_cast<short>(LM_ST_TEXT)
-                     : size);
+       xo(x);
+       yo(y);
+       int m = x + width() / 2;
+       xcell(0).draw(pain, m - xcell(0).width() / 2, y - xcell(0).descent() - 3 - 5);
+       xcell(1).draw(pain, m - xcell(1).width() / 2, y + xcell(1).ascent()  + 3 - 5);
+       
+       if (objtype == LM_OT_FRAC)
+               pain.line(x + 2, y - 5, x + width() - 4, y - 5, LColor::mathline);
 }
 
 
-void MathFracInset::SetData(MathedArray * n, MathedArray * d)
+void MathFracInset::Write(std::ostream & os, bool fragile) const
 {
-       den->SetData(d);
-       MathParInset::SetData(n);
+       os << '\\' << name() << '{';
+       cell(0).Write(os, fragile);
+       os << "}{";
+       cell(1).Write(os, fragile);
+       os << '}';
 }
 
 
-void MathFracInset::SetData(MathedArray * d)
+void MathFracInset::WriteNormal(std::ostream & os) const
 {
-       if (idx == 0)
-               MathParInset::SetData(d);
-       else {
-               den->SetData(d);
-       }
+       os << '[' << name() << ' ';
+       cell(0).WriteNormal(os);
+       os << " ";
+       cell(1).WriteNormal(os);
+       os << "] ";
 }
 
 
-void MathFracInset::GetXY(int & x, int & y) const
-{  
-       if (idx == 0)
-               MathParInset::GetXY(x, y);
-       else
-               den->GetXY(x, y);
+bool MathFracInset::idxRight(int &, int &) const
+{
+       return false;
 }
 
-
-MathedArray * MathFracInset::GetData()
+bool MathFracInset::idxLeft(int &, int &) const
 {
-       if (idx == 0)
-               return array;
-       else
-               return den->GetData();
+       return false;
 }
 
 
-bool MathFracInset::Inside(int x, int y) 
+bool MathFracInset::idxUp(int & idx, int &) const
 {
-       int xx = xo - (width - w0) / 2;
-       
-       return x >= xx && x <= xx + width && y <= yo + descent && y >= yo - ascent;
+       if (idx == 0)
+               return false;
+       idx = 0;
+       return true;
 }
 
-
-void MathFracInset::SetFocus(int /*x*/, int y)
-{  
-//    lyxerr << "y " << y << " " << yo << " " << den->yo << " ";
-       idx = (y > yo) ? 1: 0;
+bool MathFracInset::idxDown(int & idx, int &) const
+{
+       if (idx == 1)
+               return false;
+       idx = 1;
+       return true;
 }
 
-
-void
-MathFracInset::draw(Painter & pain, int x, int y)
-{ 
-       short idxp = idx;
-       short sizex = size;
-       
+bool MathFracInset::idxFirstUp(int & idx, int & pos) const
+{
        idx = 0;
-       if (size == LM_ST_DISPLAY) ++size;
-       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;
+       pos = 0;
+       return true;
 }
 
+bool MathFracInset::idxFirstDown(int & idx, int & pos) const
+{
+       idx = 1;
+       pos = 0;
+       return true;
+}
 
-void
-MathFracInset::Metrics()
+bool MathFracInset::idxLastUp(int & idx, int & pos) const
 {
-       if (!dh) {
-               int a, b;
-               dh = mathed_char_height(LM_TC_CONST, size, 'I', a, b)/2;
-       }
-       short idxp = idx;
-       short sizex = size; 
        idx = 0;
-       if (size == LM_ST_DISPLAY) ++size; 
-       MathParInset::Metrics();
-       size = sizex;
-       w0 = width;
-       int 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;
+       pos = cell(idx).size();
+       return true;
 }
 
-
-void MathFracInset::Write(ostream & os, bool fragile)
+bool MathFracInset::idxLastDown(int & idx, int & pos) const
 {
-       os << '\\' << name << '{';
-       MathParInset::Write(os, fragile);
-       os << "}{";
-       den->Write(os, fragile);
-       os << '}';
+       idx = 1;
+       pos = cell(idx).size();
+       return true;
 }
+