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)
73 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
74 input_length && input_length->objclass == FL_INPUT &&
75 choice_length && choice_length->objclass == FL_CHOICE);
77 switch (fl_get_choice(choice_type)) {
79 return VSpace(VSpace::NONE);
81 return VSpace(VSpace::DEFSKIP);
83 return VSpace(VSpace::SMALLSKIP);
85 return VSpace(VSpace::MEDSKIP);
87 return VSpace(VSpace::BIGSKIP);
89 return VSpace(VSpace::VFILL);
91 return VSpace(LyXGlueLength(
92 getLengthFromWidgets(input_length, choice_length)));
95 return VSpace(VSpace::NONE);
99 void setWidgetsFromVSpace(VSpace const & space,
100 FL_OBJECT * choice_type,
101 FL_OBJECT * input_length,
102 FL_OBJECT * choice_length)
105 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
106 input_length && input_length->objclass == FL_INPUT &&
107 choice_length && choice_length->objclass == FL_CHOICE);
110 switch (space.kind()) {
114 case VSpace::DEFSKIP:
117 case VSpace::SMALLSKIP:
120 case VSpace::MEDSKIP:
123 case VSpace::BIGSKIP:
133 fl_set_choice(choice_type, pos);
135 bool const custom_vspace = space.kind() == VSpace::LENGTH;
137 string const length = space.length().asString();
138 updateWidgetsFromLengthString(input_length, choice_length,
139 length, defaultUnit);
141 fl_set_input(input_length, "");
142 fl_set_choice_text(choice_length, defaultUnit.c_str());
149 typedef FormController<ControlVSpace, FormView<FD_vspace> > base_class;
151 FormVSpace::FormVSpace(Dialog & parent)
152 : base_class(parent, _("VSpace Settings"))
156 void FormVSpace::build()
159 dialog_.reset(build_vspace(this));
161 // Manage the ok, apply, restore and cancel/close buttons
162 bcview().setOK(dialog_->button_ok);
163 bcview().setApply(dialog_->button_apply);
164 bcview().setCancel(dialog_->button_close);
165 bcview().setRestore(dialog_->button_restore);
167 // disable for read-only documents
168 bcview().addReadOnly(dialog_->choice_space);
169 bcview().addReadOnly(dialog_->input_space);
170 bcview().addReadOnly(dialog_->choice_unit_space);
172 // check validity of "length + unit" input.
173 // If invalid, the label of choice_space is displayed in red.
174 addCheckedGlueLength(bcview(),
175 dialog_->input_space,
176 dialog_->choice_space);
178 // trigger an input event for cut&paste with middle mouse button.
179 setPrehandler(dialog_->input_space);
181 fl_set_input_return(dialog_->input_space, FL_RETURN_CHANGED);
183 string const spacing =
184 _("None|DefSkip|SmallSkip|MedSkip|BigSkip|VFill|Length");
185 fl_addto_choice(dialog_->choice_space, spacing.c_str());
187 // Create the contents of the unit choices; don't include the "%" terms.
188 vector<string> units_vec = getLatexUnits();
189 vector<string>::iterator del =
190 remove_if(units_vec.begin(), units_vec.end(),
191 bind2nd(contains_functor(), "%"));
192 units_vec.erase(del, units_vec.end());
194 string const units = getStringFromVector(units_vec, "|");
195 fl_addto_choice(dialog_->choice_unit_space, units.c_str());
197 // set up the tooltips
198 string str = _("Additional vertical space.");
199 tooltips().init(dialog_->choice_space, str);
201 // set default unit for custom length
202 switch (lyxrc.default_papersize) {
205 case PAPER_LEGALPAPER:
206 case PAPER_EXECUTIVEPAPER:
219 void FormVSpace::apply()
225 // If a vspace choice is "Length" but there's no text in
226 // the input field, reset the choice to "None".
227 validateVSpaceWidgets(dialog_->choice_space, dialog_->input_space);
230 setVSpaceFromWidgets(dialog_->choice_space,
231 dialog_->input_space,
232 dialog_->choice_unit_space);
234 controller().params() = space;
238 void FormVSpace::update()
240 setWidgetsFromVSpace(controller().params(),
241 dialog_->choice_space,
242 dialog_->input_space,
243 dialog_->choice_unit_space);
245 bool const custom_length =
246 fl_get_choice(dialog_->choice_space) == 7;
247 setEnabled(dialog_->input_space, custom_length);
248 setEnabled(dialog_->choice_unit_space, custom_length);
252 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
254 // Enable input when custum length is choosen,
255 // disable 'keep' when no space is choosen
256 if (ob == dialog_->choice_space) {
257 bool const custom_length =
258 fl_get_choice(dialog_->choice_space) == 7;
259 setEnabled(dialog_->input_space, custom_length);
260 setEnabled(dialog_->choice_unit_space, custom_length);
262 return ButtonPolicy::SMI_VALID;