]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetTabular.cpp
Allow LyX format to be written to View>Source window.
[lyx.git] / src / insets / InsetTabular.cpp
index 01ee12c516513731fb0d65f5fecd49d49e046bd7..8e796dbbf9561832593ecaab76b0c3fe7fe86cf1 100644 (file)
@@ -149,12 +149,12 @@ TabularFeature tabularFeature[] =
        { Tabular::UNSET_LONGTABULAR, "unset-longtabular", false },
        { Tabular::SET_PWIDTH, "set-pwidth", true },
        { Tabular::SET_MPWIDTH, "set-mpwidth", true },
-       { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", false },
-       { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", false },
-       { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", false },
-       { Tabular::SET_ROTATE_CELL, "set-rotate-cell", false },
-       { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", false },
-       { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", false },
+       { Tabular::SET_ROTATE_TABULAR, "set-rotate-tabular", true },
+       { Tabular::UNSET_ROTATE_TABULAR, "unset-rotate-tabular", true },
+       { Tabular::TOGGLE_ROTATE_TABULAR, "toggle-rotate-tabular", true },
+       { Tabular::SET_ROTATE_CELL, "set-rotate-cell", true },
+       { Tabular::UNSET_ROTATE_CELL, "unset-rotate-cell", true },
+       { Tabular::TOGGLE_ROTATE_CELL, "toggle-rotate-cell", true },
        { Tabular::SET_USEBOX, "set-usebox", true },
        { Tabular::SET_LTHEAD, "set-lthead", true },
        { Tabular::UNSET_LTHEAD, "unset-lthead", true },
@@ -165,6 +165,7 @@ TabularFeature tabularFeature[] =
        { Tabular::SET_LTLASTFOOT, "set-ltlastfoot", true },
        { Tabular::UNSET_LTLASTFOOT, "unset-ltlastfoot", true },
        { Tabular::SET_LTNEWPAGE, "set-ltnewpage", false },
+       { Tabular::UNSET_LTNEWPAGE, "unset-ltnewpage", false },
        { Tabular::TOGGLE_LTCAPTION, "toggle-ltcaption", false },
        { Tabular::SET_LTCAPTION, "set-ltcaption", false },
        { Tabular::UNSET_LTCAPTION, "unset-ltcaption", false },
@@ -565,7 +566,7 @@ Tabular::CellData::CellData(Buffer * buf)
          left_line(false),
          right_line(false),
          usebox(BOX_NONE),
-         rotate(false),
+         rotate(0),
          inset(new InsetTableCell(buf))
 {
        inset->setBuffer(*buf);
@@ -686,7 +687,7 @@ void Tabular::init(Buffer * buf, row_type rows_arg,
        tabular_valignment = LYX_VALIGN_MIDDLE;
        tabular_width = Length();
        longtabular_alignment = LYX_LONGTABULAR_ALIGN_CENTER;
-       rotate = false;
+       rotate = 0;
        use_booktabs = false;
        // set silly default lines
        for (row_type r = 0; r < nrows(); ++r)
@@ -700,43 +701,6 @@ void Tabular::init(Buffer * buf, row_type rows_arg,
 }
 
 
-void Tabular::appendRow(idx_type const cell)
-{
-       row_type const row = cellRow(cell);
-
-       row_info.insert(row_info.begin() + row + 1, RowData());
-       row_info[row + 1] = row_info[row];
-
-       cell_info.insert(cell_info.begin() + row + 1,
-               cell_vector(ncols(), CellData(buffer_)));
-       for (col_type c = 0; c < ncols(); ++c) {
-               if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
-                       cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
-               else
-                       cell_info[row + 1][c].multirow = cell_info[row][c].multirow;
-       }
-       updateIndexes();
-
-       for (col_type c = 0; c < ncols(); ++c) {
-               if (isPartOfMultiRow(row, c))
-                       continue;
-               // inherit line settings
-               idx_type const i = cellIndex(row + 1, c);
-               idx_type const j = cellIndex(row, c);
-               setLeftLine(i, leftLine(j));
-               setRightLine(i, rightLine(j));
-               setTopLine(i, topLine(j));
-               if (topLine(j) && bottomLine(j)) {
-                       setBottomLine(i, true);
-                       setBottomLine(j, false);
-               }
-               // mark track changes
-               if (buffer().params().trackChanges)
-                       cellInfo(i).inset->setChange(Change(Change::INSERTED));
-       }
-}
-
-
 void Tabular::deleteRow(row_type const row)
 {
        // Not allowed to delete last row
@@ -759,44 +723,46 @@ void Tabular::deleteRow(row_type const row)
 
 void Tabular::copyRow(row_type const row)
 {
-       row_info.insert(row_info.begin() + row, row_info[row]);
-       cell_info.insert(cell_info.begin() + row, cell_info[row]);
+       insertRow(row, true);
+}
 
-       if (buffer().params().trackChanges)
-               for (col_type c = 0; c < ncols(); ++c)
-                       cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
 
-       updateIndexes();
+void Tabular::appendRow(row_type row)
+{
+       insertRow(row, false);
 }
 
 
-void Tabular::appendColumn(idx_type const cell)
+void Tabular::insertRow(row_type const row, bool copy)
 {
-       col_type const c = cellColumn(cell);
+       row_info.insert(row_info.begin() + row + 1, RowData(row_info[row]));
+       cell_info.insert(cell_info.begin() + row + 1, 
+               cell_vector(0, CellData(buffer_)));
        
-       column_info.insert(column_info.begin() + c + 1, ColumnData());
-       column_info[c + 1] = column_info[c];
-
-       for (row_type r = 0; r < nrows(); ++r) {
-               cell_info[r].insert(cell_info[r].begin() + c + 1, 
-                       CellData(buffer_));
-               if (cell_info[r][c].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
-                       cell_info[r][c + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
-               else
-                       cell_info[r][c + 1].multicolumn = cell_info[r][c].multicolumn;
+       for (col_type c = 0; c < ncols(); ++c) {
+               cell_info[row + 1].insert(cell_info[row + 1].begin() + c,
+                       copy ? CellData(cell_info[row][c]) : CellData(buffer_));
+               if (buffer().params().trackChanges)
+                       cell_info[row + 1][c].inset->setChange(Change(Change::INSERTED));
+               if (cell_info[row][c].multirow == CELL_BEGIN_OF_MULTIROW)
+                       cell_info[row + 1][c].multirow = CELL_PART_OF_MULTIROW;
        }
+       
        updateIndexes();
-       for (row_type r = 0; r < nrows(); ++r) {
+       for (col_type c = 0; c < ncols(); ++c) {
+               if (isPartOfMultiRow(row, c))
+                       continue;
                // inherit line settings
-               idx_type const i = cellIndex(r, c + 1);
-               idx_type const j = cellIndex(r, c);
-               setBottomLine(i, bottomLine(j));
-               setTopLine(i, topLine(j));
+               idx_type const i = cellIndex(row + 1, c);
+               idx_type const j = cellIndex(row, c);
                setLeftLine(i, leftLine(j));
-               if (rightLine(j) && rightLine(j)) {
-                       setRightLine(i, true);
-                       setRightLine(j, false);
+               setRightLine(i, rightLine(j));
+               setTopLine(i, topLine(j));
+               if (topLine(j) && bottomLine(j)) {
+                       setBottomLine(i, true);
+                       setBottomLine(j, false);
                }
+               // mark track changes
                if (buffer().params().trackChanges)
                        cellInfo(i).inset->setChange(Change(Change::INSERTED));
        }
@@ -824,16 +790,45 @@ void Tabular::deleteColumn(col_type const col)
 
 
 void Tabular::copyColumn(col_type const col)
+{
+       insertColumn(col, true);
+}
+
+
+void Tabular::appendColumn(col_type col)
+{      
+       insertColumn(col, false);
+}
+
+
+void Tabular::insertColumn(col_type const col, bool copy)
 {
        BufferParams const & bp = buffer().params();
-       column_info.insert(column_info.begin() + col, column_info[col]);
+       column_info.insert(column_info.begin() + col + 1, ColumnData(column_info[col]));
 
        for (row_type r = 0; r < nrows(); ++r) {
-               cell_info[r].insert(cell_info[r].begin() + col, cell_info[r][col]);
+               cell_info[r].insert(cell_info[r].begin() + col + 1,
+                       copy ? CellData(cell_info[r][col]) : CellData(buffer_));
                if (bp.trackChanges)
                        cell_info[r][col + 1].inset->setChange(Change(Change::INSERTED));
+               if (cell_info[r][col].multicolumn == CELL_BEGIN_OF_MULTICOLUMN)
+                       cell_info[r][col + 1].multicolumn = CELL_PART_OF_MULTICOLUMN;
        }
        updateIndexes();
+       for (row_type r = 0; r < nrows(); ++r) {
+               // inherit line settings
+               idx_type const i = cellIndex(r, col + 1);
+               idx_type const j = cellIndex(r, col);
+               setBottomLine(i, bottomLine(j));
+               setTopLine(i, topLine(j));
+               setLeftLine(i, leftLine(j));
+               if (rightLine(j) && rightLine(j)) {
+                       setRightLine(i, true);
+                       setRightLine(j, false);
+               }
+               if (buffer().params().trackChanges)
+                       cellInfo(i).inset->setChange(Change(Change::INSERTED));
+       }
 }
 
 
@@ -1394,7 +1389,7 @@ void Tabular::write(ostream & os) const
           << ">\n";
        // global longtable options
        os << "<features"
-          << write_attribute("rotate", rotate)
+          << write_attribute("rotate", convert<string>(rotate))
           << write_attribute("booktabs", use_booktabs)
           << write_attribute("islongtable", is_long_tabular)
           << write_attribute("firstHeadTopDL", endfirsthead.topDL)
@@ -1747,13 +1742,13 @@ void Tabular::unsetMultiRow(idx_type cell)
 }
 
 
-void Tabular::setRotateCell(idx_type cell, bool flag)
+void Tabular::setRotateCell(idx_type cell, int value)
 {
-       cellInfo(cell).rotate = flag;
+       cellInfo(cell).rotate = value;
 }
 
 
-bool Tabular::getRotateCell(idx_type cell) const
+int Tabular::getRotateCell(idx_type cell) const
 {
        return cellInfo(cell).rotate;
 }
@@ -1765,7 +1760,7 @@ bool Tabular::needRotating() const
                return true;
        for (row_type r = 0; r < nrows(); ++r)
                for (col_type c = 0; c < ncols(); ++c)
-                       if (cell_info[r][c].rotate)
+                       if (cell_info[r][c].rotate != 0)
                                return true;
        return false;
 }
@@ -2321,9 +2316,9 @@ void Tabular::TeXCellPreamble(otexstream & os, idx_type cell,
                os << "{";
        } // end if ismultirow
 
-       if (getRotateCell(cell)) {
-               os << "\\begin{sideways}\n";
-       }
+       if (getRotateCell(cell) != 0)
+               os << "\\begin{turn}{" << convert<string>(getRotateCell(cell)) << "}\n";
+
        if (getUsebox(cell) == BOX_PARBOX) {
                os << "\\parbox[";
                switch (valign) {
@@ -2370,8 +2365,8 @@ void Tabular::TeXCellPostamble(otexstream & os, idx_type cell,
                os << '}';
        else if (getUsebox(cell) == BOX_MINIPAGE)
                os << breakln << "\\end{minipage}";
-       if (getRotateCell(cell))
-               os << breakln << "\\end{sideways}";
+       if (getRotateCell(cell) != 0)
+               os << breakln << "\\end{turn}";
        if (ismultirow)
                os << '}';
        if (ismulticol)
@@ -2605,8 +2600,8 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
        if (runparams.lastid != -1)
                os.texrow().start(runparams.lastid, runparams.lastpos);
 
-       if (rotate)
-               os << "\\begin{sideways}\n";
+       if (rotate != 0)
+               os << "\\begin{turn}{" << convert<string>(rotate) << "}\n";
 
        if (is_long_tabular) {
                os << "\\begin{longtable}";
@@ -2730,8 +2725,8 @@ void Tabular::latex(otexstream & os, OutputParams const & runparams) const
                        os << "\\end{tabular}";
        }
 
-       if (rotate)
-               os << breakln << "\\end{sideways}";
+       if (rotate != 0)
+               os << breakln << "\\end{turn}";
 }
 
 
@@ -4550,8 +4545,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
 
                case Tabular::TOGGLE_ROTATE_TABULAR:
                case Tabular::SET_ROTATE_TABULAR:
-                       status.setEnabled(tabular.tabular_width.zero());
-                       status.setOnOff(tabular.rotate);
+                       status.setOnOff(tabular.rotate != 0);
                        break;
 
                case Tabular::TABULAR_VALIGN_TOP:
@@ -4584,7 +4578,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                case Tabular::UNSET_ROTATE_TABULAR:
-                       status.setOnOff(!tabular.rotate);
+                       status.setOnOff(tabular.rotate == 0);
                        break;
 
                case Tabular::TOGGLE_ROTATE_CELL:
@@ -4610,6 +4604,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                case Tabular::UNSET_LTFIRSTHEAD:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTFirstHead(sel_row_start, dummyltt));
                        break;
 
@@ -4619,6 +4614,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                case Tabular::UNSET_LTHEAD:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTHead(sel_row_start, dummyltt));
                        break;
 
@@ -4628,6 +4624,7 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                case Tabular::UNSET_LTFOOT:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTFoot(sel_row_start, dummyltt));
                        break;
 
@@ -4637,12 +4634,16 @@ bool InsetTabular::getStatus(Cursor & cur, FuncRequest const & cmd,
                        break;
 
                case Tabular::UNSET_LTLASTFOOT:
+                       status.setEnabled(sel_row_start == sel_row_end && !tabular.ltCaption(sel_row_start));
                        status.setOnOff(!tabular.getRowOfLTLastFoot(sel_row_start, dummyltt));
                        break;
 
                case Tabular::SET_LTNEWPAGE:
                        status.setOnOff(tabular.getLTNewPage(sel_row_start));
                        break;
+               case Tabular::UNSET_LTNEWPAGE:
+                       status.setOnOff(!tabular.getLTNewPage(sel_row_start));
+                       break;
 
                // only one row in head/firsthead/foot/lasthead can be the caption
                // and a multirow cannot be set as caption
@@ -4895,7 +4896,7 @@ int InsetTabular::dist(BufferView & bv, idx_type const cell, int x, int y) const
        int const xend = xbeg + tabular.cellWidth(cell);
        row_type const row = tabular.cellRow(cell);
        int const ybeg = o.y_ - tabular.rowAscent(row)
-               - tabular.interRowSpace(row);
+               - tabular.interRowSpace(row) - tabular.textVOffset(cell);
        int const yend = ybeg + tabular.cellHeight(cell);
 
        if (x < xbeg)
@@ -5146,7 +5147,6 @@ bool InsetTabular::tabularFeatures(Cursor & cur, string const & argument)
                for (; tabularFeature[i].action != Tabular::LAST_ACTION; ++i) {
                        if (s != tabularFeature[i].feature)
                                continue;
-
                        action = tabularFeature[i].action;
                        break;
                }
@@ -5189,12 +5189,17 @@ bool InsetTabular::oneCellHasRotationState(bool rotated,
 {
        for (row_type r = row_start; r <= row_end; ++r)
                for (col_type c = col_start; c <= col_end; ++c)
-                       if (tabular.getRotateCell(tabular.cellIndex(r, c)) == rotated)
-                               return true;
-
+                       if (rotated) {
+                               if (tabular.getRotateCell(tabular.cellIndex(r, c)) != 0)
+                                       return true;
+                       } else {
+                               if (tabular.getRotateCell(tabular.cellIndex(r, c)) == 0)
+                                       return true;
+                       }
        return false;
 }
 
+
 void InsetTabular::tabularFeatures(Cursor & cur,
        Tabular::Feature feature, string const & value)
 {
@@ -5291,12 +5296,12 @@ void InsetTabular::tabularFeatures(Cursor & cur,
 
        case Tabular::APPEND_ROW:
                // append the row into the tabular
-               tabular.appendRow(cur.idx());
+               tabular.appendRow(row);
                break;
 
        case Tabular::APPEND_COLUMN:
                // append the column into the tabular
-               tabular.appendColumn(cur.idx());
+               tabular.appendColumn(column);
                cur.idx() = tabular.cellIndex(row, column);
                break;
 
@@ -5548,15 +5553,16 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                break;
 
        case Tabular::SET_ROTATE_TABULAR:
-               tabular.rotate = true;
+               tabular.rotate = convert<int>(value);
                break;
 
        case Tabular::UNSET_ROTATE_TABULAR:
-               tabular.rotate = false;
+               tabular.rotate = 0;
                break;
 
        case Tabular::TOGGLE_ROTATE_TABULAR:
-               tabular.rotate = !tabular.rotate;
+               // when pressing the rotate button we default to 90° rotation
+               tabular.rotate != 0 ? tabular.rotate = 0 : tabular.rotate = 90;
                break;
 
        case Tabular::TABULAR_VALIGN_TOP:
@@ -5588,13 +5594,13 @@ void InsetTabular::tabularFeatures(Cursor & cur,
        case Tabular::SET_ROTATE_CELL:
                for (row_type r = sel_row_start; r <= sel_row_end; ++r)
                        for (col_type c = sel_col_start; c <= sel_col_end; ++c)
-                               tabular.setRotateCell(tabular.cellIndex(r, c), true);
+                               tabular.setRotateCell(tabular.cellIndex(r, c), convert<int>(value));
                break;
 
        case Tabular::UNSET_ROTATE_CELL:
                for (row_type r = sel_row_start; r <= sel_row_end; ++r)
                        for (col_type c = sel_col_start; c <= sel_col_end; ++c)
-                               tabular.setRotateCell(tabular.cellIndex(r, c), false);
+                               tabular.setRotateCell(tabular.cellIndex(r, c), 0);
                break;
 
        case Tabular::TOGGLE_ROTATE_CELL:
@@ -5603,9 +5609,13 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                        sel_row_start, sel_row_end, sel_col_start, sel_col_end);
 
                for (row_type r = sel_row_start; r <= sel_row_end; ++r)
-                       for (col_type c = sel_col_start; c <= sel_col_end; ++c)
-                               tabular.setRotateCell(tabular.cellIndex(r, c),
-                                                                         oneNotRotated);
+                       for (col_type c = sel_col_start; c <= sel_col_end; ++c) {
+                               // when pressing the rotate cell button we default to 90° rotation
+                               if (oneNotRotated)
+                                       tabular.setRotateCell(tabular.cellIndex(r, c), 90);
+                               else
+                                       tabular.setRotateCell(tabular.cellIndex(r, c), 0);
+                       }
                }
                break;
 
@@ -5651,8 +5661,10 @@ void InsetTabular::tabularFeatures(Cursor & cur,
                tabular.setLTFoot(row, flag, ltt, true);
                break;
 
+       case Tabular::UNSET_LTNEWPAGE:
+               flag = false;
        case Tabular::SET_LTNEWPAGE:
-               tabular.setLTNewPage(row, !tabular.getLTNewPage(row));
+               tabular.setLTNewPage(row, flag);
                break;
 
        case Tabular::SET_LTCAPTION: {