]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
remove old "logic"
[lyx.git] / src / insets / InsetTabular.cpp
index 8a05f057096f95e7a09584b4d7e24bb1be01b15d..2dcd1ff7fc4273a6f150846b884bad958d58f475 100644 (file)
@@ -1035,6 +1035,11 @@ void Tabular::setColumnPWidth(Cursor & cur, idx_type cell,
        col_type const j = cellColumn(cell);
 
        column_info[j].p_width = width;
+       // reset the vertical alignment to top if the fixed with
+       // is removed or zero because only fixed width columns can
+       // have a vertical alignment
+       if (column_info[j].p_width.zero())
+               column_info[j].valignment = LYX_VALIGN_TOP;
        for (row_type i = 0; i < row_info.size(); ++i) {
                idx_type const cell = cellIndex(i, j);
                // because of multicolumns
@@ -1095,15 +1100,6 @@ void Tabular::setAlignSpecial(idx_type cell, docstring const & special,
 }
 
 
-void Tabular::setAllLines(idx_type cell, bool line)
-{
-       setTopLine(cell, line);
-       setBottomLine(cell, line);
-       setRightLine(cell, line);
-       setLeftLine(cell, line);
-}
-
-
 void Tabular::setTopLine(idx_type i, bool line)
 {
        cellInfo(i).top_line = line;
@@ -1306,17 +1302,6 @@ Tabular::col_type Tabular::cellColumn(idx_type cell) const
 }
 
 
-Tabular::col_type Tabular::cellRightColumn(idx_type cell) const
-{
-       row_type const row = cellRow(cell);
-       col_type column = cellColumn(cell);
-       while (column < column_info.size() - 1 &&
-                  cell_info[row][column + 1].multicolumn == CELL_PART_OF_MULTICOLUMN)
-               ++column;
-       return column;
-}
-
-
 void Tabular::write(ostream & os) const
 {
        // header line
@@ -1528,19 +1513,8 @@ void Tabular::read(Lexer & lex)
 
 bool Tabular::isMultiColumn(idx_type cell) const
 {
-       if (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
-               || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN)
-               return true;
-       else
-               return false;
-}
-
-
-bool Tabular::isMultiColumnReal(idx_type cell) const
-{
-       return cellColumn(cell) != cellRightColumn(cell) &&
-               (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN
-                || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
+       return (cellInfo(cell).multicolumn == CELL_BEGIN_OF_MULTICOLUMN 
+               || cellInfo(cell).multicolumn == CELL_PART_OF_MULTICOLUMN);
 }
 
 
@@ -1552,10 +1526,16 @@ Tabular::CellData & Tabular::cellInfo(idx_type cell) const
 
 void Tabular::setMultiColumn(idx_type cell, idx_type number)
 {
+       idx_type const col = cellColumn(cell);
+       idx_type const row = cellRow(cell);
+       for (idx_type i = 0; i < number; ++i)
+               unsetMultiRow(cellIndex(row, col + i));
+
        CellData & cs = cellInfo(cell);
        cs.multicolumn = CELL_BEGIN_OF_MULTICOLUMN;
        cs.alignment = column_info[cellColumn(cell)].alignment;
        setRightLine(cell, rightLine(cell + number - 1));
+
        for (idx_type i = 1; i < number; ++i) {
                CellData & cs1 = cellInfo(cell + i);
                cs1.multicolumn = CELL_PART_OF_MULTICOLUMN;
@@ -1575,9 +1555,12 @@ bool Tabular::isMultiRow(idx_type cell) const
 
 void Tabular::setMultiRow(idx_type cell, idx_type number)
 {
-       idx_type const column = cellColumn(cell);
-       idx_type row = cellRow(cell);
-       idx_type const ncolumns = column_info.size();
+       idx_type const col = cellColumn(cell);
+       idx_type const row = cellRow(cell);
+       for (idx_type i = 0; i < number; ++i)
+               unsetMultiColumn(cellIndex(row + i, col));
+
+       idx_type const ncols = column_info.size();
        CellData & cs = cellInfo(cell);
        cs.multirow = CELL_BEGIN_OF_MULTIROW;
        // FIXME: the horizontal alignment can only be changed for
@@ -1585,9 +1568,10 @@ void Tabular::setMultiRow(idx_type cell, idx_type number)
        // assigning this to uwestoehr
        cs.valignment = LYX_VALIGN_MIDDLE;
        // set the bottom row of the last selected cell
-       setBottomLine(cell, bottomLine(cell + (number - 1)*ncolumns));
+       setBottomLine(cell, bottomLine(cell + (number - 1)*ncols));
+
        for (idx_type i = 1; i < number; ++i) {
-               CellData & cs1 = cell_info[row + i][column];
+               CellData & cs1 = cell_info[row + i][col];
                cs1.multirow = CELL_PART_OF_MULTIROW;
                cs.inset->appendParagraphs(cs1.inset->paragraphs());
                cs1.inset->clear();
@@ -1622,22 +1606,28 @@ Tabular::idx_type Tabular::rowSpan(idx_type cell) const
 
 void Tabular::unsetMultiColumn(idx_type cell)
 {
+       if (!isMultiColumn(cell))
+               return;
+
        row_type const row = cellRow(cell);
-       col_type const column = cellColumn(cell);
+       col_type const col = cellColumn(cell);
        row_type const span = columnSpan(cell);
        for (col_type c = 0; c < span; ++c)
-               cell_info[row][column + c].multicolumn = CELL_NORMAL;
+               cell_info[row][col + c].multicolumn = CELL_NORMAL;
        updateIndexes();
 }
 
 
 void Tabular::unsetMultiRow(idx_type cell)
 {
+       if (!isMultiRow(cell))
+               return;
+
        row_type const row = cellRow(cell);
-       col_type const column = cellColumn(cell);
+       col_type const col = cellColumn(cell);
        row_type const span = rowSpan(cell);
        for (row_type r = 0; r < span; ++r)
-               cell_info[row + r][column].multirow = CELL_NORMAL;
+               cell_info[row + r][col].multirow = CELL_NORMAL;
        updateIndexes();
 }
 
@@ -2950,12 +2940,12 @@ void Tabular::plaintext(odocstream & os,
        vector<unsigned int> clen(column_info.size());
 
        if (!onlydata) {
-               // first all non (real) multicolumn cells!
+               // first all non multicolumn cells!
                for (col_type j = 0; j < column_info.size(); ++j) {
                        clen[j] = 0;
                        for (row_type i = 0; i < row_info.size(); ++i) {
                                idx_type cell = cellIndex(i, j);
-                               if (isMultiColumnReal(cell))
+                               if (isMultiColumn(cell))
                                        continue;
                                odocstringstream sstr;
                                cellInset(cell)->plaintext(sstr, runparams);
@@ -2963,11 +2953,11 @@ void Tabular::plaintext(odocstream & os,
                                        clen[j] = sstr.str().length();
                        }
                }
-               // then all (real) multicolumn cells!
+               // then all multicolumn cells!
                for (col_type j = 0; j < column_info.size(); ++j) {
                        for (row_type i = 0; i < row_info.size(); ++i) {
                                idx_type cell = cellIndex(i, j);
-                               if (!isMultiColumnReal(cell) || isPartOfMultiColumn(i, j))
+                               if (cell_info[i][j].multicolumn != CELL_BEGIN_OF_MULTICOLUMN)
                                        continue;
                                odocstringstream sstr;
                                cellInset(cell)->plaintext(sstr, runparams);
@@ -3494,7 +3484,7 @@ void InsetTabular::edit(Cursor & cur, bool front, EntryDirection)
 }
 
 
-void InsetTabular::updateLabels(ParIterator const & it, UpdateType utype)
+void InsetTabular::updateBuffer(ParIterator const & it, UpdateType utype)
 {
        // In a longtable, tell captions what the current float is
        Counters & cnts = buffer().masterBuffer()->params().documentClass().counters();
@@ -3506,7 +3496,7 @@ void InsetTabular::updateLabels(ParIterator const & it, UpdateType utype)
        it2.forwardPos();
        size_t const end = it2.nargs();
        for ( ; it2.idx() < end; it2.top().forwardIdx())
-               buffer().updateLabels(it2, utype);
+               buffer().updateBuffer(it2, utype);
 
        //reset afterwards
        if (tabular.is_long_tabular)
@@ -4135,6 +4125,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                case Tabular::M_VALIGN_TOP:
                        flag = false;
                case Tabular::VALIGN_TOP:
+                       status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
                        status.setOnOff(
                                tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_TOP);
                        break;
@@ -4142,6 +4133,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                case Tabular::M_VALIGN_BOTTOM:
                        flag = false;
                case Tabular::VALIGN_BOTTOM:
+                       status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
                        status.setOnOff(
                                tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_BOTTOM);
                        break;
@@ -4149,6 +4141,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                case Tabular::M_VALIGN_MIDDLE:
                        flag = false;
                case Tabular::VALIGN_MIDDLE:
+                       status.setEnabled(!tabular.getPWidth(cur.idx()).zero());
                        status.setOnOff(
                                tabular.getVAlignment(cur.idx(), flag) == Tabular::LYX_VALIGN_MIDDLE);
                        break;
@@ -4258,6 +4251,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                // only one row can be the caption
+               // and a multirow cannot be set as caption
                case Tabular::TOGGLE_LTCAPTION:
                        status.setEnabled(sel_row_start == sel_row_end
                                && !tabular.getRowOfLTFirstHead(sel_row_start, dummyltt)
@@ -4265,7 +4259,8 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                                && !tabular.getRowOfLTFoot(sel_row_start, dummyltt)
                                && !tabular.getRowOfLTLastFoot(sel_row_start, dummyltt)
                                && (!tabular.haveLTCaption()
-                                       || tabular.ltCaption(sel_row_start)));
+                                       || tabular.ltCaption(sel_row_start))
+                               && !tabular.isMultiRow(sel_row_start));
                        status.setOnOff(tabular.ltCaption(sel_row_start));
                        break;
 
@@ -5034,9 +5029,13 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                setLines = true;
        case Tabular::UNSET_ALL_LINES:
                for (row_type i = sel_row_start; i <= sel_row_end; ++i)
-                       for (col_type j = sel_col_start; j <= sel_col_end; ++j)
-                               tabular.setAllLines(
-                                       tabular.cellIndex(i,j), setLines);
+                       for (col_type j = sel_col_start; j <= sel_col_end; ++j) {
+                               idx_type const cell = tabular.cellIndex(i,j);
+                               tabular.setTopLine(cell, setLines);
+                               tabular.setBottomLine(cell, setLines);
+                               tabular.setRightLine(cell, setLines);
+                               tabular.setLeftLine(cell, setLines);
+                       }
                break;
 
        case Tabular::SET_BORDER_LINES:
@@ -5420,12 +5419,8 @@ void InsetTabular::getSelection(Cursor & cur,
        CursorSlice const & end = cur.selEnd();
        cs = tabular.cellColumn(beg.idx());
        ce = tabular.cellColumn(end.idx());
-       if (cs > ce) {
-               ce = cs;
-               cs = tabular.cellColumn(end.idx());
-       } else {
-               ce = tabular.cellRightColumn(end.idx());
-       }
+       if (cs > ce)
+               swap(cs, ce);
 
        rs = tabular.cellRow(beg.idx());
        re = tabular.cellRow(end.idx());