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 "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"
38 typedef FormCB<ControlParagraph, FormDB<FD_form_paragraph> > base_class;
40 FormParagraph::FormParagraph(ControlParagraph & c)
41 : base_class(c, _("Paragraph Layout"), false)
44 void FormParagraph::build()
47 dialog_.reset(build_paragraph());
49 // Allow the base class to control messages
50 setMessageWidget(dialog_->text_warning);
52 fl_addto_choice(dialog_->choice_space_above,
53 _(" None | Defskip | Smallskip "
54 "| Medskip | Bigskip | VFill | Length "));
55 fl_addto_choice(dialog_->choice_space_below,
56 _(" None | Defskip | Smallskip "
57 "| Medskip | Bigskip | VFill | Length "));
59 fl_addto_choice(dialog_->choice_linespacing,
60 _(" Default | Single | OneHalf | Double | Custom "));
62 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
63 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
64 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
65 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
66 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
68 setPrehandler(dialog_->input_space_above);
69 setPrehandler(dialog_->input_space_below);
70 setPrehandler(dialog_->input_labelwidth);
71 setPrehandler(dialog_->input_linespacing);
73 // Create the contents of the unit choices
74 // Don't include the "%" terms...
75 vector<string> units_vec = getLatexUnits();
77 for (vector<string>::iterator it = units_vec.begin();
78 it != units_vec.end(); ++it) {
79 if (contains(*it, "%"))
80 it = units_vec.erase(it, it+1) - 1;
83 // Something similar to this is a better way to erase
84 vector<string>::iterator del =
85 remove_if(units_vec.begin(), units_vec.end(),
86 bind2nd(contains_functor(), "%"));
87 units_vec.erase(del, units_vec.end());
90 string units = getStringFromVector(units_vec, "|");
92 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
93 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
95 // Manage the ok, apply, restore and cancel/close buttons
96 bc().setOK(dialog_->button_ok);
97 bc().setApply(dialog_->button_apply);
98 bc().setCancel(dialog_->button_close);
99 bc().setRestore(dialog_->button_restore);
101 bc().addReadOnly(dialog_->radio_align_right);
102 bc().addReadOnly(dialog_->radio_align_left);
103 bc().addReadOnly(dialog_->radio_align_block);
104 bc().addReadOnly(dialog_->radio_align_center);
105 bc().addReadOnly(dialog_->check_lines_top);
106 bc().addReadOnly(dialog_->check_lines_bottom);
107 bc().addReadOnly(dialog_->check_pagebreaks_top);
108 bc().addReadOnly(dialog_->check_pagebreaks_bottom);
109 bc().addReadOnly(dialog_->choice_space_above);
110 bc().addReadOnly(dialog_->input_space_above);
111 bc().addReadOnly(dialog_->check_space_above);
112 bc().addReadOnly(dialog_->choice_space_below);
113 bc().addReadOnly(dialog_->input_space_below);
114 bc().addReadOnly(dialog_->check_space_below);
115 bc().addReadOnly(dialog_->choice_linespacing);
116 bc().addReadOnly(dialog_->input_linespacing);
117 bc().addReadOnly(dialog_->check_noindent);
118 bc().addReadOnly(dialog_->input_labelwidth);
123 VSpace setVSpaceFromWidgets(FL_OBJECT * choice_type,
124 FL_OBJECT * input_length,
125 FL_OBJECT * choice_length,
126 FL_OBJECT * check_keep)
129 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
130 input_length && input_length->objclass == FL_INPUT &&
131 choice_length && choice_length->objclass == FL_CHOICE &&
132 check_keep && check_keep->objclass == FL_CHECKBUTTON);
136 switch (fl_get_choice(choice_type)) {
138 space = VSpace(VSpace::NONE);
141 space = VSpace(VSpace::DEFSKIP);
144 space = VSpace(VSpace::SMALLSKIP);
147 space = VSpace(VSpace::MEDSKIP);
150 space = VSpace(VSpace::BIGSKIP);
153 space = VSpace(VSpace::VFILL);
157 string const length =
158 getLengthFromWidgets(input_length, choice_length);
159 space = VSpace(LyXGlueLength(length));
164 if (fl_get_button(check_keep))
170 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
173 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
174 input_length && input_length->objclass == FL_INPUT);
176 if (fl_get_choice(choice_type) != 7)
179 // If a vspace kind is "Length" but there's no text in
180 // the input field, reset the kind to "None".
181 string const input = strip(getStringFromInput(input_length));
183 fl_set_choice(choice_type, 1);
188 void FormParagraph::apply()
193 // If a vspace kind is "Length" but there's no text in
194 // the input field, reset the kind to "None".
195 validateVSpaceWidgets(dialog_->choice_space_above,
196 dialog_->input_space_above);
198 VSpace const space_top =
199 setVSpaceFromWidgets(dialog_->choice_space_above,
200 dialog_->input_space_above,
201 dialog_->choice_value_space_above,
202 dialog_->check_space_above);
204 controller().params().spaceTop(space_top);
206 validateVSpaceWidgets(dialog_->choice_space_below,
207 dialog_->input_space_below);
209 VSpace const space_bottom =
210 setVSpaceFromWidgets(dialog_->choice_space_below,
211 dialog_->input_space_below,
212 dialog_->choice_value_space_below,
213 dialog_->check_space_below);
215 controller().params().spaceBottom(space_bottom);
217 /* lines and pagebreaks */
218 bool const line_top = fl_get_button(dialog_->check_lines_top);
219 controller().params().lineTop(line_top);
221 bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
222 controller().params().lineBottom(line_bottom);
224 bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
225 controller().params().pagebreakTop(pagebreak_top);
227 bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
228 controller().params().pagebreakBottom(pagebreak_bottom);
233 if (fl_get_button(dialog_->radio_align_left))
234 align = LYX_ALIGN_LEFT;
235 else if (fl_get_button(dialog_->radio_align_right))
236 align = LYX_ALIGN_RIGHT;
237 else if (fl_get_button(dialog_->radio_align_center))
238 align = LYX_ALIGN_CENTER;
240 align = LYX_ALIGN_BLOCK;
241 controller().params().align(align);
244 string const labelwidthstring =
245 getStringFromInput(dialog_->input_labelwidth);
246 controller().params().labelWidthString(labelwidthstring);
249 bool const noindent = fl_get_button(dialog_->check_noindent);
250 controller().params().noindent(noindent);
253 Spacing::Space linespacing = Spacing::Default;
255 switch (fl_get_choice(dialog_->choice_linespacing)) {
257 linespacing = Spacing::Default;
260 linespacing = Spacing::Single;
263 linespacing = Spacing::Onehalf;
266 linespacing = Spacing::Double;
269 linespacing = Spacing::Other;
270 other = getStringFromInput(dialog_->input_linespacing);
274 Spacing const spacing(linespacing, other);
275 controller().params().spacing(spacing);
281 void setWidgetsFromVSpace(VSpace const & space,
282 FL_OBJECT * choice_type,
283 FL_OBJECT * input_length,
284 FL_OBJECT * choice_length,
285 FL_OBJECT * check_keep)
288 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
289 input_length && input_length->objclass == FL_INPUT &&
290 choice_length && choice_length->objclass == FL_CHOICE &&
291 check_keep && check_keep->objclass == FL_CHECKBUTTON);
293 fl_set_input(input_length, "");
294 setEnabled(input_length, false);
295 setEnabled(choice_length, false);
297 switch (space.kind()) {
299 fl_set_choice(choice_type, 1);
301 case VSpace::DEFSKIP:
302 fl_set_choice(choice_type, 2);
304 case VSpace::SMALLSKIP:
305 fl_set_choice(choice_type, 3);
307 case VSpace::MEDSKIP:
308 fl_set_choice(choice_type, 4);
310 case VSpace::BIGSKIP:
311 fl_set_choice(choice_type, 5);
314 fl_set_choice(choice_type, 6);
318 fl_set_choice(choice_type, 7);
320 setEnabled(input_length, true);
321 setEnabled(choice_length, true);
323 bool const metric = lyxrc.default_papersize > 3;
324 string const default_unit = metric ? "cm" : "in";
325 string const length = space.length().asString();
327 updateWidgetsFromLengthString(input_length, choice_length,
328 length, default_unit);
333 fl_set_button(check_keep, space.keep());
338 void FormParagraph::update()
344 string labelwidth = controller().params().labelWidthString();
345 fl_set_input(dialog_->input_labelwidth, labelwidth.c_str());
346 setEnabled(dialog_->input_labelwidth,
347 labelwidth != _("Senseless with this layout!"));
350 fl_set_button(dialog_->radio_align_right, 0);
351 fl_set_button(dialog_->radio_align_left, 0);
352 fl_set_button(dialog_->radio_align_center, 0);
353 fl_set_button(dialog_->radio_align_block, 0);
355 LyXAlignment align = controller().params().align();
358 case LYX_ALIGN_RIGHT:
359 fl_set_button(dialog_->radio_align_right, 1);
362 fl_set_button(dialog_->radio_align_left, 1);
364 case LYX_ALIGN_CENTER:
365 fl_set_button(dialog_->radio_align_center, 1);
368 fl_set_button(dialog_->radio_align_block, 1);
372 LyXAlignment alignpos = controller().alignPossible();
374 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
375 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
376 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
377 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
379 // no inset-text-owned paragraph may have pagebreaks
380 bool ininset = controller().inInset();
381 setEnabled(dialog_->check_pagebreaks_top, !ininset);
382 setEnabled(dialog_->check_pagebreaks_bottom, !ininset);
384 /* lines, pagebreaks and indent */
385 fl_set_button(dialog_->check_lines_top,
386 controller().params().lineTop());
387 fl_set_button(dialog_->check_lines_bottom,
388 controller().params().lineBottom());
389 fl_set_button(dialog_->check_pagebreaks_top,
390 controller().params().pagebreakTop());
391 fl_set_button(dialog_->check_pagebreaks_bottom,
392 controller().params().pagebreakBottom());
393 fl_set_button(dialog_->check_noindent,
394 controller().params().noindent());
398 Spacing const space = controller().params().spacing();
400 switch (space.getSpace()) {
401 default: linespacing = 1; break;
402 case Spacing::Single: linespacing = 2; break;
403 case Spacing::Onehalf: linespacing = 3; break;
404 case Spacing::Double: linespacing = 4; break;
405 case Spacing::Other: linespacing = 5; break;
408 fl_set_choice(dialog_->choice_linespacing, linespacing);
409 if (space.getSpace() == Spacing::Other) {
410 string const sp = tostr(space.getValue());
411 fl_set_input(dialog_->input_linespacing, sp.c_str());
412 setEnabled(dialog_->input_linespacing, true);
414 fl_set_input(dialog_->input_linespacing, "");
415 setEnabled(dialog_->input_linespacing, false);
419 setWidgetsFromVSpace(controller().params().spaceTop(),
420 dialog_->choice_space_above,
421 dialog_->input_space_above,
422 dialog_->choice_value_space_above,
423 dialog_->check_space_above);
426 setWidgetsFromVSpace(controller().params().spaceBottom(),
427 dialog_->choice_space_below,
428 dialog_->input_space_below,
429 dialog_->choice_value_space_below,
430 dialog_->check_space_below);
433 fl_set_button(dialog_->check_noindent,
434 controller().params().noindent());
439 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
440 FL_OBJECT * input_length,
441 FL_OBJECT * choice_length)
444 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
445 input_length && input_length->objclass == FL_INPUT &&
446 choice_length && choice_length->objclass == FL_CHOICE);
448 if (fl_get_choice(choice_type) != 7) {
449 fl_set_input(input_length, "");
450 setEnabled(input_length, false);
451 setEnabled(choice_length, false);
454 setEnabled(input_length, true);
455 setEnabled(choice_length, true);
457 string const length = getStringFromInput(input_length);
459 if (strip(length).empty()) {
460 bool const metric = lyxrc.default_papersize > 3;
461 int const default_unit = metric ? 8 : 9;
463 fl_set_choice(choice_length, default_unit);
468 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
471 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
472 input_length && input_length->objclass == FL_INPUT);
474 if (fl_get_choice(choice_type) != 7)
477 string const input = getStringFromInput(input_length);
478 return (input.empty() ||
479 isValidGlueLength(input) ||
485 ButtonPolicy::SMInput FormParagraph::input(FL_OBJECT * ob, long)
489 // First check the buttons which are exclusive and you have to
490 // check only the actuall de/activated button.
492 // "Synchronize" the choices and input fields, making it
493 // impossible to commit senseless data.
494 if (ob == dialog_->choice_space_above) {
495 synchronizeSpaceWidgets(dialog_->choice_space_above,
496 dialog_->input_space_above,
497 dialog_->choice_value_space_above);
500 if (ob == dialog_->choice_space_below) {
501 synchronizeSpaceWidgets(dialog_->choice_space_below,
502 dialog_->input_space_below,
503 dialog_->choice_value_space_below);
506 // Display a warning if the input is senseless
507 bool valid = (validSpaceWidgets(dialog_->choice_space_above,
508 dialog_->input_space_above) &&
509 validSpaceWidgets(dialog_->choice_space_below,
510 dialog_->input_space_below));
513 postWarning(_("Invalid Length (valid example: 10mm)"));
516 int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
518 if (choice_spacing == 5)
519 setEnabled(dialog_->input_linespacing, true);
521 fl_set_input(dialog_->input_linespacing, "");
522 setEnabled(dialog_->input_linespacing, false);
525 double const spacing =
526 strToDbl(getStringFromInput(dialog_->input_linespacing));
528 if (choice_spacing == 5 && int(spacing) == 0)
531 return ButtonPolicy::SMI_VALID;