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