]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiTabular.cpp
booktabs trimming: fix some thinkos
[lyx.git] / src / frontends / qt4 / GuiTabular.cpp
index ef85e10121d6828bcef7e8ef88a03cd6bc881bf2..a09d9df62128efcffe74365dac91e44549024e7e 100644 (file)
@@ -31,7 +31,9 @@
 
 #include "insets/InsetTabular.h"
 
+#include "support/convert.h"
 #include "support/debug.h"
+#include "support/lstrings.h"
 
 #include <QCheckBox>
 #include <QPushButton>
@@ -44,7 +46,10 @@ namespace lyx {
 namespace frontend {
 
 GuiTabular::GuiTabular(QWidget * parent)
-       : InsetParamsWidget(parent)
+       : InsetParamsWidget(parent), firstheader_suppressable_(false),
+         lastfooter_suppressable_(false), orig_leftborder_(GuiSetBorder::LINE_UNDEF),
+         orig_rightborder_(GuiSetBorder::LINE_UNDEF), lastrow_(0)
+
 {
        setupUi(this);
 
@@ -75,13 +80,15 @@ GuiTabular::GuiTabular(QWidget * parent)
        connect(interlinespaceUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SLOT(checkEnabled()));
        connect(booktabsRB, SIGNAL(clicked(bool)),
-               this, SLOT(checkEnabled()));
+               this, SLOT(booktabs_toggled(bool)));
        connect(borderDefaultRB, SIGNAL(clicked(bool)),
-               this, SLOT(checkEnabled()));
+               this, SLOT(nonbooktabs_toggled(bool)));
        connect(borderSetPB, SIGNAL(clicked()),
                this, SLOT(borderSet_clicked()));
        connect(borderUnsetPB, SIGNAL(clicked()),
                this, SLOT(borderUnset_clicked()));
+       connect(resetFormalCB, SIGNAL(clicked()),
+               this, SLOT(checkEnabled()));
        connect(hAlignCO, SIGNAL(activated(int)),
                this, SLOT(checkEnabled()));
        connect(vAlignCO, SIGNAL(activated(int)),
@@ -126,24 +133,36 @@ GuiTabular::GuiTabular(QWidget * parent)
                this, SLOT(checkEnabled()));
        connect(captionStatusCB, SIGNAL(clicked()),
                this, SLOT(checkEnabled()));
-       connect(specialAlignmentED, SIGNAL(editingFinished()),
+       connect(specialAlignmentED, SIGNAL(textEdited(const QString &)),
                this, SLOT(checkEnabled()));
        connect(columnWidthED, SIGNAL(textEdited(const QString &)),
                this, SLOT(checkEnabled()));
        connect(columnWidthUnitLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(topSet(bool)),
+       connect(borders, SIGNAL(topSet()),
+               this, SLOT(checkEnabled()));
+       connect(borders, SIGNAL(bottomSet()),
+               this, SLOT(checkEnabled()));
+       connect(borders, SIGNAL(rightSet()),
+               this, SLOT(checkEnabled()));
+       connect(borders, SIGNAL(leftSet()),
+               this, SLOT(checkEnabled()));
+       connect(borders, SIGNAL(topLTSet()),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(bottomSet(bool)),
+       connect(borders, SIGNAL(topRTSet()),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(rightSet(bool)),
+       connect(borders, SIGNAL(bottomLTSet()),
                this, SLOT(checkEnabled()));
-       connect(borders, SIGNAL(leftSet(bool)),
+       connect(borders, SIGNAL(bottomRTSet()),
                this, SLOT(checkEnabled()));
        connect(rotateTabularCB, SIGNAL(clicked()),
                this, SLOT(checkEnabled()));
+       connect(rotateTabularAngleSB, SIGNAL(valueChanged(int)),
+               this, SLOT(checkEnabled()));
        connect(rotateCellCB, SIGNAL(clicked()),
                this, SLOT(checkEnabled()));
+       connect(rotateCellAngleSB, SIGNAL(valueChanged(int)),
+               this, SLOT(checkEnabled()));
        connect(TableAlignCO, SIGNAL(activated(int)),
                this, SLOT(checkEnabled()));
        connect(longTabularCB, SIGNAL(clicked()),
@@ -194,29 +213,48 @@ void GuiTabular::on_interlinespaceCO_activated(int index)
 }
 
 
-void GuiTabular::checkEnabled()
+void GuiTabular::on_columnTypeCO_activated(int index)
+{
+       checkEnabled();
+       if (index == 2)
+               columnWidthED->setFocus();
+}
+
+
+void GuiTabular::enableWidgets() const
 {
        // if there is a LaTeX argument, the width and alignment will be overwritten
        // therefore disable them in this case
-       columnWidthED->setEnabled(specialAlignmentED->text().isEmpty());
+       bool const fixed = specialAlignmentED->text().isEmpty()
+                       && columnTypeCO->currentIndex() == 2;
+       columnWidthED->setEnabled(fixed);
+       columnWidthUnitLC->setEnabled(fixed);
        // if the column has a width, multirows are always left-aligned
        // therefore disable hAlignCB in this case
        hAlignCO->setEnabled(!(multirowCB->isChecked()
                && !widgetsToLength(columnWidthED, columnWidthUnitLC).empty())
                && specialAlignmentED->text().isEmpty());
-       bool const dalign = 
+       // decimal alignment is only possible for non-multicol and non-multirow cells
+       if ((multicolumnCB->isChecked() || multirowCB->isChecked() || columnTypeCO->currentIndex() == 1)
+               && hAlignCO->findData(toqstr("decimal")))
+               hAlignCO->removeItem(hAlignCO->findData(toqstr("decimal")));
+       else if (!multicolumnCB->isChecked() && !multirowCB->isChecked()
+               && hAlignCO->findData(toqstr("decimal")) == -1)
+               hAlignCO->addItem(qt_("At Decimal Separator"), toqstr("decimal"));
+       bool const dalign =
                hAlignCO->itemData(hAlignCO->currentIndex()).toString() == QString("decimal");
        decimalPointED->setEnabled(dalign);
        decimalLA->setEnabled(dalign);
+       resetFormalCB->setEnabled(booktabsRB->isChecked());
 
-       bool const setwidth = TableAlignCO->currentText() == qt_("Middle")
-               && !longTabularCB->isChecked() && !rotateTabularCB->isChecked();
+       bool const setwidth = TableAlignCO->currentText() == qt_("Middle");
        tabularWidthLA->setEnabled(setwidth);
        tabularWidthED->setEnabled(setwidth);
        tabularWidthUnitLC->setEnabled(setwidth);
 
-       bool const is_tabular_star = !tabularWidthED->text().isEmpty();
-       rotateTabularCB->setDisabled(is_tabular_star);
+       rotateTabularAngleSB->setEnabled(rotateTabularCB->isChecked()
+                                        && !longTabularCB->isChecked());
+       rotateCellAngleSB->setEnabled(rotateCellCB->isChecked());
 
        bool const enable_valign =
                !multirowCB->isChecked()
@@ -234,7 +272,8 @@ void GuiTabular::checkEnabled()
        interlinespaceUnitLC->setEnabled(interlinespaceCO->currentIndex() == 2);
 
        // setting as longtable is not allowed when table is inside a float
-       longTabularCB->setEnabled(!is_tabular_star && funcEnabled(Tabular::SET_LONGTABULAR));
+       bool const is_tabular_star = !tabularWidthED->text().isEmpty();
+       longTabularCB->setEnabled(funcEnabled(Tabular::SET_LONGTABULAR));
        bool const longtabular = longTabularCB->isChecked();
        longtableGB->setEnabled(true);
        newpageCB->setEnabled(longtabular);
@@ -242,6 +281,9 @@ void GuiTabular::checkEnabled()
        // longtables and tabular* cannot have a vertical alignment
        TableAlignLA->setDisabled(is_tabular_star || longtabular);
        TableAlignCO->setDisabled(is_tabular_star || longtabular);
+       // longtable cannot be rotated with rotating package, only
+       // with [pdf]lscape, which only supports 90 deg.
+       rotateTabularAngleSB->setDisabled(longtabular);
 
        // FIXME: This Dialog is really horrible, disabling/enabling a checkbox
        // depending on the cursor position is very very unintuitive...
@@ -298,23 +340,52 @@ void GuiTabular::checkEnabled()
        captionStatusCB->setEnabled(funcEnabled(Tabular::TOGGLE_LTCAPTION)
                && longtabular);
 
-       multicolumnCB->setEnabled(funcEnabled(Tabular::MULTICOLUMN));
-       multirowCB->setEnabled(funcEnabled(Tabular::MULTIROW));
+       multicolumnCB->setEnabled(funcEnabled(Tabular::MULTICOLUMN)
+               && !dalign && !multirowCB->isChecked());
+       multirowCB->setEnabled(funcEnabled(Tabular::MULTIROW)
+               && !dalign && !multicolumnCB->isChecked());
        bool const enable_mr = multirowCB->isChecked();
        multirowOffsetLA->setEnabled(enable_mr);
        multirowOffsetED->setEnabled(enable_mr);
        multirowOffsetUnitLC->setEnabled(enable_mr);
 
+       // Vertical lines cannot be set in formal tables
+       borders->setLeftEnabled(!booktabsRB->isChecked());
+       borders->setRightEnabled(!booktabsRB->isChecked());
+       // Trimming is only allowed in booktabs and if the line is set
+       int const row = tabularRowED->text().toInt();
+       borders->setTopLeftTrimEnabled(booktabsRB->isChecked()
+                                      && borders->topLineSet()
+                                      && row > 1);
+       borders->setTopRightTrimEnabled(booktabsRB->isChecked()
+                                       && borders->topLineSet()
+                                       && row > 1);
+       borders->setBottomLeftTrimEnabled(booktabsRB->isChecked()
+                                         && borders->bottomLineSet()
+                                         && row < lastrow_);
+       borders->setBottomRightTrimEnabled(booktabsRB->isChecked()
+                                          && borders->bottomLineSet()
+                                          && row < lastrow_);
+}
+
+
+void GuiTabular::checkEnabled()
+{
+       enableWidgets();
        changed();
 }
 
 
 void GuiTabular::borderSet_clicked()
 {
-       borders->setTop(true);
-       borders->setBottom(true);
-       borders->setLeft(true);
-       borders->setRight(true);
+       borders->setTop(GuiSetBorder::LINE_SET);
+       borders->setBottom(GuiSetBorder::LINE_SET);
+       borders->setLeft(GuiSetBorder::LINE_SET);
+       borders->setRight(GuiSetBorder::LINE_SET);
+       borders->setTopLeftTrim(GuiSetBorder::LINE_SET);
+       borders->setBottomLeftTrim(GuiSetBorder::LINE_SET);
+       borders->setTopRightTrim(GuiSetBorder::LINE_SET);
+       borders->setBottomRightTrim(GuiSetBorder::LINE_SET);
        // repaint the setborder widget
        borders->update();
        checkEnabled();
@@ -323,16 +394,39 @@ void GuiTabular::borderSet_clicked()
 
 void GuiTabular::borderUnset_clicked()
 {
-       borders->setTop(false);
-       borders->setBottom(false);
-       borders->setLeft(false);
-       borders->setRight(false);
+       borders->setTop(GuiSetBorder::LINE_UNSET);
+       borders->setBottom(GuiSetBorder::LINE_UNSET);
+       borders->setLeft(GuiSetBorder::LINE_UNSET);
+       borders->setRight(GuiSetBorder::LINE_UNSET);
+       borders->setTopLeftTrim(GuiSetBorder::LINE_UNSET);
+       borders->setBottomLeftTrim(GuiSetBorder::LINE_UNSET);
+       borders->setTopRightTrim(GuiSetBorder::LINE_UNSET);
+       borders->setBottomRightTrim(GuiSetBorder::LINE_UNSET);
        // repaint the setborder widget
        borders->update();
        checkEnabled();
 }
 
 
+void GuiTabular::booktabs_toggled(bool const check)
+{
+       // when switching from formal, restore the left/right lines
+       if (!check) {
+               borders->setLeft(orig_leftborder_);
+               borders->setRight(orig_rightborder_);
+       }
+       // repaint the setborder widget
+       borders->update();
+       checkEnabled();
+}
+
+
+void GuiTabular::nonbooktabs_toggled(bool const check)
+{
+       booktabs_toggled(!check);
+}
+
+
 static void setParam(string & param_str, Tabular::Feature f, string const & arg = string())
 {
        param_str += ' ';
@@ -422,8 +516,7 @@ void GuiTabular::setTableAlignment(string & param_str) const
 
 docstring GuiTabular::dialogToParams() const
 {
-       // FIXME: We should use Tabular directly.
-       string param_str = "tabular from-dialog";
+       string param_str = "tabular";
 
        // table width
        string tabwidth = widgetsToLength(tabularWidthED, tabularWidthUnitLC);
@@ -435,16 +528,23 @@ docstring GuiTabular::dialogToParams() const
        // this must be done before applying the column alignment
        // because its value influences the alignment of multirow cells
        string width = widgetsToLength(columnWidthED, columnWidthUnitLC);
-       if (width.empty())
+       if (width.empty() || columnTypeCO->currentIndex() != 2)
                width = "0pt";
        if (multicolumnCB->isChecked())
                setParam(param_str, Tabular::SET_MPWIDTH, width);
        else
                setParam(param_str, Tabular::SET_PWIDTH, width);
 
+       bool const varwidth = specialAlignmentED->text().isEmpty()
+                       && columnTypeCO->currentIndex() == 1;
+       if (varwidth)
+               setParam(param_str, Tabular::TOGGLE_VARWIDTH_COLUMN, "on");
+       else
+               setParam(param_str, Tabular::TOGGLE_VARWIDTH_COLUMN, "off");
+
        // apply the column alignment
        // multirows inherit the alignment from the column; if a column width
-       // is set, multicolumns are always left-aligned so that in this case
+       // is set, multirows are always left-aligned so that in this case
        // its alignment must not be applied (see bug #8084)
        if (!(multirowCB->isChecked() && width != "0pt"))
                setHAlign(param_str);
@@ -511,26 +611,48 @@ docstring GuiTabular::dialogToParams() const
        }
 
        //
-       if (borders->getTop() && borders->getBottom() && borders->getLeft()
-               && borders->getRight())
+       if (resetFormalCB->isChecked())
+               setParam(param_str, Tabular::RESET_FORMAL_DEFAULT);
+       else if (borders->topLineSet() && borders->bottomLineSet() && borders->leftLineSet()
+               && borders->rightLineSet())
                setParam(param_str, Tabular::SET_ALL_LINES);
-       else if (!borders->getTop() && !borders->getBottom() && !borders->getLeft()
-               && !borders->getRight())
+       else if (borders->topLineUnset() && borders->bottomLineUnset() && borders->leftLineUnset()
+               && borders->rightLineUnset())
                setParam(param_str, Tabular::UNSET_ALL_LINES);
        else {
-               setParam(param_str, Tabular::SET_LINE_LEFT,
-                        borders->getLeft() ? "true" : "false");
-               setParam(param_str, Tabular::SET_LINE_RIGHT,
-                        borders->getRight() ? "true" : "false");
-               setParam(param_str, Tabular::SET_LINE_TOP,
-                        borders->getTop() ? "true" : "false");
-               setParam(param_str, Tabular::SET_LINE_BOTTOM,
-                        borders->getBottom() ? "true" : "false");
+               if (borders->getLeft() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_LEFT,
+                                borders->leftLineSet() ? "true" : "false");
+               if (borders->getRight() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_RIGHT,
+                                borders->rightLineSet() ? "true" : "false");
+               if (borders->getTop() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_TOP,
+                                borders->topLineSet() ? "true" : "false");
+               if (borders->getBottom() != GuiSetBorder::LINE_UNDECIDED)
+                       setParam(param_str, Tabular::SET_LINE_BOTTOM,
+                                borders->bottomLineSet() ? "true" : "false");
        }
+       if (borders->topLineLTSet())
+               setParam(param_str, Tabular::SET_LTRIM_TOP, "false");
+       else if (borders->topLineLTUnset())
+               setParam(param_str, Tabular::SET_LTRIM_TOP, "true");
+       if (borders->topLineRTSet())
+               setParam(param_str, Tabular::SET_RTRIM_TOP, "false");
+       else if (borders->topLineRTUnset())
+               setParam(param_str, Tabular::SET_RTRIM_TOP, "true");
+       if (borders->bottomLineLTSet())
+               setParam(param_str, Tabular::SET_LTRIM_BOTTOM, "false");
+       else if (borders->bottomLineLTUnset())
+               setParam(param_str, Tabular::SET_LTRIM_BOTTOM, "true");
+       if (borders->bottomLineRTSet())
+               setParam(param_str, Tabular::SET_RTRIM_BOTTOM, "false");
+       else if (borders->bottomLineRTUnset())
+               setParam(param_str, Tabular::SET_RTRIM_BOTTOM, "true");
 
        // apply the special alignment
        string special = fromqstr(specialAlignmentED->text());
-       if (special.empty())
+       if (support::trim(special).empty())
                special = "none";
        if (multicolumnCB->isChecked())
                setParam(param_str, Tabular::SET_SPECIAL_MULTICOLUMN, special);
@@ -554,16 +676,18 @@ docstring GuiTabular::dialogToParams() const
                setParam(param_str, Tabular::SET_MULTIROW);
        else
                setParam(param_str, Tabular::UNSET_MULTIROW);
-       //
+       // store the table rotation angle
+       string const tabular_angle = convert<string>(rotateTabularAngleSB->value());
        if (rotateTabularCB->isChecked())
-               setParam(param_str, Tabular::SET_ROTATE_TABULAR);
+               setParam(param_str, Tabular::SET_ROTATE_TABULAR, tabular_angle);
        else
-               setParam(param_str, Tabular::UNSET_ROTATE_TABULAR);
-       //
+               setParam(param_str, Tabular::UNSET_ROTATE_TABULAR, tabular_angle);
+       // store the cell rotation angle
+       string const cell_angle = convert<string>(rotateCellAngleSB->value());
        if (rotateCellCB->isChecked())
-               setParam(param_str, Tabular::SET_ROTATE_CELL);
+               setParam(param_str, Tabular::SET_ROTATE_CELL, cell_angle);
        else
-               setParam(param_str, Tabular::UNSET_ROTATE_CELL);
+               setParam(param_str, Tabular::UNSET_ROTATE_CELL, cell_angle);
        //
        if (longTabularCB->isChecked())
                setParam(param_str, Tabular::SET_LONGTABULAR);
@@ -572,6 +696,8 @@ docstring GuiTabular::dialogToParams() const
        //
        if (newpageCB->isChecked())
                setParam(param_str, Tabular::SET_LTNEWPAGE);
+       else
+               setParam(param_str, Tabular::UNSET_LTNEWPAGE);
        //
        if (captionStatusCB->isChecked())
                setParam(param_str, Tabular::SET_LTCAPTION);
@@ -678,6 +804,18 @@ static docstring getAlignSpecial(Tabular const & t, size_t cell, int what)
 }
 
 
+GuiSetBorder::BorderState GuiTabular::borderState(GuiSetBorder::BorderState bs,
+                                                 bool const line)
+{
+       if (bs == GuiSetBorder::LINE_UNDEF)
+               bs = line ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+       else if ((bs == GuiSetBorder::LINE_SET && !line)
+                || (bs == GuiSetBorder::LINE_UNSET && line))
+               bs = GuiSetBorder::LINE_UNDECIDED;
+       return bs;
+}
+
+
 void GuiTabular::paramsToDialog(Inset const * inset)
 {
        InsetTabular const * itab = static_cast<InsetTabular const *>(inset);
@@ -692,6 +830,7 @@ void GuiTabular::paramsToDialog(Inset const * inset)
 
        tabularRowED->setText(QString::number(row + 1));
        tabularColumnED->setText(QString::number(col + 1));
+       lastrow_ = int(tabular.nrows());
 
        bool const multicol = tabular.isMultiColumn(cell);
        multicolumnCB->setChecked(multicol);
@@ -699,15 +838,82 @@ void GuiTabular::paramsToDialog(Inset const * inset)
        bool const multirow = tabular.isMultiRow(cell);
        multirowCB->setChecked(multirow);
 
-       rotateCellCB->setChecked(tabular.getRotateCell(cell));
-       rotateTabularCB->setChecked(tabular.rotate);
+       rotateCellCB->setChecked(tabular.getRotateCell(cell) != 0);
+       if (rotateCellCB->isChecked()) {
+               if (tabular.getRotateCell(cell) != 0)
+                       rotateCellAngleSB->setValue(tabular.getRotateCell(cell));
+               else
+                       rotateCellAngleSB->setValue(90);
+       }
 
        longTabularCB->setChecked(tabular.is_long_tabular);
 
-       borders->setTop(tabular.topLine(cell));
-       borders->setBottom(tabular.bottomLine(cell));
-       borders->setLeft(tabular.leftLine(cell));
-       borders->setRight(tabular.rightLine(cell));
+       rotateTabularCB->setChecked(tabular.rotate != 0);
+       if (rotateTabularCB->isChecked()) {
+               if (longTabularCB->isChecked())
+                       rotateTabularAngleSB->setValue(90);
+               else
+                       rotateTabularAngleSB->setValue(tabular.rotate != 0 ? tabular.rotate : 90);
+       }
+
+       // In what follows, we check the borders of all selected cells,
+       // and if there are diverging settings, we use the LINE_UNDECIDED
+       // border status.
+       GuiSetBorder::BorderState ltop = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState lbottom = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState lleft = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState lright = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState ltop_ltrim = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState ltop_rtrim = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState lbottom_ltrim = GuiSetBorder::LINE_UNDEF;
+       GuiSetBorder::BorderState lbottom_rtrim = GuiSetBorder::LINE_UNDEF;
+       CursorSlice const & beg = bv->cursor().selBegin();
+       CursorSlice const & end = bv->cursor().selEnd();
+       if (beg != end) {
+               Tabular::col_type cs = tabular.cellColumn(beg.idx());
+               Tabular::col_type ce = tabular.cellColumn(end.idx());
+               if (cs > ce)
+                       swap(cs, ce);
+               Tabular::row_type rs = tabular.cellRow(beg.idx());
+               Tabular::row_type re = tabular.cellRow(end.idx());
+               if (rs > re)
+                       swap(rs, re);
+               for (Tabular::row_type r = rs; r <= re; ++r)
+                       for (Tabular::col_type c = cs; c <= ce; ++c) {
+                               idx_type const cc = tabular.cellIndex(r, c);
+                               ltop = borderState(ltop, tabular.topLine(cc));
+                               lbottom = borderState(lbottom, tabular.bottomLine(cc));
+                               lleft = borderState(lleft, tabular.leftLine(cc));
+                               lright = borderState(lright, tabular.rightLine(cc));
+                               ltop_ltrim = borderState(ltop_ltrim, !tabular.topLineTrim(cc).first);
+                               ltop_rtrim = borderState(ltop_rtrim, !tabular.topLineTrim(cc).second);
+                               lbottom_ltrim = borderState(lbottom_ltrim, !tabular.bottomLineTrim(cc).first);
+                               lbottom_rtrim = borderState(lbottom_rtrim, !tabular.bottomLineTrim(cc).second);
+                               // store left/right borders for the case of formal/nonformal switch
+                               orig_leftborder_ = borderState(lleft, tabular.leftLine(cc, true));
+                               orig_rightborder_ = borderState(lright, tabular.rightLine(cc, true));
+                       }
+       } else {
+               ltop = tabular.topLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               lbottom = tabular.bottomLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               lleft = tabular.leftLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               lright = tabular.rightLine(cell) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               ltop_ltrim = tabular.topLineTrim(cell).first ? GuiSetBorder::LINE_UNSET : GuiSetBorder::LINE_SET;
+               ltop_rtrim = tabular.topLineTrim(cell).second ? GuiSetBorder::LINE_UNSET : GuiSetBorder::LINE_SET;
+               lbottom_ltrim = tabular.bottomLineTrim(cell).first ? GuiSetBorder::LINE_UNSET : GuiSetBorder::LINE_SET;
+               lbottom_rtrim = tabular.bottomLineTrim(cell).second ? GuiSetBorder::LINE_UNSET : GuiSetBorder::LINE_SET;
+               // store left/right borders for the case of formal/nonformal switch
+               orig_leftborder_ = tabular.leftLine(cell, true) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+               orig_rightborder_ = tabular.rightLine(cell, true) ? GuiSetBorder::LINE_SET : GuiSetBorder::LINE_UNSET;
+       }
+       borders->setTop(ltop);
+       borders->setBottom(lbottom);
+       borders->setLeft(lleft);
+       borders->setRight(lright);
+       borders->setTopLeftTrim(ltop_ltrim);
+       borders->setTopRightTrim(ltop_rtrim);
+       borders->setBottomLeftTrim(lbottom_ltrim);
+       borders->setBottomRightTrim(lbottom_rtrim);
        // repaint the setborder widget
        borders->update();
 
@@ -717,7 +923,8 @@ void GuiTabular::paramsToDialog(Inset const * inset)
        // Set width and alignment
 
        Length const tabwidth = tabular.tabularWidth();
-       if (tabwidth.zero())
+       if (tabwidth.zero()
+           && !(tabularWidthED->hasFocus() && tabularWidthED->text() == "0"))
                tabularWidthED->clear();
        else
                lengthToWidgets(tabularWidthED, tabularWidthUnitLC,
@@ -734,19 +941,27 @@ void GuiTabular::paramsToDialog(Inset const * inset)
                        Tabular::SET_SPECIAL_COLUMN);
                pwidth = getColumnPWidth(tabular, cell);
        }
+       bool const varwidth = tabular.column_info[tabular.cellColumn(cell)].varwidth;
+       if (varwidth)
+               columnTypeCO->setCurrentIndex(1);
        string colwidth;
-       if (pwidth.zero())
+       if (pwidth.zero()
+           && !(columnWidthED->hasFocus() && columnWidthED->text() == "0")) {
                columnWidthED->clear();
-       else {
+               if (!varwidth)
+                       columnTypeCO->setCurrentIndex(0);
+       } else {
                colwidth = pwidth.asString();
                lengthToWidgets(columnWidthED, columnWidthUnitLC,
                        colwidth, default_unit);
+               columnTypeCO->setCurrentIndex(2);
        }
        Length mroffset;
        if (multirow)
                mroffset = getMROffset(tabular, cell);
        string offset;
-       if (mroffset.zero())
+       if (mroffset.zero()
+           && !(multirowOffsetED->hasFocus() && multirowOffsetED->text() == "0"))
                multirowOffsetED->clear();
        else {
                offset = mroffset.asString();
@@ -805,7 +1020,7 @@ void GuiTabular::paramsToDialog(Inset const * inset)
        hAlignCO->addItem(qt_("Right"), toqstr("right"));
        if (!multicol && !pwidth.zero())
                hAlignCO->addItem(qt_("Justified"), toqstr("justified"));
-       if (!multicol)
+       if (!multicol && !multirow)
                hAlignCO->addItem(qt_("At Decimal Separator"), toqstr("decimal"));
 
        string align;
@@ -827,7 +1042,7 @@ void GuiTabular::paramsToDialog(Inset const * inset)
                }
                case LYX_ALIGN_DECIMAL:
                {
-                       if (!multicol)
+                       if (!multicol && !multirow)
                                align = "decimal";
                        break;
                }
@@ -983,10 +1198,55 @@ void GuiTabular::paramsToDialog(Inset const * inset)
 }
 
 
+bool GuiTabular::checkWidgets(bool readonly) const
+{
+       tabularRowED->setReadOnly(readonly);
+       tabularColumnED->setReadOnly(readonly);
+       tabularWidthED->setReadOnly(readonly);
+       specialAlignmentED->setReadOnly(readonly);
+       columnWidthED->setReadOnly(readonly);
+       multirowOffsetED->setReadOnly(readonly);
+       decimalPointED->setReadOnly(readonly);
+
+       if (readonly) {
+               multicolumnCB->setEnabled(false);
+               multirowCB->setEnabled(false);
+               rotateCellCB->setEnabled(false);
+               rotateCellAngleSB->setEnabled(false);
+               rotateTabularCB->setEnabled(false);
+               rotateTabularAngleSB->setEnabled(false);
+               longTabularCB->setEnabled(false);
+               borders->setEnabled(false);
+               tabularWidthUnitLC->setEnabled(false);
+               columnWidthUnitLC->setEnabled(false);
+               columnTypeCO->setEnabled(false);
+               multirowOffsetUnitLC->setEnabled(false);
+               setBordersGB->setEnabled(false);
+               allBordersGB->setEnabled(false);
+               borderStyleGB->setEnabled(false);
+               booktabsRB->setEnabled(false);
+               topspaceCO->setEnabled(false);
+               topspaceUnitLC->setEnabled(false);
+               bottomspaceCO->setEnabled(false);
+               bottomspaceUnitLC->setEnabled(false);
+               interlinespaceCO->setEnabled(false);
+               interlinespaceUnitLC->setEnabled(false);
+               hAlignCO->setEnabled(false);
+               vAlignCO->setEnabled(false);
+               TableAlignCO->setEnabled(false);
+               longtableGB->setEnabled(false);
+               alignmentGB->setEnabled(false);
+       } else
+               enableWidgets();
+
+       return InsetParamsWidget::checkWidgets();
+}
+
+
 bool GuiTabular::funcEnabled(Tabular::Feature f) const
 {
-       string cmd = "tabular " + featureAsString(f);
-       return getStatus(FuncRequest(LFUN_INSET_MODIFY, cmd)).enabled();
+       FuncRequest r(LFUN_INSET_MODIFY, "tabular for-dialog" + featureAsString(f));
+       return getStatus(r).enabled();
 }