]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormVSpace.C
Introduce LFUN_PRINT.
[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 #include <boost/bind.hpp>
37
38 using lyx::support::contains;
39 using lyx::support::getStringFromVector;
40 using lyx::support::rtrim;
41
42 using boost::bind;
43
44 using std::remove_if;
45
46 using std::vector;
47 using std::string;
48
49
50 namespace {
51
52 string defaultUnit("cm");
53
54 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
55 {
56         // Paranoia check!
57         BOOST_ASSERT(choice_type  && choice_type->objclass  == FL_CHOICE &&
58                      input_length && input_length->objclass == FL_INPUT);
59
60         if (fl_get_choice(choice_type) != 6)
61                 return;
62
63         // If a vspace kind is "Length" but there's no text in
64         // the input field, insert nothing.
65         string const input = rtrim(getString(input_length));
66         if (input.empty())
67                 return;
68 }
69
70
71 VSpace const setVSpaceFromWidgets(FL_OBJECT * choice_type,
72                                   FL_OBJECT * input_length,
73                                   FL_OBJECT * choice_length,
74                                   bool keep)
75 {
76         // Paranoia check!
77         BOOST_ASSERT(choice_type   && choice_type->objclass   == FL_CHOICE &&
78                      input_length  && input_length->objclass  == FL_INPUT &&
79                      choice_length && choice_length->objclass == FL_CHOICE);
80
81         VSpace space;
82
83         switch (fl_get_choice(choice_type)) {
84         case 1:
85                 space = VSpace(VSpace::DEFSKIP);
86                 break;
87         case 2:
88                 space = VSpace(VSpace::SMALLSKIP);
89                 break;
90         case 3:
91                 space = VSpace(VSpace::MEDSKIP);
92                 break;
93         case 4:
94                 space = VSpace(VSpace::BIGSKIP);
95                 break;
96         case 5:
97                 space = VSpace(VSpace::VFILL);
98                 break;
99         case 6:
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::DEFSKIP:
125                 pos = 1;
126                 break;
127         case VSpace::SMALLSKIP:
128                 pos = 2;
129                 break;
130         case VSpace::MEDSKIP:
131                 pos = 3;
132                 break;
133         case VSpace::BIGSKIP:
134                 pos = 4;
135                 break;
136         case VSpace::VFILL:
137                 pos = 5;
138                 break;
139         case VSpace::LENGTH:
140                 pos = 6;
141                 break;
142         }
143         fl_set_choice(choice_type, pos);
144         fl_set_button(check_keep, space.keep());
145
146         bool const custom_vspace = space.kind() == VSpace::LENGTH;
147         if (custom_vspace) {
148                 string const length = space.length().asString();
149                 updateWidgetsFromLengthString(input_length, choice_length,
150                                               length, defaultUnit);
151         } else {
152                 fl_set_input(input_length, "");
153                 fl_set_choice_text(choice_length, defaultUnit.c_str());
154         }
155 }
156
157 } // namespace anon
158
159
160 typedef FormController<ControlVSpace, FormView<FD_vspace> > base_class;
161
162 FormVSpace::FormVSpace(Dialog & parent)
163         : base_class(parent, _("VSpace Settings"))
164 {}
165
166
167 void FormVSpace::build()
168 {
169         // the tabbed folder
170         dialog_.reset(build_vspace(this));
171
172         // Manage the ok, apply, restore and cancel/close buttons
173         bcview().setOK(dialog_->button_ok);
174         bcview().setApply(dialog_->button_apply);
175         bcview().setCancel(dialog_->button_close);
176         bcview().setRestore(dialog_->button_restore);
177
178         // disable for read-only documents
179         bcview().addReadOnly(dialog_->choice_space);
180         bcview().addReadOnly(dialog_->input_space);
181         bcview().addReadOnly(dialog_->choice_unit_space);
182
183         // check validity of "length + unit" input.
184         // If invalid, the label of input_space is displayed in red.
185         addCheckedGlueLength(bcview(),
186                              dialog_->input_space,
187                              dialog_->input_space);
188
189         // trigger an input event for cut&paste with middle mouse button.
190         setPrehandler(dialog_->input_space);
191
192         fl_set_input_return(dialog_->input_space, FL_RETURN_CHANGED);
193
194         string const spacing =
195                 _("DefSkip|SmallSkip|MedSkip|BigSkip|VFill|Length");
196         fl_addto_choice(dialog_->choice_space, spacing.c_str());
197
198         // Create the contents of the unit choices; don't include the "%" terms.
199         vector<string> units_vec = getLatexUnits();
200         vector<string>::iterator del =
201                 remove_if(units_vec.begin(), units_vec.end(),
202                           bind(contains<char>, _1, '%'));
203         units_vec.erase(del, units_vec.end());
204
205         string const units = getStringFromVector(units_vec, "|");
206         fl_addto_choice(dialog_->choice_unit_space, units.c_str());
207
208         // set up the tooltips
209         string str = _("Additional vertical space.");
210         tooltips().init(dialog_->choice_space, str);
211
212         // set default unit for custom length
213         switch (lyxrc.default_papersize) {
214         case PAPER_DEFAULT:
215         case PAPER_USLETTER:
216         case PAPER_LEGALPAPER:
217         case PAPER_EXECUTIVEPAPER:
218                 defaultUnit = "in";
219                 break;
220         case PAPER_A3PAPER:
221         case PAPER_A4PAPER:
222         case PAPER_A5PAPER:
223         case PAPER_B5PAPER:
224                 defaultUnit = "cm";
225                 break;
226         }
227 }
228
229
230 void FormVSpace::apply()
231 {
232         // If a vspace choice is "Length" but there's no text in
233         // the input field, insert nothing.
234         validateVSpaceWidgets(dialog_->choice_space, dialog_->input_space);
235
236         VSpace const space =
237                 setVSpaceFromWidgets(dialog_->choice_space,
238                                      dialog_->input_space,
239                                      dialog_->choice_unit_space,
240                                      fl_get_button(dialog_->check_keep));
241
242         controller().params() = space;
243 }
244
245
246 void FormVSpace::update()
247 {
248         setWidgetsFromVSpace(controller().params(),
249                              dialog_->choice_space,
250                              dialog_->input_space,
251                              dialog_->choice_unit_space,
252                              dialog_->check_keep);
253
254         bool const custom_length =
255                 fl_get_choice(dialog_->choice_space) == 6;
256         setEnabled(dialog_->input_space, custom_length);
257         setEnabled(dialog_->choice_unit_space, custom_length);
258 }
259
260
261 ButtonPolicy::SMInput FormVSpace::input(FL_OBJECT * ob, long)
262 {
263         // Enable input when custom length is chosen,
264         // disable 'keep' when no space is chosen
265         if (ob == dialog_->choice_space) {
266                 bool const custom_length =
267                         fl_get_choice(dialog_->choice_space) == 6;
268                 setEnabled(dialog_->input_space, custom_length);
269                 setEnabled(dialog_->choice_unit_space, custom_length);
270         }
271         return ButtonPolicy::SMI_VALID;
272 }