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 lyx::support::rtrim;
32 typedef FormController<ControlInclude, FormView<FD_include> > base_class;
34 FormInclude::FormInclude(Dialog & parent)
35 : base_class(parent, _("Child Document"))
39 void FormInclude::build()
41 dialog_.reset(build_include(this));
43 // Manage the ok and cancel buttons
44 bcview().setOK(dialog_->button_ok);
45 bcview().setCancel(dialog_->button_close);
47 // trigger an input event for cut&paste with middle mouse button.
48 setPrehandler(dialog_->input_filename);
50 fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
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);
58 type_.init(dialog_->radio_useinput, ControlInclude::INPUT);
59 type_.init(dialog_->radio_useinclude, ControlInclude::INCLUDE);
60 type_.init(dialog_->radio_verbatim, ControlInclude::VERBATIM);
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);
82 void FormInclude::update()
84 string const filename = controller().params().getContents();
85 string const cmdname = controller().params().getCmdName();
86 bool const preview = static_cast<bool>((controller().params().preview()));
88 fl_set_input(dialog_->input_filename, filename.c_str());
90 bool const inputCommand = cmdname == "input";
91 bool const includeCommand = cmdname == "include";
92 bool const verbatimStarCommand = cmdname == "verbatiminput*";
93 bool const verbatimCommand = cmdname == "verbatiminput";
95 setEnabled(dialog_->check_preview, inputCommand);
96 fl_set_button(dialog_->check_preview, inputCommand ? preview : 0);
99 type_.set(ControlInclude::INPUT);
102 type_.set(ControlInclude::INCLUDE);
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);
110 fl_set_button(dialog_->check_visiblespace, 0);
111 setEnabled(dialog_->check_visiblespace, false);
112 setEnabled(dialog_->button_load, true);
117 void FormInclude::apply()
119 InsetCommandParams params = controller().params();
121 params.preview(fl_get_button(dialog_->check_preview));
122 params.setContents(getString(dialog_->input_filename));
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*");
133 params.setCmdName("verbatiminput");
136 controller().setParams(params);
140 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
142 ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
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());
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)) {
156 controller().load(rtrim(in_name));
157 action = ButtonPolicy::SMI_NOOP;
160 } else if (ob == dialog_->radio_verbatim) {
161 setEnabled(dialog_->check_visiblespace, true);
162 setEnabled(dialog_->button_load, false);
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);
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;
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);