]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormExternal.C
introduce namespace lyx::support
[lyx.git] / src / frontends / xforms / FormExternal.C
1 /**
2  * \file FormExternal.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author John Levon
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 #include <config.h>
14
15 #include "xformsBC.h"
16 #include "ControlExternal.h"
17 #include "FormExternal.h"
18 #include "forms/form_external.h"
19
20 #include "helper_funcs.h"
21 #include "input_validators.h"
22 #include "Tooltips.h"
23 #include "xforms_helpers.h"
24
25 #include "gettext.h"
26
27 #include "insets/ExternalTemplate.h"
28
29 #include "support/tostr.h"
30 #include "support/lstrings.h"
31 #include "lyx_forms.h"
32
33 using namespace lyx::support;
34
35 typedef FormController<ControlExternal, FormView<FD_external> > base_class;
36
37 FormExternal::FormExternal(Dialog & parent)
38         : base_class(parent, _("External Material"))
39 {}
40
41
42 void FormExternal::apply()
43 {
44         InsetExternal::Params params = controller().params();
45
46         params.filename = fl_get_input(dialog_->input_filename);
47
48         int const choice = fl_get_choice(dialog_->choice_template) - 1;
49         params.templatename = controller().getTemplate(choice).lyxName;
50
51         params.lyxscale = strToInt(getString(dialog_->input_lyxscale));
52         if (params.lyxscale == 0)
53                 params.lyxscale = 100;
54
55         switch (fl_get_choice(dialog_->choice_display)) {
56         case 5:
57                 params.display = grfx::NoDisplay;
58                 break;
59         case 4:
60                 params.display = grfx::ColorDisplay;
61                 break;
62         case 3:
63                 params.display = grfx::GrayscaleDisplay;
64                 break;
65         case 2:
66                 params.display = grfx::MonochromeDisplay;
67                 break;
68         case 1:
69                 params.display = grfx::DefaultDisplay;
70         }
71
72         controller().setParams(params);
73 }
74
75
76 void FormExternal::build()
77 {
78         dialog_.reset(build_external(this));
79
80         string const choice =
81                 ' ' + getStringFromVector(controller().getTemplates(), " | ") + ' ';
82         fl_addto_choice(dialog_->choice_template, choice.c_str());
83
84         fl_set_input_return (dialog_->input_filename,  FL_RETURN_CHANGED);
85
86         // Disable for read-only documents.
87         bcview().addReadOnly(dialog_->input_filename);
88         bcview().addReadOnly(dialog_->button_browse);
89
90         // Trigger an input event for cut&paste with middle mouse button.
91         setPrehandler(dialog_->input_filename);
92
93         // Activate ok/apply immediately upon input.
94         fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
95         fl_set_input_return(dialog_->input_lyxscale, FL_RETURN_CHANGED);
96
97         fl_set_input_filter(dialog_->input_lyxscale, fl_unsigned_int_filter);
98
99         string const display_List =
100                 _("Default|Monochrome|Grayscale|Color|Do not display");
101         fl_addto_choice(dialog_->choice_display, display_List.c_str());
102
103         // Set up the tooltips.
104         string str = _("The file you want to insert.");
105         tooltips().init(dialog_->input_filename, str);
106         str = _("Browse the directories.");
107         tooltips().init(dialog_->button_browse, str);
108
109         str = _("Scale the image to inserted percentage value.");
110         tooltips().init(dialog_->input_lyxscale, str);
111         str = _("Select display mode for this image.");
112         tooltips().init(dialog_->choice_display, str);
113
114         // Manage the ok, apply and cancel/close buttons
115         bcview().setOK(dialog_->button_ok);
116         bcview().setApply(dialog_->button_apply);
117         bcview().setCancel(dialog_->button_close);
118 }
119
120
121 void FormExternal::update()
122 {
123         InsetExternal::Params const & params = controller().params();
124
125         fl_set_input(dialog_->input_filename, params.filename.c_str());
126
127         int ID = controller().getTemplateNumber(params.templatename);
128         if (ID < 0) ID = 0;
129         fl_set_choice(dialog_->choice_template, ID+1);
130
131         updateComboChange();
132
133         fl_set_input(dialog_->input_lyxscale, tostr(params.lyxscale).c_str());
134
135         switch (params.display) {
136         case grfx::NoDisplay:
137                 fl_set_choice(dialog_->choice_display, 5);
138                 break;
139         case grfx::ColorDisplay:
140                 fl_set_choice(dialog_->choice_display, 4);
141                 break;
142         case grfx::GrayscaleDisplay:
143                 fl_set_choice(dialog_->choice_display, 3);
144                 break;
145         case grfx::MonochromeDisplay:
146                 fl_set_choice(dialog_->choice_display, 2);
147                 break;
148         case grfx::DefaultDisplay:
149                 fl_set_choice(dialog_->choice_display, 1);
150         }
151 }
152
153
154 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
155 {
156         ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
157         if (ob == dialog_->choice_template) {
158
159                 // set to the chosen template
160                 updateComboChange();
161
162         } else if (ob == dialog_->button_browse) {
163
164                 string const in_name  = fl_get_input(dialog_->input_filename);
165                 string const out_name = controller().Browse(in_name);
166                 fl_set_input(dialog_->input_filename, out_name.c_str());
167
168         } else if (ob == dialog_->button_edit) {
169                 controller().editExternal();
170                 result = ButtonPolicy::SMI_NOOP;
171         }
172
173         return result;
174 }
175
176
177 void FormExternal::updateComboChange()
178 {
179         int const choice = fl_get_choice(dialog_->choice_template) - 1;
180         ExternalTemplate templ = controller().getTemplate(choice);
181
182         // Update the help text
183         fl_clear_browser(dialog_->browser_helptext);
184         fl_addto_browser(dialog_->browser_helptext, templ.helpText.c_str());
185         fl_set_browser_topline(dialog_->browser_helptext, 0);
186 }