]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiBox.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiBox.cpp
index 91243256fd1a30235ccf462b7abdc85f7d47ea96..6a991b5df5063e4fc6a61827c70397038ed76466 100644 (file)
@@ -3,9 +3,10 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Jürgen Vigna (Minipage stuff)
+ * \author Jürgen Vigna (Minipage stuff)
  * \author Martin Vermeer
- * \author Jürgen Spitzmüller
+ * \author Jürgen Spitzmüller
+ * \author Uwe Stöhr
  *
  * Full author contact details are available in file CREDITS.
  */
 
 #include "GuiBox.h"
 
-#include "FuncRequest.h"
-#include "support/gettext.h"
-
+#include "GuiApplication.h"
+#include "ColorCache.h"
+#include "ColorSet.h"
 #include "LengthCombo.h"
 #include "Length.h"
 #include "qt_helpers.h"
-#include "LyXRC.h" // to set the default length values
 #include "Validator.h"
 
 #include "insets/InsetBox.h"
 
+#include "support/gettext.h"
 #include "support/lstrings.h"
 
-#include <QPushButton>
+#include <QComboBox>
 #include <QLineEdit>
-#include <QCloseEvent>
+#include <QPushButton>
+
+#ifdef IN
+#undef IN
+#endif
 
 using namespace std;
 
