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);
171 // check validity of "length + unit" input
172 addCheckedGlueLength(bcview(),
173 dialog_->input_space,
174 dialog_->choice_space);
176 // trigger an input event for cut&paste with middle mouse button.
177 setPrehandler(dialog_->input_space);
179 fl_set_input_return(dialog_->input_space, FL_RETURN_CHANGED);
181 // Create the contents of the unit choices; don't include the "%" terms.
182 vector<string> units_vec = getLatexUnits();
183 vector<string>::iterator del =
184 remove_if(units_vec.begin(), units_vec.end(),
185 bind2nd(contains_functor(), "%"));
186 units_vec.erase(del, units_vec.end());
188 string const units = getStringFromVector(units_vec, "|");
189 fl_addto_choice(dialog_->choice_unit_space, units.c_str());
191 // set up the tooltips
192 string str = _("Additional vertical space.");
193 tooltips().init(dialog_->choice_space, str);
195 // set default unit for custom length
196 switch (lyxrc.default_papersize) {
199 case PAPER_LEGALPAPER:
200 case PAPER_EXECUTIVEPAPER:
213 void FormVSpace::apply()
219 // If a vspace choice is "Length" but there's no text in
220 // the input field, reset the choice to "None".
221 validateVSpaceWidgets(dialog_->choice_space, dialog_->input_space);
224 setVSpaceFromWidgets(dialog_->choice_space,
225 dialog_->input_space,
226 dialog_->choice_unit_space);
228 controller().params() = space;
232 void FormVSpace::update()
234 setWidgetsFromVSpace(controller().params(),
235 dialog_->choice_space,
236 dialog_->input_space,
237 dialog_->choice_unit_space);
242 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
244 // Enable input when custum length is choosen,
245 // disable 'keep' when no space is choosen
246 if (ob == dialog_->choice_space) {
247 bool const custom_length =
248 fl_get_choice(dialog_->choice_space) == 7;
250 return ButtonPolicy::SMI_VALID;