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.
15 #include "FormInclude.h"
16 #include "ControlInclude.h"
17 #include "forms/form_include.h"
20 #include "xforms_helpers.h" // setEnabled
23 #include "support/lstrings.h" // strip
25 #include "lyx_forms.h"
27 using namespace lyx::support;
30 typedef FormController<ControlInclude, FormView<FD_include> > base_class;
32 FormInclude::FormInclude(Dialog & parent)
33 : base_class(parent, _("Child Document"))
37 void FormInclude::build()
39 dialog_.reset(build_include(this));
41 // Manage the ok and cancel buttons
42 bcview().setOK(dialog_->button_ok);
43 bcview().setCancel(dialog_->button_close);
45 // trigger an input event for cut&paste with middle mouse button.
46 setPrehandler(dialog_->input_filename);
48 fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
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);
56 type_.init(dialog_->radio_useinput, ControlInclude::INPUT);
57 type_.init(dialog_->radio_useinclude, ControlInclude::INCLUDE);
58 type_.init(dialog_->radio_verbatim, ControlInclude::VERBATIM);
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);
80 void FormInclude::update()
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()));
86 fl_set_input(dialog_->input_filename, filename.c_str());
88 bool const inputCommand = cmdname == "input";
89 bool const includeCommand = cmdname == "include";
90 bool const verbatimStarCommand = cmdname == "verbatiminput*";
91 bool const verbatimCommand = cmdname == "verbatiminput";
93 setEnabled(dialog_->check_preview, inputCommand);
94 fl_set_button(dialog_->check_preview, inputCommand ? preview : 0);
97 type_.set(ControlInclude::INPUT);
100 type_.set(ControlInclude::INCLUDE);
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);
108 fl_set_button(dialog_->check_visiblespace, 0);
109 setEnabled(dialog_->check_visiblespace, false);
110 setEnabled(dialog_->button_load, true);
115 void FormInclude::apply()
117 InsetInclude::Params params = controller().params();
119 params.cparams.preview(fl_get_button(dialog_->check_preview));
121 string const file = fl_get_input(dialog_->input_filename);
122 if (controller().fileExists(file))
123 params.cparams.setContents(file);
125 params.cparams.setContents("");
127 ControlInclude::Type const type = ControlInclude::Type(type_.get());
128 if (type == ControlInclude::INPUT)
129 params.flag = InsetInclude::INPUT;
130 else if (type == ControlInclude::INCLUDE)
131 params.flag = InsetInclude::INCLUDE;
132 else if (type == ControlInclude::VERBATIM) {
133 if (fl_get_button(dialog_->check_visiblespace))
134 params.flag = InsetInclude::VERBAST;
136 params.flag = InsetInclude::VERB;
139 controller().setParams(params);
143 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
145 ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
147 if (ob == dialog_->button_browse) {
148 string const in_name = fl_get_input(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());
155 } else if (ob == dialog_->button_load) {
156 string const in_name = fl_get_input(dialog_->input_filename);
157 if (!rtrim(in_name).empty() && controller().fileExists(in_name)) {
159 controller().load(rtrim(in_name));
160 action = ButtonPolicy::SMI_NOOP;
163 } else if (ob == dialog_->radio_verbatim) {
164 setEnabled(dialog_->check_visiblespace, true);
165 setEnabled(dialog_->button_load, false);
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);
173 } else if (ob == dialog_->input_filename) {
174 string const in_name = fl_get_input(dialog_->input_filename);
175 if (rtrim(in_name).empty())
176 action = ButtonPolicy::SMI_INVALID;
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);