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 "forms/form_paragraph.h"
19 #include "ControlParagraph.h"
20 #include "ParagraphParameters.h"
21 #include "xforms_helpers.h"
22 #include "lyxrc.h" // to set the deafult length values
23 #include "input_validators.h"
24 #include "helper_funcs.h"
27 #include "layout.h" // LyXAlignment
29 #include "support/lstrings.h"
30 #include "support/LAssert.h"
31 #include FORMS_H_LOCATION
39 typedef FormCB<ControlParagraph, FormDB<FD_paragraph> > base_class;
41 FormParagraph::FormParagraph(ControlParagraph & c)
42 : base_class(c, _("Paragraph Layout"), false)
45 void FormParagraph::build()
48 dialog_.reset(build_paragraph(this));
50 // Allow the base class to control messages
51 setMessageWidget(dialog_->text_warning);
53 fl_addto_choice(dialog_->choice_space_above,
54 _(" None | Defskip | Smallskip "
55 "| Medskip | Bigskip | VFill | Length "));
56 fl_addto_choice(dialog_->choice_space_below,
57 _(" None | Defskip | Smallskip "
58 "| Medskip | Bigskip | VFill | Length "));
60 fl_addto_choice(dialog_->choice_linespacing,
61 _(" Default | Single | OneHalf | Double | Custom "));
63 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
64 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
65 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
66 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
67 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
69 setPrehandler(dialog_->input_space_above);
70 setPrehandler(dialog_->input_space_below);
71 setPrehandler(dialog_->input_labelwidth);
72 setPrehandler(dialog_->input_linespacing);
74 // Create the contents of the unit choices
75 // Don't include the "%" terms...
76 vector<string> units_vec = getLatexUnits();
78 for (vector<string>::iterator it = units_vec.begin();
79 it != units_vec.end(); ++it) {
80 if (contains(*it, "%"))
81 it = units_vec.erase(it, it+1) - 1;
84 // Something similar to this is a better way to erase
85 vector<string>::iterator del =
86 remove_if(units_vec.begin(), units_vec.end(),
87 bind2nd(contains_functor(), "%"));
88 units_vec.erase(del, units_vec.end());
91 string units = getStringFromVector(units_vec, "|");
93 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
94 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
96 // Manage the ok, apply, restore and cancel/close buttons
97 bc().setOK(dialog_->button_ok);
98 bc().setApply(dialog_->button_apply);
99 bc().setCancel(dialog_->button_close);
100 bc().setRestore(dialog_->button_restore);
102 bc().addReadOnly(dialog_->radio_align_right);
103 bc().addReadOnly(dialog_->radio_align_left);
104 bc().addReadOnly(dialog_->radio_align_block);
105 bc().addReadOnly(dialog_->radio_align_center);
106 bc().addReadOnly(dialog_->check_lines_top);
107 bc().addReadOnly(dialog_->check_lines_bottom);
108 bc().addReadOnly(dialog_->check_pagebreaks_top);
109 bc().addReadOnly(dialog_->check_pagebreaks_bottom);
110 bc().addReadOnly(dialog_->choice_space_above);
111 bc().addReadOnly(dialog_->input_space_above);
112 bc().addReadOnly(dialog_->check_space_above);
113 bc().addReadOnly(dialog_->choice_space_below);
114 bc().addReadOnly(dialog_->input_space_below);
115 bc().addReadOnly(dialog_->check_space_below);
116 bc().addReadOnly(dialog_->choice_linespacing);
117 bc().addReadOnly(dialog_->input_linespacing);
118 bc().addReadOnly(dialog_->check_noindent);
119 bc().addReadOnly(dialog_->input_labelwidth);
124 VSpace setVSpaceFromWidgets(FL_OBJECT * choice_type,
125 FL_OBJECT * input_length,
126 FL_OBJECT * choice_length,
127 FL_OBJECT * check_keep)
130 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
131 input_length && input_length->objclass == FL_INPUT &&
132 choice_length && choice_length->objclass == FL_CHOICE &&
133 check_keep && check_keep->objclass == FL_CHECKBUTTON);
137 switch (fl_get_choice(choice_type)) {
139 space = VSpace(VSpace::NONE);
142 space = VSpace(VSpace::DEFSKIP);
145 space = VSpace(VSpace::SMALLSKIP);
148 space = VSpace(VSpace::MEDSKIP);
151 space = VSpace(VSpace::BIGSKIP);
154 space = VSpace(VSpace::VFILL);
158 string const length =
159 getLengthFromWidgets(input_length, choice_length);
160 space = VSpace(LyXGlueLength(length));
165 if (fl_get_button(check_keep))
171 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
174 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
175 input_length && input_length->objclass == FL_INPUT);
177 if (fl_get_choice(choice_type) != 7)
180 // If a vspace kind is "Length" but there's no text in
181 // the input field, reset the kind to "None".
182 string const input = strip(getStringFromInput(input_length));
184 fl_set_choice(choice_type, 1);
189 void FormParagraph::apply()
194 // If a vspace kind is "Length" but there's no text in
195 // the input field, reset the kind to "None".
196 validateVSpaceWidgets(dialog_->choice_space_above,
197 dialog_->input_space_above);
199 VSpace const space_top =
200 setVSpaceFromWidgets(dialog_->choice_space_above,
201 dialog_->input_space_above,
202 dialog_->choice_value_space_above,
203 dialog_->check_space_above);
205 controller().params().spaceTop(space_top);
207 validateVSpaceWidgets(dialog_->choice_space_below,
208 dialog_->input_space_below);
210 VSpace const space_bottom =
211 setVSpaceFromWidgets(dialog_->choice_space_below,
212 dialog_->input_space_below,
213 dialog_->choice_value_space_below,
214 dialog_->check_space_below);
216 controller().params().spaceBottom(space_bottom);
218 /* lines and pagebreaks */
219 bool const line_top = fl_get_button(dialog_->check_lines_top);
220 controller().params().lineTop(line_top);
222 bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
223 controller().params().lineBottom(line_bottom);
225 bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
226 controller().params().pagebreakTop(pagebreak_top);
228 bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
229 controller().params().pagebreakBottom(pagebreak_bottom);
234 if (fl_get_button(dialog_->radio_align_left))
235 align = LYX_ALIGN_LEFT;
236 else if (fl_get_button(dialog_->radio_align_right))
237 align = LYX_ALIGN_RIGHT;
238 else if (fl_get_button(dialog_->radio_align_center))
239 align = LYX_ALIGN_CENTER;
241 align = LYX_ALIGN_BLOCK;
242 controller().params().align(align);
245 string const labelwidthstring =
246 getStringFromInput(dialog_->input_labelwidth);
247 controller().params().labelWidthString(labelwidthstring);
250 bool const noindent = fl_get_button(dialog_->check_noindent);
251 controller().params().noindent(noindent);
254 Spacing::Space linespacing = Spacing::Default;
256 switch (fl_get_choice(dialog_->choice_linespacing)) {
258 linespacing = Spacing::Default;
261 linespacing = Spacing::Single;
264 linespacing = Spacing::Onehalf;
267 linespacing = Spacing::Double;
270 linespacing = Spacing::Other;
271 other = getStringFromInput(dialog_->input_linespacing);
275 Spacing const spacing(linespacing, other);
276 controller().params().spacing(spacing);
282 void setWidgetsFromVSpace(VSpace const & space,
283 FL_OBJECT * choice_type,
284 FL_OBJECT * input_length,
285 FL_OBJECT * choice_length,
286 FL_OBJECT * check_keep)
289 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
290 input_length && input_length->objclass == FL_INPUT &&
291 choice_length && choice_length->objclass == FL_CHOICE &&
292 check_keep && check_keep->objclass == FL_CHECKBUTTON);
294 fl_set_input(input_length, "");
295 setEnabled(input_length, false);
296 setEnabled(choice_length, false);
298 switch (space.kind()) {
300 fl_set_choice(choice_type, 1);
302 case VSpace::DEFSKIP:
303 fl_set_choice(choice_type, 2);
305 case VSpace::SMALLSKIP:
306 fl_set_choice(choice_type, 3);
308 case VSpace::MEDSKIP:
309 fl_set_choice(choice_type, 4);
311 case VSpace::BIGSKIP:
312 fl_set_choice(choice_type, 5);
315 fl_set_choice(choice_type, 6);
319 fl_set_choice(choice_type, 7);
321 setEnabled(input_length, true);
322 setEnabled(choice_length, true);
324 bool const metric = lyxrc.default_papersize > 3;
325 string const default_unit = metric ? "cm" : "in";
326 string const length = space.length().asString();
328 updateWidgetsFromLengthString(input_length, choice_length,
329 length, default_unit);
334 fl_set_button(check_keep, space.keep());
339 void FormParagraph::update()
345 string labelwidth = controller().params().labelWidthString();
346 fl_set_input(dialog_->input_labelwidth, labelwidth.c_str());
347 setEnabled(dialog_->input_labelwidth,
348 labelwidth != _("Senseless with this layout!"));
351 fl_set_button(dialog_->radio_align_right, 0);
352 fl_set_button(dialog_->radio_align_left, 0);
353 fl_set_button(dialog_->radio_align_center, 0);
354 fl_set_button(dialog_->radio_align_block, 0);
356 LyXAlignment align = controller().params().align();
359 case LYX_ALIGN_RIGHT:
360 fl_set_button(dialog_->radio_align_right, 1);
363 fl_set_button(dialog_->radio_align_left, 1);
365 case LYX_ALIGN_CENTER:
366 fl_set_button(dialog_->radio_align_center, 1);
369 fl_set_button(dialog_->radio_align_block, 1);
373 LyXAlignment alignpos = controller().alignPossible();
375 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
376 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
377 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
378 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
380 // no inset-text-owned paragraph may have pagebreaks
381 bool ininset = controller().inInset();
382 setEnabled(dialog_->check_pagebreaks_top, !ininset);
383 setEnabled(dialog_->check_pagebreaks_bottom, !ininset);
385 /* lines, pagebreaks and indent */
386 fl_set_button(dialog_->check_lines_top,
387 controller().params().lineTop());
388 fl_set_button(dialog_->check_lines_bottom,
389 controller().params().lineBottom());
390 fl_set_button(dialog_->check_pagebreaks_top,
391 controller().params().pagebreakTop());
392 fl_set_button(dialog_->check_pagebreaks_bottom,
393 controller().params().pagebreakBottom());
394 fl_set_button(dialog_->check_noindent,
395 controller().params().noindent());
399 Spacing const space = controller().params().spacing();
401 switch (space.getSpace()) {
402 default: linespacing = 1; break;
403 case Spacing::Single: linespacing = 2; break;
404 case Spacing::Onehalf: linespacing = 3; break;
405 case Spacing::Double: linespacing = 4; break;
406 case Spacing::Other: linespacing = 5; break;
409 fl_set_choice(dialog_->choice_linespacing, linespacing);
410 if (space.getSpace() == Spacing::Other) {
411 string const sp = tostr(space.getValue());
412 fl_set_input(dialog_->input_linespacing, sp.c_str());
413 setEnabled(dialog_->input_linespacing, true);
415 fl_set_input(dialog_->input_linespacing, "");
416 setEnabled(dialog_->input_linespacing, false);
420 setWidgetsFromVSpace(controller().params().spaceTop(),
421 dialog_->choice_space_above,
422 dialog_->input_space_above,
423 dialog_->choice_value_space_above,
424 dialog_->check_space_above);
427 setWidgetsFromVSpace(controller().params().spaceBottom(),
428 dialog_->choice_space_below,
429 dialog_->input_space_below,
430 dialog_->choice_value_space_below,
431 dialog_->check_space_below);
434 fl_set_button(dialog_->check_noindent,
435 controller().params().noindent());
440 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
441 FL_OBJECT * input_length,
442 FL_OBJECT * choice_length)
445 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
446 input_length && input_length->objclass == FL_INPUT &&
447 choice_length && choice_length->objclass == FL_CHOICE);
449 if (fl_get_choice(choice_type) != 7) {
450 fl_set_input(input_length, "");
451 setEnabled(input_length, false);
452 setEnabled(choice_length, false);
455 setEnabled(input_length, true);
456 setEnabled(choice_length, true);
458 string const length = getStringFromInput(input_length);
460 if (strip(length).empty()) {
461 bool const metric = lyxrc.default_papersize > 3;
462 int const default_unit = metric ? 8 : 9;
464 fl_set_choice(choice_length, default_unit);
469 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
472 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
473 input_length && input_length->objclass == FL_INPUT);
475 if (fl_get_choice(choice_type) != 7)
478 string const input = getStringFromInput(input_length);
479 return (input.empty() ||
480 isValidGlueLength(input) ||
486 ButtonPolicy::SMInput FormParagraph::input(FL_OBJECT * ob, long)
490 // First check the buttons which are exclusive and you have to
491 // check only the actuall de/activated button.
493 // "Synchronize" the choices and input fields, making it
494 // impossible to commit senseless data.
495 if (ob == dialog_->choice_space_above) {
496 synchronizeSpaceWidgets(dialog_->choice_space_above,
497 dialog_->input_space_above,
498 dialog_->choice_value_space_above);
501 if (ob == dialog_->choice_space_below) {
502 synchronizeSpaceWidgets(dialog_->choice_space_below,
503 dialog_->input_space_below,
504 dialog_->choice_value_space_below);
507 // Display a warning if the input is senseless
508 bool valid = (validSpaceWidgets(dialog_->choice_space_above,
509 dialog_->input_space_above) &&
510 validSpaceWidgets(dialog_->choice_space_below,
511 dialog_->input_space_below));
514 postWarning(_("Invalid Length (valid example: 10mm)"));
517 int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
519 if (choice_spacing == 5)
520 setEnabled(dialog_->input_linespacing, true);
522 fl_set_input(dialog_->input_linespacing, "");
523 setEnabled(dialog_->input_linespacing, false);
526 double const spacing =
527 strToDbl(getStringFromInput(dialog_->input_linespacing));
529 if (choice_spacing == 5 && int(spacing) == 0)
532 return ButtonPolicy::SMI_VALID;