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"
24 #include "xforms_helpers.h"
25 #include "lyxrc.h" // to set the deafult length values
26 #include "BufferView.h"
27 #include "lyxtextclasslist.h"
29 #include "ParagraphParameters.h"
30 #include "input_validators.h"
31 #include "helper_funcs.h"
33 #include "support/lstrings.h"
34 #include "support/LAssert.h"
38 using Liason::setMinibuffer;
45 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
46 : FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
48 // let the dialog be shown
49 // This is a permanent connection so we won't bother
50 // storing a copy because we won't be disconnecting.
51 d->showParagraph.connect(slot(this, &FormParagraph::show));
55 void FormParagraph::connect()
57 cp_ = d_->updateParagraph
58 .connect(slot(this, &FormParagraph::changedParagraph));
59 FormBaseBD::connect();
63 void FormParagraph::disconnect()
66 FormBaseBD::disconnect();
70 Paragraph const * FormParagraph::getCurrentParagraph() const
72 return lv_->view()->getLyXText()->cursor.par();
76 void FormParagraph::changedParagraph()
78 /// Record the paragraph
79 Paragraph const * const p = getCurrentParagraph();
80 if (p == 0 || p == par_)
85 // shouldn't we chage the par_ pointer too?
86 // anyway for me the below function does just nothing!
89 // For now don't bother checking if the params are different,
90 // just activate the Apply button
95 void FormParagraph::redraw()
97 if (form() && form()->visible)
98 fl_redraw_form(form());
102 FL_FORM * FormParagraph::form() const
105 return dialog_->form;
110 void FormParagraph::build()
113 dialog_.reset(build_paragraph());
115 // Allow the base class to control messages
116 setMessageWidget(dialog_->text_warning);
118 fl_addto_choice(dialog_->choice_space_above,
119 _(" None | Defskip | Smallskip "
120 "| Medskip | Bigskip | VFill | Length "));
121 fl_addto_choice(dialog_->choice_space_below,
122 _(" None | Defskip | Smallskip "
123 "| Medskip | Bigskip | VFill | Length "));
125 fl_addto_choice(dialog_->choice_linespacing,
126 _(" Default | Single | OneHalf | Double | Other "));
128 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
129 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
130 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
131 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
132 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
134 setPrehandler(dialog_->input_space_above);
135 setPrehandler(dialog_->input_space_below);
136 setPrehandler(dialog_->input_labelwidth);
137 setPrehandler(dialog_->input_linespacing);
139 // Create the contents of the unit choices
140 // Don't include the "%" terms...
141 vector<string> units_vec = getLatexUnits();
143 for (vector<string>::iterator it = units_vec.begin();
144 it != units_vec.end(); ++it) {
145 if (contains(*it, "%"))
146 it = units_vec.erase(it, it+1) - 1;
149 // Something similar to this is a better way to erase
150 vector<string>::iterator del =
151 remove_if(units_vec.begin(), units_vec.end(),
152 bind2nd(contains_functor(), "%"));
153 units_vec.erase(del, units_vec.end());
156 string units = getStringFromVector(units_vec, "|");
158 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
159 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
161 // Manage the ok, apply, restore and cancel/close buttons
162 bc_.setOK(dialog_->button_ok);
163 bc_.setApply(dialog_->button_apply);
164 bc_.setCancel(dialog_->button_close);
165 bc_.setRestore(dialog_->button_restore);
167 bc_.addReadOnly(dialog_->radio_align_right);
168 bc_.addReadOnly(dialog_->radio_align_left);
169 bc_.addReadOnly(dialog_->radio_align_block);
170 bc_.addReadOnly(dialog_->radio_align_center);
171 bc_.addReadOnly(dialog_->check_lines_top);
172 bc_.addReadOnly(dialog_->check_lines_bottom);
173 bc_.addReadOnly(dialog_->check_pagebreaks_top);
174 bc_.addReadOnly(dialog_->check_pagebreaks_bottom);
175 bc_.addReadOnly(dialog_->choice_space_above);
176 bc_.addReadOnly(dialog_->input_space_above);
177 bc_.addReadOnly(dialog_->check_space_above);
178 bc_.addReadOnly(dialog_->choice_space_below);
179 bc_.addReadOnly(dialog_->input_space_below);
180 bc_.addReadOnly(dialog_->check_space_below);
181 bc_.addReadOnly(dialog_->choice_linespacing);
182 bc_.addReadOnly(dialog_->input_linespacing);
183 bc_.addReadOnly(dialog_->check_noindent);
184 bc_.addReadOnly(dialog_->input_labelwidth);
190 VSpace setVSpaceFromWidgets(FL_OBJECT * choice_type,
191 FL_OBJECT * input_length,
192 FL_OBJECT * choice_length,
193 FL_OBJECT * check_keep)
196 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
197 input_length && input_length->objclass == FL_INPUT &&
198 choice_length && choice_length->objclass == FL_CHOICE &&
199 check_keep && check_keep->objclass == FL_CHECKBUTTON);
203 switch (fl_get_choice(choice_type)) {
205 space = VSpace(VSpace::NONE);
208 space = VSpace(VSpace::DEFSKIP);
211 space = VSpace(VSpace::SMALLSKIP);
214 space = VSpace(VSpace::MEDSKIP);
217 space = VSpace(VSpace::BIGSKIP);
220 space = VSpace(VSpace::VFILL);
224 string const length =
225 getLengthFromWidgets(input_length, choice_length);
226 space = VSpace(LyXGlueLength(length));
231 if (fl_get_button(check_keep))
238 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
241 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
242 input_length && input_length->objclass == FL_INPUT);
244 if (fl_get_choice(choice_type) != 7)
247 // If a vspace kind is "Length" but there's no text in
248 // the input field, reset the kind to "None".
249 string const input = strip(getStringFromInput(input_length));
251 fl_set_choice(choice_type, 1);
257 void FormParagraph::apply()
259 if (!lv_->view()->available() || !dialog_.get())
262 // If a vspace kind is "Length" but there's no text in
263 // the input field, reset the kind to "None".
264 validateVSpaceWidgets(dialog_->choice_space_above,
265 dialog_->input_space_above);
266 validateVSpaceWidgets(dialog_->choice_space_below,
267 dialog_->input_space_below);
269 bool const line_top = fl_get_button(dialog_->check_lines_top);
270 bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
271 bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
272 bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
274 VSpace const space_top =
275 setVSpaceFromWidgets(dialog_->choice_space_above,
276 dialog_->input_space_above,
277 dialog_->choice_value_space_above,
278 dialog_->check_space_above);
280 VSpace const space_bottom =
281 setVSpaceFromWidgets(dialog_->choice_space_below,
282 dialog_->input_space_below,
283 dialog_->choice_value_space_below,
284 dialog_->check_space_below);
287 if (fl_get_button(dialog_->radio_align_left))
288 align = LYX_ALIGN_LEFT;
289 else if (fl_get_button(dialog_->radio_align_right))
290 align = LYX_ALIGN_RIGHT;
291 else if (fl_get_button(dialog_->radio_align_center))
292 align = LYX_ALIGN_CENTER;
294 align = LYX_ALIGN_BLOCK;
296 string const labelwidthstring =
297 getStringFromInput(dialog_->input_labelwidth);
299 bool const noindent = fl_get_button(dialog_->check_noindent);
301 Spacing::Space linespacing = Spacing::Default;
303 switch (fl_get_choice(dialog_->choice_linespacing)) {
305 linespacing = Spacing::Default;
308 linespacing = Spacing::Single;
311 linespacing = Spacing::Onehalf;
314 linespacing = Spacing::Double;
317 linespacing = Spacing::Other;
318 other = getStringFromInput(dialog_->input_linespacing);
322 Spacing const spacing(linespacing, other);
324 LyXText * text(lv_->view()->getLyXText());
325 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
326 pagebreak_bottom, space_top, space_bottom, spacing,
327 align, labelwidthstring, noindent);
329 // Actually apply these settings
330 lv_->view()->update(text,
331 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
332 lv_->buffer()->markDirty();
333 setMinibuffer(lv_, _("Paragraph layout set"));
339 void setWidgetsFromVSpace(VSpace const & space,
340 FL_OBJECT * choice_type,
341 FL_OBJECT * input_length,
342 FL_OBJECT * choice_length,
343 FL_OBJECT * check_keep)
346 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
347 input_length && input_length->objclass == FL_INPUT &&
348 choice_length && choice_length->objclass == FL_CHOICE &&
349 check_keep && check_keep->objclass == FL_CHECKBUTTON);
351 fl_set_input(input_length, "");
352 setEnabled(input_length, false);
353 setEnabled(choice_length, false);
355 switch (space.kind()) {
357 fl_set_choice(choice_type, 1);
359 case VSpace::DEFSKIP:
360 fl_set_choice(choice_type, 2);
362 case VSpace::SMALLSKIP:
363 fl_set_choice(choice_type, 3);
365 case VSpace::MEDSKIP:
366 fl_set_choice(choice_type, 4);
368 case VSpace::BIGSKIP:
369 fl_set_choice(choice_type, 5);
372 fl_set_choice(choice_type, 6);
376 fl_set_choice(choice_type, 7);
378 setEnabled(input_length, true);
379 setEnabled(choice_length, true);
381 bool const metric = lyxrc.default_papersize > 3;
382 string const default_unit = metric ? "cm" : "in";
383 string const length = space.length().asString();
385 updateWidgetsFromLengthString(input_length, choice_length,
386 length, default_unit);
391 fl_set_button(check_keep, space.keep());
397 void FormParagraph::update()
402 // Do this first; some objects may be de/activated subsequently.
403 bc_.readOnly(lv_->buffer()->isReadonly());
405 /// Record the paragraph
406 par_ = getCurrentParagraph();
408 fl_set_input(dialog_->input_labelwidth,
409 par_->getLabelWidthString().c_str());
410 setEnabled(dialog_->input_labelwidth,
411 (par_->getLabelWidthString() != _("Senseless with this layout!")));
413 fl_set_button(dialog_->radio_align_right, 0);
414 fl_set_button(dialog_->radio_align_left, 0);
415 fl_set_button(dialog_->radio_align_center, 0);
416 fl_set_button(dialog_->radio_align_block, 0);
418 LyXTextClass const & tclass =
419 textclasslist[lv_->view()->buffer()->params.textclass];
421 int align = par_->getAlign();
422 if (align == LYX_ALIGN_LAYOUT)
423 align = tclass[par_->layout()].align;
426 case LYX_ALIGN_RIGHT:
427 fl_set_button(dialog_->radio_align_right, 1);
430 fl_set_button(dialog_->radio_align_left, 1);
432 case LYX_ALIGN_CENTER:
433 fl_set_button(dialog_->radio_align_center, 1);
436 fl_set_button(dialog_->radio_align_block, 1);
440 LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
442 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
443 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
444 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
445 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
447 // no inset-text-owned paragraph may have pagebreaks
448 setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
449 setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
451 fl_set_button(dialog_->check_lines_top,
452 par_->params().lineTop());
453 fl_set_button(dialog_->check_lines_bottom,
454 par_->params().lineBottom());
455 fl_set_button(dialog_->check_pagebreaks_top,
456 par_->params().pagebreakTop());
457 fl_set_button(dialog_->check_pagebreaks_bottom,
458 par_->params().pagebreakBottom());
459 fl_set_button(dialog_->check_noindent,
460 par_->params().noindent());
463 Spacing const space = par_->params().spacing();
465 switch (space.getSpace()) {
466 default: linespacing = 1; break;
467 case Spacing::Single: linespacing = 2; break;
468 case Spacing::Onehalf: linespacing = 3; break;
469 case Spacing::Double: linespacing = 4; break;
470 case Spacing::Other: linespacing = 5; break;
473 fl_set_choice(dialog_->choice_linespacing, linespacing);
474 if (space.getSpace() == Spacing::Other) {
475 string const sp = tostr(space.getValue());
476 fl_set_input(dialog_->input_linespacing, sp.c_str());
477 setEnabled(dialog_->input_linespacing, true);
479 fl_set_input(dialog_->input_linespacing, "");
480 setEnabled(dialog_->input_linespacing, false);
483 setWidgetsFromVSpace(par_->params().spaceTop(),
484 dialog_->choice_space_above,
485 dialog_->input_space_above,
486 dialog_->choice_value_space_above,
487 dialog_->check_space_above);
489 setWidgetsFromVSpace(par_->params().spaceBottom(),
490 dialog_->choice_space_below,
491 dialog_->input_space_below,
492 dialog_->choice_value_space_below,
493 dialog_->check_space_below);
495 fl_set_button(dialog_->check_noindent,
496 par_->params().noindent());
502 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
503 FL_OBJECT * input_length,
504 FL_OBJECT * choice_length,
508 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
509 input_length && input_length->objclass == FL_INPUT &&
510 choice_length && choice_length->objclass == FL_CHOICE);
512 if (fl_get_choice(choice_type) != 7) {
513 fl_set_input(input_length, "");
514 setEnabled(input_length, false);
515 setEnabled(choice_length, false);
518 setEnabled(input_length, !readonly);
519 setEnabled(choice_length, !readonly);
521 string const length = getStringFromInput(input_length);
523 if (strip(length).empty()) {
524 bool const metric = lyxrc.default_papersize > 3;
525 int const default_unit = metric ? 8 : 9;
527 fl_set_choice(choice_length, default_unit);
532 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
535 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
536 input_length && input_length->objclass == FL_INPUT);
538 if (fl_get_choice(choice_type) != 7)
541 string const input = getStringFromInput(input_length);
542 return (input.empty() ||
543 isValidGlueLength(input) ||
550 bool FormParagraph::input(FL_OBJECT * ob, long)
554 // First check the buttons which are exclusive and you have to
555 // check only the actuall de/activated button.
557 // "Synchronize" the choices and input fields, making it
558 // impossible to commit senseless data.
560 if (ob == dialog_->choice_space_above) {
561 synchronizeSpaceWidgets(dialog_->choice_space_above,
562 dialog_->input_space_above,
563 dialog_->choice_value_space_above,
564 lv_->buffer()->isReadonly());
567 if (ob == dialog_->choice_space_below) {
568 synchronizeSpaceWidgets(dialog_->choice_space_below,
569 dialog_->input_space_below,
570 dialog_->choice_value_space_below,
571 lv_->buffer()->isReadonly());
574 // Display a warning if the input is senseless
575 bool valid = (validSpaceWidgets(dialog_->choice_space_above,
576 dialog_->input_space_above) &&
577 validSpaceWidgets(dialog_->choice_space_below,
578 dialog_->input_space_below));
581 postWarning(_("Invalid Length (valid example: 10mm)"));
584 int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
586 if (choice_spacing == 5)
587 setEnabled(dialog_->input_linespacing, true);
589 fl_set_input(dialog_->input_linespacing, "");
590 setEnabled(dialog_->input_linespacing, false);
593 double const spacing =
594 strToDbl(getStringFromInput(dialog_->input_linespacing));
596 if (choice_spacing == 5 && int(spacing) == 0)