]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormExternal.C
Tiny clean-ups.
[lyx.git] / src / frontends / xforms / FormExternal.C
index 6c581b4db67671cd974c44b077a7d43acd10c9f6..77513de129573241a8223579087596d84ad86777 100644 (file)
 /**
  * \file FormExternal.C
- * Copyright 2001 the LyX Team
- * Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * \author unknown
+ * \author Asger Alstrup
  * \author John Levon
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
-#include <utility>
-
-#include FORMS_H_LOCATION
-
-#ifdef __GNUG__
-#pragma implementation
-#endif
 
-#include "debug.h"
-#include "gettext.h"
-#include "support/LAssert.h"
-#include "lyx_gui_misc.h"
-#include "Dialogs.h"
-#include "LyXView.h"
-#include "buffer.h"
 #include "FormExternal.h"
-#include "form_external.h"
-#include "frontends/FileDialog.h"
-#include "LString.h"
-#include "support/filetools.h"
-
-using std::pair;
-using std::make_pair;
-using std::endl;
-
-FormExternal::FormExternal(LyXView * lv, Dialogs * d)
-       : FormBaseBD(lv, d, _("Edit external file")),
-       inset_(0), ih_(0)
-{
-       d->showExternal.connect(slot(this, &FormExternal::showInset));
-}
-
-
-extern "C" void ExternalTemplateCB(FL_OBJECT * ob, long data)
-{
-       FormExternal::templateCB(ob, data);
-}
-
-
-extern "C" void ExternalBrowseCB(FL_OBJECT * ob, long data)
-{
-       FormExternal::browseCB(ob, data);
-}
-
-
-extern "C" void ExternalEditCB(FL_OBJECT * ob, long data)
-{
-       FormExternal::editCB(ob, data);
-}
-
-
-extern "C" void ExternalViewCB(FL_OBJECT * ob, long data)
-{
-       FormExternal::viewCB(ob, data);
-}
+#include "ControlExternal.h"
+#include "forms/form_external.h"
 
+#include "input_validators.h"
+#include "Tooltips.h"
+#include "xforms_helpers.h"
+#include "xformsBC.h"
 
-extern "C" void ExternalUpdateCB(FL_OBJECT * ob, long data)
-{
-       FormExternal::updateCB(ob, data);
-}
+#include "insets/ExternalTemplate.h"
+#include "insets/insetexternal.h"
 
+#include "support/lstrings.h"
+#include "support/tostr.h"
 
-FL_FORM * FormExternal::form() const
-{
-       if (dialog_.get())
-               return dialog_->form;
-       return 0;
-}
-
+#include "lyx_forms.h"
 
-void FormExternal::connect()
-{
-       u_ = d_->updateBufferDependent.
-                connect(slot(this, &FormExternal::updateSlot));
-       h_ = d_->hideBufferDependent.
-                connect(slot(this, &FormExternal::hide));
-       FormBaseDeprecated::connect();
-}
+using lyx::support::getStringFromVector;
+using lyx::support::strToInt;
+using lyx::support::trim;
 
+using std::string;
 
-void FormExternal::disconnect()
-{
-       inset_ = 0;
-       ih_.disconnect();
-       FormBaseBD::disconnect();
-}
 
+typedef FormController<ControlExternal, FormView<FD_external> > base_class;
 
-void FormExternal::updateSlot(bool switched)
-{
-       if (switched)
-               hide();
-       else
-               update();
-}
+FormExternal::FormExternal(Dialog & parent)
+       : base_class(parent, _("External Material"))
+{}
 
 
-void FormExternal::showInset(InsetExternal * inset)
+void FormExternal::apply()
 {
-       Assert(inset);
-
-       // If connected to another inset, disconnect from it.
-       if (inset_)
-               ih_.disconnect();
+       InsetExternalParams params = controller().params();
+
+       string const buffer_path = kernel().bufferFilepath();
+       params.filename.set(getString(dialog_->input_filename), buffer_path);
+
+       int const choice = fl_get_choice(dialog_->choice_template) - 1;
+       params.settemplate(controller().getTemplate(choice).lyxName);
+
+       params.lyxscale = strToInt(getString(dialog_->input_lyxscale));
+       if (params.lyxscale == 0)
+               params.lyxscale = 100;
+
+       switch (fl_get_choice(dialog_->choice_display)) {
+       case 6:
+               params.display = lyx::external::NoDisplay;
+               break;
+       case 5:
+               params.display = lyx::external::PreviewDisplay;
+               break;
+       case 4:
+               params.display = lyx::external::ColorDisplay;
+               break;
+       case 3:
+               params.display = lyx::external::GrayscaleDisplay;
+               break;
+       case 2:
+               params.display = lyx::external::MonochromeDisplay;
+               break;
+       case 1:
+               params.display = lyx::external::DefaultDisplay;
+       }
 
-       inset_ = inset;
-       params_ = inset_->params();
+       std::map<string, string>::const_iterator it  = extra_.begin();
+       std::map<string, string>::const_iterator end = extra_.end();
+       for (; it != end; ++it)
+               params.extradata.set(it->first, trim(it->second));
 
-       ih_ = inset->hideDialog.connect(slot(this, &FormExternal::hide));
-       show();
+       controller().setParams(params);
 }
 
 
 void FormExternal::build()
 {
-       dialog_.reset(build_external());
+       dialog_.reset(build_external(this));
 
-       fl_addto_choice(dialog_->choice_template,
-                       getTemplatesComboString().c_str());
+       string const choice =
+               ' ' + getStringFromVector(controller().getTemplates(), " | ") + ' ';
+       fl_addto_choice(dialog_->choice_template, choice.c_str());
 
-       // Workaround dumb xforms sizing bug
-       minw_ = form()->w;
-       minh_ = form()->h;
+       fl_set_input_return (dialog_->input_filename,  FL_RETURN_CHANGED);
 
-       bc_.setOK(dialog_->button_ok);
-       bc_.setCancel(dialog_->button_cancel);
-       bc_.refresh();
-
-       bc_.addReadOnly(dialog_->input_filename);
-       bc_.addReadOnly(dialog_->button_filenamebrowse);
-       bc_.addReadOnly(dialog_->input_parameters);
-}
+       // Disable for read-only documents.
+       bcview().addReadOnly(dialog_->input_filename);
+       bcview().addReadOnly(dialog_->button_browse);
+       bcview().addReadOnly(dialog_->input_extra);
 
+       // Trigger an input event for cut&paste with middle mouse button.
+       setPrehandler(dialog_->input_filename);
 
-string const FormExternal::getTemplatesComboString() const
-{
-       string result;
-       bool first = true;
-       ExternalTemplateManager::Templates::const_iterator i1, i2;
-       i1 = ExternalTemplateManager::get().getTemplates().begin();
-       i2 = ExternalTemplateManager::get().getTemplates().end();
-       for (; i1 != i2; ++i1) {
-               if (!first)
-                       result += "|";
-               else
-                       first = false;
-
-               result += (*i1).second.lyxName;
-       }
-       return result;
-}
+       // Activate ok/apply immediately upon input.
+       fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
+       fl_set_input_return(dialog_->input_lyxscale, FL_RETURN_CHANGED);
+       fl_set_input_return(dialog_->input_extra,    FL_RETURN_CHANGED);
+       fl_set_input_return(dialog_->input_lyxscale, FL_RETURN_CHANGED);
 
+       fl_set_input_filter(dialog_->input_lyxscale, fl_unsigned_int_filter);
 
-int FormExternal::getTemplateComboNumber(string const & name) const
-{
-       int i = 1;
-       ExternalTemplateManager::Templates::const_iterator i1, i2;
-       i1 = ExternalTemplateManager::get().getTemplates().begin();
-       i2 = ExternalTemplateManager::get().getTemplates().end();
-       for (; i1 != i2; ++i1) {
-               if (i1->second.lyxName == name)
-                       return i;
-               ++i;
-       }
-       // we can get here if a LyX document has a template not installed
-       // on this machine.
-       return 0;
-}
+       string const display_List =
+               _("Default|Monochrome|Grayscale|Color|Preview|Do not display");
+       fl_addto_choice(dialog_->choice_display, display_List.c_str());
 
+       // Set up the tooltips.
+       string str = _("The file you want to insert.");
+       tooltips().init(dialog_->input_filename, str);
+       str = _("Browse the directories.");
+       tooltips().init(dialog_->button_browse, str);
 
-ExternalTemplate FormExternal::getTemplate(int i) const
-{
-       ExternalTemplateManager::Templates::const_iterator i1;
-       i1 = ExternalTemplateManager::get().getTemplates().begin();
-       for (int n = 1; n < i; ++n)
-               ++i1;
+       str = _("Scale the image to inserted percentage value.");
+       tooltips().init(dialog_->input_lyxscale, str);
+       str = _("Select display mode for this image.");
+       tooltips().init(dialog_->choice_display, str);
 
-       return (*i1).second;
+       // Manage the ok, apply and cancel/close buttons
+       bcview().setOK(dialog_->button_ok);
+       bcview().setApply(dialog_->button_apply);
+       bcview().setCancel(dialog_->button_close);
 }
 
 
 void FormExternal::update()
 {
-       fl_set_input(dialog_->input_filename, params_.filename.c_str());
-       fl_set_input(dialog_->input_parameters, params_.parameters.c_str());
+       InsetExternalParams const & params = controller().params();
 
-       fl_set_choice(dialog_->choice_template, getTemplateComboNumber(params_.templ.lyxName));
+       string const buffer_path = kernel().bufferFilepath();
+       string const name = params.filename.outputFilename(buffer_path);
+       fl_set_input(dialog_->input_filename, name.c_str());
 
-       updateComboChange();
-
-       bc_.valid();
-}
+       int ID = controller().getTemplateNumber(params.templatename());
+       if (ID < 0) ID = 0;
+       fl_set_choice(dialog_->choice_template, ID+1);
 
+       updateComboChange();
 
-void FormExternal::updateComboChange()
-{
-       // Update the help text
-       fl_clear_browser(dialog_->browser_helptext);
-       fl_addto_browser(dialog_->browser_helptext, params_.templ.helpText.c_str());
-       fl_set_browser_topline(dialog_->browser_helptext, 0);
-
-       if (params_.templ.automaticProduction) {
-               fl_deactivate_object(dialog_->button_update);
-               fl_set_object_lcol(dialog_->button_update, FL_INACTIVE);
-       } else {
-               fl_activate_object(dialog_->button_update);
-               fl_set_object_lcol(dialog_->button_update, FL_BLACK);
+       fl_set_input(dialog_->input_lyxscale, tostr(params.lyxscale).c_str());
+
+       switch (params.display) {
+       case lyx::external::NoDisplay:
+               fl_set_choice(dialog_->choice_display, 6);
+               break;
+       case lyx::external::PreviewDisplay:
+               fl_set_choice(dialog_->choice_display, 5);
+               break;
+       case lyx::external::ColorDisplay:
+               fl_set_choice(dialog_->choice_display, 4);
+               break;
+       case lyx::external::GrayscaleDisplay:
+               fl_set_choice(dialog_->choice_display, 3);
+               break;
+       case lyx::external::MonochromeDisplay:
+               fl_set_choice(dialog_->choice_display, 2);
+               break;
+       case lyx::external::DefaultDisplay:
+               fl_set_choice(dialog_->choice_display, 1);
        }
 }
 
 
-bool FormExternal::input(FL_OBJECT *, long)
-{
-       // FIXME: anything to do here ?
-       return true;
-}
-
-
-void FormExternal::apply()
+ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
 {
-       Assert(inset_);
+       ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
+       if (ob == dialog_->choice_template) {
 
-       if (lv_->buffer()->isReadonly())
-               return;
+               // set to the chosen template
+               updateComboChange();
 
-       params_.filename = fl_get_input(dialog_->input_filename);
-       params_.parameters = fl_get_input(dialog_->input_parameters);
-       params_.templ = getTemplate(fl_get_choice(dialog_->choice_template));
+       } else if (ob == dialog_->button_browse) {
 
-       inset_->setFromParams(params_);
-       lv_->view()->updateInset(inset_, true);
-}
-
-
-void FormExternal::templateCB(FL_OBJECT * ob, long)
-{
-       FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
-
-       // set to the chosen template
-       form->params_.templ = form->getTemplate(fl_get_choice(form->dialog_->choice_template));
+               string const in_name  = fl_get_input(dialog_->input_filename);
+               string const out_name = controller().Browse(in_name);
+               fl_set_input(dialog_->input_filename, out_name.c_str());
 
-       form->updateComboChange();
-}
+       } else if (ob == dialog_->button_edit) {
+               controller().editExternal();
+               result = ButtonPolicy::SMI_NOOP;
 
+       } else if (ob == dialog_->input_extra) {
+               string const format =
+                       fl_get_choice_text(dialog_->choice_extra_format);
+               extra_[format] = getString(dialog_->input_extra);
 
-void FormExternal::browseCB(FL_OBJECT * ob, long)
-{
-       FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
-
-       static string current_path;
-       static int once = 0;
-       
-       string p = fl_get_input(form->dialog_->input_filename);
-       string buf = MakeAbsPath(form->lv_->buffer()->fileName());
-       string buf2 = OnlyPath(buf);
-
-       if (!p.empty()) {
-               buf = MakeAbsPath(p, buf2);
-               buf = OnlyPath(buf);
-       } else {
-               buf = OnlyPath(form->lv_->buffer()->fileName());
-       }
-    
-       FileDialog fileDlg(form->lv_, _("Select external file"),
-               LFUN_SELECT_FILE_SYNC,
-               make_pair(string(_("Document")), string(buf)));
-       
-       /// Determine the template file extension
-       ExternalTemplate const & et = form->params_.templ;
-
-       string regexp = et.fileRegExp;
-       if (regexp.empty())
-               regexp = "*";
-
-       // FIXME: a temporary hack until the FileDialog interface is updated
-       regexp += "|";
-
-       while (1) {
-               string const path = (once) ? current_path : buf;
-               FileDialog::Result result = fileDlg.Select(path, regexp, fl_get_input(form->dialog_->input_filename));
-
-               if (result.second.empty())
-                       return;
-
-               string p = result.second;
-
-               buf = MakeRelPath(p, buf2);
-               current_path = OnlyPath(p);
-               once = 1;
-               
-               if (contains(p, "#") || contains(p, "~") || contains(p, "$")
-                   || contains(p, "%")) {
-                       WriteAlert(_("Filename can't contain any "
-                                    "of these characters:"),
-                                  // xgettext:no-c-format
-                                  _("'#', '~', '$' or '%'."));
-               } else
-                       break;
+       } else if (ob == dialog_->choice_extra_format) {
+               string const format =
+                       fl_get_choice_text(dialog_->choice_extra_format);
+               fl_set_input(dialog_->input_extra, extra_[format].c_str());
+               result = ButtonPolicy::SMI_NOOP;
        }
 
-       fl_set_input(form->dialog_->input_filename, buf.c_str());
+       return result;
 }
 
 
-void FormExternal::editCB(FL_OBJECT * ob, long)
+void FormExternal::updateComboChange()
 {
-       FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
+       namespace external = lyx::external;
 
-       form->apply();
-       form->inset_->editExternal();
-}
+       int const choice = fl_get_choice(dialog_->choice_template) - 1;
+       external::Template templ = controller().getTemplate(choice);
 
+       // Update the help text
+       string const txt = formatted(templ.helpText,
+                                    dialog_->browser_helptext->w - 20);
+       fl_clear_browser(dialog_->browser_helptext);
+       fl_addto_browser(dialog_->browser_helptext, txt.c_str());
+       fl_set_browser_topline(dialog_->browser_helptext, 0);
 
-void FormExternal::viewCB(FL_OBJECT * ob, long)
-{
-       FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
-       
-       form->apply();
-       form->inset_->viewExternal();
-}
+       // Ascertain whether the template has any formats supporting
+       // the 'Extra' option
+       FL_OBJECT * const ob_input  = dialog_->input_extra;
+       FL_OBJECT * const ob_choice = dialog_->choice_extra_format;
+       extra_.clear();
+       fl_set_input(ob_input, "");
+       fl_clear_choice(ob_choice);
+
+       external::Template::Formats::const_iterator it  = templ.formats.begin();
+       external::Template::Formats::const_iterator end = templ.formats.end();
+       for (; it != end; ++it) {
+               if (it->second.option_transformers.find(external::Extra) ==
+                   it->second.option_transformers.end())
+                       continue;
+               string const format = it->first;
+               string const opt = controller().params().extradata.get(format);
+               fl_addto_choice(ob_choice, format.c_str());
+               extra_[format] = opt;
+       }
 
+       bool const enabled = fl_get_choice_maxitems(ob_choice) > 0;
 
-void FormExternal::updateCB(FL_OBJECT * ob, long)
-{
-       FormExternal * form = static_cast<FormExternal*>(ob->form->u_vdata);
-       
-       form->apply();
-       form->inset_->updateExternal();
+       setEnabled(ob_input,  enabled && !kernel().isBufferReadonly());
+       setEnabled(ob_choice, enabled);
+
+       if (enabled) {
+               fl_set_choice(ob_choice, 1);
+               string const format = fl_get_choice_text(ob_choice);
+               fl_set_input(ob_input, extra_[format].c_str());
+       }
 }