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 using lyx::support::contains_functor;
37 using lyx::support::getStringFromVector;
38 using lyx::support::rtrim;
49 string defaultUnit("cm");
51 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
54 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
55 input_length && input_length->objclass == FL_INPUT);
57 if (fl_get_choice(choice_type) != 7)
60 // If a vspace kind is "Length" but there's no text in
61 // the input field, reset the kind to "None".
62 string const input = rtrim(getString(input_length));
64 fl_set_choice(choice_type, 1);
68 VSpace const setVSpaceFromWidgets(FL_OBJECT * choice_type,
69 FL_OBJECT * input_length,
70 FL_OBJECT * choice_length,
74 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
75 input_length && input_length->objclass == FL_INPUT &&
76 choice_length && choice_length->objclass == FL_CHOICE);
78 VSpace space = VSpace(VSpace::NONE);
80 switch (fl_get_choice(choice_type)) {
82 space = VSpace(VSpace::NONE);
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()) {
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 _("None|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 bind2nd(contains_functor(), "%"));
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()
239 // If a vspace choice is "Length" but there's no text in
240 // the input field, reset the choice to "None".
241 validateVSpaceWidgets(dialog_->choice_space, dialog_->input_space);
244 setVSpaceFromWidgets(dialog_->choice_space,
245 dialog_->input_space,
246 dialog_->choice_unit_space,
247 fl_get_button(dialog_->check_keep));
249 controller().params() = space;
253 void FormVSpace::update()
255 setWidgetsFromVSpace(controller().params(),
256 dialog_->choice_space,
257 dialog_->input_space,
258 dialog_->choice_unit_space,
259 dialog_->check_keep);
261 bool const custom_length =
262 fl_get_choice(dialog_->choice_space) == 7;
263 setEnabled(dialog_->input_space, custom_length);
264 setEnabled(dialog_->choice_unit_space, custom_length);
268 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
270 // Enable input when custum length is choosen,
271 // disable 'keep' when no space is choosen
272 if (ob == dialog_->choice_space) {
273 bool const custom_length =
274 fl_get_choice(dialog_->choice_space) == 7;
275 setEnabled(dialog_->input_space, custom_length);
276 setEnabled(dialog_->choice_unit_space, custom_length);
278 return ButtonPolicy::SMI_VALID;