]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormExternal.C
Tiny clean-ups.
[lyx.git] / src / frontends / xforms / FormExternal.C
index b44895f1e2c9d25d0b61e422fd9f94becd97bb45..77513de129573241a8223579087596d84ad86777 100644 (file)
@@ -5,44 +5,84 @@
  *
  * \author Asger Alstrup
  * \author John Levon
- * \author Angus Leeming 
+ * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
-#include "xformsBC.h"
-#include "ControlExternal.h"
 #include "FormExternal.h"
+#include "ControlExternal.h"
 #include "forms/form_external.h"
-#include "gettext.h"
+
+#include "input_validators.h"
+#include "Tooltips.h"
 #include "xforms_helpers.h"
-#include "helper_funcs.h"
+#include "xformsBC.h"
+
+#include "insets/ExternalTemplate.h"
+#include "insets/insetexternal.h"
 
 #include "support/lstrings.h"
-#include FORMS_H_LOCATION
+#include "support/tostr.h"
 
-typedef FormCB<ControlExternal, FormDB<FD_external> > base_class;
+#include "lyx_forms.h"
 
-FormExternal::FormExternal()
-       : base_class(_("Edit external file"))
+using lyx::support::getStringFromVector;
+using lyx::support::strToInt;
+using lyx::support::trim;
+
+using std::string;
+
+
+typedef FormController<ControlExternal, FormView<FD_external> > base_class;
+
+FormExternal::FormExternal(Dialog & parent)
+       : base_class(parent, _("External Material"))
 {}
 
 
 void FormExternal::apply()
 {
-       controller().params().filename =
-               fl_get_input(dialog_->input_filename);
-       controller().params().parameters =
-               fl_get_input(dialog_->input_parameters);
+       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;
+       }
+
+       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));
 
-       int const choice = fl_get_choice(dialog_->choice_template);
-       controller().params().templ = controller().getTemplate(choice);
+       controller().setParams(params);
 }
 
 
@@ -51,54 +91,96 @@ void FormExternal::build()
        dialog_.reset(build_external(this));
 
        string const choice =
-               " " + getStringFromVector(controller().getTemplates(), " | ") + " ";
+               ' ' + getStringFromVector(controller().getTemplates(), " | ") + ' ';
        fl_addto_choice(dialog_->choice_template, choice.c_str());
 
        fl_set_input_return (dialog_->input_filename,  FL_RETURN_CHANGED);
-       fl_set_input_return (dialog_->input_parameters, FL_RETURN_CHANGED);
 
-       setPrehandler(dialog_->input_filename);
-       setPrehandler(dialog_->input_parameters);
+       // Disable for read-only documents.
+       bcview().addReadOnly(dialog_->input_filename);
+       bcview().addReadOnly(dialog_->button_browse);
+       bcview().addReadOnly(dialog_->input_extra);
 
-       bc().setOK(dialog_->button_ok);
-       bc().setApply(dialog_->button_apply);
-       bc().setCancel(dialog_->button_close);
+       // Trigger an input event for cut&paste with middle mouse button.
+       setPrehandler(dialog_->input_filename);
 
-       bc().addReadOnly(dialog_->input_filename);
-       bc().addReadOnly(dialog_->button_filenamebrowse);
-       bc().addReadOnly(dialog_->input_parameters);
+       // 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);
+
+       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);
+
+       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);
+
+       // 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()
 {
-       InsetExternal::Params const & params = controller().params();
+       InsetExternalParams const & params = controller().params();
 
-       fl_set_input(dialog_->input_filename, params.filename.c_str());
-       fl_set_input(dialog_->input_parameters, params.parameters.c_str());
+       string const buffer_path = kernel().bufferFilepath();
+       string const name = params.filename.outputFilename(buffer_path);
+       fl_set_input(dialog_->input_filename, name.c_str());
 
-       int const ID = controller().getTemplateNumber(params.templ.lyxName);
-       if (ID >= 0) {
-               setEnabled(dialog_->choice_template, true);
-               fl_set_choice(dialog_->choice_template, ID+1);
-       } else
-               setEnabled(dialog_->choice_template, false);
+       int ID = controller().getTemplateNumber(params.templatename());
+       if (ID < 0) ID = 0;
+       fl_set_choice(dialog_->choice_template, ID+1);
 
        updateComboChange();
+
+       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);
+       }
 }
 
 
 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
 {
+       ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
        if (ob == dialog_->choice_template) {
 
                // set to the chosen template
-               int const choice = fl_get_choice(dialog_->choice_template);
-               controller().params().templ = controller().getTemplate(choice);
-
                updateComboChange();
 
-       } else if (ob == dialog_->button_filenamebrowse) {
+       } else if (ob == dialog_->button_browse) {
 
                string const in_name  = fl_get_input(dialog_->input_filename);
                string const out_name = controller().Browse(in_name);
@@ -106,26 +188,66 @@ ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
 
        } else if (ob == dialog_->button_edit) {
                controller().editExternal();
-
-       } else if (ob == dialog_->button_view) {
-               controller().viewExternal();
-
-       } else if (ob == dialog_->button_update) {
-               controller().updateExternal();
+               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);
+
+       } 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;
        }
 
-       return ButtonPolicy::SMI_VALID;
+       return result;
 }
 
 
 void FormExternal::updateComboChange()
 {
+       namespace external = lyx::external;
+
+       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,
-                        controller().params().templ.helpText.c_str());
+       fl_addto_browser(dialog_->browser_helptext, txt.c_str());
        fl_set_browser_topline(dialog_->browser_helptext, 0);
 
-       bool const enabled = (!controller().params().templ.automaticProduction);
-       setEnabled(dialog_->button_update, enabled);
+       // 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;
+
+       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());
+       }
 }