2 * \file FormParagraph.C
3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Jürgen Vigna, jug@sad.it
12 #pragma implementation
15 #include FORMS_H_LOCATION
17 #include "FormParagraph.h"
18 #include "form_paragraph.h"
19 #include "ControlParagraph.h"
20 #include "xforms_helpers.h"
21 #include "lyxrc.h" // to set the deafult length values
22 #include "input_validators.h"
23 #include "helper_funcs.h"
26 #include "layout.h" // LyXAlignment
28 #include "support/lstrings.h"
29 #include "support/LAssert.h"
37 typedef FormCB<ControlParagraph, FormDB<FD_form_paragraph> > base_class;
39 FormParagraph::FormParagraph(ControlParagraph & c)
40 : base_class(c, _("Paragraph Layout"), false)
43 void FormParagraph::build()
46 dialog_.reset(build_paragraph());
48 // Allow the base class to control messages
49 setMessageWidget(dialog_->text_warning);
51 fl_addto_choice(dialog_->choice_space_above,
52 _(" None | Defskip | Smallskip "
53 "| Medskip | Bigskip | VFill | Length "));
54 fl_addto_choice(dialog_->choice_space_below,
55 _(" None | Defskip | Smallskip "
56 "| Medskip | Bigskip | VFill | Length "));
58 fl_addto_choice(dialog_->choice_linespacing,
59 _(" Default | Single | OneHalf | Double | Custom "));
61 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
62 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
63 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
64 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
65 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
67 setPrehandler(dialog_->input_space_above);
68 setPrehandler(dialog_->input_space_below);
69 setPrehandler(dialog_->input_labelwidth);
70 setPrehandler(dialog_->input_linespacing);
72 // Create the contents of the unit choices
73 // Don't include the "%" terms...
74 vector<string> units_vec = getLatexUnits();
76 for (vector<string>::iterator it = units_vec.begin();
77 it != units_vec.end(); ++it) {
78 if (contains(*it, "%"))
79 it = units_vec.erase(it, it+1) - 1;
82 // Something similar to this is a better way to erase
83 vector<string>::iterator del =
84 remove_if(units_vec.begin(), units_vec.end(),
85 bind2nd(contains_functor(), "%"));
86 units_vec.erase(del, units_vec.end());
89 string units = getStringFromVector(units_vec, "|");
91 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
92 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
94 // Manage the ok, apply, restore and cancel/close buttons
95 bc().setOK(dialog_->button_ok);
96 bc().setApply(dialog_->button_apply);
97 bc().setCancel(dialog_->button_close);
98 bc().setRestore(dialog_->button_restore);
100 bc().addReadOnly(dialog_->radio_align_right);
101 bc().addReadOnly(dialog_->radio_align_left);
102 bc().addReadOnly(dialog_->radio_align_block);
103 bc().addReadOnly(dialog_->radio_align_center);
104 bc().addReadOnly(dialog_->check_lines_top);
105 bc().addReadOnly(dialog_->check_lines_bottom);
106 bc().addReadOnly(dialog_->check_pagebreaks_top);
107 bc().addReadOnly(dialog_->check_pagebreaks_bottom);
108 bc().addReadOnly(dialog_->choice_space_above);
109 bc().addReadOnly(dialog_->input_space_above);
110 bc().addReadOnly(dialog_->check_space_above);
111 bc().addReadOnly(dialog_->choice_space_below);
112 bc().addReadOnly(dialog_->input_space_below);
113 bc().addReadOnly(dialog_->check_space_below);
114 bc().addReadOnly(dialog_->choice_linespacing);
115 bc().addReadOnly(dialog_->input_linespacing);
116 bc().addReadOnly(dialog_->check_noindent);
117 bc().addReadOnly(dialog_->input_labelwidth);
122 VSpace setVSpaceFromWidgets(FL_OBJECT * choice_type,
123 FL_OBJECT * input_length,
124 FL_OBJECT * choice_length,
125 FL_OBJECT * check_keep)
128 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
129 input_length && input_length->objclass == FL_INPUT &&
130 choice_length && choice_length->objclass == FL_CHOICE &&
131 check_keep && check_keep->objclass == FL_CHECKBUTTON);
135 switch (fl_get_choice(choice_type)) {
137 space = VSpace(VSpace::NONE);
140 space = VSpace(VSpace::DEFSKIP);
143 space = VSpace(VSpace::SMALLSKIP);
146 space = VSpace(VSpace::MEDSKIP);
149 space = VSpace(VSpace::BIGSKIP);
152 space = VSpace(VSpace::VFILL);
156 string const length =
157 getLengthFromWidgets(input_length, choice_length);
158 space = VSpace(LyXGlueLength(length));
163 if (fl_get_button(check_keep))
169 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
172 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
173 input_length && input_length->objclass == FL_INPUT);
175 if (fl_get_choice(choice_type) != 7)
178 // If a vspace kind is "Length" but there's no text in
179 // the input field, reset the kind to "None".
180 string const input = strip(getStringFromInput(input_length));
182 fl_set_choice(choice_type, 1);
187 void FormParagraph::apply()
192 // If a vspace kind is "Length" but there's no text in
193 // the input field, reset the kind to "None".
194 validateVSpaceWidgets(dialog_->choice_space_above,
195 dialog_->input_space_above);
197 VSpace const space_top =
198 setVSpaceFromWidgets(dialog_->choice_space_above,
199 dialog_->input_space_above,
200 dialog_->choice_value_space_above,
201 dialog_->check_space_above);
203 controller().params().spaceTop(space_top);
205 validateVSpaceWidgets(dialog_->choice_space_below,
206 dialog_->input_space_below);
208 VSpace const space_bottom =
209 setVSpaceFromWidgets(dialog_->choice_space_below,
210 dialog_->input_space_below,
211 dialog_->choice_value_space_below,
212 dialog_->check_space_below);
214 controller().params().spaceBottom(space_bottom);
216 /* lines and pagebreaks */
217 bool const line_top = fl_get_button(dialog_->check_lines_top);
218 controller().params().lineTop(line_top);
220 bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
221 controller().params().lineBottom(line_bottom);
223 bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
224 controller().params().pagebreakTop(pagebreak_top);
226 bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
227 controller().params().pagebreakBottom(pagebreak_bottom);
232 if (fl_get_button(dialog_->radio_align_left))
233 align = LYX_ALIGN_LEFT;
234 else if (fl_get_button(dialog_->radio_align_right))
235 align = LYX_ALIGN_RIGHT;
236 else if (fl_get_button(dialog_->radio_align_center))
237 align = LYX_ALIGN_CENTER;
239 align = LYX_ALIGN_BLOCK;
240 controller().params().align(align);
243 string const labelwidthstring =
244 getStringFromInput(dialog_->input_labelwidth);
245 controller().params().labelWidthString(labelwidthstring);
248 bool const noindent = fl_get_button(dialog_->check_noindent);
249 controller().params().noindent(noindent);
252 Spacing::Space linespacing = Spacing::Default;
254 switch (fl_get_choice(dialog_->choice_linespacing)) {
256 linespacing = Spacing::Default;
259 linespacing = Spacing::Single;
262 linespacing = Spacing::Onehalf;
265 linespacing = Spacing::Double;
268 linespacing = Spacing::Other;
269 other = getStringFromInput(dialog_->input_linespacing);
273 Spacing const spacing(linespacing, other);
274 controller().params().spacing(spacing);
280 void setWidgetsFromVSpace(VSpace const & space,
281 FL_OBJECT * choice_type,
282 FL_OBJECT * input_length,
283 FL_OBJECT * choice_length,
284 FL_OBJECT * check_keep)
287 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
288 input_length && input_length->objclass == FL_INPUT &&
289 choice_length && choice_length->objclass == FL_CHOICE &&
290 check_keep && check_keep->objclass == FL_CHECKBUTTON);
292 fl_set_input(input_length, "");
293 setEnabled(input_length, false);
294 setEnabled(choice_length, false);
296 switch (space.kind()) {
298 fl_set_choice(choice_type, 1);
300 case VSpace::DEFSKIP:
301 fl_set_choice(choice_type, 2);
303 case VSpace::SMALLSKIP:
304 fl_set_choice(choice_type, 3);
306 case VSpace::MEDSKIP:
307 fl_set_choice(choice_type, 4);
309 case VSpace::BIGSKIP:
310 fl_set_choice(choice_type, 5);
313 fl_set_choice(choice_type, 6);
317 fl_set_choice(choice_type, 7);
319 setEnabled(input_length, true);
320 setEnabled(choice_length, true);
322 bool const metric = lyxrc.default_papersize > 3;
323 string const default_unit = metric ? "cm" : "in";
324 string const length = space.length().asString();
326 updateWidgetsFromLengthString(input_length, choice_length,
327 length, default_unit);
332 fl_set_button(check_keep, space.keep());
337 void FormParagraph::update()
343 string labelwidth = controller().params().labelWidthString();
344 fl_set_input(dialog_->input_labelwidth, labelwidth.c_str());
345 setEnabled(dialog_->input_labelwidth,
346 labelwidth != _("Senseless with this layout!"));
349 fl_set_button(dialog_->radio_align_right, 0);
350 fl_set_button(dialog_->radio_align_left, 0);
351 fl_set_button(dialog_->radio_align_center, 0);
352 fl_set_button(dialog_->radio_align_block, 0);
354 LyXAlignment align = controller().params().align();
357 case LYX_ALIGN_RIGHT:
358 fl_set_button(dialog_->radio_align_right, 1);
361 fl_set_button(dialog_->radio_align_left, 1);
363 case LYX_ALIGN_CENTER:
364 fl_set_button(dialog_->radio_align_center, 1);
367 fl_set_button(dialog_->radio_align_block, 1);
371 LyXAlignment alignpos = controller().alignPossible();
373 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
374 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
375 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
376 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
378 // no inset-text-owned paragraph may have pagebreaks
379 bool ininset = controller().inInset();
380 setEnabled(dialog_->check_pagebreaks_top, !ininset);
381 setEnabled(dialog_->check_pagebreaks_bottom, !ininset);
383 /* lines, pagebreaks and indent */
384 fl_set_button(dialog_->check_lines_top,
385 controller().params().lineTop());
386 fl_set_button(dialog_->check_lines_bottom,
387 controller().params().lineBottom());
388 fl_set_button(dialog_->check_pagebreaks_top,
389 controller().params().pagebreakTop());
390 fl_set_button(dialog_->check_pagebreaks_bottom,
391 controller().params().pagebreakBottom());
392 fl_set_button(dialog_->check_noindent,
393 controller().params().noindent());
397 Spacing const space = controller().params().spacing();
399 switch (space.getSpace()) {
400 default: linespacing = 1; break;
401 case Spacing::Single: linespacing = 2; break;
402 case Spacing::Onehalf: linespacing = 3; break;
403 case Spacing::Double: linespacing = 4; break;
404 case Spacing::Other: linespacing = 5; break;
407 fl_set_choice(dialog_->choice_linespacing, linespacing);
408 if (space.getSpace() == Spacing::Other) {
409 string const sp = tostr(space.getValue());
410 fl_set_input(dialog_->input_linespacing, sp.c_str());
411 setEnabled(dialog_->input_linespacing, true);
413 fl_set_input(dialog_->input_linespacing, "");
414 setEnabled(dialog_->input_linespacing, false);
418 setWidgetsFromVSpace(controller().params().spaceTop(),
419 dialog_->choice_space_above,
420 dialog_->input_space_above,
421 dialog_->choice_value_space_above,
422 dialog_->check_space_above);
425 setWidgetsFromVSpace(controller().params().spaceBottom(),
426 dialog_->choice_space_below,
427 dialog_->input_space_below,
428 dialog_->choice_value_space_below,
429 dialog_->check_space_below);
432 fl_set_button(dialog_->check_noindent,
433 controller().params().noindent());
438 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
439 FL_OBJECT * input_length,
440 FL_OBJECT * choice_length)
443 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
444 input_length && input_length->objclass == FL_INPUT &&
445 choice_length && choice_length->objclass == FL_CHOICE);
447 if (fl_get_choice(choice_type) != 7) {
448 fl_set_input(input_length, "");
449 setEnabled(input_length, false);
450 setEnabled(choice_length, false);
453 setEnabled(input_length, true);
454 setEnabled(choice_length, true);
456 string const length = getStringFromInput(input_length);
458 if (strip(length).empty()) {
459 bool const metric = lyxrc.default_papersize > 3;
460 int const default_unit = metric ? 8 : 9;
462 fl_set_choice(choice_length, default_unit);
467 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
470 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
471 input_length && input_length->objclass == FL_INPUT);
473 if (fl_get_choice(choice_type) != 7)
476 string const input = getStringFromInput(input_length);
477 return (input.empty() ||
478 isValidGlueLength(input) ||
484 ButtonPolicy::SMInput FormParagraph::input(FL_OBJECT * ob, long)
488 // First check the buttons which are exclusive and you have to
489 // check only the actuall de/activated button.
491 // "Synchronize" the choices and input fields, making it
492 // impossible to commit senseless data.
493 if (ob == dialog_->choice_space_above) {
494 synchronizeSpaceWidgets(dialog_->choice_space_above,
495 dialog_->input_space_above,
496 dialog_->choice_value_space_above);
499 if (ob == dialog_->choice_space_below) {
500 synchronizeSpaceWidgets(dialog_->choice_space_below,
501 dialog_->input_space_below,
502 dialog_->choice_value_space_below);
505 // Display a warning if the input is senseless
506 bool valid = (validSpaceWidgets(dialog_->choice_space_above,
507 dialog_->input_space_above) &&
508 validSpaceWidgets(dialog_->choice_space_below,
509 dialog_->input_space_below));
512 postWarning(_("Invalid Length (valid example: 10mm)"));
515 int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
517 if (choice_spacing == 5)
518 setEnabled(dialog_->input_linespacing, true);
520 fl_set_input(dialog_->input_linespacing, "");
521 setEnabled(dialog_->input_linespacing, false);
524 double const spacing =
525 strToDbl(getStringFromInput(dialog_->input_linespacing));
527 if (choice_spacing == 5 && int(spacing) == 0)
530 return ButtonPolicy::SMI_VALID;