3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
15 #include "FormExternal.h"
16 #include "ControlExternal.h"
17 #include "forms/form_external.h"
19 #include "input_validators.h"
21 #include "xforms_helpers.h"
24 #include "insets/ExternalTemplate.h"
26 #include "support/lstrings.h"
27 #include "support/tostr.h"
29 #include "lyx_forms.h"
31 using lyx::support::getStringFromVector;
32 using lyx::support::strToInt;
35 typedef FormController<ControlExternal, FormView<FD_external> > base_class;
37 FormExternal::FormExternal(Dialog & parent)
38 : base_class(parent, _("External Material"))
42 void FormExternal::apply()
44 InsetExternal::Params params = controller().params();
46 string const buffer_path = kernel().bufferFilepath();
47 params.filename.set(getString(dialog_->input_filename), buffer_path);
49 int const choice = fl_get_choice(dialog_->choice_template) - 1;
50 params.templatename = controller().getTemplate(choice).lyxName;
52 params.lyxscale = strToInt(getString(dialog_->input_lyxscale));
53 if (params.lyxscale == 0)
54 params.lyxscale = 100;
56 switch (fl_get_choice(dialog_->choice_display)) {
58 params.display = lyx::graphics::NoDisplay;
61 params.display = lyx::graphics::ColorDisplay;
64 params.display = lyx::graphics::GrayscaleDisplay;
67 params.display = lyx::graphics::MonochromeDisplay;
70 params.display = lyx::graphics::DefaultDisplay;
73 controller().setParams(params);
77 void FormExternal::build()
79 dialog_.reset(build_external(this));
82 ' ' + getStringFromVector(controller().getTemplates(), " | ") + ' ';
83 fl_addto_choice(dialog_->choice_template, choice.c_str());
85 fl_set_input_return (dialog_->input_filename, FL_RETURN_CHANGED);
87 // Disable for read-only documents.
88 bcview().addReadOnly(dialog_->input_filename);
89 bcview().addReadOnly(dialog_->button_browse);
91 // Trigger an input event for cut&paste with middle mouse button.
92 setPrehandler(dialog_->input_filename);
94 // Activate ok/apply immediately upon input.
95 fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
96 fl_set_input_return(dialog_->input_lyxscale, FL_RETURN_CHANGED);
98 fl_set_input_filter(dialog_->input_lyxscale, fl_unsigned_int_filter);
100 string const display_List =
101 _("Default|Monochrome|Grayscale|Color|Do not display");
102 fl_addto_choice(dialog_->choice_display, display_List.c_str());
104 // Set up the tooltips.
105 string str = _("The file you want to insert.");
106 tooltips().init(dialog_->input_filename, str);
107 str = _("Browse the directories.");
108 tooltips().init(dialog_->button_browse, str);
110 str = _("Scale the image to inserted percentage value.");
111 tooltips().init(dialog_->input_lyxscale, str);
112 str = _("Select display mode for this image.");
113 tooltips().init(dialog_->choice_display, str);
115 // Manage the ok, apply and cancel/close buttons
116 bcview().setOK(dialog_->button_ok);
117 bcview().setApply(dialog_->button_apply);
118 bcview().setCancel(dialog_->button_close);
122 void FormExternal::update()
124 InsetExternal::Params const & params = controller().params();
126 string const buffer_path = kernel().bufferFilepath();
127 string const name = params.filename.outputFilename(buffer_path);
128 fl_set_input(dialog_->input_filename, name.c_str());
130 int ID = controller().getTemplateNumber(params.templatename);
132 fl_set_choice(dialog_->choice_template, ID+1);
136 fl_set_input(dialog_->input_lyxscale, tostr(params.lyxscale).c_str());
138 switch (params.display) {
139 case lyx::graphics::NoDisplay:
140 fl_set_choice(dialog_->choice_display, 5);
142 case lyx::graphics::ColorDisplay:
143 fl_set_choice(dialog_->choice_display, 4);
145 case lyx::graphics::GrayscaleDisplay:
146 fl_set_choice(dialog_->choice_display, 3);
148 case lyx::graphics::MonochromeDisplay:
149 fl_set_choice(dialog_->choice_display, 2);
151 case lyx::graphics::DefaultDisplay:
152 fl_set_choice(dialog_->choice_display, 1);
157 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
159 ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
160 if (ob == dialog_->choice_template) {
162 // set to the chosen template
165 } else if (ob == dialog_->button_browse) {
167 string const in_name = fl_get_input(dialog_->input_filename);
168 string const out_name = controller().Browse(in_name);
169 fl_set_input(dialog_->input_filename, out_name.c_str());
171 } else if (ob == dialog_->button_edit) {
172 controller().editExternal();
173 result = ButtonPolicy::SMI_NOOP;
180 void FormExternal::updateComboChange()
182 int const choice = fl_get_choice(dialog_->choice_template) - 1;
183 ExternalTemplate templ = controller().getTemplate(choice);
185 // Update the help text
186 fl_clear_browser(dialog_->browser_helptext);
187 fl_addto_browser(dialog_->browser_helptext, templ.helpText.c_str());
188 fl_set_browser_topline(dialog_->browser_helptext, 0);