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_)
83 // For now, don't bother checking if the params are different.
85 // Will the underlying paragraph accept our changes?
86 Inset * const inset = p->inInset();
87 bool const accept = !(inset && inset->forceDefaultParagraphs(inset));
91 postWarning(_("Cannot apply paragraph settings to this inset!"));
96 void FormParagraph::redraw()
98 if (form() && form()->visible)
99 fl_redraw_form(form());
103 FL_FORM * FormParagraph::form() const
106 return dialog_->form;
111 void FormParagraph::build()
114 dialog_.reset(build_paragraph());
116 // Allow the base class to control messages
117 setMessageWidget(dialog_->text_warning);
119 fl_addto_choice(dialog_->choice_space_above,
120 _(" None | Defskip | Smallskip "
121 "| Medskip | Bigskip | VFill | Length "));
122 fl_addto_choice(dialog_->choice_space_below,
123 _(" None | Defskip | Smallskip "
124 "| Medskip | Bigskip | VFill | Length "));
126 fl_addto_choice(dialog_->choice_linespacing,
127 _(" Default | Single | OneHalf | Double | Other "));
129 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
130 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
131 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
132 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
133 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
135 setPrehandler(dialog_->input_space_above);
136 setPrehandler(dialog_->input_space_below);
137 setPrehandler(dialog_->input_labelwidth);
138 setPrehandler(dialog_->input_linespacing);
140 // Create the contents of the unit choices
141 // Don't include the "%" terms...
142 vector<string> units_vec = getLatexUnits();
144 for (vector<string>::iterator it = units_vec.begin();
145 it != units_vec.end(); ++it) {
146 if (contains(*it, "%"))
147 it = units_vec.erase(it, it+1) - 1;
150 // Something similar to this is a better way to erase
151 vector<string>::iterator del =
152 remove_if(units_vec.begin(), units_vec.end(),
153 bind2nd(contains_functor(), "%"));
154 units_vec.erase(del, units_vec.end());
157 string units = getStringFromVector(units_vec, "|");
159 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
160 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
162 // Manage the ok, apply, restore and cancel/close buttons
163 bc_.setOK(dialog_->button_ok);
164 bc_.setApply(dialog_->button_apply);
165 bc_.setCancel(dialog_->button_close);
166 bc_.setRestore(dialog_->button_restore);
168 bc_.addReadOnly(dialog_->radio_align_right);
169 bc_.addReadOnly(dialog_->radio_align_left);
170 bc_.addReadOnly(dialog_->radio_align_block);
171 bc_.addReadOnly(dialog_->radio_align_center);
172 bc_.addReadOnly(dialog_->check_lines_top);
173 bc_.addReadOnly(dialog_->check_lines_bottom);
174 bc_.addReadOnly(dialog_->check_pagebreaks_top);
175 bc_.addReadOnly(dialog_->check_pagebreaks_bottom);
176 bc_.addReadOnly(dialog_->choice_space_above);
177 bc_.addReadOnly(dialog_->input_space_above);
178 bc_.addReadOnly(dialog_->check_space_above);
179 bc_.addReadOnly(dialog_->choice_space_below);
180 bc_.addReadOnly(dialog_->input_space_below);
181 bc_.addReadOnly(dialog_->check_space_below);
182 bc_.addReadOnly(dialog_->choice_linespacing);
183 bc_.addReadOnly(dialog_->input_linespacing);
184 bc_.addReadOnly(dialog_->check_noindent);
185 bc_.addReadOnly(dialog_->input_labelwidth);
191 VSpace setVSpaceFromWidgets(FL_OBJECT * choice_type,
192 FL_OBJECT * input_length,
193 FL_OBJECT * choice_length,
194 FL_OBJECT * check_keep)
197 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
198 input_length && input_length->objclass == FL_INPUT &&
199 choice_length && choice_length->objclass == FL_CHOICE &&
200 check_keep && check_keep->objclass == FL_CHECKBUTTON);
204 switch (fl_get_choice(choice_type)) {
206 space = VSpace(VSpace::NONE);
209 space = VSpace(VSpace::DEFSKIP);
212 space = VSpace(VSpace::SMALLSKIP);
215 space = VSpace(VSpace::MEDSKIP);
218 space = VSpace(VSpace::BIGSKIP);
221 space = VSpace(VSpace::VFILL);
225 string const length =
226 getLengthFromWidgets(input_length, choice_length);
227 space = VSpace(LyXGlueLength(length));
232 if (fl_get_button(check_keep))
239 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
242 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
243 input_length && input_length->objclass == FL_INPUT);
245 if (fl_get_choice(choice_type) != 7)
248 // If a vspace kind is "Length" but there's no text in
249 // the input field, reset the kind to "None".
250 string const input = strip(getStringFromInput(input_length));
252 fl_set_choice(choice_type, 1);
258 void FormParagraph::apply()
260 if (!lv_->view()->available() || !dialog_.get())
263 // If a vspace kind is "Length" but there's no text in
264 // the input field, reset the kind to "None".
265 validateVSpaceWidgets(dialog_->choice_space_above,
266 dialog_->input_space_above);
267 validateVSpaceWidgets(dialog_->choice_space_below,
268 dialog_->input_space_below);
270 bool const line_top = fl_get_button(dialog_->check_lines_top);
271 bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
272 bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
273 bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
275 VSpace const space_top =
276 setVSpaceFromWidgets(dialog_->choice_space_above,
277 dialog_->input_space_above,
278 dialog_->choice_value_space_above,
279 dialog_->check_space_above);
281 VSpace const space_bottom =
282 setVSpaceFromWidgets(dialog_->choice_space_below,
283 dialog_->input_space_below,
284 dialog_->choice_value_space_below,
285 dialog_->check_space_below);
288 if (fl_get_button(dialog_->radio_align_left))
289 align = LYX_ALIGN_LEFT;
290 else if (fl_get_button(dialog_->radio_align_right))
291 align = LYX_ALIGN_RIGHT;
292 else if (fl_get_button(dialog_->radio_align_center))
293 align = LYX_ALIGN_CENTER;
295 align = LYX_ALIGN_BLOCK;
297 string const labelwidthstring =
298 getStringFromInput(dialog_->input_labelwidth);
300 bool const noindent = fl_get_button(dialog_->check_noindent);
302 Spacing::Space linespacing = Spacing::Default;
304 switch (fl_get_choice(dialog_->choice_linespacing)) {
306 linespacing = Spacing::Default;
309 linespacing = Spacing::Single;
312 linespacing = Spacing::Onehalf;
315 linespacing = Spacing::Double;
318 linespacing = Spacing::Other;
319 other = getStringFromInput(dialog_->input_linespacing);
323 Spacing const spacing(linespacing, other);
325 LyXText * text(lv_->view()->getLyXText());
326 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
327 pagebreak_bottom, space_top, space_bottom, spacing,
328 align, labelwidthstring, noindent);
330 // Actually apply these settings
331 lv_->view()->update(text,
332 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
333 lv_->buffer()->markDirty();
334 setMinibuffer(lv_, _("Paragraph layout set"));
340 void setWidgetsFromVSpace(VSpace const & space,
341 FL_OBJECT * choice_type,
342 FL_OBJECT * input_length,
343 FL_OBJECT * choice_length,
344 FL_OBJECT * check_keep)
347 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
348 input_length && input_length->objclass == FL_INPUT &&
349 choice_length && choice_length->objclass == FL_CHOICE &&
350 check_keep && check_keep->objclass == FL_CHECKBUTTON);
352 fl_set_input(input_length, "");
353 setEnabled(input_length, false);
354 setEnabled(choice_length, false);
356 switch (space.kind()) {
358 fl_set_choice(choice_type, 1);
360 case VSpace::DEFSKIP:
361 fl_set_choice(choice_type, 2);
363 case VSpace::SMALLSKIP:
364 fl_set_choice(choice_type, 3);
366 case VSpace::MEDSKIP:
367 fl_set_choice(choice_type, 4);
369 case VSpace::BIGSKIP:
370 fl_set_choice(choice_type, 5);
373 fl_set_choice(choice_type, 6);
377 fl_set_choice(choice_type, 7);
379 setEnabled(input_length, true);
380 setEnabled(choice_length, true);
382 bool const metric = lyxrc.default_papersize > 3;
383 string const default_unit = metric ? "cm" : "in";
384 string const length = space.length().asString();
386 updateWidgetsFromLengthString(input_length, choice_length,
387 length, default_unit);
392 fl_set_button(check_keep, space.keep());
398 void FormParagraph::update()
403 // Do this first; some objects may be de/activated subsequently.
404 bc_.readOnly(lv_->buffer()->isReadonly());
406 /// Record the paragraph
407 par_ = getCurrentParagraph();
409 fl_set_input(dialog_->input_labelwidth,
410 par_->getLabelWidthString().c_str());
411 setEnabled(dialog_->input_labelwidth,
412 (par_->getLabelWidthString() != _("Senseless with this layout!")));
414 fl_set_button(dialog_->radio_align_right, 0);
415 fl_set_button(dialog_->radio_align_left, 0);
416 fl_set_button(dialog_->radio_align_center, 0);
417 fl_set_button(dialog_->radio_align_block, 0);
419 LyXTextClass const & tclass =
420 textclasslist[lv_->view()->buffer()->params.textclass];
422 int align = par_->getAlign();
423 if (align == LYX_ALIGN_LAYOUT)
424 align = tclass[par_->layout()].align;
427 case LYX_ALIGN_RIGHT:
428 fl_set_button(dialog_->radio_align_right, 1);
431 fl_set_button(dialog_->radio_align_left, 1);
433 case LYX_ALIGN_CENTER:
434 fl_set_button(dialog_->radio_align_center, 1);
437 fl_set_button(dialog_->radio_align_block, 1);
441 LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
443 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
444 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
445 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
446 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
448 // no inset-text-owned paragraph may have pagebreaks
449 setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
450 setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
452 fl_set_button(dialog_->check_lines_top,
453 par_->params().lineTop());
454 fl_set_button(dialog_->check_lines_bottom,
455 par_->params().lineBottom());
456 fl_set_button(dialog_->check_pagebreaks_top,
457 par_->params().pagebreakTop());
458 fl_set_button(dialog_->check_pagebreaks_bottom,
459 par_->params().pagebreakBottom());
460 fl_set_button(dialog_->check_noindent,
461 par_->params().noindent());
464 Spacing const space = par_->params().spacing();
466 switch (space.getSpace()) {
467 default: linespacing = 1; break;
468 case Spacing::Single: linespacing = 2; break;
469 case Spacing::Onehalf: linespacing = 3; break;
470 case Spacing::Double: linespacing = 4; break;
471 case Spacing::Other: linespacing = 5; break;
474 fl_set_choice(dialog_->choice_linespacing, linespacing);
475 if (space.getSpace() == Spacing::Other) {
476 string const sp = tostr(space.getValue());
477 fl_set_input(dialog_->input_linespacing, sp.c_str());
478 setEnabled(dialog_->input_linespacing, true);
480 fl_set_input(dialog_->input_linespacing, "");
481 setEnabled(dialog_->input_linespacing, false);
484 setWidgetsFromVSpace(par_->params().spaceTop(),
485 dialog_->choice_space_above,
486 dialog_->input_space_above,
487 dialog_->choice_value_space_above,
488 dialog_->check_space_above);
490 setWidgetsFromVSpace(par_->params().spaceBottom(),
491 dialog_->choice_space_below,
492 dialog_->input_space_below,
493 dialog_->choice_value_space_below,
494 dialog_->check_space_below);
496 fl_set_button(dialog_->check_noindent,
497 par_->params().noindent());
503 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
504 FL_OBJECT * input_length,
505 FL_OBJECT * choice_length,
509 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
510 input_length && input_length->objclass == FL_INPUT &&
511 choice_length && choice_length->objclass == FL_CHOICE);
513 if (fl_get_choice(choice_type) != 7) {
514 fl_set_input(input_length, "");
515 setEnabled(input_length, false);
516 setEnabled(choice_length, false);
519 setEnabled(input_length, !readonly);
520 setEnabled(choice_length, !readonly);
522 string const length = getStringFromInput(input_length);
524 if (strip(length).empty()) {
525 bool const metric = lyxrc.default_papersize > 3;
526 int const default_unit = metric ? 8 : 9;
528 fl_set_choice(choice_length, default_unit);
533 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
536 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
537 input_length && input_length->objclass == FL_INPUT);
539 if (fl_get_choice(choice_type) != 7)
542 string const input = getStringFromInput(input_length);
543 return (input.empty() ||
544 isValidGlueLength(input) ||
551 bool FormParagraph::input(FL_OBJECT * ob, long)
555 // First check the buttons which are exclusive and you have to
556 // check only the actuall de/activated button.
558 // "Synchronize" the choices and input fields, making it
559 // impossible to commit senseless data.
561 if (ob == dialog_->choice_space_above) {
562 synchronizeSpaceWidgets(dialog_->choice_space_above,
563 dialog_->input_space_above,
564 dialog_->choice_value_space_above,
565 lv_->buffer()->isReadonly());
568 if (ob == dialog_->choice_space_below) {
569 synchronizeSpaceWidgets(dialog_->choice_space_below,
570 dialog_->input_space_below,
571 dialog_->choice_value_space_below,
572 lv_->buffer()->isReadonly());
575 // Display a warning if the input is senseless
576 bool valid = (validSpaceWidgets(dialog_->choice_space_above,
577 dialog_->input_space_above) &&
578 validSpaceWidgets(dialog_->choice_space_below,
579 dialog_->input_space_below));
582 postWarning(_("Invalid Length (valid example: 10mm)"));
585 int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
587 if (choice_spacing == 5)
588 setEnabled(dialog_->input_linespacing, true);
590 fl_set_input(dialog_->input_linespacing, "");
591 setEnabled(dialog_->input_linespacing, false);
594 double const spacing =
595 strToDbl(getStringFromInput(dialog_->input_linespacing));
597 if (choice_spacing == 5 && int(spacing) == 0)