]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormExternal.C
0a5e0e03bc6de2cdeb57da7956af7f8b3ba9a4c9
[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 "support/tostr.h"
28 #include "support/lstrings.h"
29 #include "lyx_forms.h"
30
31 typedef FormController<ControlExternal, FormView<FD_external> > base_class;
32
33 FormExternal::FormExternal(Dialog & parent)
34         : base_class(parent, _("External Material"))
35 {}
36
37
38 void FormExternal::apply()
39 {
40         InsetExternal::Params params = controller().params();
41
42         params.filename = fl_get_input(dialog_->input_filename);
43
44         int const choice = fl_get_choice(dialog_->choice_template) - 1;
45         params.templ = controller().getTemplate(choice);
46
47         params.lyxscale = strToInt(getString(dialog_->input_lyxscale));
48         if (params.lyxscale == 0)
49                 params.lyxscale = 100;
50
51         switch (fl_get_choice(dialog_->choice_display)) {
52         case 5:
53                 params.display = grfx::NoDisplay;
54                 break;
55         case 4:
56                 params.display = grfx::ColorDisplay;
57                 break;
58         case 3:
59                 params.display = grfx::GrayscaleDisplay;
60                 break;
61         case 2:
62                 params.display = grfx::MonochromeDisplay;
63                 break;
64         case 1:
65                 params.display = grfx::DefaultDisplay;
66         }
67
68         controller().setParams(params);
69 }
70
71
72 void FormExternal::build()
73 {
74         dialog_.reset(build_external(this));
75
76         string const choice =
77                 ' ' + getStringFromVector(controller().getTemplates(), " | ") + ' ';
78         fl_addto_choice(dialog_->choice_template, choice.c_str());
79
80         fl_set_input_return (dialog_->input_filename,  FL_RETURN_CHANGED);
81
82         // Disable for read-only documents.
83         bcview().addReadOnly(dialog_->input_filename);
84         bcview().addReadOnly(dialog_->button_browse);
85
86         // Trigger an input event for cut&paste with middle mouse button.
87         setPrehandler(dialog_->input_filename);
88
89         // Activate ok/apply immediately upon input.
90         fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
91         fl_set_input_return(dialog_->input_lyxscale, FL_RETURN_CHANGED);
92
93         fl_set_input_filter(dialog_->input_lyxscale, fl_unsigned_int_filter);
94
95         string const display_List =
96                 _("Default|Monochrome|Grayscale|Color|Do not display");
97         fl_addto_choice(dialog_->choice_display, display_List.c_str());
98
99         // Set up the tooltips.
100         string str = _("The file you want to insert.");
101         tooltips().init(dialog_->input_filename, str);
102         str = _("Browse the directories.");
103         tooltips().init(dialog_->button_browse, str);
104
105         str = _("Scale the image to inserted percentage value.");
106         tooltips().init(dialog_->input_lyxscale, str);
107         str = _("Select display mode for this image.");
108         tooltips().init(dialog_->choice_display, str);
109
110         // Manage the ok, apply and cancel/close buttons
111         bcview().setOK(dialog_->button_ok);
112         bcview().setApply(dialog_->button_apply);
113         bcview().setCancel(dialog_->button_close);
114 }
115
116
117 void FormExternal::update()
118 {
119         InsetExternal::Params const & params = controller().params();
120
121         fl_set_input(dialog_->input_filename, params.filename.c_str());
122
123         int ID = controller().getTemplateNumber(params.templ.lyxName);
124         if (ID < 0) ID = 0;
125         fl_set_choice(dialog_->choice_template, ID+1);
126
127         updateComboChange();
128
129         fl_set_input(dialog_->input_lyxscale, tostr(params.lyxscale).c_str());
130
131         switch (params.display) {
132         case grfx::NoDisplay:
133                 fl_set_choice(dialog_->choice_display, 5);
134                 break;
135         case grfx::ColorDisplay:
136                 fl_set_choice(dialog_->choice_display, 4);
137                 break;
138         case grfx::GrayscaleDisplay:
139                 fl_set_choice(dialog_->choice_display, 3);
140                 break;
141         case grfx::MonochromeDisplay:
142                 fl_set_choice(dialog_->choice_display, 2);
143                 break;
144         case grfx::DefaultDisplay:
145                 fl_set_choice(dialog_->choice_display, 1);
146         }
147 }
148
149
150 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
151 {
152         if (ob == dialog_->choice_template) {
153
154                 // set to the chosen template
155                 int const choice = fl_get_choice(dialog_->choice_template) - 1;
156                 InsetExternal::Params params = controller().params();
157                 params.templ = controller().getTemplate(choice);
158                 controller().setParams(params);
159
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         }
171
172         return ButtonPolicy::SMI_VALID;
173 }
174
175
176 void FormExternal::updateComboChange()
177 {
178         // Update the help text
179         fl_clear_browser(dialog_->browser_helptext);
180         fl_addto_browser(dialog_->browser_helptext,
181                          controller().params().templ.helpText.c_str());
182         fl_set_browser_topline(dialog_->browser_helptext, 0);
183 }