]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiExternal.cpp
Docstringify getLongString in general and preamble snippets in particular
[lyx.git] / src / frontends / qt4 / GuiExternal.cpp
index 10d4f35670cd90dca0ee267c82b75df472551ffc..2e7b6f633c91f3b73d030b3efa6594cbb24ed54e 100644 (file)
@@ -15,7 +15,7 @@
 #include "GuiExternal.h"
 
 #include "FuncRequest.h"
-#include "gettext.h"
+#include "support/gettext.h"
 #include "Length.h"
 #include "LyXRC.h"
 
 #include "insets/ExternalTemplate.h"
 #include "insets/InsetExternal.h"
 
+#include "graphics/epstools.h"
 #include "graphics/GraphicsCache.h"
 #include "graphics/GraphicsCacheItem.h"
 #include "graphics/GraphicsImage.h"
 
 #include "support/convert.h"
-#include "support/FileFilterList.h"
 #include "support/filetools.h"
 #include "support/lstrings.h"
 #include "support/lyxlib.h"
 #include "qt_helpers.h"
 #include "Validator.h"
 
-#include <QCloseEvent>
 #include <QCheckBox>
+#include <QGroupBox>
 #include <QLineEdit>
 #include <QPushButton>
 #include <QTabWidget>
 #include <QTextBrowser>
 
