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