]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormVSpace.C
648ed57c8843f438b17a1bb3286f136f94ff8b2d
[lyx.git] / src / frontends / xforms / FormVSpace.C
1 /**
2  * \file FormVSpace.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Jürgen Vigna
8  * \author Rob Lahaye
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "FormVSpace.h"
16 #include "ControlVSpace.h"
17 #include "forms/form_vspace.h"
18
19 #include "checkedwidgets.h"
20 #include "input_validators.h"
21 #include "Tooltips.h"
22 #include "xforms_helpers.h"
23 #include "xformsBC.h"
24
25 #include "controllers/helper_funcs.h"
26
27 #include "lyxrc.h" // to set the deafult length values
28 #include "Spacing.h"
29 #include "vspace.h"
30
31 #include "support/lstrings.h"
32 #include "support/tostr.h"
33
34 #include "lyx_forms.h"
35
36 using lyx::support::contains_functor;
37 using lyx::support::getStringFromVector;
38 using lyx::support::rtrim;
39
40 using std::bind2nd;
41 using std::remove_if;
42
43 using std::vector;
44 using std::string;
45
46
47 namespace {
48
49 string defaultUnit("cm");
50
51 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
52 {
53         // Paranoia check!
54         BOOST_ASSERT(choice_type  && choice_type->objclass  == FL_CHOICE &&
55                      input_length && input_length->objclass == FL_INPUT);
56
57         if (fl_get_choice(choice_type) != 7)
58                 return;
59
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));
63         if (input.empty())
64                 fl_set_choice(choice_type, 1);
65 }
66
67
68 VSpace const setVSpaceFromWidgets(FL_OBJECT * choice_type,
69                                   FL_OBJECT * input_length,
70                                   FL_OBJECT * choice_length,
71                                   bool keep)
72 {
73         // Paranoia check!
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);
77
78         VSpace space = VSpace(VSpace::NONE);
79
80         switch (fl_get_choice(choice_type)) {
81         case 1:
82                 space = VSpace(VSpace::NONE);
83                 break;
84         case 2:
85                 space = VSpace(VSpace::DEFSKIP);
86                 break;
87         case 3:
88                 space = VSpace(VSpace::SMALLSKIP);
89                 break;
90         case 4:
91                 space = VSpace(VSpace::MEDSKIP);
92                 break;
93         case 5:
94                 space = VSpace(VSpace::BIGSKIP);
95                 break;
96         case 6:
97                 space = VSpace(VSpace::VFILL);
98                 break;
99         case 7:
100                 space = VSpace(LyXGlueLength(
101                         getLengthFromWidgets(input_length, choice_length)));
102                 break;
103         }
104
105         space.setKeep(keep);
106         return space;
107 }
108
109
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)
115 {
116         // Paranoia check!
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);
121
122         int pos = 1;
123         switch (space.kind()) {
124         case VSpace::NONE:
125                 pos = 1;
126                 break;
127         case VSpace::DEFSKIP:
128                 pos = 2;
129                 break;
130         case VSpace::SMALLSKIP:
131                 pos = 3;
132                 break;
133         case VSpace::MEDSKIP:
134                 pos = 4;
135                 break;
136         case VSpace::BIGSKIP:
137                 pos = 5;
138                 break;
139         case VSpace::VFILL:
140                 pos = 6;
141                 break;
142         case VSpace::LENGTH:
143                 pos = 7;
144                 break;
145         }
146         fl_set_choice(choice_type, pos);
147         fl_set_button(check_keep, space.keep());
148
149         bool const custom_vspace = space.kind() == VSpace::LENGTH;
150         if (custom_vspace) {
151                 string const length = space.length().asString();
152                 updateWidgetsFromLengthString(input_length, choice_length,
153                                               length, defaultUnit);
154         } else {
155                 fl_set_input(input_length, "");
156                 fl_set_choice_text(choice_length, defaultUnit.c_str());
157         }
158 }
159
160 } // namespace anon
161
162
163 typedef FormController<ControlVSpace, FormView<FD_vspace> > base_class;
164
165 FormVSpace::FormVSpace(Dialog & parent)
166         : base_class(parent, _("VSpace Settings"))
167 {}
168
169
170 void FormVSpace::build()
171 {
172         // the tabbed folder
173         dialog_.reset(build_vspace(this));
174
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);
180
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);
185
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);
191
192         // trigger an input event for cut&paste with middle mouse button.
193         setPrehandler(dialog_->input_space);
194
195         fl_set_input_return(dialog_->input_space, FL_RETURN_CHANGED);
196
197         string const spacing =
198                 _("None|DefSkip|SmallSkip|MedSkip|BigSkip|VFill|Length");
199         fl_addto_choice(dialog_->choice_space, spacing.c_str());
200
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());
207
208         string const units = getStringFromVector(units_vec, "|");
209         fl_addto_choice(dialog_->choice_unit_space, units.c_str());
210
211         // set up the tooltips
212         string str = _("Additional vertical space.");
213         tooltips().init(dialog_->choice_space, str);
214
215         // set default unit for custom length
216         switch (lyxrc.default_papersize) {
217         case PAPER_DEFAULT:
218         case PAPER_USLETTER:
219         case PAPER_LEGALPAPER:
220         case PAPER_EXECUTIVEPAPER:
221                 defaultUnit = "in";
222                 break;
223         case PAPER_A3PAPER:
224         case PAPER_A4PAPER:
225         case PAPER_A5PAPER:
226         case PAPER_B5PAPER:
227                 defaultUnit = "cm";
228                 break;
229         }
230 }
231
232
233 void FormVSpace::apply()
234 {
235         if (!form())
236                 return;
237
238         // spacing
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);
242
243         VSpace const space =
244                 setVSpaceFromWidgets(dialog_->choice_space,
245                                      dialog_->input_space,
246                                      dialog_->choice_unit_space,
247                                      fl_get_button(dialog_->check_keep));
248
249         controller().params() = space;
250 }
251
252
253 void FormVSpace::update()
254 {
255         setWidgetsFromVSpace(controller().params(),
256                              dialog_->choice_space,
257                              dialog_->input_space,
258                              dialog_->choice_unit_space,
259                              dialog_->check_keep);
260
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);
265 }
266
267
268 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
269 {
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);
277         }
278         return ButtonPolicy::SMI_VALID;
279 }