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) != 6)
60 // If a vspace kind is "Length" but there's no text in
61 // the input field, insert nothing.
62 string const input = rtrim(getString(input_length));
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);
80 switch (fl_get_choice(choice_type)) {
82 space = VSpace(VSpace::DEFSKIP);
85 space = VSpace(VSpace::SMALLSKIP);
88 space = VSpace(VSpace::MEDSKIP);
91 space = VSpace(VSpace::BIGSKIP);
94 space = VSpace(VSpace::VFILL);
97 space = VSpace(LyXGlueLength(
98 getLengthFromWidgets(input_length, choice_length)));
107 void setWidgetsFromVSpace(VSpace const & space,
108 FL_OBJECT * choice_type,
109 FL_OBJECT * input_length,
110 FL_OBJECT * choice_length,
111 FL_OBJECT * check_keep)
114 BOOST_ASSERT(choice_type && choice_type->objclass == FL_CHOICE &&
115 input_length && input_length->objclass == FL_INPUT &&
116 choice_length && choice_length->objclass == FL_CHOICE &&
117 check_keep && check_keep->objclass == FL_CHECKBUTTON);
120 switch (space.kind()) {
121 case VSpace::DEFSKIP:
124 case VSpace::SMALLSKIP:
127 case VSpace::MEDSKIP:
130 case VSpace::BIGSKIP:
140 fl_set_choice(choice_type, pos);
141 fl_set_button(check_keep, space.keep());
143 bool const custom_vspace = space.kind() == VSpace::LENGTH;
145 string const length = space.length().asString();
146 updateWidgetsFromLengthString(input_length, choice_length,
147 length, defaultUnit);
149 fl_set_input(input_length, "");
150 fl_set_choice_text(choice_length, defaultUnit.c_str());
157 typedef FormController<ControlVSpace, FormView<FD_vspace> > base_class;
159 FormVSpace::FormVSpace(Dialog & parent)
160 : base_class(parent, _("VSpace Settings"))
164 void FormVSpace::build()
167 dialog_.reset(build_vspace(this));
169 // Manage the ok, apply, restore and cancel/close buttons
170 bcview().setOK(dialog_->button_ok);
171 bcview().setApply(dialog_->button_apply);
172 bcview().setCancel(dialog_->button_close);
173 bcview().setRestore(dialog_->button_restore);
175 // disable for read-only documents
176 bcview().addReadOnly(dialog_->choice_space);
177 bcview().addReadOnly(dialog_->input_space);
178 bcview().addReadOnly(dialog_->choice_unit_space);
180 // check validity of "length + unit" input.
181 // If invalid, the label of input_space is displayed in red.
182 addCheckedGlueLength(bcview(),
183 dialog_->input_space,
184 dialog_->input_space);
186 // trigger an input event for cut&paste with middle mouse button.
187 setPrehandler(dialog_->input_space);
189 fl_set_input_return(dialog_->input_space, FL_RETURN_CHANGED);
191 string const spacing =
192 _("DefSkip|SmallSkip|MedSkip|BigSkip|VFill|Length");
193 fl_addto_choice(dialog_->choice_space, spacing.c_str());
195 // Create the contents of the unit choices; don't include the "%" terms.
196 vector<string> units_vec = getLatexUnits();
197 vector<string>::iterator del =
198 remove_if(units_vec.begin(), units_vec.end(),
199 bind2nd(contains_functor(), "%"));
200 units_vec.erase(del, units_vec.end());
202 string const units = getStringFromVector(units_vec, "|");
203 fl_addto_choice(dialog_->choice_unit_space, units.c_str());
205 // set up the tooltips
206 string str = _("Additional vertical space.");
207 tooltips().init(dialog_->choice_space, str);
209 // set default unit for custom length
210 switch (lyxrc.default_papersize) {
213 case PAPER_LEGALPAPER:
214 case PAPER_EXECUTIVEPAPER:
227 void FormVSpace::apply()
233 // If a vspace choice is "Length" but there's no text in
234 // the input field, insert nothing.
235 validateVSpaceWidgets(dialog_->choice_space, dialog_->input_space);
238 setVSpaceFromWidgets(dialog_->choice_space,
239 dialog_->input_space,
240 dialog_->choice_unit_space,
241 fl_get_button(dialog_->check_keep));
243 controller().params() = space;
247 void FormVSpace::update()
249 setWidgetsFromVSpace(controller().params(),
250 dialog_->choice_space,
251 dialog_->input_space,
252 dialog_->choice_unit_space,
253 dialog_->check_keep);
255 bool const custom_length =
256 fl_get_choice(dialog_->choice_space) == 6;
257 setEnabled(dialog_->input_space, custom_length);
258 setEnabled(dialog_->choice_unit_space, custom_length);
262 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
264 // Enable input when custum length is choosen,
265 // disable 'keep' when no space is choosen
266 if (ob == dialog_->choice_space) {
267 bool const custom_length =
268 fl_get_choice(dialog_->choice_space) == 6;
269 setEnabled(dialog_->input_space, custom_length);
270 setEnabled(dialog_->choice_unit_space, custom_length);
272 return ButtonPolicy::SMI_VALID;