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))
240 void FormParagraph::apply()
242 if (!lv_->view()->available() || !dialog_.get())
245 // If a vspace kind is "Length" but there's no text in
246 // the input field, reset the kind to "None".
247 if ((fl_get_choice(dialog_->choice_space_above) == 7) &&
248 !*(fl_get_input(dialog_->input_space_above)))
250 fl_set_choice(dialog_->choice_space_above, 1);
253 if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
254 !*(fl_get_input (dialog_->input_space_below)))
256 fl_set_choice(dialog_->choice_space_below, 1);
259 bool const line_top = fl_get_button(dialog_->check_lines_top);
260 bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
261 bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
262 bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
264 VSpace const space_top =
265 setVSpaceFromWidgets(dialog_->choice_space_above,
266 dialog_->input_space_above,
267 dialog_->choice_value_space_above,
268 dialog_->check_space_above);
270 VSpace const space_bottom =
271 setVSpaceFromWidgets(dialog_->choice_space_below,
272 dialog_->input_space_below,
273 dialog_->choice_value_space_below,
274 dialog_->check_space_below);
277 if (fl_get_button(dialog_->radio_align_left))
278 align = LYX_ALIGN_LEFT;
279 else if (fl_get_button(dialog_->radio_align_right))
280 align = LYX_ALIGN_RIGHT;
281 else if (fl_get_button(dialog_->radio_align_center))
282 align = LYX_ALIGN_CENTER;
284 align = LYX_ALIGN_BLOCK;
286 string const labelwidthstring =
287 getStringFromInput(dialog_->input_labelwidth);
289 bool const noindent = fl_get_button(dialog_->check_noindent);
291 Spacing::Space linespacing = Spacing::Default;
292 string other_linespacing;
293 switch (fl_get_choice(dialog_->choice_linespacing)) {
294 case 1: linespacing = Spacing::Default; break;
295 case 2: linespacing = Spacing::Single; break;
296 case 3: linespacing = Spacing::Onehalf; break;
297 case 4: linespacing = Spacing::Double; break;
299 linespacing = Spacing::Other;
300 other_linespacing = fl_get_input(dialog_->input_linespacing);
304 Spacing const spacing(linespacing, other_linespacing);
306 LyXText * text(lv_->view()->getLyXText());
307 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
308 pagebreak_bottom, space_top, space_bottom, spacing,
309 align, labelwidthstring, noindent);
311 // Actually apply these settings
312 lv_->view()->update(text,
313 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
314 lv_->buffer()->markDirty();
315 setMinibuffer(lv_, _("Paragraph layout set"));
321 void setWidgetsFromVSpace(VSpace const & space,
322 FL_OBJECT * choice_type,
323 FL_OBJECT * input_length,
324 FL_OBJECT * choice_length,
325 FL_OBJECT * check_keep)
328 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
329 input_length && input_length->objclass == FL_INPUT &&
330 choice_length && choice_length->objclass == FL_CHOICE &&
331 check_keep && check_keep->objclass == FL_CHECKBUTTON);
333 fl_set_input(input_length, "");
334 setEnabled(input_length, false);
335 setEnabled(choice_length, false);
337 switch (space.kind()) {
339 fl_set_choice(choice_type, 1);
341 case VSpace::DEFSKIP:
342 fl_set_choice(choice_type, 2);
344 case VSpace::SMALLSKIP:
345 fl_set_choice(choice_type, 3);
347 case VSpace::MEDSKIP:
348 fl_set_choice(choice_type, 4);
350 case VSpace::BIGSKIP:
351 fl_set_choice(choice_type, 5);
354 fl_set_choice(choice_type, 6);
358 fl_set_choice(choice_type, 7);
360 setEnabled(input_length, true);
361 setEnabled(choice_length, true);
363 bool const metric = lyxrc.default_papersize > 3;
364 string const default_unit = metric ? "cm" : "in";
365 string const length = space.length().asString();
367 updateWidgetsFromLengthString(input_length, choice_length,
368 length, default_unit);
373 fl_set_button(check_keep, space.keep());
379 void FormParagraph::update()
384 // Do this first; some objects may be de/activated subsequently.
385 bc_.readOnly(lv_->buffer()->isReadonly());
387 /// Record the paragraph
388 par_ = getCurrentParagraph();
390 fl_set_input(dialog_->input_labelwidth,
391 par_->getLabelWidthString().c_str());
392 setEnabled(dialog_->input_labelwidth,
393 (par_->getLabelWidthString() != _("Senseless with this layout!")));
395 fl_set_button(dialog_->radio_align_right, 0);
396 fl_set_button(dialog_->radio_align_left, 0);
397 fl_set_button(dialog_->radio_align_center, 0);
398 fl_set_button(dialog_->radio_align_block, 0);
400 LyXTextClass const & tclass =
401 textclasslist[lv_->view()->buffer()->params.textclass];
403 int align = par_->getAlign();
404 if (align == LYX_ALIGN_LAYOUT)
405 align = tclass[par_->layout()].align;
408 case LYX_ALIGN_RIGHT:
409 fl_set_button(dialog_->radio_align_right, 1);
412 fl_set_button(dialog_->radio_align_left, 1);
414 case LYX_ALIGN_CENTER:
415 fl_set_button(dialog_->radio_align_center, 1);
418 fl_set_button(dialog_->radio_align_block, 1);
422 LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
424 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
425 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
426 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
427 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
429 // no inset-text-owned paragraph may have pagebreaks
430 setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
431 setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
433 fl_set_button(dialog_->check_lines_top,
434 par_->params().lineTop());
435 fl_set_button(dialog_->check_lines_bottom,
436 par_->params().lineBottom());
437 fl_set_button(dialog_->check_pagebreaks_top,
438 par_->params().pagebreakTop());
439 fl_set_button(dialog_->check_pagebreaks_bottom,
440 par_->params().pagebreakBottom());
441 fl_set_button(dialog_->check_noindent,
442 par_->params().noindent());
445 Spacing const space = par_->params().spacing();
447 switch (space.getSpace()) {
448 default: linespacing = 1; break;
449 case Spacing::Single: linespacing = 2; break;
450 case Spacing::Onehalf: linespacing = 3; break;
451 case Spacing::Double: linespacing = 4; break;
452 case Spacing::Other: linespacing = 5; break;
455 fl_set_choice(dialog_->choice_linespacing, linespacing);
456 if (space.getSpace() == Spacing::Other) {
457 string const sp = tostr(space.getValue());
458 fl_set_input(dialog_->input_linespacing, sp.c_str());
459 setEnabled(dialog_->input_linespacing, true);
461 fl_set_input(dialog_->input_linespacing, "");
462 setEnabled(dialog_->input_linespacing, false);
465 setWidgetsFromVSpace(par_->params().spaceTop(),
466 dialog_->choice_space_above,
467 dialog_->input_space_above,
468 dialog_->choice_value_space_above,
469 dialog_->check_space_above);
471 setWidgetsFromVSpace(par_->params().spaceBottom(),
472 dialog_->choice_space_below,
473 dialog_->input_space_below,
474 dialog_->choice_value_space_below,
475 dialog_->check_space_below);
477 fl_set_button(dialog_->check_noindent,
478 par_->params().noindent());
484 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
485 FL_OBJECT * input_length,
486 FL_OBJECT * choice_length,
490 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
491 input_length && input_length->objclass == FL_INPUT &&
492 choice_length && choice_length->objclass == FL_CHOICE);
494 if (fl_get_choice(choice_type) != 7) {
495 fl_set_input(input_length, "");
496 setEnabled(input_length, false);
497 setEnabled(choice_length, false);
500 setEnabled(input_length, !readonly);
501 setEnabled(choice_length, !readonly);
503 string const length = getStringFromInput(input_length);
505 if (strip(length).empty()) {
506 bool const metric = lyxrc.default_papersize > 3;
507 int const default_unit = metric ? 8 : 9;
509 fl_set_choice(choice_length, default_unit);
514 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
517 lyx::Assert(choice_type && choice_type->objclass == FL_CHOICE &&
518 input_length && input_length->objclass == FL_INPUT);
520 if (fl_get_choice(choice_type) != 7)
523 string const input = getStringFromInput(input_length);
524 return (input.empty() ||
525 isValidGlueLength(input) ||
532 bool FormParagraph::input(FL_OBJECT * ob, long)
536 // First check the buttons which are exclusive and you have to
537 // check only the actuall de/activated button.
539 // "Synchronize" the choices and input fields, making it
540 // impossible to commit senseless data.
542 if (ob == dialog_->choice_space_above) {
543 synchronizeSpaceWidgets(dialog_->choice_space_above,
544 dialog_->input_space_above,
545 dialog_->choice_value_space_above,
546 lv_->buffer()->isReadonly());
549 if (ob == dialog_->choice_space_below) {
550 synchronizeSpaceWidgets(dialog_->choice_space_below,
551 dialog_->input_space_below,
552 dialog_->choice_value_space_below,
553 lv_->buffer()->isReadonly());
556 // Display a warning if the input is senseless
557 bool valid = (validSpaceWidgets(dialog_->choice_space_above,
558 dialog_->input_space_above) &&
559 validSpaceWidgets(dialog_->choice_space_below,
560 dialog_->input_space_below));
563 postWarning(_("Invalid Length (valid example: 10mm)"));
566 int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
568 if (choice_spacing == 5)
569 setEnabled(dialog_->input_linespacing, true);
571 fl_set_input(dialog_->input_linespacing, "");
572 setEnabled(dialog_->input_linespacing, false);
575 double const spacing =
576 strToDbl(getStringFromInput(dialog_->input_linespacing));
578 if (choice_spacing == 5 && int(spacing) == 0)