]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_gridinset.C
whichFont down to 5.3%
[lyx.git] / src / mathed / math_gridinset.C
index f2d3ba939bc3b3cf52a84d5d88d0b99558478cbb..a766dac6708596de86eeb246a8c8c66b661c1f75 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "math_gridinset.h"
 #include "math_mathmlstream.h"
+#include "math_streamstr.h"
 #include "lyxfont.h"
 #include "Painter.h"
 #include "debug.h"
 
 namespace {
 
-///
-int const COLSEP = 6;
-///
-int const ROWSEP = 6;
-///
-int const HLINESEP = 3;
-///
-int const VLINESEP = 3;
-///
-int const BORDER = 2;
-
-
 string verboseHLine(int n)
 {
        string res;
@@ -38,7 +27,7 @@ string verboseHLine(int n)
 
 
 MathGridInset::RowInfo::RowInfo()
-       : skip_(0), lines_(0)
+       : lines_(0), skip_(0)
 {}
 
 
@@ -89,6 +78,12 @@ MathGridInset::MathGridInset(col_type m, row_type n, char v, string const & h)
 }
 
 
+MathInset * MathGridInset::clone() const
+{
+       return new MathGridInset(*this);
+}
+
+
 MathInset::idx_type MathGridInset::index(row_type row, col_type col) const
 {
        return col + ncols() * row;
@@ -220,8 +215,7 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const
        // let the cells adjust themselves
        MathNestInset::metrics(mi);
 
-       // adjust vertical structure
-       rowinfo_[0].offset_ = 0;
+       // compute absolute sizes of vertical structure
        for (row_type row = 0; row < nrows(); ++row) {
                int asc  = 0;
                int desc = 0;
@@ -232,17 +226,22 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const
                }
                rowinfo_[row].ascent_  = asc;
                rowinfo_[row].descent_ = desc;
-               rowinfo_[row].offset_  += asc + HLINESEP * rowinfo_[row].lines_;
-
-               rowinfo_[row + 1].offset_ =
-                               rowinfo_[row].offset_ +
-                               rowinfo_[row].descent_ +
-                               rowinfo_[row].skipPixels() +
-                               ROWSEP;
        }
+       rowinfo_[0].ascent_       += hlinesep() * rowinfo_[0].lines_;
        rowinfo_[nrows()].ascent_  = 0;
        rowinfo_[nrows()].descent_ = 0;
-       rowinfo_[nrows()].offset_  += HLINESEP * rowinfo_[nrows()].lines_;
+
+       // compute vertical offsets
+       rowinfo_[0].offset_ = 0;
+       for (row_type row = 1; row <= nrows(); ++row) {
+               rowinfo_[row].offset_  =        
+                       rowinfo_[row - 1].offset_  +
+                       rowinfo_[row - 1].descent_ +
+                       rowinfo_[row - 1].skipPixels() +
+                       rowsep() +
+                       rowinfo_[row].lines_ * hlinesep() +
+                       rowinfo_[row].ascent_;
+       }
 
        // adjust vertical offset
        int h = 0;
@@ -251,51 +250,50 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const
                        h = 0;
                        break;
                case 'b':
-                       h = rowinfo_[nrows()].offset_;
+                       h = rowinfo_[nrows() - 1].offset_;
                        break;
                default:
-                       h = rowinfo_[nrows()].offset_ / 2;
+                       h = rowinfo_[nrows() - 1].offset_ / 2;
        }
-       //lyxerr << "\nnrows: " << nrows() << " h: " << h << '\n';
-       for (row_type row = 0; row <= nrows(); ++row) {
+       for (row_type row = 0; row <= nrows(); ++row)
                rowinfo_[row].offset_ -= h;
-               //lyxerr << "row: " << row << " off: " << rowinfo_[row].offset_  << '\n';
-       }
-
        
-       // adjust horizontal structure
-       colinfo_[0].offset_ = BORDER;
+
+       // compute absolute sizes of horizontal structure
        for (col_type col = 0; col < ncols(); ++col) {
                int wid = 0;
                for (row_type row = 0; row < nrows(); ++row) 
                        wid = std::max(wid, xcell(index(row, col)).width());
-               colinfo_[col].width_  = wid;
-               colinfo_[col].offset_ += VLINESEP * colinfo_[col].lines_;
-
-               colinfo_[col + 1].offset_ =
-                       colinfo_[col].offset_ +
-                       colinfo_[col].width_ + 
-                       colinfo_[col].skip_ +
-                       COLSEP;
+               colinfo_[col].width_ = wid;
        }
        colinfo_[ncols()].width_  = 0;
