3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
10 * Full author contact details are available in file CREDITS.
15 #include "FormVSpace.h"
16 #include "ControlVSpace.h"
17 #include "forms/form_vspace.h"
19 #include "checkedwidgets.h"
20 #include "input_validators.h"
22 #include "xforms_helpers.h"
25 #include "controllers/helper_funcs.h"
27 #include "lyxrc.h" // to set the deafult length values
31 #include "support/lstrings.h"
32 #include "support/tostr.h"
34 #include "lyx_forms.h"
36 #include <boost/bind.hpp>
47 using support::contains;
48 using support::getStringFromVector;
55 string defaultUnit("cm");
57 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
60 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
61 input_length && input_length->objclass == FL_INPUT);
63 if (fl_get_choice(choice_type) != 6)
66 // If a vspace kind is "Length" but there's no text in
67 // the input field, insert nothing.
68 string const input = rtrim(getString(input_length));
74 VSpace const setVSpaceFromWidgets(FL_OBJECT * choice_type,
75 FL_OBJECT * input_length,
76 FL_OBJECT * choice_length,
80 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
81 input_length && input_length->objclass == FL_INPUT &&
82 choice_length && choice_length->objclass == FL_CHOICE);
86 switch (fl_get_choice(choice_type)) {
88 space = VSpace(VSpace::DEFSKIP);
91 space = VSpace(VSpace::SMALLSKIP);
94 space = VSpace(VSpace::MEDSKIP);
97 space = VSpace(VSpace::BIGSKIP);
100 space = VSpace(VSpace::VFILL);
103 space = VSpace(LyXGlueLength(
104 getLengthFromWidgets(input_length, choice_length)));
113 void setWidgetsFromVSpace(VSpace const & space,
114 FL_OBJECT * choice_type,
115 FL_OBJECT * input_length,
116 FL_OBJECT * choice_length,
117 FL_OBJECT * check_keep)
120 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
121 input_length && input_length->objclass == FL_INPUT &&
122 choice_length && choice_length->objclass == FL_CHOICE &&
123 check_keep && check_keep->objclass == FL_CHECKBUTTON);
126 switch (space.kind()) {
127 case VSpace::DEFSKIP:
130 case VSpace::SMALLSKIP:
133 case VSpace::MEDSKIP:
136 case VSpace::BIGSKIP:
146 fl_set_choice(choice_type, pos);
147 fl_set_button(check_keep, space.keep());
149 bool const custom_vspace = space.kind() == VSpace::LENGTH;
151 string const length = space.length().asString();
152 updateWidgetsFromLengthString(input_length, choice_length,
153 length, defaultUnit);
155 fl_set_input(input_length, "");
156 fl_set_choice_text(choice_length, defaultUnit.c_str());
163 typedef FormController<ControlVSpace, FormView<FD_vspace> > base_class;
165 FormVSpace::FormVSpace(Dialog & parent)
166 : base_class(parent, _("VSpace Settings"))
170 void FormVSpace::build()
173 dialog_.reset(build_vspace(this));
175 // Manage the ok, apply, restore and cancel/close buttons
176 bcview().setOK(dialog_->button_ok);
177 bcview().setApply(dialog_->button_apply);
178 bcview().setCancel(dialog_->button_close);
179 bcview().setRestore(dialog_->button_restore);
181 // disable for read-only documents
182 bcview().addReadOnly(dialog_->choice_space);
183 bcview().addReadOnly(dialog_->input_space);
184 bcview().addReadOnly(dialog_->choice_unit_space);
186 // check validity of "length + unit" input.
187 // If invalid, the label of input_space is displayed in red.
188 addCheckedGlueLength(bcview(),
189 dialog_->input_space,
190 dialog_->input_space);
192 // trigger an input event for cut&paste with middle mouse button.
193 setPrehandler(dialog_->input_space);
195 fl_set_input_return(dialog_->input_space, FL_RETURN_CHANGED);
197 string const spacing =
198 _("DefSkip|SmallSkip|MedSkip|BigSkip|VFill|Length");
199 fl_addto_choice(dialog_->choice_space, spacing.c_str());
201 // Create the contents of the unit choices; don't include the "%" terms.
202 vector<string> units_vec = getLatexUnits();
203 vector<string>::iterator del =
204 remove_if(units_vec.begin(), units_vec.end(),
205 bind(contains<char>, _1, '%'));
206 units_vec.erase(del, units_vec.end());
208 string const units = getStringFromVector(units_vec, "|");
209 fl_addto_choice(dialog_->choice_unit_space, units.c_str());
211 // set up the tooltips
212 string str = _("Additional vertical space.");
213 tooltips().init(dialog_->choice_space, str);
215 // set default unit for custom length
216 switch (lyxrc.default_papersize) {
219 case PAPER_LEGALPAPER:
220 case PAPER_EXECUTIVEPAPER:
233 void FormVSpace::apply()
235 // If a vspace choice is "Length" but there's no text in
236 // the input field, insert nothing.
237 validateVSpaceWidgets(dialog_->choice_space, dialog_->input_space);
240 setVSpaceFromWidgets(dialog_->choice_space,
241 dialog_->input_space,
242 dialog_->choice_unit_space,
243 fl_get_button(dialog_->check_keep));
245 controller().params() = space;
249 void FormVSpace::update()
251 setWidgetsFromVSpace(controller().params(),
252 dialog_->choice_space,
253 dialog_->input_space,
254 dialog_->choice_unit_space,
255 dialog_->check_keep);
257 bool const custom_length =
258 fl_get_choice(dialog_->choice_space) == 6;
259 setEnabled(dialog_->input_space, custom_length);
260 setEnabled(dialog_->choice_unit_space, custom_length);
264 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
266 // Enable input when custom length is chosen,
267 // disable 'keep' when no space is chosen
268 if (ob == dialog_->choice_space) {
269 bool const custom_length =
270 fl_get_choice(dialog_->choice_space) == 6;
271 setEnabled(dialog_->input_space, custom_length);
272 setEnabled(dialog_->choice_unit_space, custom_length);
274 return ButtonPolicy::SMI_VALID;
277 } // namespace frontend