]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_gridinset.C
further code uglification to make Jean-Marc's compiler happy
[lyx.git] / src / mathed / math_gridinset.C
index 5b4afb14e29b0cfc72abb8c604814713e696d0f0..8f66538195fca5b52be2ad4278946b9dc8b31b50 100644 (file)
@@ -24,67 +24,107 @@ MathGridInset::RowInfo::RowInfo()
 {}
 
 
+
+int MathGridInset::RowInfo::skipPixels() const
+{
+#ifdef WITH_WARNINGS
+#warning fix this once the interface to LyXLength has improved
+#endif
+       return int(skip_.value());
+}
+
+
+
 MathGridInset::ColInfo::ColInfo()
-       : h_align_('c'), leftline_(false), rightline_(false)
+       : align_('c'), leftline_(false), rightline_(false), skip_(MATH_COLSEP)
 {}
 
 
-MathGridInset::MathGridInset(int m, int n, string const & nm, MathInsetTypes ot)
-       : MathInset(m * n, nm, ot), rowinfo_(n), colinfo_(m), v_align_('c')
+MathGridInset::MathGridInset(unsigned int m, unsigned int n)
+       : MathNestInset(m * n), rowinfo_(n), colinfo_(m), v_align_('c')
 {
        if (m <= 0)
                lyxerr << "positve number of columns expected\n";
        if (n <= 0)
                lyxerr << "positve number of rows expected\n";
+       setDefaults();
 }
 
 
-int MathGridInset::index(int row, int col) const
+unsigned int MathGridInset::index(unsigned int row, unsigned int col) const
 {
        return col + ncols() * row;
 }
 
 
+void MathGridInset::setDefaults()
+{
+       for (unsigned int col = 0; col < ncols(); ++col) {
+               colinfo_[col].align_ = defaultColAlign(col);
+               colinfo_[col].skip_  = defaultColSpace(col);
+       }
+}
+
+
 void MathGridInset::halign(string const & hh)
 {
-       int n = hh.size();
+       unsigned int n = hh.size();
        if (n > ncols())
                n = ncols();
-       for (int i = 0; i < n; ++i)
-               colinfo_[i].h_align_ = hh[i];
+       for (unsigned int i = 0; i < n; ++i)
+               colinfo_[i].align_ = hh[i];
 }
 
-void MathGridInset::halign(char h, int col)
+
+void MathGridInset::halign(char h, unsigned int col)
 {
-       colinfo_[col].h_align_ = h;
+       colinfo_[col].align_ = h;
 }
 
-char MathGridInset::halign(int col) const
+
+char MathGridInset::halign(unsigned int col) const
 {
-       return colinfo_[col].h_align_;
+       return colinfo_[col].align_;
 }
 
+
+
 void MathGridInset::valign(char c)
 {
        v_align_ = c;
 }
 
+
 char MathGridInset::valign() const
 {
        return v_align_;
 }
 