-using std::advance;
-using std::vector;
-using std::string;
-
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
-using support::FileFilterList;
-using support::FileName;
-using support::float_equal;
-using support::isStrDbl;
-using support::makeAbsPath;
-using support::readBB_from_PSFile;
-using support::token;
-using support::trim;
-using support::os::internal_path;
-
 using namespace external;
 
 namespace {
 
-RotationDataType origins_array[] = {
+RotationDataType origins[] = {
        RotationData::DEFAULT,
        RotationData::TOPLEFT,
        RotationData::BOTTOMLEFT,
@@ -82,12 +70,6 @@ RotationDataType origins_array[] = {
 };
 
 
-size_type const origins_array_size =
-sizeof(origins_array) / sizeof(origins_array[0]);
-
-vector<external::RotationDataType> const
-all_origins(origins_array, origins_array + origins_array_size);
-
 // These are the strings, corresponding to the above, that the GUI should
 // use. Note that they can/should be translated.
 char const * const origin_gui_strs[] = {
@@ -97,33 +79,38 @@ char const * const origin_gui_strs[] = {
        N_("Top right"), N_("Bottom right"), N_("Baseline right")
 };
 
+external::Template getTemplate(int i)
+{
+       if (external::TemplateManager::get().getTemplates().empty())
+               return Template();
+       external::TemplateManager::Templates::const_iterator i1
+               = external::TemplateManager::get().getTemplates().begin();
+       advance(i1, i);
+       return i1->second;
+}
+
 } // namespace anon
 
 
 GuiExternal::GuiExternal(GuiView & lv)
-       : GuiDialog(lv, "external"), bbChanged_(false)
+       : GuiDialog(lv, "external", qt_("External Material")), bbChanged_(false)
 {
        setupUi(this);
-       setViewTitle(_("External Material"));
 
        connect(okPB, SIGNAL(clicked()), this, SLOT(slotOK()));
        connect(applyPB, SIGNAL(clicked()), this, SLOT(slotApply()));
        connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
 
-       connect(displayCB, SIGNAL(toggled(bool)),
-               showCO, SLOT(setEnabled(bool)));
-       connect(displayCB, SIGNAL(toggled(bool)),
+       /*
+       connect(displayGB, SIGNAL(toggled(bool)),
                displayscaleED, SLOT(setEnabled(bool)));
-       connect(showCO, SIGNAL(activated(QString)),
-               this, SLOT(change_adaptor()));
+               */
        connect(originCO, SIGNAL(activated(int)),
                this, SLOT(change_adaptor()));
        connect(aspectratioCB, SIGNAL(stateChanged(int)),
                this, SLOT(change_adaptor()));
        connect(browsePB, SIGNAL(clicked()),
                this, SLOT(browseClicked()));
-       connect(editPB, SIGNAL(clicked()),
-               this, SLOT(editClicked()));
        connect(externalCO, SIGNAL(activated(QString)),
                this, SLOT(templateChanged()));
        connect(extraED, SIGNAL(textChanged(QString)),
@@ -134,7 +121,7 @@ GuiExternal::GuiExternal(GuiView & lv)
                this, SLOT(widthUnitChanged()));
        connect(heightUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
                this, SLOT(change_adaptor()));
-       connect(displayCB, SIGNAL(stateChanged(int)),
+       connect(displayGB, SIGNAL(toggled(bool)),
                this, SLOT(change_adaptor()));
        connect(displayscaleED, SIGNAL(textChanged(QString)),
                this, SLOT(change_adaptor()));
@@ -153,6 +140,14 @@ GuiExternal::GuiExternal(GuiView & lv)
        connect(ytED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
        connect(xlED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
        connect(ybED, SIGNAL(textChanged(QString)), this, SLOT(bbChanged()));
+       connect(xrUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
+       connect(ytUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
+       connect(xlUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
+       connect(ybUnitCO, SIGNAL(selectionChanged(lyx::Length::UNIT)),
+               this, SLOT(bbChanged()));
        connect(draftCB, SIGNAL(clicked()), this, SLOT(change_adaptor()));
 
        QIntValidator * validator = new QIntValidator(displayscaleED);
@@ -161,10 +156,10 @@ GuiExternal::GuiExternal(GuiView & lv)
 
        angleED->setValidator(new QDoubleValidator(-360, 360, 2, angleED));
 
-       xlED->setValidator(new QIntValidator(xlED));
-       ybED->setValidator(new QIntValidator(ybED));
-       xrED->setValidator(new QIntValidator(xrED));
-       ytED->setValidator(new QIntValidator(ytED));
+       xlED->setValidator(unsignedLengthValidator(xlED));
+       ybED->setValidator(unsignedLengthValidator(ybED));
+       xrED->setValidator(unsignedLengthValidator(xrED));
+       ytED->setValidator(unsignedLengthValidator(ytED));
 
        widthED->setValidator(unsignedLengthValidator(widthED));
        heightED->setValidator(unsignedLengthValidator(heightED));
@@ -179,12 +174,10 @@ GuiExternal::GuiExternal(GuiView & lv)
 
        bc().addReadOnly(fileED);
        bc().addReadOnly(browsePB);
-       bc().addReadOnly(editPB);
        bc().addReadOnly(externalCO);
        bc().addReadOnly(draftCB);
        bc().addReadOnly(displayscaleED);
-       bc().addReadOnly(showCO);
-       bc().addReadOnly(displayCB);
+       bc().addReadOnly(displayGB);
        bc().addReadOnly(angleED);
        bc().addReadOnly(originCO);
        bc().addReadOnly(heightUnitCO);
@@ -198,6 +191,10 @@ GuiExternal::GuiExternal(GuiView & lv)
        bc().addReadOnly(xlED);
        bc().addReadOnly(xrED);
        bc().addReadOnly(ybED);
+       bc().addReadOnly(ytUnitCO);
+       bc().addReadOnly(xlUnitCO);
+       bc().addReadOnly(xrUnitCO);
+       bc().addReadOnly(ybUnitCO);
        bc().addReadOnly(extraFormatCO);
        bc().addReadOnly(extraED);
 
@@ -211,52 +208,70 @@ GuiExternal::GuiExternal(GuiView & lv)
        bc().addCheckedLineEdit(ytED, rtLA);
        bc().addCheckedLineEdit(fileED, fileLA);
 
-       std::vector<string> templates = getTemplates();
-
-       for (std::vector<string>::const_iterator cit = templates.begin();
-               cit != templates.end(); ++cit) {
-               externalCO->addItem(qt_(*cit));
-       }
+       external::TemplateManager::Templates::const_iterator i1, i2;
+       i1 = external::TemplateManager::get().getTemplates().begin();
+       i2 = external::TemplateManager::get().getTemplates().end();
+       for (; i1 != i2; ++i1)
+               externalCO->addItem(qt_(i1->second.guiName));
 
        // Fill the origins combo
-       for (size_t i = 0; i != all_origins.size(); ++i)
+       for (size_t i = 0; i != sizeof(origins) / sizeof(origins[0]); ++i)
                originCO->addItem(qt_(origin_gui_strs[i]));
 
-       // Fill the width combo
-       widthUnitCO->addItem(qt_("Scale%"));
-       for (int i = 0; i < num_units; i++)
-               widthUnitCO->addItem(qt_(unit_name_gui[i]));
+       // add scale item
+       widthUnitCO->insertItem(0, qt_("Scale%"), "scale");
+
+       // remove all units from bb that depend on font or other dimensions
+       // we cannot use these, since we need to compare against absolute
+       // values from the image file.
+       xlUnitCO->noPercents();
+       xlUnitCO->removeFontDependent();
+       xrUnitCO->noPercents();
+       xrUnitCO->removeFontDependent();
+       ytUnitCO->noPercents();
+       ytUnitCO->removeFontDependent();
+       ybUnitCO->noPercents();
+       ybUnitCO->removeFontDependent();
 }
 
 
 bool GuiExternal::activateAspectratio() const
 {
-       if (widthUnitCO->currentIndex() == 0)
+       if (usingScale())
                return false;
 
-       string const wstr = fromqstr(widthED->text());
-       if (wstr.empty())
+       QString const wstr = widthED->text();
+       if (wstr.isEmpty())
                return false;
-       bool const wIsDbl = isStrDbl(wstr);
-       if (wIsDbl && float_equal(convert<double>(wstr), 0.0, 0.05))
+       bool wIsDbl;
+       double val = wstr.trimmed().toDouble(&wIsDbl);
+       if (wIsDbl && float_equal(val, 0.0, 0.05))
                return false;
        Length l;
-       if (!wIsDbl && (!isValidLength(wstr, &l) || l.zero()))
+       if (!wIsDbl && (!isValidLength(fromqstr(wstr), &l) || l.zero()))
                return false;
 
-       string const hstr = fromqstr(heightED->text());
-       if (hstr.empty())
+       QString const hstr = heightED->text();
+       if (hstr.isEmpty())
                return false;
-       bool const hIsDbl = isStrDbl(hstr);
-       if (hIsDbl && float_equal(convert<double>(hstr), 0.0, 0.05))
+       bool hIsDbl;
+       val = hstr.trimmed().toDouble(&hIsDbl);
+       if (hIsDbl && float_equal(val, 0.0, 0.05))
                return false;
-       if (!hIsDbl && (!isValidLength(hstr, &l) || l.zero()))
+       if (!hIsDbl && (!isValidLength(fromqstr(hstr), &l) || l.zero()))
                return false;
 
        return true;
 }
 
 
+bool GuiExternal::usingScale() const
+{
+       return (widthUnitCO->itemData(
+               widthUnitCO->currentIndex()).toString() == "scale");
+}
+
+
 void GuiExternal::bbChanged()
 {
        bbChanged_ = true;
@@ -267,10 +282,10 @@ void GuiExternal::bbChanged()
 void GuiExternal::browseClicked()
 {
        int const choice =  externalCO->currentIndex();
-       docstring const template_name = from_utf8(getTemplate(choice).lyxName);
-       docstring const str = browse(qstring_to_ucs4(fileED->text()), template_name);
-       if (!str.empty()) {
-               fileED->setText(toqstr(str));
+       QString const template_name = toqstr(getTemplate(choice).lyxName);
+       QString const str = browse(fileED->text(), template_name);
+       if (!str.isEmpty()) {
+               fileED->setText(str);
                changed();
        }
 }
@@ -282,37 +297,62 @@ void GuiExternal::change_adaptor()
 }
 
 
-void GuiExternal::closeEvent(QCloseEvent * e)
+void GuiExternal::extraChanged(const QString & text)
 {
-       slotClose();
-       e->accept();
+       extra_[extraFormatCO->currentText()] = text;
+       changed();
 }
 
 
-void GuiExternal::editClicked()
+void GuiExternal::formatChanged(const QString & format)
 {
-       editExternal();
+       extraED->setText(extra_[format]);
 }
 
 
-
-void GuiExternal::extraChanged(const QString& text)
+void GuiExternal::getbbClicked()
 {
-       std::string const format = fromqstr(extraFormatCO->currentText());
-       extra_[format] = text;
-       changed();
-}
+       xlED->setText("0");
+       ybED->setText("0");
+       xrED->setText("0");
+       ytED->setText("0");
 
+       string const filename = fromqstr(fileED->text());
+       if (filename.empty())
+               return;
 
-void GuiExternal::formatChanged(const QString& format)
-{
-       extraED->setText(extra_[fromqstr(format)]);
-}
+       FileName const abs_file(support::makeAbsPath(filename, fromqstr(bufferFilePath())));
 
+       // try to get it from the file, if possible
+       string bb = graphics::readBB_from_PSFile(abs_file);
+       if (bb.empty()) {
+               // we don't, so ask the Graphics Cache if it has loaded the file
+               int width = 0;
+               int height = 0;
 
-void GuiExternal::getbbClicked()
-{
-       getBB();
+               graphics::Cache & gc = graphics::Cache::get();
+               if (gc.inCache(abs_file)) {
+                       graphics::Image const * image = gc.item(abs_file)->image();
+
+                       if (image) {
+                               width  = image->width();
+                               height = image->height();
+                       }
+               }
+               bb = "0 0 " + convert<string>(width) + ' ' + convert<string>(height);
+       }
+
+       doubleToWidget(xlED, token(bb, ' ', 0));
+       doubleToWidget(ybED, token(bb, ' ', 1));
+       doubleToWidget(xrED, token(bb, ' ', 2));
+       doubleToWidget(ytED, token(bb, ' ', 3));
+       // the values from the file always have the bigpoint-unit bp
+       xlUnitCO->setCurrentIndex(0);
+       ybUnitCO->setCurrentIndex(0);
+       xrUnitCO->setCurrentIndex(0);
+       ytUnitCO->setCurrentIndex(0);
+
+       bbChanged_ = false;
 }
 
 
@@ -332,106 +372,22 @@ void GuiExternal::templateChanged()
 
 void GuiExternal::widthUnitChanged()
 {
-       bool useHeight = (widthUnitCO->currentIndex() > 0);
-
-       if (useHeight)
-               widthED->setValidator(unsignedLengthValidator(widthED));
-       else
+       if (usingScale())
                widthED->setValidator(new QDoubleValidator(0, 1000, 2, widthED));
+       else
+               widthED->setValidator(unsignedLengthValidator(widthED));
 
-       heightED->setEnabled(useHeight);
-       heightUnitCO->setEnabled(useHeight);
+       heightED->setEnabled(!usingScale());
+       heightUnitCO->setEnabled(!usingScale());
        changed();
 }
 
 
-static Length::UNIT defaultUnit()
-{
-       Length::UNIT default_unit = Length::CM;
-       switch (lyxrc.default_papersize) {
-       case PAPER_USLETTER:
-       case PAPER_USLEGAL:
-       case PAPER_USEXECUTIVE:
-               default_unit = Length::IN;
-               break;
-       default:
-               break;
-       }
-       return default_unit;
-}
-
-
-static void setDisplay(
-       QCheckBox & displayCB, QComboBox & showCO, QLineEdit & scaleED,
-       external::DisplayType display, unsigned int scale, bool read_only)
-{
-       int item = 0;
-       switch (display) {
-       case external::DefaultDisplay:
-               item = 0;
-               break;
-       case external::MonochromeDisplay:
-               item = 1;
-               break;
-       case external::GrayscaleDisplay:
-               item = 2;
-               break;
-       case external::ColorDisplay:
-               item = 3;
-               break;
-       case external::PreviewDisplay:
-               item = 4;
-               break;
-       case external::NoDisplay:
-               item = 0;
-               break;
-       }
-
-       showCO.setCurrentIndex(item);
-       bool const no_display = display == external::NoDisplay;
-       showCO.setEnabled(!no_display && !read_only);
-       displayCB.setChecked(!no_display);
-       scaleED.setEnabled(!no_display && !read_only);
-       scaleED.setText(QString::number(scale));
-}
-
-
-static void getDisplay(external::DisplayType & display,
-               unsigned int & scale,
-               QCheckBox const & displayCB,
-               QComboBox const & showCO,
-               QLineEdit const & scaleED)
-{
-       switch (showCO.currentIndex()) {
-       case 0:
-               display = external::DefaultDisplay;
-               break;
-       case 1:
-               display = external::MonochromeDisplay;
-               break;
-       case 2:
-               display = external::GrayscaleDisplay;
-               break;
-       case 3:
-               display = external::ColorDisplay;
-               break;
-       case 4:
-               display = external::PreviewDisplay;
-               break;
-       }
-
-       if (!displayCB.isChecked())
-               display = external::NoDisplay;
-
-       scale = scaleED.text().toInt();
-}
-
-
 static void setRotation(QLineEdit & angleED, QComboBox & originCO,
        external::RotationData const & data)
 {
        originCO.setCurrentIndex(int(data.origin()));
-       angleED.setText(toqstr(data.angle));
+       doubleToWidget(&angleED, data.angle);
 }
 
 
@@ -441,17 +397,17 @@ static void getRotation(external::RotationData & data,
        typedef external::RotationData::OriginType OriginType;
 
        data.origin(static_cast<OriginType>(originCO.currentIndex()));
-       data.angle = fromqstr(angleED.text());
+       data.angle = widgetToDoubleStr(&angleED);
 }
 
 
-static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
+static void setSize(QLineEdit & widthED, LengthCombo & widthUnitCO,
        QLineEdit & heightED, LengthCombo & heightUnitCO,
        QCheckBox & aspectratioCB,
        external::ResizeData const & data)
 {
        bool using_scale = data.usingScale();
-       std::string scale = data.scale;
+       string scale = data.scale;
        if (data.no_resize()) {
                // Everything is zero, so default to this!
                using_scale = true;
@@ -459,19 +415,15 @@ static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
        }
 
        if (using_scale) {
-               widthED.setText(toqstr(scale));
-               widthUnitCO.setCurrentIndex(0);
-       } else {
-               widthED.setText(QString::number(data.width.value()));
-               // Because 'Scale' is position 0...
-               // Note also that width cannot be zero here, so
-               // we don't need to worry about the default unit.
-               widthUnitCO.setCurrentIndex(data.width.unit() + 1);
-       }
+               doubleToWidget(&widthED, scale);
+               widthUnitCO.setCurrentItem("scale");
+       } else
+               lengthToWidgets(&widthED, &widthUnitCO,
+                               data.width.asString(), Length::defaultUnit());
 
        string const h = data.height.zero() ? string() : data.height.asString();
-       Length::UNIT default_unit = data.width.zero() ?
-               defaultUnit() : data.width.unit();
+       Length::UNIT const default_unit = data.width.zero() ?
+               Length::defaultUnit() : data.width.unit();
        lengthToWidgets(&heightED, &heightUnitCO, h, default_unit);
 
        heightED.setEnabled(!using_scale);
@@ -488,33 +440,17 @@ static void setSize(QLineEdit & widthED, QComboBox & widthUnitCO,
 static void getSize(external::ResizeData & data,
        QLineEdit const & widthED, QComboBox const & widthUnitCO,
        QLineEdit const & heightED, LengthCombo const & heightUnitCO,
-       QCheckBox const & aspectratioCB)
+       QCheckBox const & aspectratioCB, bool const scaling)
 {
-       string const width = fromqstr(widthED.text());
-
-       if (widthUnitCO.currentIndex() > 0) {
-               // Subtract one, because scale is 0.
-               int const unit = widthUnitCO.currentIndex() - 1;
-
-               Length w;
-               if (isValidLength(width, &w))
-                       data.width = w;
-               else if (isStrDbl(width))
-                       data.width = Length(convert<double>(width),
-                                          static_cast<Length::UNIT>(unit));
-               else
-                       data.width = Length();
-
-               data.scale = string();
-
-       } else {
+       if (scaling) {
                // scaling instead of a width
-               data.scale = width;
+               data.scale = widgetToDoubleStr(&widthED);
                data.width = Length();
+       } else {
+               data.width = Length(widgetsToLength(&widthED, &widthUnitCO));
+               data.scale = string();
        }
-
        data.height = Length(widgetsToLength(&heightED, &heightUnitCO));
-
        data.keepAspectRatio = aspectratioCB.isChecked();
 }
 
@@ -522,14 +458,17 @@ static void getSize(external::ResizeData & data,
 void setCrop(QCheckBox & clipCB,
        QLineEdit & xlED, QLineEdit & ybED,
        QLineEdit & xrED, QLineEdit & ytED,
+       LengthCombo & xlUnitCO, LengthCombo & ybUnitCO,
+       LengthCombo & xrUnitCO, LengthCombo & ytUnitCO,
        external::ClipData const & data)
 {
        clipCB.setChecked(data.clip);
-       graphics::BoundingBox const & bbox = data.bbox;
-       xlED.setText(QString::number(bbox.xl));
-       ybED.setText(QString::number(bbox.yb));
-       xrED.setText(QString::number(bbox.xr));
-       ytED.setText(QString::number(bbox.yt));
+       Length::UNIT const default_unit = data.bbox.xl.zero() ?
+               Length::defaultUnit() : data.bbox.xl.unit();
+       lengthToWidgets(&xlED, &xlUnitCO, data.bbox.xl, default_unit);
+       lengthToWidgets(&ybED, &ybUnitCO, data.bbox.yb, default_unit);
+       lengthToWidgets(&xrED, &xrUnitCO, data.bbox.xr, default_unit);
+       lengthToWidgets(&ytED, &ytUnitCO, data.bbox.yt, default_unit);
 }
 
 
@@ -537,6 +476,8 @@ static void getCrop(external::ClipData & data,
        QCheckBox const & clipCB,
        QLineEdit const & xlED, QLineEdit const & ybED,
        QLineEdit const & xrED, QLineEdit const & ytED,
+       LengthCombo const & xlUnitCO, LengthCombo const & ybUnitCO,
+       LengthCombo const & xrUnitCO, LengthCombo const & ytUnitCO,
        bool bb_changed)
 {
        data.clip = clipCB.isChecked();
@@ -544,46 +485,51 @@ static void getCrop(external::ClipData & data,
        if (!bb_changed)
                return;
 
-       data.bbox.xl = xlED.text().toInt();
-       data.bbox.yb = ybED.text().toInt();
-       data.bbox.xr = xrED.text().toInt();
-       data.bbox.yt = ytED.text().toInt();
-}
-
-
-static void getExtra(external::ExtraData & data,
-             GuiExternal::MapType const & extra)
-{
-       typedef GuiExternal::MapType MapType;
-       MapType::const_iterator it = extra.begin();
-       MapType::const_iterator const end = extra.end();
-       for (; it != end; ++it)
-               data.set(it->first, trim(fromqstr(it->second)));
+       data.bbox.xl = Length(widgetsToLength(&xlED, &xlUnitCO));
+       data.bbox.yb = Length(widgetsToLength(&ybED, &ybUnitCO));
+       data.bbox.xr = Length(widgetsToLength(&xrED, &xrUnitCO));
+       data.bbox.yt = Length(widgetsToLength(&ytED, &ytUnitCO));
 }
 
 
 void GuiExternal::updateContents()
 {
-       tab->setCurrentIndex(0);
-
        string const name =
-               params_.filename.outputFilename(bufferFilepath());
+               params_.filename.outputFileName(fromqstr(bufferFilePath()));
        fileED->setText(toqstr(name));
 
-       externalCO->setCurrentIndex(getTemplateNumber(params_.templatename()));
+       int index = 0;
+       external::TemplateManager::Templates::const_iterator i1, i2;
+       i1 = external::TemplateManager::get().getTemplates().begin();
+       i2 = external::TemplateManager::get().getTemplates().end();
+       for (int i = 0; i1 != i2; ++i1, ++i) {
+               if (i1->second.lyxName == params_.templatename()) {
+                       index = i;
+                       break;
+               }
+       }
+
+       externalCO->setCurrentIndex(index);
        updateTemplate();
 
        draftCB->setChecked(params_.draft);
 
-       setDisplay(*displayCB, *showCO, *displayscaleED,
-                  params_.display, params_.lyxscale, isBufferReadonly());
+       displayGB->setChecked(params_.display);
+       displayscaleED->setText(QString::number(params_.lyxscale));
+       bool scaled = params_.display && !isBufferReadonly() &&
+                       (params_.preview_mode != PREVIEW_INSTANT);
+       displayscaleED->setEnabled(scaled);
+       scaleLA->setEnabled(scaled);
+       displayGB->setEnabled(lyxrc.display_graphics);
+
 
        setRotation(*angleED, *originCO, params_.rotationdata);
 
        setSize(*widthED, *widthUnitCO, *heightED, *heightUnitCO,
                *aspectratioCB, params_.resizedata);
 
-       setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED, params_.clipdata);
+       setCrop(*clipCB, *xlED, *ybED, *xrED, *ytED,
+               *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, params_.clipdata);
        bbChanged_ = !params_.clipdata.bbox.empty();
 
        isValid();
@@ -593,25 +539,34 @@ void GuiExternal::updateContents()
 void GuiExternal::updateTemplate()
 {
        external::Template templ = getTemplate(externalCO->currentIndex());
-       externalTB->setPlainText(qt_(templ.helpText));
+       externalTB->setPlainText(toqstr(translateIfPossible(templ.helpText)));
 
        // Ascertain which (if any) transformations the template supports
-       // and disable tabs hosting unsupported transforms.
+       // and disable tabs and Group Boxes hosting unsupported transforms.
        typedef vector<external::TransformID> TransformIDs;
        TransformIDs const transformIds = templ.transformIds;
        TransformIDs::const_iterator tr_begin = transformIds.begin();
        TransformIDs::const_iterator const tr_end = transformIds.end();
 
-       bool found = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
-       tab->setTabEnabled(tab->indexOf(rotatetab), found);
-       found = std::find(tr_begin, tr_end, external::Resize) != tr_end;
-       tab->setTabEnabled(tab->indexOf(scaletab), found);
+       bool rotate = std::find(tr_begin, tr_end, external::Rotate) != tr_end;
+       rotationGB->setEnabled(rotate);
+
+       bool resize = std::find(tr_begin, tr_end, external::Resize) != tr_end;
+       scaleGB->setEnabled(resize);
 
-       found = std::find(tr_begin, tr_end, external::Clip) != tr_end;
-       tab->setTabEnabled(tab->indexOf(croptab), found);
+       bool clip = std::find(tr_begin, tr_end, external::Clip) != tr_end;
+       cropGB->setEnabled(clip);
 
-       found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
-       tab->setTabEnabled(tab->indexOf(optionstab), found);
+       sizetab->setEnabled(rotate || resize || clip);
+       tab->setTabEnabled(tab->indexOf(sizetab), rotate || resize || clip);
+
+       bool found = std::find(tr_begin, tr_end, external::Extra) != tr_end;
+       optionsGB->setEnabled(found);
+
+       bool scaled = displayGB->isChecked() && displayGB->isEnabled() &&
+                       !isBufferReadonly() && (templ.preview_mode != PREVIEW_INSTANT);
+       displayscaleED->setEnabled(scaled);
+       scaleLA->setEnabled(scaled);
 
        if (!found)
                return;
@@ -631,79 +586,54 @@ void GuiExternal::updateTemplate()
                string const format = it->first;
                string const opt = params_.extradata.get(format);
                extraFormatCO->addItem(toqstr(format));
-               extra_[format] = toqstr(opt);
+               extra_[toqstr(format)] = toqstr(opt);
        }
 
        bool const enabled = extraFormatCO->count()  > 0;
 
-       tab->setTabEnabled(
-               tab->indexOf(optionstab), enabled);
+       optionsGB->setEnabled(enabled);
        extraED->setEnabled(enabled && !isBufferReadonly());
        extraFormatCO->setEnabled(enabled);
 
        if (enabled) {
                extraFormatCO->setCurrentIndex(0);
-               extraED->setText(extra_[fromqstr(extraFormatCO->currentText())]);
+               extraED->setText(extra_[extraFormatCO->currentText()]);
        }
 }
 
 
 void GuiExternal::applyView()
 {
-       params_.filename.set(internal_path(fromqstr(fileED->text())),
-                           bufferFilepath());
-
+       params_.filename.set(fromqstr(fileED->text()), fromqstr(bufferFilePath()));
        params_.settemplate(getTemplate(externalCO->currentIndex()).lyxName);
 
        params_.draft = draftCB->isChecked();
+       params_.lyxscale = displayscaleED->text().toInt();
+       params_.display = displayGB->isChecked();
 
-       getDisplay(params_.display, params_.lyxscale,
-                  *displayCB, *showCO,
-                  *displayscaleED);
-
-       if (tab->isTabEnabled(tab->indexOf(rotatetab)))
+       if (rotationGB->isEnabled())
                getRotation(params_.rotationdata, *angleED, *originCO);
 
-       if (tab->isTabEnabled(tab->indexOf(scaletab)))
+       if (scaleGB->isEnabled())
                getSize(params_.resizedata, *widthED, *widthUnitCO,
-                       *heightED, *heightUnitCO, *aspectratioCB);
-
-       if (tab->isTabEnabled(tab->indexOf(croptab)))
-               getCrop(params_.clipdata, *clipCB, *xlED, *ybED,
-                       *xrED, *ytED, bbChanged_);
-
-       if (tab->isTabEnabled(tab->indexOf(optionstab)))
-               getExtra(params_.extradata, extra_);
-}
+                       *heightED, *heightUnitCO, *aspectratioCB, usingScale());
 
+       if (cropGB->isEnabled())
+               getCrop(params_.clipdata, *clipCB, *xlED, *ybED, *xrED, *ytED,
+                       *xlUnitCO, *ybUnitCO, *xrUnitCO, *ytUnitCO, bbChanged_);
 
-void GuiExternal::getBB()
-{
-       xlED->setText("0");
-       ybED->setText("0");
-       xrED->setText("0");
-       ytED->setText("0");
-
-       string const filename = fromqstr(fileED->text());
-       if (filename.empty())
-               return;
-
-       string const bb = readBB(filename);
-       if (bb.empty())
-               return;
-
-       xlED->setText(toqstr(token(bb, ' ', 0)));
-       ybED->setText(toqstr(token(bb, ' ', 1)));
-       xrED->setText(toqstr(token(bb, ' ', 2)));
-       ytED->setText(toqstr(token(bb, ' ', 3)));
-
-       bbChanged_ = false;
+       if (optionsGB->isEnabled()) {
+               MapType::const_iterator it = extra_.begin();
+               MapType::const_iterator const end = extra_.end();
+               for (; it != end; ++it)
+                       params_.extradata.set(fromqstr(it.key()), fromqstr(it.value().trimmed()));
+       }
 }
 
 
 bool GuiExternal::initialiseParams(string const & data)
 {
-       InsetExternalMailer::string2params(data, buffer(), params_);
+       InsetExternal::string2params(data, buffer(), params_);
        return true;
 }
 
@@ -716,118 +646,41 @@ void GuiExternal::clearParams()
 
 void GuiExternal::dispatchParams()
 {
-       string const lfun = InsetExternalMailer::params2string(params_, buffer());
+       string const lfun = InsetExternal::params2string(params_, buffer());
        dispatch(FuncRequest(getLfun(), lfun));
 }
 
 
-void GuiExternal::editExternal()
-{
-       applyView();
-       string const lfun = InsetExternalMailer::params2string(params_, buffer());
-       dispatch(FuncRequest(LFUN_EXTERNAL_EDIT, lfun));
-}
-
-
-vector<string> const GuiExternal::getTemplates() const
-{
-       vector<string> result;
-
-       external::TemplateManager::Templates::const_iterator i1, i2;
-       i1 = external::TemplateManager::get().getTemplates().begin();
-       i2 = external::TemplateManager::get().getTemplates().end();
-
-       for (; i1 != i2; ++i1) {
-               result.push_back(i1->second.lyxName);
-       }
-       return result;
-}
-
-
-int GuiExternal::getTemplateNumber(string const & name) const
-{
-       external::TemplateManager::Templates::const_iterator i1, i2;
-       i1 = external::TemplateManager::get().getTemplates().begin();
-       i2 = external::TemplateManager::get().getTemplates().end();
-       for (int i = 0; i1 != i2; ++i1, ++i) {
-               if (i1->second.lyxName == name)
-                       return i;
-       }
-
-       // we can get here if a LyX document has a template not installed
-       // on this machine.
-       return -1;
-}
-
-
-external::Template GuiExternal::getTemplate(int i) const
-{
-       external::TemplateManager::Templates::const_iterator i1
-               = external::TemplateManager::get().getTemplates().begin();
-
-       advance(i1, i);
-
-       return i1->second;
-}
-
-
-string const
-GuiExternal::getTemplateFilters(string const & template_name) const
+static QStringList templateFilters(QString const & template_name)
 {
        /// Determine the template file extension
        external::TemplateManager const & etm =
                external::TemplateManager::get();
        external::Template const * const et_ptr =
-               etm.getTemplateByName(template_name);
-
-       if (et_ptr)
-               return et_ptr->fileRegExp;
-
-       return string();
-}
-
-
-docstring const GuiExternal::browse(docstring const & input,
-                                    docstring const & template_name) const
-{
-       docstring const title =  _("Select external file");
-
-       docstring const bufpath = from_utf8(bufferFilepath());
-       FileFilterList const filter =
-               FileFilterList(from_utf8(getTemplateFilters(to_utf8(template_name))));
-
-       docstring const label1 = _("Documents|#o#O");
-       docstring const dir1 = from_utf8(lyxrc.document_path);
-
-       return browseRelFile(input, bufpath, title, filter, false, label1, dir1);
+               etm.getTemplateByName(fromqstr(template_name));
+
+       string filter;
+       if (et_ptr && et_ptr->fileRegExp != "" && et_ptr->fileRegExp != "*") {
+               filter += et_ptr->guiName;
+               filter += " (";
+               filter += et_ptr->fileRegExp;
+               filter += ")";
+       }
+       return fileFilters(toqstr(filter));
 }
 
 
-string const GuiExternal::readBB(string const & file)
+QString GuiExternal::browse(QString const & input,
+                                    QString const & template_name) const
 {
-       FileName const abs_file(makeAbsPath(file, bufferFilepath()));
+       QString const title = qt_("Select external file");
+       QString const bufpath = bufferFilePath();
+       QStringList const filter = templateFilters(template_name);
 
-       // try to get it from the file, if possible. Zipped files are
-       // unzipped in the readBB_from_PSFile-Function
-       string const bb = readBB_from_PSFile(abs_file);
-       if (!bb.empty())
-               return bb;
-
-       // we don't, so ask the Graphics Cache if it has loaded the file
-       int width = 0;
-       int height = 0;
-
-       graphics::Cache & gc = graphics::Cache::get();
-       if (gc.inCache(abs_file)) {
-               graphics::Image const * image = gc.item(abs_file)->image();
-
-               if (image) {
-                       width  = image->getWidth();
-                       height = image->getHeight();
-               }
-       }
+       QString const label1 = qt_("Documents|#o#O");
+       QString const dir1 = toqstr(lyxrc.document_path);
 
-       return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
+       return browseRelToParent(input, bufpath, title, filter, false, label1, dir1);
 }
 
 
@@ -837,4 +690,4 @@ Dialog * createGuiExternal(GuiView & lv) { return new GuiExternal(lv); }
 } // namespace frontend
 } // namespace lyx
 
-#include "GuiExternal_moc.cpp"
+#include "moc_GuiExternal.cpp"