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>
38 using lyx::support::contains;
39 using lyx::support::getStringFromVector;
40 using lyx::support::rtrim;
52 string defaultUnit("cm");
54 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
57 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
58 input_length && input_length->objclass == FL_INPUT);
60 if (fl_get_choice(choice_type) != 6)
63 // If a vspace kind is "Length" but there's no text in
64 // the input field, insert nothing.
65 string const input = rtrim(getString(input_length));
71 VSpace const setVSpaceFromWidgets(FL_OBJECT * choice_type,
72 FL_OBJECT * input_length,
73 FL_OBJECT * choice_length,
77 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
78 input_length && input_length->objclass == FL_INPUT &&
79 choice_length && choice_length->objclass == FL_CHOICE);
83 switch (fl_get_choice(choice_type)) {
85 space = VSpace(VSpace::DEFSKIP);
88 space = VSpace(VSpace::SMALLSKIP);
91 space = VSpace(VSpace::MEDSKIP);
94 space = VSpace(VSpace::BIGSKIP);
97 space = VSpace(VSpace::VFILL);
100 space = VSpace(LyXGlueLength(
101 getLengthFromWidgets(input_length, choice_length)));
110 void setWidgetsFromVSpace(VSpace const & space,
111 FL_OBJECT * choice_type,
112 FL_OBJECT * input_length,
113 FL_OBJECT * choice_length,
114 FL_OBJECT * check_keep)
117 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
118 input_length && input_length->objclass == FL_INPUT &&
119 choice_length && choice_length->objclass == FL_CHOICE &&
120 check_keep && check_keep->objclass == FL_CHECKBUTTON);
123 switch (space.kind()) {
124 case VSpace::DEFSKIP:
127 case VSpace::SMALLSKIP:
130 case VSpace::MEDSKIP:
133 case VSpace::BIGSKIP:
143 fl_set_choice(choice_type, pos);
144 fl_set_button(check_keep, space.keep());
146 bool const custom_vspace = space.kind() == VSpace::LENGTH;
148 string const length = space.length().asString();
149 updateWidgetsFromLengthString(input_length, choice_length,
150 length, defaultUnit);
152 fl_set_input(input_length, "");
153 fl_set_choice_text(choice_length, defaultUnit.c_str());
160 typedef FormController<ControlVSpace, FormView<FD_vspace> > base_class;
162 FormVSpace::FormVSpace(Dialog & parent)
163 : base_class(parent, _("VSpace Settings"))
167 void FormVSpace::build()
170 dialog_.reset(build_vspace(this));
172 // Manage the ok, apply, restore and cancel/close buttons
173 bcview().setOK(dialog_->button_ok);
174 bcview().setApply(dialog_->button_apply);
175 bcview().setCancel(dialog_->button_close);
176 bcview().setRestore(dialog_->button_restore);
178 // disable for read-only documents
179 bcview().addReadOnly(dialog_->choice_space);
180 bcview().addReadOnly(dialog_->input_space);
181 bcview().addReadOnly(dialog_->choice_unit_space);
183 // check validity of "length + unit" input.
184 // If invalid, the label of input_space is displayed in red.
185 addCheckedGlueLength(bcview(),
186 dialog_->input_space,
187 dialog_->input_space);
189 // trigger an input event for cut&paste with middle mouse button.
190 setPrehandler(dialog_->input_space);
192 fl_set_input_return(dialog_->input_space, FL_RETURN_CHANGED);
194 string const spacing =
195 _("DefSkip|SmallSkip|MedSkip|BigSkip|VFill|Length");
196 fl_addto_choice(dialog_->choice_space, spacing.c_str());
198 // Create the contents of the unit choices; don't include the "%" terms.
199 vector<string> units_vec = getLatexUnits();
200 vector<string>::iterator del =
201 remove_if(units_vec.begin(), units_vec.end(),
202 bind(contains<char>, _1, '%'));
203 units_vec.erase(del, units_vec.end());
205 string const units = getStringFromVector(units_vec, "|");
206 fl_addto_choice(dialog_->choice_unit_space, units.c_str());
208 // set up the tooltips
209 string str = _("Additional vertical space.");
210 tooltips().init(dialog_->choice_space, str);
212 // set default unit for custom length
213 switch (lyxrc.default_papersize) {
216 case PAPER_LEGALPAPER:
217 case PAPER_EXECUTIVEPAPER:
230 void FormVSpace::apply()
232 // If a vspace choice is "Length" but there's no text in
233 // the input field, insert nothing.
234 validateVSpaceWidgets(dialog_->choice_space, dialog_->input_space);
237 setVSpaceFromWidgets(dialog_->choice_space,
238 dialog_->input_space,
239 dialog_->choice_unit_space,
240 fl_get_button(dialog_->check_keep));
242 controller().params() = space;
246 void FormVSpace::update()
248 setWidgetsFromVSpace(controller().params(),
249 dialog_->choice_space,
250 dialog_->input_space,
251 dialog_->choice_unit_space,
252 dialog_->check_keep);
254 bool const custom_length =
255 fl_get_choice(dialog_->choice_space) == 6;
256 setEnabled(dialog_->input_space, custom_length);
257 setEnabled(dialog_->choice_unit_space, custom_length);
261 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
263 // Enable input when custom length is chosen,
264 // disable 'keep' when no space is chosen
265 if (ob == dialog_->choice_space) {
266 bool const custom_length =
267 fl_get_choice(dialog_->choice_space) == 6;
268 setEnabled(dialog_->input_space, custom_length);
269 setEnabled(dialog_->choice_unit_space, custom_length);
271 return ButtonPolicy::SMI_VALID;