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