-void MathGridInset::Metrics(MathStyles st, int, int)
+
+
+void MathGridInset::vskip(LyXLength const & skip, unsigned int row)
+{
+       rowinfo_[row].skip_ = skip;
+}
+
+
+LyXLength MathGridInset::vskip(unsigned int row) const
+{
+       return rowinfo_[row].skip_;
+}
+
+
+void MathGridInset::metrics(MathStyles st) const
 {
        // let the cells adjust themselves
-       MathInset::Metrics(st);
+       MathNestInset::metrics(st);
        size_ = st;
 
        // adjust vertical structure
-       for (int row = 0; row < nrows(); ++row) {
+       for (unsigned int row = 0; row < nrows(); ++row) {
                int asc  = 0;
                int desc = 0;
-               for (int col = 0; col < ncols(); ++col) {
+               for (unsigned int col = 0; col < ncols(); ++col) {
                        MathXArray const & c = xcell(index(row, col));
                        asc  = std::max(asc,  c.ascent());
                        desc = std::max(desc, c.descent());
@@ -96,6 +136,7 @@ void MathGridInset::Metrics(MathStyles st, int, int)
                        rowinfo_[row].offset_ = 
                                rowinfo_[row - 1].offset_ +
                                rowinfo_[row - 1].descent_ +
+                               rowinfo_[row - 1].skipPixels() +
                                MATH_ROWSEP +
                                rowinfo_[row].ascent_;
                else 
@@ -115,22 +156,24 @@ void MathGridInset::Metrics(MathStyles st, int, int)
                h = rowinfo_.back().offset_ / 2;
        }
 
-       for (int row = 0; row < nrows(); ++row) {
+       for (unsigned int row = 0; row < nrows(); ++row) {
                rowinfo_[row].offset_ -= h;
                rowinfo_[row].offset_ += MATH_BORDER;
        }
        
        // adjust horizontal structure
-       for (int col = 0; col < ncols(); ++col) {
+       for (unsigned int col = 0; col < ncols(); ++col) {
                int wid  = 0;
-               for (int row = 0; row < nrows(); ++row) 
+               for (unsigned int row = 0; row < nrows(); ++row) 
                        wid = std::max(wid, xcell(index(row, col)).width());
                colinfo_[col].width_  = wid;
                colinfo_[col].offset_ = colinfo_[col].width_;
 
                if (col) 
                        colinfo_[col].offset_ =
-                               colinfo_[col - 1].offset_ + colinfo_[col - 1].width_ + MATH_COLSEP;
+                               colinfo_[col - 1].offset_ +
+                               colinfo_[col - 1].width_ + 
+                               colinfo_[col - 1].skip_;
                else
                        colinfo_[col].offset_ = 0;
 
@@ -144,10 +187,10 @@ void MathGridInset::Metrics(MathStyles st, int, int)
 /*     
        // Increase ws_[i] for 'R' columns (except the first one)
        for (int i = 1; i < nc_; ++i)
-               if (h_align_[i] == 'R')
+               if (align_[i] == 'R')
                        ws_[i] += 10 * df_width;
        // Increase ws_[i] for 'C' column
-       if (h_align_[0] == 'C')
+       if (align_[0] == 'C')
                if (ws_[0] < 7 * workwidth / 8)
                        ws_[0] = 7 * workwidth / 8;
        
@@ -162,7 +205,7 @@ void MathGridInset::Metrics(MathStyles st, int, int)
                                cxrow->setTab(i, df_width);
                                isvoid = true;
                        }
-                       switch (h_align_[i]) {
+                       switch (align_[i]) {
                        case 'l':
                                lf = 0;
                                break;
@@ -193,54 +236,69 @@ void MathGridInset::Metrics(MathStyles st, int, int)
 */
 }
 
-void MathGridInset::draw(Painter & pain, int x, int y)
+
+void MathGridInset::draw(Painter & pain, int x, int y) const
 {
        xo(x);
        yo(y);
-       for (int row = 0; row < nrows(); ++row) {
-               int yy = y + rowinfo_[row].offset_;
-               for (int col = 0; col < ncols(); ++col) {
-                       int xx = x + colinfo_[col].offset_;
-                       char align = colinfo_[col].h_align_;
-                       if (align == 'r' || align == 'R')
-                               xx += colinfo_[col].width_ - xcell(index(row, col)).width(); 
-                       if (align == 'c' || align == 'C')
-                               xx += (colinfo_[col].width_ - xcell(index(row, col)).width()) / 2; 
-                       xcell(index(row, col)).draw(pain, xx, yy);
-               }
-       }
+       for (unsigned int idx = 0; idx < nargs(); ++idx)
+               xcell(idx).draw(pain, x + cellXOffset(idx), y + cellYOffset(idx));
 }
 
 
-void MathGridInset::Write(std::ostream & os, bool fragile) const
+void MathGridInset::write(std::ostream & os, bool fragile) const
 {
-       for (int row = 0; row < nrows(); ++row) {
-               if (row)
-                       os << " \\\\\n";
-               for (int col = 0; col < ncols(); ++col) {
-                       if (col)
-                               os << " & ";
-                       cell(index(row, col)).Write(os, fragile);
+       for (unsigned int row = 0; row < nrows(); ++row) {
+               for (unsigned int col = 0; col < ncols(); ++col) {
+                       cell(index(row, col)).write(os, fragile);
+                       os << eocString(col);
                }
+               os << eolString(row);
        }
 }
 
 
-void MathGridInset::addRow(int row)
+string MathGridInset::eolString(unsigned int row) const
+{
+       if (row + 1 == nrows()) 
+               return "";
+
+       if (rowinfo_[row].skip_.value() != 0)
+               return "\\\\[" + rowinfo_[row].skip_.asLatexString() + "]\n";
+
+       // make sure an upcoming '[' does not break anything
+       MathArray const & c = cell(index(row + 1, 0));
+       if (c.size() && c.begin()->nucleus()->getChar() == '[')
+               return "\\\\[0pt]\n";
+
+       return "\\\\\n";
+}
+
+
+string MathGridInset::eocString(unsigned int col) const
+{
+       if (col + 1 == ncols())
+               return "";
+       return " & ";
+}
+
+
+void MathGridInset::addRow(unsigned int row)
 {
        rowinfo_.insert(rowinfo_.begin() + row + 1, RowInfo());
        cells_.insert(cells_.begin() + (row + 1) * ncols(), ncols(), MathXArray());
 }
 
+
 void MathGridInset::appendRow()
 {
        rowinfo_.push_back(RowInfo());
-       for (int i = 0; i < ncols(); ++i)
+       for (unsigned int i = 0; i < ncols(); ++i)
                cells_.push_back(cells_type::value_type());
 }
 
 
-void MathGridInset::delRow(int row)
+void MathGridInset::delRow(unsigned int row)
 {
        if (nrows() == 1)
                return;
@@ -252,29 +310,32 @@ void MathGridInset::delRow(int row)
 }
 
 
-void MathGridInset::addCol(int newcol)
+void MathGridInset::addCol(unsigned int newcol)
 {
-       int const nc = ncols();
-       int const nr = nrows();
+       unsigned int const nc = ncols();
+       unsigned int const nr = nrows();
        cells_type new_cells((nc + 1) * nr);
        
-       for (int row = 0; row < nr; ++row)
-               for (int col = 0; col < nc; ++col)
+       for (unsigned int row = 0; row < nr; ++row)
+               for (unsigned int col = 0; col < nc; ++col)
                        new_cells[row * (nc + 1) + col + (col > newcol)]
                                = cells_[row * nc + col];
        std::swap(cells_, new_cells);
 
-       colinfo_.insert(colinfo_.begin() + newcol, ColInfo());
+       ColInfo inf;
+       inf.skip_  = defaultColSpace(newcol);
+       inf.align_ = defaultColAlign(newcol);
+       colinfo_.insert(colinfo_.begin() + newcol, inf);
 }
 
 
-void MathGridInset::delCol(int col)
+void MathGridInset::delCol(unsigned int col)
 {
        if (ncols() == 1)
                return;
 
        cells_type tmpcells;
-       for (int i = 0; i < nargs(); ++i) 
+       for (unsigned int i = 0; i < nargs(); ++i) 
                if (i % ncols() != col)
                        tmpcells.push_back(cells_[i]);
        std::swap(cells_, tmpcells);
@@ -283,7 +344,26 @@ void MathGridInset::delCol(int col)
 }
 
 
-bool MathGridInset::idxUp(int & idx, int & pos) const
+int MathGridInset::cellXOffset(unsigned int idx) const
+{
+       unsigned int c = col(idx);
+       int x = colinfo_[c].offset_;
+       char align = colinfo_[c].align_;
+       if (align == 'r' || align == 'R')
+               x += colinfo_[c].width_ - xcell(idx).width(); 
+       if (align == 'c' || align == 'C')
+               x += (colinfo_[c].width_ - xcell(idx).width()) / 2; 
+       return x;
+}
+
+
+int MathGridInset::cellYOffset(unsigned int idx) const
+{
+       return rowinfo_[row(idx)].offset_;
+}
+
+
+bool MathGridInset::idxUp(unsigned int & idx, unsigned int & pos) const
 {
        if (idx < ncols())
                return false;
@@ -293,7 +373,7 @@ bool MathGridInset::idxUp(int & idx, int & pos) const
 }
 
        
-bool MathGridInset::idxDown(int & idx, int & pos) const
+bool MathGridInset::idxDown(unsigned int & idx, unsigned int & pos) const
 {
        if (idx >= ncols() * (nrows() - 1))
                return false;
@@ -303,7 +383,7 @@ bool MathGridInset::idxDown(int & idx, int & pos) const
 }
        
        
-bool MathGridInset::idxLeft(int & idx, int & pos) const
+bool MathGridInset::idxLeft(unsigned int & idx, unsigned int & pos) const
 {
        // leave matrix if on the left hand edge
        if (col(idx) == 0)
@@ -314,7 +394,7 @@ bool MathGridInset::idxLeft(int & idx, int & pos) const
 }
        
        
-bool MathGridInset::idxRight(int & idx, int & pos) const
+bool MathGridInset::idxRight(unsigned int & idx, unsigned int & pos) const
 {
        // leave matrix if on the right hand edge
        if (col(idx) == ncols() - 1)
@@ -325,7 +405,7 @@ bool MathGridInset::idxRight(int & idx, int & pos) const
 }
 
 
-bool MathGridInset::idxFirst(int & idx, int & pos) const
+bool MathGridInset::idxFirst(unsigned int & idx, unsigned int & pos) const
 {
        switch (v_align_) {
                case 't':
@@ -342,7 +422,7 @@ bool MathGridInset::idxFirst(int & idx, int & pos) const
 }
 
 
-bool MathGridInset::idxLast(int & idx, int & pos) const
+bool MathGridInset::idxLast(unsigned int & idx, unsigned int & pos) const
 {
        switch (v_align_) {
                case 't':
@@ -359,7 +439,7 @@ bool MathGridInset::idxLast(int & idx, int & pos) const
 }
 
 
-void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
+void MathGridInset::idxDelete(unsigned int & idx, bool & popit, bool & deleteit)
 {
        popit    = false;
        deleteit = false;
@@ -367,7 +447,7 @@ void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
        // delete entire row if in first cell of empty row
        if (col(idx) == 0 && nrows() > 1) {
                bool deleterow = true;
-               for (int i = idx; i < idx + ncols(); ++i)
+               for (unsigned int i = idx; i < idx + ncols(); ++i)
                        if (cell(i).size()) {
                                deleterow = false;
                                break;
@@ -386,7 +466,7 @@ void MathGridInset::idxDelete(int & idx, bool & popit, bool & deleteit)
 }
 
 
-void MathGridInset::idxDeleteRange(int /*from*/, int /*to*/)
+void MathGridInset::idxDeleteRange(unsigned int /*from*/, unsigned int /*to*/)
 {
 // leave this unimplemented unless someone wants to have it.
 /*
@@ -402,27 +482,28 @@ void MathGridInset::idxDeleteRange(int /*from*/, int /*to*/)
 }
 
 
-MathGridInset::RowInfo const & MathGridInset::rowinfo(int i) const
+MathGridInset::RowInfo const & MathGridInset::rowinfo(unsigned int i) const
 {
        return rowinfo_[i];
 }
 
 
-MathGridInset::RowInfo & MathGridInset::rowinfo(int i)
+MathGridInset::RowInfo & MathGridInset::rowinfo(unsigned int i)
 {
        return rowinfo_[i];
 }
 
 
-std::vector<int> MathGridInset::idxBetween(int from, int to) const
+std::vector<unsigned int>
+       MathGridInset::idxBetween(unsigned int from, unsigned int to) const
 {
-       int r1 = std::min(row(from), row(to));
-       int r2 = std::max(row(from), row(to));
-       int c1 = std::min(col(from), col(to));
-       int c2 = std::max(col(from), col(to));
-       std::vector<int> res;
-       for (int i = r1; i <= r2; ++i)
-               for (int j = c1; j <= c2; ++j)
+       unsigned int r1 = std::min(row(from), row(to));
+       unsigned int r2 = std::max(row(from), row(to));
+       unsigned int c1 = std::min(col(from), col(to));
+       unsigned int c2 = std::max(col(from), col(to));
+       std::vector<unsigned int> res;
+       for (unsigned int i = r1; i <= r2; ++i)
+               for (unsigned int j = c1; j <= c2; ++j)
                        res.push_back(index(i, j));
        return res;
 }