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