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
20 #include "ControlInclude.h"
21 #include "FormInclude.h"
22 #include "forms/form_include.h"
23 #include "insets/insetinclude.h"
25 #include "xforms_helpers.h" // setEnabled
26 #include "support/lstrings.h" // strip
27 #include "lyx_forms.h"
29 typedef FormController<ControlInclude, FormView<FD_include> > base_class;
31 FormInclude::FormInclude(Dialog & parent)
32 : base_class(parent, _("Child Document"))
36 void FormInclude::build()
38 dialog_.reset(build_include(this));
40 // Manage the ok and cancel buttons
41 bcview().setOK(dialog_->button_ok);
42 bcview().setCancel(dialog_->button_close);
44 // trigger an input event for cut&paste with middle mouse button.
45 setPrehandler(dialog_->input_filename);
47 fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
49 // disable for read-only documents
50 bcview().addReadOnly(dialog_->button_browse);
51 bcview().addReadOnly(dialog_->radio_useinput);
52 bcview().addReadOnly(dialog_->radio_useinclude);
53 bcview().addReadOnly(dialog_->radio_verbatim);
55 type_.init(dialog_->radio_useinput, ControlInclude::INPUT);
56 type_.init(dialog_->radio_useinclude, ControlInclude::INCLUDE);
57 type_.init(dialog_->radio_verbatim, ControlInclude::VERBATIM);
59 // set up the tooltips
60 string str = _("File name to include.");
61 tooltips().init(dialog_->input_filename, str);
62 str = _("Browse directories for file name.");
63 tooltips().init(dialog_->button_browse, str);
64 str = _("Use LaTeX \\input.");
65 tooltips().init(dialog_->radio_useinput, str);
66 str = _("Use LaTeX \\include.");
67 tooltips().init(dialog_->radio_useinclude, str);
68 str = _("Use LaTeX \\verbatiminput.");
69 tooltips().init(dialog_->radio_verbatim, str);
70 str = _("Underline spaces in generated output.");
71 tooltips().init(dialog_->check_visiblespace, str);
72 str = _("Show LaTeX preview.");
73 tooltips().init(dialog_->check_preview, str);
74 str = _("Load the file.");
75 tooltips().init(dialog_->button_load, str);
79 void FormInclude::update()
81 string const filename = controller().params().cparams.getContents();
82 string const cmdname = controller().params().cparams.getCmdName();
83 bool const preview = static_cast<bool>((controller().params().cparams.preview()));
85 fl_set_input(dialog_->input_filename, filename.c_str());
87 bool const inputCommand = cmdname == "input";
88 bool const includeCommand = cmdname == "include";
89 bool const verbatimStarCommand = cmdname == "verbatiminput*";
90 bool const verbatimCommand = cmdname == "verbatiminput";
92 setEnabled(dialog_->check_preview, inputCommand);
93 fl_set_button(dialog_->check_preview, inputCommand ? preview : 0);
96 type_.set(ControlInclude::INPUT);
99 type_.set(ControlInclude::INCLUDE);
101 if (verbatimCommand || verbatimStarCommand) {
102 type_.set(ControlInclude::VERBATIM);
103 fl_set_button(dialog_->check_visiblespace, verbatimStarCommand);
104 setEnabled(dialog_->check_visiblespace, true);
105 setEnabled(dialog_->button_load, false);
107 fl_set_button(dialog_->check_visiblespace, 0);
108 setEnabled(dialog_->check_visiblespace, false);
109 setEnabled(dialog_->button_load, true);
114 void FormInclude::apply()
116 InsetInclude::Params params = controller().params();
118 params.cparams.preview(fl_get_button(dialog_->check_preview));
120 string const file = fl_get_input(dialog_->input_filename);
121 if (controller().fileExists(file))
122 params.cparams.setContents(file);
124 params.cparams.setContents("");
126 ControlInclude::Type const type = ControlInclude::Type(type_.get());
127 if (type == ControlInclude::INPUT)
128 params.flag = InsetInclude::INPUT;
129 else if (type == ControlInclude::INCLUDE)
130 params.flag = InsetInclude::INCLUDE;
131 else if (type == ControlInclude::VERBATIM) {
132 if (fl_get_button(dialog_->check_visiblespace))
133 params.flag = InsetInclude::VERBAST;
135 params.flag = InsetInclude::VERB;
138 controller().setParams(params);
142 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
144 ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
146 if (ob == dialog_->button_browse) {
147 string const in_name = fl_get_input(dialog_->input_filename);
148 fl_freeze_form(form());
149 ControlInclude::Type const type = ControlInclude::Type(type_.get());
150 string const out_name = controller().Browse(in_name, type);
151 fl_set_input(dialog_->input_filename, out_name.c_str());
152 fl_unfreeze_form(form());
154 } else if (ob == dialog_->button_load) {
155 string const in_name = fl_get_input(dialog_->input_filename);
156 if (!rtrim(in_name).empty() && controller().fileExists(in_name)) {
158 controller().load(rtrim(in_name));
159 action = ButtonPolicy::SMI_NOOP;
162 } else if (ob == dialog_->radio_verbatim) {
163 setEnabled(dialog_->check_visiblespace, true);
164 setEnabled(dialog_->button_load, false);
166 } else if (ob == dialog_->radio_useinclude ||
167 ob == dialog_->radio_useinput) {
168 fl_set_button(dialog_->check_visiblespace, 0);
169 setEnabled(dialog_->check_visiblespace, false);
170 setEnabled(dialog_->button_load, true);
172 } else if (ob == dialog_->input_filename) {
173 string const in_name = fl_get_input(dialog_->input_filename);
174 if (rtrim(in_name).empty())
175 action = ButtonPolicy::SMI_INVALID;
178 if (ob == dialog_->radio_useinput) {
179 setEnabled(dialog_->check_preview, true);
180 } else if (ob == dialog_->radio_verbatim ||
181 ob == dialog_->radio_useinclude) {
182 fl_set_button(dialog_->check_preview, 0);
183 setEnabled(dialog_->check_preview, false);