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 FORMS_H_LOCATION
29 typedef FormCB<ControlInclude, FormDB<FD_include> > base_class;
31 FormInclude::FormInclude()
32 : base_class(_("Include file"))
36 void FormInclude::build()
38 dialog_.reset(build_include(this));
40 // Manage the ok and cancel buttons
41 bc().setOK(dialog_->button_ok);
42 bc().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 bc().addReadOnly(dialog_->button_browse);
51 bc().addReadOnly(dialog_->radio_useinput);
52 bc().addReadOnly(dialog_->radio_useinclude);
53 bc().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 controller().params().cparams
117 .preview(fl_get_button(dialog_->check_preview));
119 string const file = fl_get_input(dialog_->input_filename);
120 if (controller().fileExists(file))
121 controller().params().cparams.setContents(file);
123 controller().params().cparams.setContents("");
125 ControlInclude::Type const type = ControlInclude::Type(type_.get());
126 if (type == ControlInclude::INPUT)
127 controller().params().flag = InsetInclude::INPUT;
128 else if (type == ControlInclude::INCLUDE)
129 controller().params().flag = InsetInclude::INCLUDE;
130 else if (type == ControlInclude::VERBATIM) {
131 if (fl_get_button(dialog_->check_visiblespace))
132 controller().params().flag = InsetInclude::VERBAST;
134 controller().params().flag = InsetInclude::VERB;
139 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
141 ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
143 if (ob == dialog_->button_browse) {
144 string const in_name = fl_get_input(dialog_->input_filename);
145 fl_freeze_form(form());
146 ControlInclude::Type const type = ControlInclude::Type(type_.get());
147 string const out_name = controller().Browse(in_name, type);
148 fl_set_input(dialog_->input_filename, out_name.c_str());
149 fl_unfreeze_form(form());
151 } else if (ob == dialog_->button_load) {
152 string const in_name = fl_get_input(dialog_->input_filename);
153 if (!rtrim(in_name).empty() && controller().fileExists(in_name)) {
154 controller().OKButton();
155 controller().load(rtrim(in_name));
156 action = ButtonPolicy::SMI_NOOP;
159 } else if (ob == dialog_->radio_verbatim) {
160 setEnabled(dialog_->check_visiblespace, true);
161 setEnabled(dialog_->button_load, false);
163 } else if (ob == dialog_->radio_useinclude ||
164 ob == dialog_->radio_useinput) {
165 fl_set_button(dialog_->check_visiblespace, 0);
166 setEnabled(dialog_->check_visiblespace, false);
167 setEnabled(dialog_->button_load, true);
169 } else if (ob == dialog_->input_filename) {
170 string const in_name = fl_get_input(dialog_->input_filename);
171 if (rtrim(in_name).empty())
172 action = ButtonPolicy::SMI_INVALID;
175 if (ob == dialog_->radio_useinput) {
176 setEnabled(dialog_->check_preview, true);
177 } else if (ob == dialog_->radio_verbatim ||
178 ob == dialog_->radio_useinclude) {
179 fl_set_button(dialog_->check_preview, 0);
180 setEnabled(dialog_->check_preview, false);