+
 namespace lyx {
 namespace frontend {
 
+static QStringList boxGuiIds()
+{
+       return QStringList()
+               << "Frameless" << "Boxed"
+               << "ovalbox" << "Ovalbox"
+               << "Shadowbox" << "Shaded"
+               << "Doublebox";
+}
+
+
+static QStringList boxGuiNames()
+{
+       return QStringList()
+               << qt_("No frame") << qt_("Simple rectangular frame")
+               << qt_("Oval frame, thin") << qt_("Oval frame, thick")
+               << qt_("Drop shadow") << qt_("Shaded background")
+               << qt_("Double rectangular frame");
+}
+
 
-void box_gui_tokens(vector<string> & ids, vector<docstring> & gui_names)
+static QStringList boxGuiSpecialLengthIds()
 {
-       char const * const ids_[] = {
-               "Frameless", "Boxed", "ovalbox",
-               "Ovalbox", "Shadowbox", "Shaded", "Doublebox"};
-       size_t const ids_size = sizeof(ids_) / sizeof(char *);
-       ids = vector<string>(ids_, ids_ + ids_size);
-       gui_names.clear();
-       gui_names.push_back(_("No frame"));
-       gui_names.push_back(_("Simple rectangular frame"));
-       gui_names.push_back(_("Oval frame, thin"));
-       gui_names.push_back(_("Oval frame, thick"));
-       gui_names.push_back(_("Drop shadow"));
-       gui_names.push_back(_("Shaded background"));
-       gui_names.push_back(_("Double rectangular frame"));
+       return QStringList() << "height" << "depth"
+               << "totalheight" << "width";
 }
 
 
-void box_gui_tokens_special_length(vector<string> & ids,
-       vector<docstring> & gui_names)
+static QStringList boxGuiSpecialLengthNames()
 {
-       char const * const ids_[] = {
-               "none", "height", "depth",
-               "totalheight", "width"};
-       size_t const ids_size = sizeof(ids_) / sizeof(char *);
-       ids = vector<string>(ids_, ids_ + ids_size);
-       gui_names.clear();
-       gui_names.push_back(_("None"));
-       gui_names.push_back(_("Height"));
-       gui_names.push_back(_("Depth"));
-       gui_names.push_back(_("Total Height"));
-       gui_names.push_back(_("Width"));
+       return QStringList() << qt_("Height") << qt_("Depth")
+               << qt_("Total Height") << qt_("Width");
 }
 
 
-GuiBox::GuiBox(GuiView & lv)
-       : GuiDialog(lv, "box"), params_("")
+static QList<ColorCode> colors()
+{
+       QList<ColorCode> colors;
+       colors << Color_black;
+       colors << Color_white;
+       colors << Color_blue;
+       colors << Color_brown;
+       colors << Color_cyan;
+       colors << Color_darkgray;
+       colors << Color_gray;
+       colors << Color_green;
+       colors << Color_lightgray;
+       colors << Color_lime;
+       colors << Color_magenta;
+       colors << Color_olive;
+       colors << Color_orange;
+       colors << Color_pink;
+       colors << Color_purple;
+       colors << Color_red;
+       colors << Color_teal;
+       colors << Color_violet;
+       colors << Color_yellow;
+       return colors;
+}
+
+
+GuiBox::GuiBox(QWidget * parent) : InsetParamsWidget(parent)
 {
        setupUi(this);
-       setViewTitle(_("Box Settings"));
 
        // fill the box type choice
-       box_gui_tokens(ids_, gui_names_);
-       for (unsigned int i = 0; i < gui_names_.size(); ++i)
-               typeCO->addItem(toqstr(gui_names_[i]));
+       ids_ = boxGuiIds();
+       gui_names_ = boxGuiNames();
+       for (int i = 0; i != ids_.size(); ++i)
+               typeCO->addItem(gui_names_[i], ids_[i]);
 
        // add the special units to the height choice
        // width needs different handling
-       box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
-       for (unsigned int i = 1; i < gui_names_spec_.size(); ++i)
-               heightUnitsLC->addItem(toqstr(gui_names_spec_[i]));
+       ids_spec_ = boxGuiSpecialLengthIds();
+       gui_names_spec_ = boxGuiSpecialLengthNames();
+       for (int i = 0; i != ids_spec_.size(); ++i)
+               heightUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
 
-       connect(restorePB, SIGNAL(clicked()), this, SLOT(slotRestore()));
-       connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
-       connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
-       connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
-
-       connect(widthED, SIGNAL(textChanged(QString)),
-               this, SLOT(change_adaptor()));
+       connect(widthED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
        connect(widthUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
-               this, SLOT(change_adaptor()));
-       connect(valignCO, SIGNAL(highlighted(QString)),
-               this, SLOT(change_adaptor()));
-       connect(heightCB, SIGNAL(stateChanged(int)),
-               this, SLOT(change_adaptor()));
-       connect(heightED, SIGNAL(textChanged(const QString &)),
-               this, SLOT(change_adaptor()));
-       connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT) ),
-               this, SLOT(change_adaptor()));
-       connect(restorePB, SIGNAL(clicked()), this, SLOT(restoreClicked()));
-       connect(typeCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
-       connect(typeCO, SIGNAL(activated(int)), this, SLOT(typeChanged(int)));
-       connect(halignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
-       connect(ialignCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
-       connect(innerBoxCO, SIGNAL(activated(const QString&)),
-               this, SLOT(innerBoxChanged(const QString &)));
-       connect(innerBoxCO, SIGNAL(activated(int)), this, SLOT(change_adaptor()));
-       connect(pagebreakCB, SIGNAL(stateChanged(int)),
-               this, SLOT(pagebreakClicked()));
+               this, SIGNAL(changed()));
+       connect(valignCO, SIGNAL(highlighted(QString)), this, SIGNAL(changed()));
+       connect(heightED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
+       connect(heightUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SIGNAL(changed()));
+       connect(halignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
+       connect(ialignCO, SIGNAL(activated(int)), this, SIGNAL(changed()));
+       connect(thicknessED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
+       connect(thicknessUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SIGNAL(changed()));
+       connect(separationED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
+       connect(separationUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SIGNAL(changed()));
+       connect(shadowsizeED, SIGNAL(textChanged(QString)), this, SIGNAL(changed()));
+       connect(shadowsizeUnitsLC, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SIGNAL(changed()));
+       connect(backgroundColorCO, SIGNAL(currentIndexChanged(int)),
+               this, SIGNAL(changed()));
 
        heightED->setValidator(unsignedLengthValidator(heightED));
        widthED->setValidator(unsignedLengthValidator(widthED));
-
-       bc().setPolicy(ButtonPolicy::OkApplyCancelReadOnlyPolicy);
-
-       bc().addReadOnly(typeCO);
-       bc().addReadOnly(innerBoxCO);
-       bc().addReadOnly(valignCO);
-       bc().addReadOnly(ialignCO);
-       bc().addReadOnly(halignCO);
-       bc().addReadOnly(widthED);
-       bc().addReadOnly(widthUnitsLC);
-       bc().addReadOnly(heightCB);
-       bc().addReadOnly(heightED);
-       bc().addReadOnly(heightUnitsLC);
-       bc().addReadOnly(pagebreakCB);
-
-       bc().setRestore(restorePB);
-       bc().setOK(okPB);
-       bc().setApply(applyPB);
-       bc().setCancel(closePB);
+       thicknessED->setValidator(unsignedLengthValidator(thicknessED));
+       separationED->setValidator(unsignedLengthValidator(separationED));
+       shadowsizeED->setValidator(unsignedLengthValidator(shadowsizeED));
 
        // initialize the length validator
-       bc().addCheckedLineEdit(widthED, widthLA);
-       bc().addCheckedLineEdit(heightED, heightCB);
+       addCheckedWidget(widthED, widthCB);
+       addCheckedWidget(heightED, heightCB);
+       addCheckedWidget(thicknessED, thicknessLA);
+       addCheckedWidget(separationED, separationLA);
+       addCheckedWidget(shadowsizeED, shadowsizeLA);
+
+       // the background can be uncolored while the frame cannot
+       color_codes_ = colors();
+       qSort(color_codes_.begin(), color_codes_.end(), ColorSorter);
+       fillComboColor(backgroundColorCO, true);
+       fillComboColor(frameColorCO, false);
+
+       initDialog();
 }
 
 
-void GuiBox::closeEvent(QCloseEvent * e)
+void GuiBox::fillComboColor(QComboBox * combo, bool const is_none)
 {
-       slotClose();
-       e->accept();
+       combo->clear();
+       QPixmap coloritem(32, 32);
+       QColor color;
+       // frameColorCO cannot be uncolored
+       if (is_none)
+               combo->addItem(toqstr(translateIfPossible(lcolor.getGUIName(Color_none))),
+                              toqstr(lcolor.getLaTeXName(Color_none)));
+       QList<ColorCode>::const_iterator cit = color_codes_.begin();
+       for (; cit != color_codes_.end(); ++cit) {
+               QString const latexname = toqstr(lcolor.getLaTeXName(*cit));
+               QString const guiname = toqstr(translateIfPossible(lcolor.getGUIName(*cit)));
+               color = QColor(guiApp->colorCache().get(*cit, false));
+               coloritem.fill(color);
+               combo->addItem(QIcon(coloritem), guiname, latexname);
+       }
 }
 
 
-void GuiBox::change_adaptor()
+void GuiBox::on_innerBoxCO_activated(int index)
 {
+       QString itype = innerBoxCO->itemData(index).toString();
+       // handle parbox and minipage the same way
+       bool const ibox = (itype != "none" && itype != "makebox");
+       if (heightCB->isChecked() && !ibox)
+               heightCB->setChecked(false);
+       widthCB->setChecked(!widthED->text().isEmpty());
+       setSpecial(ibox);
        changed();
 }
 
 
-void GuiBox::innerBoxChanged(const QString & str)
+void GuiBox::on_typeCO_activated(int index)
 {
-       bool const ibox = (str != qt_("None"));
-       valignCO->setEnabled(ibox);
-       ialignCO->setEnabled(ibox);
-       halignCO->setEnabled(!ibox);
-       heightCB->setEnabled(ibox);
-       if (heightCB->checkState() == Qt::Checked && ibox) {
-               heightED->setEnabled(true);
-               heightUnitsLC->setEnabled(true);
+       QString const type =
+               typeCO->itemData(index).toString();
+       bool const frameless = (type == "Frameless");
+       QString itype =
+               innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
+       setInnerType(frameless, itype);
+       // refresh itype because it might have been changed in setInnerType
+       itype =
+               innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
+       // handle parbox and minipage the same way
+       bool const ibox = (itype != "none" && itype != "makebox");
+       if (frameless && itype != "makebox") {
+               if (heightCB->isChecked() && !ibox)
+                       heightCB->setChecked(false);
+               setSpecial(ibox);
        }
-       setSpecial(ibox);
+       if (type != "Boxed") {
+               if (type != "Frameless")
+                       widthCB->setChecked(itype != "none");
+               pagebreakCB->setChecked(false);
+       }
+       // assure that the frame color is black for frameless boxes to
+       // provide the color "none"
+       int const b = frameColorCO->findData("black");
+       if (frameless && frameColorCO->currentIndex() != b)
+               frameColorCO->setCurrentIndex(b);
+       changed();
 }
 
 
-void GuiBox::typeChanged(int index)
+void GuiBox::on_frameColorCO_currentIndexChanged(int index)
 {
-       bool const frameless = (index == 0);
-       if (frameless) {
-               valignCO->setEnabled(true);
-               ialignCO->setEnabled(true);
-               halignCO->setEnabled(false);
-               heightCB->setEnabled(true);
-               heightED->setEnabled(true);
-               heightUnitsLC->setEnabled(true);
-               setSpecial(true);
+       // if there is a non-black frame color the background cannot be uncolored
+       // therefore remove the entry "none" in this case
+       int const n = backgroundColorCO->findData("none");
+       if (index != frameColorCO->findData("black")) {
+               if (n != -1) {
+                       if (backgroundColorCO->currentIndex() == n)
+                               backgroundColorCO->setCurrentIndex(
+                                           backgroundColorCO->findData("white"));
+                       backgroundColorCO->removeItem(n);
+               }
+       } else {
+               if (n == -1)
+                       backgroundColorCO->insertItem(0, toqstr(translateIfPossible((lcolor.getGUIName(Color_none)))),
+                                                     toqstr(lcolor.getLaTeXName(Color_none)));
        }
-       if (index != 1)
-               pagebreakCB->setChecked(false);
-       pagebreakCB->setEnabled(index == 1);
-       int itype = innerBoxCO->currentIndex();
-       setInnerType(frameless, itype);
+       changed();
 }
 
 
-void GuiBox::restoreClicked()
+void GuiBox::initDialog()
 {
-       setInnerType(true, 2);
+       setInnerType(true, toqstr("minipage"));
        widthED->setText("100");
+       widthCB->setChecked(true);
+       widthCB->setEnabled(false);
        widthUnitsLC->setCurrentItem(Length::PCW);
-       heightCB->setCheckState(Qt::Checked);
        heightED->setText("1");
-       for (int j = 0; j < heightUnitsLC->count(); j++) {
-               if (heightUnitsLC->itemText(j) == qt_("Total Height"))
-                       heightUnitsLC->setCurrentItem(j);
-       }
+       heightUnitsLC->setCurrentItem("totalheight");
+       // LaTeX's default for \fboxrule is 0.4 pt
+       thicknessED->setText("0.4");
+       thicknessUnitsLC->setCurrentItem(Length::PT);
+       // LaTeX's default for \fboxsep is 3 pt
+       separationED->setText("3");
+       separationUnitsLC->setCurrentItem(Length::PT);
+       // LaTeX's default for \shadowsize is 4 pt
+       shadowsizeED->setText("4");
+       shadowsizeUnitsLC->setCurrentItem(Length::PT);
+}
+
+
+void GuiBox::on_widthCB_stateChanged(int)
+{
+       changed();
+}
+
+
+void GuiBox::on_heightCB_stateChanged(int /*state*/)
+{
+       changed();
 }
 
 
-void GuiBox::pagebreakClicked()
+void GuiBox::on_pagebreakCB_stateChanged()
 {
        bool pbreak = (pagebreakCB->checkState() == Qt::Checked);
-       innerBoxCO->setEnabled(!pbreak);
-       if (pbreak) {
-               valignCO->setEnabled(false);
-               ialignCO->setEnabled(false);
-               halignCO->setEnabled(false);
-               heightCB->setEnabled(false);
-               heightED->setEnabled(false);
-               heightUnitsLC->setEnabled(false);
-               setSpecial(false);
-       } else
-               typeChanged(typeCO->currentIndex());
-       change_adaptor();
+       if (pbreak)
+               widthCB->setChecked(!pbreak);
+       if (!pbreak) {
+               on_typeCO_activated(typeCO->currentIndex());
+               return;
+       }
+       setSpecial(false);
+       changed();
 }
 
-void GuiBox::updateContents()
+
+void GuiBox::paramsToDialog(Inset const * inset)
 {
-       string type = params_.type;
+       InsetBox const * box = static_cast<InsetBox const *>(inset);
+       InsetBoxParams const & params = box->params();
+       QString type = toqstr(params.type);
        if (type == "Framed") {
                pagebreakCB->setChecked(true);
                type = "Boxed";
-       } else
+       } else {
                pagebreakCB->setChecked(false);
-
-       pagebreakCB->setEnabled(type == "Boxed");
-
-       for (unsigned int i = 0; i < gui_names_.size(); ++i) {
-               if (type == ids_[i])
-                       typeCO->setCurrentIndex(i);
        }
 
+       typeCO->setCurrentIndex(typeCO->findData(type));
+
        // default: minipage
-       unsigned int inner_type = 2;
-       if (!params_.inner_box)
-               // none
-               inner_type = 0;
-       if (params_.use_parbox)
-               // parbox
-               inner_type = 1;
-       bool frameless = (params_.type == "Frameless");
+       QString inner_type = "minipage";
+       if (!params.inner_box)
+               inner_type = "none";
+       if (params.use_parbox)
+               inner_type = "parbox";
+       if (params.use_makebox)
+               inner_type = "makebox";
+       bool const frameless = (params.type == "Frameless");
        setInnerType(frameless, inner_type);
 
-       char c = params_.pos;
+       char c = params.pos;
        valignCO->setCurrentIndex(string("tcb").find(c, 0));
-       c = params_.inner_pos;
+       c = params.inner_pos;
        ialignCO->setCurrentIndex(string("tcbs").find(c, 0));
-       c = params_.hor_pos;
+       c = params.hor_pos;
        halignCO->setCurrentIndex(string("lcrs").find(c, 0));
 
-       bool ibox = params_.inner_box;
+       bool ibox = (params.inner_box && !params.use_makebox);
        valignCO->setEnabled(ibox);
        ialignCO->setEnabled(ibox);
-       halignCO->setEnabled(!ibox);
        setSpecial(ibox);
 
-       Length::UNIT default_unit =
-               (lyxrc.default_papersize > 3) ? Length::CM : Length::IN;
-
-       lengthToWidgets(widthED, widthUnitsLC,
-               (params_.width).asString(), default_unit);
-
-       string const special = params_.special;
-       if (!special.empty() && special != "none") {
-               QString spc;
-               for (unsigned int i = 0; i < gui_names_spec_.size(); i++) {
-                       if (special == ids_spec_[i])
-                               spc = toqstr(gui_names_spec_[i].c_str());
-               }
-               for (int j = 0; j < widthUnitsLC->count(); j++) {
-                       if (widthUnitsLC->itemText(j) == spc)
-                               widthUnitsLC->setCurrentIndex(j);
-               }
+       // halign is only allowed without inner box and if a width is used and if
+       // pagebreak is not used
+       halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
+                            && ((!ibox && type == "Boxed") || inner_type == "makebox"));
+       // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
+       if ((inner_type == "makebox" || (type == "Boxed" && inner_type == "none"))
+               && halignCO->count() < 4)
+               halignCO->addItem(qt_("Stretch"));
+       else if (inner_type != "makebox" && (type != "Boxed" && inner_type != "none"))
+               halignCO->removeItem(3);
+       // pagebreak is only allowed for Boxed without inner box
+       pagebreakCB->setEnabled(!ibox && type == "Boxed");
+
+       Length::UNIT const default_unit = Length::defaultUnit();
+
+       // the width can only be selected for makebox or framebox
+       widthCB->setEnabled(inner_type == "makebox"
+                           || (type == "Boxed"
+                               && !ibox && !pagebreakCB->isChecked()));
+       if (params.width.empty()) {
+               widthCB->setChecked(false);
+               lengthToWidgets(widthED, widthUnitsLC,
+                       params.width, default_unit);
+       } else {
+               widthCB->setChecked(true);
+               lengthToWidgets(widthED, widthUnitsLC,
+                       params.width, default_unit);
+               QString const special = toqstr(params.special);
+               if (!special.isEmpty() && special != "none")
+                       widthUnitsLC->setCurrentItem(special);
        }
 
+       widthED->setEnabled(widthCB->isChecked());
+       widthUnitsLC->setEnabled(widthCB->isChecked());
+
        lengthToWidgets(heightED, heightUnitsLC,
-               (params_.height).asString(), default_unit);
-       
-       string const height_special = params_.height_special;
-       if (!height_special.empty() && height_special != "none") {
-               QString hspc;
-               for (unsigned int i = 0; i != gui_names_spec_.size(); i++) {
-                       if (height_special == ids_spec_[i])
-                               hspc = toqstr(gui_names_spec_[i].c_str());
-               }
-               for (int j = 0; j != heightUnitsLC->count(); j++) {
-                       if (heightUnitsLC->itemText(j) == hspc)
-                               heightUnitsLC->setCurrentIndex(j);
-               }
-       }
-       // set no optional height when the value is the default "1\height"
+               (params.height).asString(), default_unit);
+
+       QString const height_special = toqstr(params.height_special);
+       if (!height_special.isEmpty() && height_special != "none")
+               heightUnitsLC->setCurrentItem(height_special);
+       // set no optional height if the value is the default "1\height"
        // (special units like \height are handled as "in",
-       if (height_special == "totalheight" &&  
-               params_.height == Length("1in"))
+       // FIXME: this is a very bad UI, this check box should be disabled in
+       // this case, not forced to 'unchecked' state.
+       if (height_special == "totalheight" && params.height == Length("1in"))
                heightCB->setCheckState(Qt::Unchecked);
        else
                heightCB->setCheckState(Qt::Checked);
 
        heightCB->setEnabled(ibox);
+
+       // enable line thickness only for the rectangular frame types and drop shadow
+       thicknessED->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
+       thicknessUnitsLC->setEnabled(type == "Boxed" || type == "Doublebox" || type == "Shadowbox");
+       lengthToWidgets(thicknessED, thicknessUnitsLC,
+               (params.thickness).asString(), default_unit);
+       // enable line separation for the allowed frame types
+       separationED->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
+               || type == "Doublebox" || type == "Shadowbox");
+       separationUnitsLC->setEnabled(type == "Boxed" || type == "ovalbox" || type == "Ovalbox"
+               || type == "Doublebox" || type == "Shadowbox");
+       lengthToWidgets(separationED, separationUnitsLC,
+               (params.separation).asString(), default_unit);
+       // enable shadow size for drop shadow
+       shadowsizeED->setEnabled(type == "Shadowbox");
+       shadowsizeUnitsLC->setEnabled(type == "Shadowbox");
+       lengthToWidgets(shadowsizeED, shadowsizeUnitsLC,
+               (params.shadowsize).asString(), default_unit);
+       // set color
+       frameColorCO->setCurrentIndex(frameColorCO->findData(toqstr(params.framecolor)));
+       backgroundColorCO->setCurrentIndex(backgroundColorCO->findData(toqstr(params.backgroundcolor)));
 }
 
 
-void GuiBox::applyView()
+docstring GuiBox::dialogToParams() const
 {
-       bool pagebreak = pagebreakCB->isChecked();
+       bool const pagebreak =
+               pagebreakCB->isEnabled() && pagebreakCB->isChecked();
+       string box_type;
        if (pagebreak)
-               params_.type = "Framed";
+               box_type = "Framed";
        else
-               params_.type = ids_[typeCO->currentIndex()];
-
-       params_.inner_box = (!pagebreak && innerBoxCO->currentText() != qt_("None"));
-       params_.use_parbox = (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
-
-       params_.pos = "tcb"[valignCO->currentIndex()];
-       params_.inner_pos = "tcbs"[ialignCO->currentIndex()];
-       params_.hor_pos = "lcrs"[halignCO->currentIndex()];
-
-       int i = 0;
-       bool spec = false;
-       QString special = widthUnitsLC->currentText();
+               box_type = fromqstr(typeCO->itemData(
+                               typeCO->currentIndex()).toString());
+
+       InsetBoxParams params(box_type);
+       params.inner_box =
+               (!pagebreak && innerBoxCO->currentText() != qt_("None"));
+       params.use_parbox =
+               (!pagebreak && innerBoxCO->currentText() == qt_("Parbox"));
+       params.use_makebox =
+               (!pagebreak && innerBoxCO->currentText() == qt_("Makebox"));
+
+       params.pos = "tcb"[valignCO->currentIndex()];
+       params.inner_pos = "tcbs"[ialignCO->currentIndex()];
+       params.hor_pos = "lcrs"[halignCO->currentIndex()];
+
+       QString unit =
+               widthUnitsLC->itemData(widthUnitsLC->currentIndex()).toString();
        QString value = widthED->text();
-       if (special == qt_("Height")) {
-               i = 1;
-               spec = true;
-       } else if (special == qt_("Depth")) {
-               i = 2;
-               spec = true;
-       } else if (special == qt_("Total Height")) {
-               i = 3;
-               spec = true;
-       } else if (special == qt_("Width")) {
-               i = 4;
-               spec = true;
-       }
-       // the user might insert a non-special value in the line edit
-       if (isValidLength(fromqstr(value))) {
-               i = 0;
-               spec = false;
-       }
-       params_.special = ids_spec_[i];
 
-       string width;
-       if (spec) {
-               width = fromqstr(value);
-               // beware: bogosity! the unit is simply ignored in this case
-               width += "in";
+       if (widthED->isEnabled()) {
+               if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
+                       params.special = fromqstr(unit);
+                       // Note: the unit is simply ignored in this case
+                       params.width = Length(widgetToDouble(widthED), Length::IN);
+               } else {
+                       params.special = "none";
+                       // we must specify a valid length in this case
+                       if (value.isEmpty())
+                               widthED->setText("0");
+                       params.width = Length(widgetsToLength(widthED, widthUnitsLC));
+               }
        } else {
-               width = widgetsToLength(widthED, widthUnitsLC);
+               params.special = "none";
+               params.width = Length();
        }
 
-       params_.width = Length(width);
-
-       i = 0;
-       spec = false;
-       special = heightUnitsLC->currentText();
-       value = heightED->text();
-       if (special == qt_("Height")) {
-               i = 1;
-               spec = true;
-       } else if (special == qt_("Depth")) {
-               i = 2;
-               spec = true;
-       } else if (special == qt_("Total Height")) {
-               i = 3;
-               spec = true;
-       } else if (special == qt_("Width")) {
-               i = 4;
-               spec = true;
-       }
-       // the user might insert a non-special value in the line edit
-       if (isValidLength(fromqstr(value))) {
-               i = 0;
-               spec = false;
+       // the height parameter is omitted if the value
+       // is "1in" and "Total Height" is used as unit.
+       // 1in + "Total Height" means "1\height" which is the LaTeX default
+       // if no height is given
+       if (heightCB->checkState() == Qt::Unchecked) {
+               params.height = Length("1in");
+               params.height_special = "totalheight";
+       } else {
+               unit = heightUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
+               value = heightED->text();
+               if (ids_spec_.contains(unit) && !isValidLength(fromqstr(value))) {
+                       params.height_special = fromqstr(unit);
+                       // Note: the unit is simply ignored in this case
+                       params.height = Length(widgetToDouble(heightED), Length::IN);
+               } else {
+                       params.height_special = "none";
+                       params.height =
+                               Length(widgetsToLength(heightED, heightUnitsLC));
+               }
        }
-       params_.height_special = ids_spec_[i];
 
-       string height;
-       if (spec  && !isValidLength(fromqstr(heightED->text()))) {
-               height = fromqstr(value);
-               // beware: bogosity! the unit is simply ignored in this case
-               height += "in";
-       } else
-               height = widgetsToLength(heightED, heightUnitsLC);
+       // handle the line thickness, line separation and shadow size
+       if (thicknessED->isEnabled())
+               params.thickness = Length(widgetsToLength(thicknessED, thicknessUnitsLC));
+       else
+               params.thickness = Length("0.4pt");
+       if (separationED->isEnabled())
+               params.separation = Length(widgetsToLength(separationED, separationUnitsLC));
+       else
+               params.separation = Length("3pt");
+       if (separationED->isEnabled())
+               params.shadowsize = Length(widgetsToLength(shadowsizeED, shadowsizeUnitsLC));
+       else
+               params.shadowsize = Length("4pt");
+       if (frameColorCO->isEnabled())
+               params.framecolor =
+                       fromqstr(frameColorCO->itemData(frameColorCO->currentIndex()).toString());
+       else
+               params.framecolor = "black";
+       if (backgroundColorCO->isEnabled())
+               params.backgroundcolor =
+                       fromqstr(backgroundColorCO->itemData(backgroundColorCO->currentIndex()).toString());
+       else
+               params.backgroundcolor = "none";
 
-       // the height parameter is omitted in InsetBox.cpp when the value
-       // is "1in" and "Total Height" is used as unit.
-       // 1in + "Total Height" means "1\height" which is the LaTeX default when
-       // no height is given
-       if (heightCB->checkState() == Qt::Checked)
-               params_.height = Length(height);
-       else {
-               params_.height = Length("1in");
-               params_.height_special = ids_spec_[3];
+       return from_ascii(InsetBox::params2string(params));
+}
+
+
+bool GuiBox::checkWidgets(bool readonly) const
+{
+       typeCO->setEnabled(!readonly);
+
+       if (readonly) {
+               pagebreakCB->setEnabled(false);
+               innerBoxCO->setEnabled(false);
+               valignCO->setEnabled(false);
+               ialignCO->setEnabled(false);
+               halignCO->setEnabled(false);
+               widthCB->setEnabled(false);
+               widthED->setEnabled(false);
+               widthUnitsLC->setEnabled(false);
+               heightED->setEnabled(false);
+               heightUnitsLC->setEnabled(false);
+               heightCB->setEnabled(false);
+               thicknessED->setEnabled(false);
+               thicknessUnitsLC->setEnabled(false);
+               separationED->setEnabled(false);
+               separationUnitsLC->setEnabled(false);
+               shadowsizeED->setEnabled(false);
+               shadowsizeUnitsLC->setEnabled(false);
+       } else {
+               QString const outer =
+                       typeCO->itemData(typeCO->currentIndex()).toString();
+               QString const itype =
+                       innerBoxCO->itemData(innerBoxCO->currentIndex()).toString();
+               bool const ibox = (itype != "none" && itype != "makebox");
+               valignCO->setEnabled(ibox);
+               ialignCO->setEnabled(ibox);
+               if (heightCB->isChecked() && !ibox)
+                       heightCB->setChecked(false);
+               heightCB->setEnabled(ibox);
+               // the width can only be selected for makebox or framebox
+               widthCB->setEnabled(itype == "makebox"
+                       || (outer == "Boxed" && itype == "none" && !pagebreakCB->isChecked()));
+               // except for Frameless and Boxed, the width cannot be specified if
+               // there is no inner box
+               bool const width_enabled =
+                       ibox || outer == "Frameless" || (outer == "Boxed" && !pagebreakCB->isChecked());
+               // enable if width_enabled
+               widthED->setEnabled(width_enabled);
+               widthUnitsLC->setEnabled(width_enabled);
+               if (!widthCB->isChecked() && widthCB->isEnabled()) {
+                       widthED->setEnabled(false);
+                       widthUnitsLC->setEnabled(false);
+               }
+               // halign is only allowed without inner box and if a width is used and if
+               // pagebreak is not used
+               halignCO->setEnabled(!pagebreakCB->isChecked() && widthCB->isChecked()
+                                    && ((!ibox && outer == "Boxed") || itype == "makebox"));
+               // add the entry "Stretch" if the box is \makebox or \framebox and if not already there
+               if ((itype == "makebox" || (outer == "Boxed" && itype == "none"))
+                       && halignCO->count() < 4)
+                       halignCO->addItem(qt_("Stretch"));
+               else if (itype != "makebox" && (outer != "Boxed" && itype != "none"))
+                       halignCO->removeItem(3);
+               // pagebreak is only allowed for Boxed without inner box
+               pagebreakCB->setEnabled(!ibox && outer == "Boxed");
+
+               heightED->setEnabled(itype != "none" && heightCB->isChecked());
+               heightUnitsLC->setEnabled(itype != "none" && heightCB->isChecked());
+               heightCB->setEnabled(ibox);
+
+               // enable line thickness for the rectangular frame types and drop shadow
+               thicknessED->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
+               thicknessUnitsLC->setEnabled(outer == "Boxed" || outer == "Doublebox" || outer == "Shadowbox");
+               // set default values if empty
+               if (thicknessED->text().isEmpty() && thicknessED->isEnabled()) {
+                       thicknessED->setText("0.4");
+                       thicknessUnitsLC->setCurrentItem(Length::PT);
+               }
+               // enable line separation for the allowed frame types
+               separationED->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
+                       || outer == "Doublebox" || outer == "Shadowbox");
+               separationUnitsLC->setEnabled(outer == "Boxed" || outer == "ovalbox" || outer == "Ovalbox"
+                       || outer == "Doublebox" || outer == "Shadowbox");
+               // set default values if empty
+               if (separationED->text().isEmpty() && separationED->isEnabled()) {
+                       separationED->setText("3");
+                       separationUnitsLC->setCurrentItem(Length::PT);
+               }
+               // enable shadow size for drop shadow
+               shadowsizeED->setEnabled(outer == "Shadowbox");
+               shadowsizeUnitsLC->setEnabled(outer == "Shadowbox");
+               // set default values if empty
+               if (shadowsizeED->text().isEmpty() && shadowsizeED->isEnabled()) {
+                       shadowsizeED->setText("4");
+                       shadowsizeUnitsLC->setCurrentItem(Length::PT);
+               }
+               // \fboxcolor and \colorbox cannot be used for fancybox boxes
+               frameColorCO->setEnabled(!pagebreakCB->isChecked() && outer == "Boxed");
+               backgroundColorCO->setEnabled(!pagebreakCB->isChecked() && (frameColorCO->isEnabled() || outer == "Frameless"));
        }
+
+       return InsetParamsWidget::checkWidgets();
 }
 
 
 void GuiBox::setSpecial(bool ibox)
 {
-       box_gui_tokens_special_length(ids_spec_, gui_names_spec_);
+       QString const last_item =
+               widthUnitsLC->itemData(heightUnitsLC->currentIndex()).toString();
+
        // check if the widget contains the special units
-       int count = widthUnitsLC->count();
-       bool has_special = false;
-       for (int i = 0; i < count; i++)
-               if (widthUnitsLC->itemText(i).contains(qt_("Total Height")) > 0)
-                       has_special = true;
+       bool const has_special = (widthUnitsLC->findData("totalheight") != -1);
        // insert 'em if needed...
        if (!ibox && !has_special) {
-               for (unsigned int i = 1; i < gui_names_spec_.size(); i++)
-                       widthUnitsLC->addItem(toqstr(gui_names_spec_[i]));
+               for (int i = 1; i < ids_spec_.size(); ++i)
+                       widthUnitsLC->addItem(gui_names_spec_[i], ids_spec_[i]);
        // ... or remove 'em if needed
        } else if (ibox && has_special) {
-               widthUnitsLC->clear();
-               for (int i = 0; i < num_units; i++)
-                       widthUnitsLC->addItem(qt_(unit_name_gui[i]));
+               for (int i = 1; i < ids_spec_.size(); ++i) {
+                       int n = widthUnitsLC->findData(ids_spec_[i]);
+                       if (n != -1)
+                               widthUnitsLC->removeItem(n);
+               }
        }
+       // restore selected text, if possible
+       widthUnitsLC->setCurrentItem(last_item);
 }
 
 
-void GuiBox::setInnerType(bool frameless, int i)
+void GuiBox::setInnerType(bool frameless, QString const & type)
 {
-       // with "frameless" boxes, inner box is mandatory (i.e. is the actual box)
+       // with "frameless" boxes, inner box is mandatory
+       // (i.e. is the actual box)
        // we have to remove "none" then and adjust the combo
-       if (frameless) {
-               innerBoxCO->clear();
-               innerBoxCO->addItem(qt_("Parbox"));
-               innerBoxCO->addItem(qt_("Minipage"));
-               if (i != 0)
-                       innerBoxCO->setCurrentIndex(i - 1);
-               else
-                       innerBoxCO->setCurrentIndex(i);
-       } else {
-               if (innerBoxCO->count() == 2)
-                       ++i;
-               innerBoxCO->clear();
-               innerBoxCO->addItem(qt_("None"));
-               innerBoxCO->addItem(qt_("Parbox"));
-               innerBoxCO->addItem(qt_("Minipage"));
-               innerBoxCO->setCurrentIndex(i);
-       }
-}
-
-bool GuiBox::initialiseParams(string const & data)
-{
-       InsetBoxMailer::string2params(data, params_);
-       return true;
-
-}
-
-
-void GuiBox::clearParams()
-{
-       params_ = InsetBoxParams("");
-}
-
-
-void GuiBox::dispatchParams()
-{
-       dispatch(FuncRequest(getLfun(), InsetBoxMailer::params2string(params_)));
+       innerBoxCO->clear();
+       if (!frameless)
+               innerBoxCO->addItem(qt_("None"), toqstr("none"));
+       else
+               innerBoxCO->addItem(qt_("Makebox"), toqstr("makebox"));
+       innerBoxCO->addItem(qt_("Parbox"), toqstr("parbox"));
+       innerBoxCO->addItem(qt_("Minipage"), toqstr("minipage"));
+       int i = (innerBoxCO->findData(type) != -1)
+               ? innerBoxCO->findData(type) : 0;
+       innerBoxCO->setCurrentIndex(i);
 }
 
-
-Dialog * createGuiBox(GuiView & lv) { return new GuiBox(lv); }
-
-
 } // namespace frontend
 } // namespace lyx
 
 
-#include "GuiBox_moc.cpp"
+#include "moc_GuiBox.cpp"