-       colinfo_[ncols()].offset_ += VLINESEP * colinfo_[ncols()].lines_;
+
+       // compute horizontal offsets
+       colinfo_[0].offset_ = border();
+       for (col_type col = 1; col <= ncols(); ++col) {
+               colinfo_[col].offset_ =
+                       colinfo_[col - 1].offset_ +
+                       colinfo_[col - 1].width_ + 
+                       colinfo_[col - 1].skip_ +
+                       colsep() + 
+                       colinfo_[col].lines_ * vlinesep();
+       }
 
 
        width_   =   colinfo_[ncols() - 1].offset_      
                       + colinfo_[ncols() - 1].width_
-                 + VLINESEP * colinfo_[ncols()].lines_
-                      + BORDER;
+                 + vlinesep() * colinfo_[ncols()].lines_
+                      + border();
 
        ascent_  = - rowinfo_[0].offset_          
                       + rowinfo_[0].ascent_
-                 + HLINESEP * rowinfo_[0].lines_
-                      + BORDER;
+                 + hlinesep() * rowinfo_[0].lines_
+                      + border();
 
        descent_ =   rowinfo_[nrows() - 1].offset_
                       + rowinfo_[nrows() - 1].descent_
-                 + HLINESEP * rowinfo_[nrows()].lines_
-                      + BORDER;
+                 + hlinesep() * rowinfo_[nrows()].lines_
+                      + border();
 
 
 /*     
@@ -309,7 +307,7 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const
                        ws_[0] = 7 * workwidth / 8;
        
        // Adjust local tabs
-       width = COLSEP;
+       width = colsep();
        for (cxrow = row_.begin(); cxrow; ++cxrow) {   
                int rg = COLSEP;
                int lf = 0;
@@ -341,9 +339,9 @@ void MathGridInset::metrics(MathMetricsInfo const & mi) const
                        }
                        int const ww = (isvoid) ? lf : lf + cxrow->getTab(i);
                        cxrow->setTab(i, lf + rg);
-                       rg = ws_[i] - ww + COLSEP;
+                       rg = ws_[i] - ww + colsep();
                        if (cxrow == row_.begin())
-                               width += ws_[i] + COLSEP;
+                               width += ws_[i] + colsep();
                }
                cxrow->setBaseline(cxrow->getBaseline() - ascent);
        }
@@ -359,16 +357,14 @@ void MathGridInset::draw(Painter & pain, int x, int y) const
        for (row_type row = 0; row <= nrows(); ++row)
                for (int i = 0; i < rowinfo_[row].lines_; ++i) {
                        int yy = y + rowinfo_[row].offset_ - rowinfo_[row].ascent_
-                               - i * HLINESEP - HLINESEP/2 - ROWSEP/2;
-                       //lyxerr << "i: " << i << " yy: " << yy << '\n';
+                               - i * hlinesep() - hlinesep()/2 - rowsep()/2;
                        pain.line(x + 1, yy, x + width_ - 1, yy);
                }
 
        for (col_type col = 0; col <= ncols(); ++col)
                for (int i = 0; i < colinfo_[col].lines_; ++i) {
                        int xx = x + colinfo_[col].offset_
-                               - i * VLINESEP - VLINESEP/2 - COLSEP/2;
-                       //lyxerr << "i: " << i << " xx: " << xx << '\n';
+                               - i * vlinesep() - vlinesep()/2 - colsep()/2;
                        pain.line(xx, y - ascent_ + 1, xx, y + descent_ - 1);
                }
 }
@@ -378,14 +374,15 @@ string MathGridInset::eolString(row_type row) const
 {
        string eol;
 
-       if (rowinfo_[row].crskip_.value() != 0)
+       if (!rowinfo_[row].crskip_.zero())
                eol += "[" + rowinfo_[row].crskip_.asLatexString() + "]";
 
        // make sure an upcoming '[' does not break anything
        if (row + 1 < nrows()) {
                MathArray const & c = cell(index(row + 1, 0));
                if (c.size() && c.front()->getChar() == '[')
-                       eol += "[0pt]";
+                       //eol += "[0pt]";
+                       eol += "{}";
        }
 
        // only add \\ if necessary
@@ -485,24 +482,20 @@ int MathGridInset::cellYOffset(idx_type idx) const
 }
 
 
-bool MathGridInset::idxUp(idx_type & idx, pos_type & pos) const
+bool MathGridInset::idxUp(idx_type & idx) const
 {
        if (idx < ncols())
                return false;
-       int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
        idx -= ncols();
-       pos = xcell(idx).x2pos(x - cellXOffset(idx));
        return true;
 }
 
        
-bool MathGridInset::idxDown(idx_type & idx, pos_type & pos) const
+bool MathGridInset::idxDown(idx_type & idx) const
 {
        if (idx >= ncols() * (nrows() - 1))
                return false;
-       int x = cellXOffset(idx) + xcell(idx).pos2x(pos);
        idx += ncols();
-       pos = xcell(idx).x2pos(x - cellXOffset(idx));
        return true;
 }
        
@@ -512,7 +505,7 @@ bool MathGridInset::idxLeft(idx_type & idx, pos_type & pos) const
        // leave matrix if on the left hand edge
        if (col(idx) == 0)
                return false;
-       idx--;
+       --idx;
        pos = cell(idx).size();
        return true;
 }
@@ -523,7 +516,7 @@ bool MathGridInset::idxRight(idx_type & idx, pos_type & pos) const
        // leave matrix if on the right hand edge
        if (col(idx) + 1 == ncols())
                return false;
-       idx++;
+       ++idx;
        pos = 0;
        return true;
 }
@@ -539,7 +532,7 @@ bool MathGridInset::idxFirst(idx_type & idx, pos_type & pos) const
                        idx = (nrows() - 1) * ncols();
                        break;
                default: 
-                       idx = (nrows() / 2) * ncols();
+                       idx = ((nrows() - 1) / 2) * ncols();
        }
        pos = 0;
        return true;
@@ -556,7 +549,7 @@ bool MathGridInset::idxLast(idx_type & idx, pos_type & pos) const
                        idx = nargs() - 1;
                        break;
                default:
-                       idx = (nrows() / 2 + 1) * ncols() - 1;
+                       idx = ((nrows() - 1) / 2 + 1) * ncols() - 1;
        }
        pos = cell(idx).size();
        return true;
@@ -632,22 +625,6 @@ void MathGridInset::idxDelete(idx_type & idx, bool & popit, bool & deleteit)
 }
 
 
-void MathGridInset::idxDeleteRange(idx_type /*from*/, idx_type /*to*/)
-{
-// leave this unimplemented unless someone wants to have it.
-/*
-       int n = (to - from) / ncols();
-       int r = from / ncols();
-
-       if (n >= 1) {
-               cells_type::iterator it = cells_.begin() + from;
-               cells_.erase(it, it + n * ncols());
-               rowinfo_.erase(rowinfo_.begin() + r, rowinfo_.begin() + r + n);
-       }
-*/
-}
-
-
 MathGridInset::RowInfo const & MathGridInset::rowinfo(row_type row) const
 {
        return rowinfo_[row];
@@ -705,14 +682,42 @@ void MathGridInset::mathmlize(MathMLStream & os) const
 void MathGridInset::write(WriteStream & os) const
 {
        for (row_type row = 0; row < nrows(); ++row) {
-               os << verboseHLine(rowinfo_[row].lines_).c_str();
+               os << verboseHLine(rowinfo_[row].lines_);
                for (col_type col = 0; col < ncols(); ++col) 
-                       os << cell(index(row, col)) << eocString(col).c_str();
-               os << eolString(row).c_str();
+                       os << cell(index(row, col)) << eocString(col);
+               os << eolString(row);
        }
-       string s = verboseHLine(rowinfo_[nrows()].lines_);
-       if (s.size())
-               os << "\\\\" << s.c_str();
+       string const s = verboseHLine(rowinfo_[nrows()].lines_);
+       if (!s.empty() && s != " ")
+               os << "\\\\" << s;
+}
+
+
+int MathGridInset::colsep() const
+{
+       return 6;
+}
+
+
+int MathGridInset::rowsep() const
+{
+       return 6;
 }
 
 
+int MathGridInset::hlinesep() const
+{
+       return 3;
+}
+
+
+int MathGridInset::vlinesep() const
+{
+       return 3;
+}
+
+
+int MathGridInset::border() const
+{
+       return 2;
+}