3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
18 #include "ControlInclude.h"
19 #include "FormInclude.h"
20 #include "forms/form_include.h"
21 #include "insets/insetinclude.h"
23 #include "xforms_helpers.h" // setEnabled
24 #include "support/lstrings.h" // strip
25 #include "lyx_forms.h"
29 using namespace lyx::support;
31 typedef FormController<ControlInclude, FormView<FD_include> > base_class;
33 FormInclude::FormInclude(Dialog & parent)
34 : base_class(parent, _("Child Document"))
38 void FormInclude::build()
40 dialog_.reset(build_include(this));
42 // Manage the ok and cancel buttons
43 bcview().setOK(dialog_->button_ok);
44 bcview().setCancel(dialog_->button_close);
46 // trigger an input event for cut&paste with middle mouse button.
47 setPrehandler(dialog_->input_filename);
49 fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
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);
57 type_.init(dialog_->radio_useinput, ControlInclude::INPUT);
58 type_.init(dialog_->radio_useinclude, ControlInclude::INCLUDE);
59 type_.init(dialog_->radio_verbatim, ControlInclude::VERBATIM);
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);
81 void FormInclude::update()
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()));
87 fl_set_input(dialog_->input_filename, filename.c_str());
89 bool const inputCommand = cmdname == "input";
90 bool const includeCommand = cmdname == "include";
91 bool const verbatimStarCommand = cmdname == "verbatiminput*";
92 bool const verbatimCommand = cmdname == "verbatiminput";
94 setEnabled(dialog_->check_preview, inputCommand);
95 fl_set_button(dialog_->check_preview, inputCommand ? preview : 0);
98 type_.set(ControlInclude::INPUT);
101 type_.set(ControlInclude::INCLUDE);
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);
109 fl_set_button(dialog_->check_visiblespace, 0);
110 setEnabled(dialog_->check_visiblespace, false);
111 setEnabled(dialog_->button_load, true);
116 void FormInclude::apply()
118 InsetInclude::Params params = controller().params();
120 params.cparams.preview(fl_get_button(dialog_->check_preview));
122 string const file = fl_get_input(dialog_->input_filename);
123 if (controller().fileExists(file))
124 params.cparams.setContents(file);
126 params.cparams.setContents("");
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;
137 params.flag = InsetInclude::VERB;
140 controller().setParams(params);
144 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
146 ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
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());
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)) {
160 controller().load(rtrim(in_name));
161 action = ButtonPolicy::SMI_NOOP;
164 } else if (ob == dialog_->radio_verbatim) {
165 setEnabled(dialog_->check_visiblespace, true);
166 setEnabled(dialog_->button_load, false);
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);
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;
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);