]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormExternal.C
Strip out another 180 #includes.
[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 "FormExternal.h"
16 #include "ControlExternal.h"
17 #include "forms/form_external.h"
18
19 #include "input_validators.h"
20 #include "Tooltips.h"
21 #include "xforms_helpers.h"
22 #include "xformsBC.h"
23
24 #include "insets/ExternalTemplate.h"
25
26 #include "support/lstrings.h"
27 #include "support/tostr.h"
28
29 #include "lyx_forms.h"
30
31 using namespace lyx::support;
32
33
34 typedef FormController<ControlExternal, FormView<FD_external> > base_class;
35
36 FormExternal::FormExternal(Dialog & parent)
37         : base_class(parent, _("External Material"))
38 {}
39
40
41 void FormExternal::apply()
42 {
43         InsetExternal::Params params = controller().params();
44
45         string const buffer_path = kernel().bufferFilepath();
46         params.filename.set(getString(dialog_->input_filename), buffer_path);
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 = lyx::graphics::NoDisplay;
58                 break;
59         case 4:
60                 params.display = lyx::graphics::ColorDisplay;
61                 break;
62         case 3:
63                 params.display = lyx::graphics::GrayscaleDisplay;
64                 break;
65         case 2:
66                 params.display = lyx::graphics::MonochromeDisplay;
67                 break;
68         case 1:
69                 params.display = lyx::graphics::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         string const buffer_path = kernel().bufferFilepath();
126         string const name = params.filename.outputFilename(buffer_path);
127         fl_set_input(dialog_->input_filename, name.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 lyx::graphics::NoDisplay:
139                 fl_set_choice(dialog_->choice_display, 5);
140                 break;
141         case lyx::graphics::ColorDisplay:
142                 fl_set_choice(dialog_->choice_display, 4);
143                 break;
144         case lyx::graphics::GrayscaleDisplay:
145                 fl_set_choice(dialog_->choice_display, 3);
146                 break;
147         case lyx::graphics::MonochromeDisplay:
148                 fl_set_choice(dialog_->choice_display, 2);
149                 break;
150         case lyx::graphics::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 }