]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormExternal.C
Martin's changes to the Note inset.
[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         string const buffer_path = kernel().bufferFilepath();
47         params.filename.set(getString(dialog_->input_filename), buffer_path);
48
49         int const choice = fl_get_choice(dialog_->choice_template) - 1;
50         params.templatename = controller().getTemplate(choice).lyxName;
51
52         params.lyxscale = strToInt(getString(dialog_->input_lyxscale));
53         if (params.lyxscale == 0)
54                 params.lyxscale = 100;
55
56         switch (fl_get_choice(dialog_->choice_display)) {
57         case 5:
58                 params.display = lyx::graphics::NoDisplay;
59                 break;
60         case 4:
61                 params.display = lyx::graphics::ColorDisplay;
62                 break;
63         case 3:
64                 params.display = lyx::graphics::GrayscaleDisplay;
65                 break;
66         case 2:
67                 params.display = lyx::graphics::MonochromeDisplay;
68                 break;
69         case 1:
70                 params.display = lyx::graphics::DefaultDisplay;
71         }
72
73         controller().setParams(params);
74 }
75
76
77 void FormExternal::build()
78 {
79         dialog_.reset(build_external(this));
80
81         string const choice =
82                 ' ' + getStringFromVector(controller().getTemplates(), " | ") + ' ';
83         fl_addto_choice(dialog_->choice_template, choice.c_str());
84
85         fl_set_input_return (dialog_->input_filename,  FL_RETURN_CHANGED);
86
87         // Disable for read-only documents.
88         bcview().addReadOnly(dialog_->input_filename);
89         bcview().addReadOnly(dialog_->button_browse);
90
91         // Trigger an input event for cut&paste with middle mouse button.
92         setPrehandler(dialog_->input_filename);
93
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);
97
98         fl_set_input_filter(dialog_->input_lyxscale, fl_unsigned_int_filter);
99
100         string const display_List =
101                 _("Default|Monochrome|Grayscale|Color|Do not display");
102         fl_addto_choice(dialog_->choice_display, display_List.c_str());
103
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);
109
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);
114
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);
119 }
120
121
122 void FormExternal::update()
123 {
124         InsetExternal::Params const & params = controller().params();
125
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());
129
130         int ID = controller().getTemplateNumber(params.templatename);
131         if (ID < 0) ID = 0;
132         fl_set_choice(dialog_->choice_template, ID+1);
133
134         updateComboChange();
135
136         fl_set_input(dialog_->input_lyxscale, tostr(params.lyxscale).c_str());
137
138         switch (params.display) {
139         case lyx::graphics::NoDisplay:
140                 fl_set_choice(dialog_->choice_display, 5);
141                 break;
142         case lyx::graphics::ColorDisplay:
143                 fl_set_choice(dialog_->choice_display, 4);
144                 break;
145         case lyx::graphics::GrayscaleDisplay:
146                 fl_set_choice(dialog_->choice_display, 3);
147                 break;
148         case lyx::graphics::MonochromeDisplay:
149                 fl_set_choice(dialog_->choice_display, 2);
150                 break;
151         case lyx::graphics::DefaultDisplay:
152                 fl_set_choice(dialog_->choice_display, 1);
153         }
154 }
155
156
157 ButtonPolicy::SMInput FormExternal::input(FL_OBJECT * ob, long)
158 {
159         ButtonPolicy::SMInput result = ButtonPolicy::SMI_VALID;
160         if (ob == dialog_->choice_template) {
161
162                 // set to the chosen template
163                 updateComboChange();
164
165         } else if (ob == dialog_->button_browse) {
166
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());
170
171         } else if (ob == dialog_->button_edit) {
172                 controller().editExternal();
173                 result = ButtonPolicy::SMI_NOOP;
174         }
175
176         return result;
177 }
178
179
180 void FormExternal::updateComboChange()
181 {
182         int const choice = fl_get_choice(dialog_->choice_template) - 1;
183         ExternalTemplate templ = controller().getTemplate(choice);
184
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);
189 }