]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
Wrap most of the frontend code up inside namespace lyx::frontend.
[lyx.git] / src / frontends / xforms / FormInclude.C
1 /**
2  * \file FormInclude.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
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 "FormInclude.h"
16 #include "ControlInclude.h"
17 #include "forms/form_include.h"
18
19 #include "Tooltips.h"
20 #include "xforms_helpers.h" // setEnabled
21 #include "xformsBC.h"
22
23 #include "support/lstrings.h" // strip
24
25 #include "lyx_forms.h"
26
27 using std::string;
28
29 namespace lyx {
30
31 using support::rtrim;
32
33 namespace frontend {
34
35 typedef FormController<ControlInclude, FormView<FD_include> > base_class;
36
37 FormInclude::FormInclude(Dialog & parent)
38         : base_class(parent, _("Child Document"))
39 {}
40
41
42 void FormInclude::build()
43 {
44         dialog_.reset(build_include(this));
45
46         // Manage the ok and cancel buttons
47         bcview().setOK(dialog_->button_ok);
48         bcview().setCancel(dialog_->button_close);
49
50         // trigger an input event for cut&paste with middle mouse button.
51         setPrehandler(dialog_->input_filename);
52
53         fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
54
55         // disable for read-only documents
56         bcview().addReadOnly(dialog_->button_browse);
57         bcview().addReadOnly(dialog_->radio_useinput);
58         bcview().addReadOnly(dialog_->radio_useinclude);
59         bcview().addReadOnly(dialog_->radio_verbatim);
60
61         type_.init(dialog_->radio_useinput,   ControlInclude::INPUT);
62         type_.init(dialog_->radio_useinclude, ControlInclude::INCLUDE);
63         type_.init(dialog_->radio_verbatim,   ControlInclude::VERBATIM);
64
65         // set up the tooltips
66         string str = _("File name to include.");
67         tooltips().init(dialog_->input_filename, str);
68         str = _("Browse directories for file name.");
69         tooltips().init(dialog_->button_browse, str);
70         str = _("Use LaTeX \\input.");
71         tooltips().init(dialog_->radio_useinput, str);
72         str = _("Use LaTeX \\include.");
73         tooltips().init(dialog_->radio_useinclude, str);
74         str = _("Use LaTeX \\verbatiminput.");
75         tooltips().init(dialog_->radio_verbatim, str);
76         str = _("Underline spaces in generated output.");
77         tooltips().init(dialog_->check_visiblespace, str);
78         str = _("Show LaTeX preview.");
79         tooltips().init(dialog_->check_preview, str);
80         str = _("Load the file.");
81         tooltips().init(dialog_->button_load, str);
82 }
83
84
85 void FormInclude::update()
86 {
87         string const filename = controller().params().getContents();
88         string const cmdname = controller().params().getCmdName();
89         bool const preview = static_cast<bool>((controller().params().preview()));
90
91         fl_set_input(dialog_->input_filename, filename.c_str());
92
93         bool const inputCommand = cmdname == "input";
94         bool const includeCommand = cmdname == "include";
95         bool const verbatimStarCommand = cmdname == "verbatiminput*";
96         bool const verbatimCommand = cmdname == "verbatiminput";
97
98         setEnabled(dialog_->check_preview, inputCommand);
99         fl_set_button(dialog_->check_preview, inputCommand ? preview : 0);
100
101         if (cmdname.empty())
102                 type_.set(ControlInclude::INPUT);
103
104         if (includeCommand)
105                 type_.set(ControlInclude::INCLUDE);
106
107         if (verbatimCommand || verbatimStarCommand) {
108                 type_.set(ControlInclude::VERBATIM);
109                 fl_set_button(dialog_->check_visiblespace, verbatimStarCommand);
110                 setEnabled(dialog_->check_visiblespace, true);
111                 setEnabled(dialog_->button_load, false);
112         } else {
113                 fl_set_button(dialog_->check_visiblespace, 0);
114                 setEnabled(dialog_->check_visiblespace, false);
115                 setEnabled(dialog_->button_load, true);
116         }
117 }
118
119
120 void FormInclude::apply()
121 {
122         InsetCommandParams params = controller().params();
123
124         params.preview(fl_get_button(dialog_->check_preview));
125         params.setContents(getString(dialog_->input_filename));
126
127         ControlInclude::Type const type = ControlInclude::Type(type_.get());
128         if (type == ControlInclude::INPUT)
129                 params.setCmdName("input");
130         else if (type == ControlInclude::INCLUDE)
131                 params.setCmdName("include");
132         else if (type == ControlInclude::VERBATIM) {
133                 if (fl_get_button(dialog_->check_visiblespace))
134                         params.setCmdName("verbatiminput*");
135                 else
136                         params.setCmdName("verbatiminput");
137         }
138
139         controller().setParams(params);
140 }
141
142
143 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
144 {
145         ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
146
147         if (ob == dialog_->button_browse) {
148                 string const in_name = getString(dialog_->input_filename);
149                 fl_freeze_form(form());
150                 ControlInclude::Type const type = ControlInclude::Type(type_.get());
151                 string const out_name = controller().browse(in_name, type);
152                 fl_set_input(dialog_->input_filename, out_name.c_str());
153                 fl_unfreeze_form(form());
154
155         } else if (ob == dialog_->button_load) {
156                 string const in_name = getString(dialog_->input_filename);
157                 if (!rtrim(in_name).empty() && controller().fileExists(in_name)) {
158                         dialog().OKButton();
159                         controller().load(rtrim(in_name));
160                         action = ButtonPolicy::SMI_NOOP;
161                 }
162
163         } else if (ob == dialog_->radio_verbatim) {
164                 setEnabled(dialog_->check_visiblespace, true);
165                 setEnabled(dialog_->button_load, false);
166
167         } else if (ob == dialog_->radio_useinclude ||
168                    ob == dialog_->radio_useinput) {
169                 fl_set_button(dialog_->check_visiblespace, 0);
170                 setEnabled(dialog_->check_visiblespace, false);
171                 setEnabled(dialog_->button_load, true);
172
173         } else if (ob == dialog_->input_filename) {
174                 string const in_name = getString(dialog_->input_filename);
175                 if (rtrim(in_name).empty())
176                         action = ButtonPolicy::SMI_INVALID;
177         }
178
179         if (ob == dialog_->radio_useinput) {
180                 setEnabled(dialog_->check_preview, true);
181         } else if (ob == dialog_->radio_verbatim ||
182                    ob == dialog_->radio_useinclude) {
183                 fl_set_button(dialog_->check_preview, 0);
184                 setEnabled(dialog_->check_preview, false);
185         }
186
187         return action;
188 }
189
190 } // namespace frontend
191 } // namespace lyx