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"
37 using Liason::setMinibuffer;
44 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
45 : FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
47 // let the dialog be shown
48 // This is a permanent connection so we won't bother
49 // storing a copy because we won't be disconnecting.
50 d->showParagraph.connect(slot(this, &FormParagraph::show));
54 void FormParagraph::connect()
56 cp_ = d_->updateParagraph
57 .connect(slot(this, &FormParagraph::changedParagraph));
58 FormBaseBD::connect();
62 void FormParagraph::disconnect()
65 FormBaseBD::disconnect();
69 Paragraph const * FormParagraph::getCurrentParagraph() const
71 return lv_->view()->getLyXText()->cursor.par();
75 void FormParagraph::changedParagraph()
77 /// Record the paragraph
78 Paragraph const * const p = getCurrentParagraph();
79 if (p == 0 || p == par_)
84 // shouldn't we chage the par_ pointer too?
85 // anyway for me the below function does just nothing!
88 // For now don't bother checking if the params are different,
89 // just activate the Apply button
94 void FormParagraph::redraw()
96 if (form() && form()->visible)
97 fl_redraw_form(form());
101 FL_FORM * FormParagraph::form() const
103 if (dialog_.get()) return dialog_->form;
108 void FormParagraph::build()
111 dialog_.reset(build_paragraph());
113 fl_addto_choice(dialog_->choice_space_above,
114 _(" None | Defskip | Smallskip "
115 "| Medskip | Bigskip | VFill | Length "));
116 fl_addto_choice(dialog_->choice_space_below,
117 _(" None | Defskip | Smallskip "
118 "| Medskip | Bigskip | VFill | Length "));
120 fl_addto_choice(dialog_->choice_linespacing,
121 _(" Default | Single | OneHalf | Double | Other "));
123 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
124 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
125 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
126 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
127 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
129 setPrehandler(dialog_->input_space_above);
130 setPrehandler(dialog_->input_space_below);
131 setPrehandler(dialog_->input_labelwidth);
132 setPrehandler(dialog_->input_linespacing);
134 // Create the contents of the unit choices
135 // Don't include the "%" terms...
136 vector<string> units_vec = getLatexUnits();
138 for (vector<string>::iterator it = units_vec.begin();
139 it != units_vec.end(); ++it) {
140 if (contains(*it, "%"))
141 it = units_vec.erase(it, it+1) - 1;
144 // Something similar to this is a better way to erase
145 vector<string>::iterator del =
146 remove_if(units_vec.begin(), units_vec.end(),
147 bind2nd(contains_functor(), "%"));
148 units_vec.erase(del, units_vec.end());
151 string units = getStringFromVector(units_vec, "|");
153 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
154 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
156 // Manage the ok, apply, restore and cancel/close buttons
157 bc_.setOK(dialog_->button_ok);
158 bc_.setApply(dialog_->button_apply);
159 bc_.setCancel(dialog_->button_cancel);
160 bc_.setRestore(dialog_->button_restore);
162 bc_.addReadOnly (dialog_->group_radio_alignment);
163 // bc_.addReadOnly (dialog_->radio_align_right);
164 // bc_.addReadOnly (dialog_->radio_align_left);
165 // bc_.addReadOnly (dialog_->radio_align_block);
166 // bc_.addReadOnly (dialog_->radio_align_center);
167 bc_.addReadOnly (dialog_->check_lines_top);
168 bc_.addReadOnly (dialog_->check_lines_bottom);
169 bc_.addReadOnly (dialog_->check_pagebreaks_top);
170 bc_.addReadOnly (dialog_->check_pagebreaks_bottom);
171 bc_.addReadOnly (dialog_->choice_space_above);
172 bc_.addReadOnly (dialog_->input_space_above);
173 bc_.addReadOnly (dialog_->check_space_above);
174 bc_.addReadOnly (dialog_->choice_space_below);
175 bc_.addReadOnly (dialog_->input_space_below);
176 bc_.addReadOnly (dialog_->check_space_below);
177 bc_.addReadOnly (dialog_->choice_linespacing);
178 bc_.addReadOnly (dialog_->input_linespacing);
179 bc_.addReadOnly (dialog_->check_noindent);
180 bc_.addReadOnly (dialog_->input_labelwidth);
184 void FormParagraph::apply()
186 if (!lv_->view()->available() || !dialog_.get())
189 VSpace space_top, space_bottom;
191 string labelwidthstring;
194 // If a vspace kind is "Length" but there's no text in
195 // the input field, reset the kind to "None".
196 if ((fl_get_choice (dialog_->choice_space_above) == 7) &&
197 !*(fl_get_input (dialog_->input_space_above)))
199 fl_set_choice (dialog_->choice_space_above, 1);
201 if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
202 !*(fl_get_input (dialog_->input_space_below)))
204 fl_set_choice (dialog_->choice_space_below, 1);
207 bool line_top = fl_get_button(dialog_->check_lines_top);
208 bool line_bottom = fl_get_button(dialog_->check_lines_bottom);
209 bool pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
210 bool pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
212 switch (fl_get_choice (dialog_->choice_space_above)) {
214 space_top = VSpace(VSpace::NONE);
217 space_top = VSpace(VSpace::DEFSKIP);
220 space_top = VSpace(VSpace::SMALLSKIP);
223 space_top = VSpace(VSpace::MEDSKIP);
226 space_top = VSpace(VSpace::BIGSKIP);
229 space_top = VSpace(VSpace::VFILL);
233 string const length =
234 getLengthFromWidgets(dialog_->input_space_above,
235 dialog_->choice_value_space_above);
237 VSpace(LyXGlueLength(length));
242 if (fl_get_button (dialog_->check_space_above))
243 space_top.setKeep (true);
244 switch (fl_get_choice (dialog_->choice_space_below)) {
246 space_bottom = VSpace(VSpace::NONE);
249 space_bottom = VSpace(VSpace::DEFSKIP);
252 space_bottom = VSpace(VSpace::SMALLSKIP);
255 space_bottom = VSpace(VSpace::MEDSKIP);
258 space_bottom = VSpace(VSpace::BIGSKIP);
261 space_bottom = VSpace(VSpace::VFILL);
264 string const length =
265 getLengthFromWidgets(dialog_->input_space_below,
266 dialog_->choice_value_space_below);
267 space_bottom = VSpace(LyXGlueLength(length));
270 if (fl_get_button (dialog_->check_space_below))
271 space_bottom.setKeep (true);
273 if (fl_get_button(dialog_->radio_align_left))
274 align = LYX_ALIGN_LEFT;
275 else if (fl_get_button(dialog_->radio_align_right))
276 align = LYX_ALIGN_RIGHT;
277 else if (fl_get_button(dialog_->radio_align_center))
278 align = LYX_ALIGN_CENTER;
280 align = LYX_ALIGN_BLOCK;
282 labelwidthstring = fl_get_input(dialog_->input_labelwidth);
283 noindent = fl_get_button(dialog_->check_noindent);
284 Spacing::Space linespacing = Spacing::Default;
285 string other_linespacing;
286 switch (fl_get_choice(dialog_->choice_linespacing)) {
287 case 1: linespacing = Spacing::Default; break;
288 case 2: linespacing = Spacing::Single; break;
289 case 3: linespacing = Spacing::Onehalf; break;
290 case 4: linespacing = Spacing::Double; break;
292 linespacing = Spacing::Other;
293 other_linespacing = fl_get_input(dialog_->input_linespacing);
297 Spacing const spacing(linespacing, other_linespacing);
298 LyXText * text(lv_->view()->getLyXText());
299 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
300 pagebreak_bottom, space_top, space_bottom, spacing,
301 align, labelwidthstring, noindent);
304 // Actually apply these settings
305 lv_->view()->update(text,
306 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
307 lv_->buffer()->markDirty();
308 setMinibuffer(lv_, _("Paragraph layout set"));
312 void FormParagraph::update()
317 // Do this first; some objects may be de/activated subsequently.
318 bc_.readOnly(lv_->buffer()->isReadonly());
320 Buffer * buf = lv_->view()->buffer();
322 /// Record the paragraph
323 par_ = getCurrentParagraph();
325 fl_set_input(dialog_->input_labelwidth,
326 par_->getLabelWidthString().c_str());
327 setEnabled(dialog_->input_labelwidth,
328 (par_->getLabelWidthString() != _("Senseless with this layout!")));
330 fl_set_button(dialog_->radio_align_right, 0);
331 fl_set_button(dialog_->radio_align_left, 0);
332 fl_set_button(dialog_->radio_align_center, 0);
333 fl_set_button(dialog_->radio_align_block, 0);
335 LyXTextClass const & tclass = textclasslist[buf->params.textclass];
337 int align = par_->getAlign();
338 if (align == LYX_ALIGN_LAYOUT)
339 align = tclass[par_->layout()].align;
342 case LYX_ALIGN_RIGHT:
343 fl_set_button(dialog_->radio_align_right, 1);
346 fl_set_button(dialog_->radio_align_left, 1);
348 case LYX_ALIGN_CENTER:
349 fl_set_button(dialog_->radio_align_center, 1);
352 fl_set_button(dialog_->radio_align_block, 1);
356 LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
358 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
359 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
360 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
361 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
363 // no inset-text-owned paragraph may have pagebreaks
364 setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
365 setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
367 fl_set_button(dialog_->check_lines_top,
368 par_->params().lineTop());
369 fl_set_button(dialog_->check_lines_bottom,
370 par_->params().lineBottom());
371 fl_set_button(dialog_->check_pagebreaks_top,
372 par_->params().pagebreakTop());
373 fl_set_button(dialog_->check_pagebreaks_bottom,
374 par_->params().pagebreakBottom());
375 fl_set_button(dialog_->check_noindent,
376 par_->params().noindent());
379 Spacing const space = par_->params().spacing();
381 switch (space.getSpace()) {
382 default: linespacing = 1; break;
383 case Spacing::Single: linespacing = 2; break;
384 case Spacing::Onehalf: linespacing = 3; break;
385 case Spacing::Double: linespacing = 4; break;
386 case Spacing::Other: linespacing = 5; break;
389 fl_set_choice(dialog_->choice_linespacing, linespacing);
390 if (space.getSpace() == Spacing::Other) {
391 string const sp = tostr(space.getValue());
392 fl_set_input(dialog_->input_linespacing, sp.c_str());
393 setEnabled(dialog_->input_linespacing, true);
395 fl_set_input(dialog_->input_linespacing, "");
396 setEnabled(dialog_->input_linespacing, false);
399 fl_set_input (dialog_->input_space_above, "");
401 setEnabled(dialog_->input_space_above, false);
402 setEnabled(dialog_->choice_value_space_above, false);
403 switch (par_->params().spaceTop().kind()) {
405 fl_set_choice (dialog_->choice_space_above, 1);
407 case VSpace::DEFSKIP:
408 fl_set_choice (dialog_->choice_space_above, 2);
410 case VSpace::SMALLSKIP:
411 fl_set_choice (dialog_->choice_space_above, 3);
413 case VSpace::MEDSKIP:
414 fl_set_choice (dialog_->choice_space_above, 4);
416 case VSpace::BIGSKIP:
417 fl_set_choice (dialog_->choice_space_above, 5);
420 fl_set_choice (dialog_->choice_space_above, 6);
424 fl_set_choice (dialog_->choice_space_above, 7);
425 setEnabled(dialog_->input_space_above, true);
426 setEnabled(dialog_->choice_value_space_above, true);
427 bool const metric = lyxrc.default_papersize > 3;
428 string const default_unit = metric ? "cm" : "in";
429 string const length = par_->params().spaceTop().length().asString();
430 updateWidgetsFromLengthString(dialog_->input_space_above,
431 dialog_->choice_value_space_above,
432 length, default_unit);
437 fl_set_button (dialog_->check_space_above,
438 par_->params().spaceTop().keep());
439 fl_set_input (dialog_->input_space_below, "");
441 setEnabled(dialog_->input_space_below, false);
442 setEnabled(dialog_->choice_value_space_below, false);
443 switch (par_->params().spaceBottom().kind()) {
445 fl_set_choice (dialog_->choice_space_below, 1);
447 case VSpace::DEFSKIP:
448 fl_set_choice (dialog_->choice_space_below, 2);
450 case VSpace::SMALLSKIP:
451 fl_set_choice (dialog_->choice_space_below, 3);
453 case VSpace::MEDSKIP:
454 fl_set_choice (dialog_->choice_space_below, 4);
456 case VSpace::BIGSKIP:
457 fl_set_choice (dialog_->choice_space_below, 5);
460 fl_set_choice (dialog_->choice_space_below, 6);
464 fl_set_choice (dialog_->choice_space_below, 7);
465 setEnabled(dialog_->input_space_below, true);
466 setEnabled(dialog_->choice_value_space_below, true);
467 bool const metric = lyxrc.default_papersize > 3;
468 string const default_unit = metric ? "cm" : "in";
469 string const length =
470 par_->params().spaceBottom().length().asString();
471 updateWidgetsFromLengthString(dialog_->input_space_below,
472 dialog_->choice_value_space_below,
473 length, default_unit);
478 fl_set_button(dialog_->check_space_below,
479 par_->params().spaceBottom().keep());
480 fl_set_button(dialog_->check_noindent,
481 par_->params().noindent());
485 bool FormParagraph::input(FL_OBJECT * ob, long)
489 fl_hide_object(dialog_->text_warning);
491 // First check the buttons which are exclusive and you have to
492 // check only the actuall de/activated button.
494 // "Synchronize" the choices and input fields, making it
495 // impossible to commit senseless data.
497 if (ob == dialog_->choice_space_above) {
498 if (fl_get_choice (dialog_->choice_space_above) != 7) {
499 fl_set_input (dialog_->input_space_above, "");
500 setEnabled (dialog_->input_space_above, false);
501 setEnabled (dialog_->choice_value_space_above, false);
503 setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
504 setEnabled (dialog_->choice_value_space_above, !lv_->buffer()->isReadonly());
505 bool const metric = lyxrc.default_papersize > 3;
506 int const default_unit = metric ? 8 : 9;
507 if (strip(fl_get_input(dialog_->input_space_above)).empty())
508 fl_set_choice(dialog_->choice_value_space_above,
512 if (ob == dialog_->choice_space_below) {
513 if (fl_get_choice (dialog_->choice_space_below) != 7) {
514 fl_set_input (dialog_->input_space_below, "");
515 setEnabled (dialog_->input_space_below, false);
516 setEnabled (dialog_->choice_value_space_below, false);
518 setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
519 setEnabled (dialog_->choice_value_space_below, !lv_->buffer()->isReadonly());
520 bool const metric = lyxrc.default_papersize > 3;
521 int const default_unit = metric ? 8 : 9;
522 if (strip(fl_get_input(dialog_->input_space_below)).empty())
523 fl_set_choice(dialog_->choice_value_space_below,
529 // warnings if input is senseless
531 string input = fl_get_input (dialog_->input_space_above);
532 bool invalid = false;
534 if (fl_get_choice(dialog_->choice_space_above)==7)
535 invalid = !input.empty() && !isValidGlueLength(input) && !isStrDbl(input);
537 input = fl_get_input (dialog_->input_space_below);
539 if (fl_get_choice(dialog_->choice_space_below)==7)
541 || (!input.empty() && !isValidGlueLength(input) && !isStrDbl(input));
543 if (ob == dialog_->input_space_above || ob == dialog_->input_space_below) {
545 fl_set_object_label(dialog_->text_warning,
546 _("Warning: Invalid Length (valid example: 10mm)"));
547 fl_show_object(dialog_->text_warning);
550 fl_hide_object(dialog_->text_warning);
555 if (fl_get_choice (dialog_->choice_linespacing) == 5)
556 setEnabled (dialog_->input_linespacing, true);
558 setEnabled (dialog_->input_linespacing, false);
559 fl_set_input (dialog_->input_linespacing, "");
562 double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));
564 if (fl_get_choice (dialog_->choice_linespacing) == 5
565 && int(spacing) == 0)