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_close);
160 bc_.setRestore(dialog_->button_restore);
162 bc_.addReadOnly (dialog_->radio_align_right);
163 bc_.addReadOnly (dialog_->radio_align_left);
164 bc_.addReadOnly (dialog_->radio_align_block);
165 bc_.addReadOnly (dialog_->radio_align_center);
166 bc_.addReadOnly (dialog_->check_lines_top);
167 bc_.addReadOnly (dialog_->check_lines_bottom);
168 bc_.addReadOnly (dialog_->check_pagebreaks_top);
169 bc_.addReadOnly (dialog_->check_pagebreaks_bottom);
170 bc_.addReadOnly (dialog_->choice_space_above);
171 bc_.addReadOnly (dialog_->input_space_above);
172 bc_.addReadOnly (dialog_->check_space_above);
173 bc_.addReadOnly (dialog_->choice_space_below);
174 bc_.addReadOnly (dialog_->input_space_below);
175 bc_.addReadOnly (dialog_->check_space_below);
176 bc_.addReadOnly (dialog_->choice_linespacing);
177 bc_.addReadOnly (dialog_->input_linespacing);
178 bc_.addReadOnly (dialog_->check_noindent);
179 bc_.addReadOnly (dialog_->input_labelwidth);
183 void FormParagraph::apply()
185 if (!lv_->view()->available() || !dialog_.get())
188 VSpace space_top, space_bottom;
190 string labelwidthstring;
193 // If a vspace kind is "Length" but there's no text in
194 // the input field, reset the kind to "None".
195 if ((fl_get_choice (dialog_->choice_space_above) == 7) &&
196 !*(fl_get_input (dialog_->input_space_above)))
198 fl_set_choice (dialog_->choice_space_above, 1);
200 if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
201 !*(fl_get_input (dialog_->input_space_below)))
203 fl_set_choice (dialog_->choice_space_below, 1);
206 bool line_top = fl_get_button(dialog_->check_lines_top);
207 bool line_bottom = fl_get_button(dialog_->check_lines_bottom);
208 bool pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
209 bool pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
211 switch (fl_get_choice (dialog_->choice_space_above)) {
213 space_top = VSpace(VSpace::NONE);
216 space_top = VSpace(VSpace::DEFSKIP);
219 space_top = VSpace(VSpace::SMALLSKIP);
222 space_top = VSpace(VSpace::MEDSKIP);
225 space_top = VSpace(VSpace::BIGSKIP);
228 space_top = VSpace(VSpace::VFILL);
232 string const length =
233 getLengthFromWidgets(dialog_->input_space_above,
234 dialog_->choice_value_space_above);
236 VSpace(LyXGlueLength(length));
241 if (fl_get_button (dialog_->check_space_above))
242 space_top.setKeep (true);
243 switch (fl_get_choice (dialog_->choice_space_below)) {
245 space_bottom = VSpace(VSpace::NONE);
248 space_bottom = VSpace(VSpace::DEFSKIP);
251 space_bottom = VSpace(VSpace::SMALLSKIP);
254 space_bottom = VSpace(VSpace::MEDSKIP);
257 space_bottom = VSpace(VSpace::BIGSKIP);
260 space_bottom = VSpace(VSpace::VFILL);
263 string const length =
264 getLengthFromWidgets(dialog_->input_space_below,
265 dialog_->choice_value_space_below);
266 space_bottom = VSpace(LyXGlueLength(length));
269 if (fl_get_button (dialog_->check_space_below))
270 space_bottom.setKeep (true);
272 if (fl_get_button(dialog_->radio_align_left))
273 align = LYX_ALIGN_LEFT;
274 else if (fl_get_button(dialog_->radio_align_right))
275 align = LYX_ALIGN_RIGHT;
276 else if (fl_get_button(dialog_->radio_align_center))
277 align = LYX_ALIGN_CENTER;
279 align = LYX_ALIGN_BLOCK;
281 labelwidthstring = fl_get_input(dialog_->input_labelwidth);
282 noindent = fl_get_button(dialog_->check_noindent);
283 Spacing::Space linespacing = Spacing::Default;
284 string other_linespacing;
285 switch (fl_get_choice(dialog_->choice_linespacing)) {
286 case 1: linespacing = Spacing::Default; break;
287 case 2: linespacing = Spacing::Single; break;
288 case 3: linespacing = Spacing::Onehalf; break;
289 case 4: linespacing = Spacing::Double; break;
291 linespacing = Spacing::Other;
292 other_linespacing = fl_get_input(dialog_->input_linespacing);
296 Spacing const spacing(linespacing, other_linespacing);
297 LyXText * text(lv_->view()->getLyXText());
298 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
299 pagebreak_bottom, space_top, space_bottom, spacing,
300 align, labelwidthstring, noindent);
303 // Actually apply these settings
304 lv_->view()->update(text,
305 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
306 lv_->buffer()->markDirty();
307 setMinibuffer(lv_, _("Paragraph layout set"));
311 void FormParagraph::update()
316 // Do this first; some objects may be de/activated subsequently.
317 bc_.readOnly(lv_->buffer()->isReadonly());
319 Buffer * buf = lv_->view()->buffer();
321 /// Record the paragraph
322 par_ = getCurrentParagraph();
324 fl_set_input(dialog_->input_labelwidth,
325 par_->getLabelWidthString().c_str());
326 setEnabled(dialog_->input_labelwidth,
327 (par_->getLabelWidthString() != _("Senseless with this layout!")));
329 fl_set_button(dialog_->radio_align_right, 0);
330 fl_set_button(dialog_->radio_align_left, 0);
331 fl_set_button(dialog_->radio_align_center, 0);
332 fl_set_button(dialog_->radio_align_block, 0);
334 LyXTextClass const & tclass = textclasslist[buf->params.textclass];
336 int align = par_->getAlign();
337 if (align == LYX_ALIGN_LAYOUT)
338 align = tclass[par_->layout()].align;
341 case LYX_ALIGN_RIGHT:
342 fl_set_button(dialog_->radio_align_right, 1);
345 fl_set_button(dialog_->radio_align_left, 1);
347 case LYX_ALIGN_CENTER:
348 fl_set_button(dialog_->radio_align_center, 1);
351 fl_set_button(dialog_->radio_align_block, 1);
355 LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
357 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
358 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
359 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
360 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
362 // no inset-text-owned paragraph may have pagebreaks
363 setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
364 setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
366 fl_set_button(dialog_->check_lines_top,
367 par_->params().lineTop());
368 fl_set_button(dialog_->check_lines_bottom,
369 par_->params().lineBottom());
370 fl_set_button(dialog_->check_pagebreaks_top,
371 par_->params().pagebreakTop());
372 fl_set_button(dialog_->check_pagebreaks_bottom,
373 par_->params().pagebreakBottom());
374 fl_set_button(dialog_->check_noindent,
375 par_->params().noindent());
378 Spacing const space = par_->params().spacing();
380 switch (space.getSpace()) {
381 default: linespacing = 1; break;
382 case Spacing::Single: linespacing = 2; break;
383 case Spacing::Onehalf: linespacing = 3; break;
384 case Spacing::Double: linespacing = 4; break;
385 case Spacing::Other: linespacing = 5; break;
388 fl_set_choice(dialog_->choice_linespacing, linespacing);
389 if (space.getSpace() == Spacing::Other) {
390 string const sp = tostr(space.getValue());
391 fl_set_input(dialog_->input_linespacing, sp.c_str());
392 setEnabled(dialog_->input_linespacing, true);
394 fl_set_input(dialog_->input_linespacing, "");
395 setEnabled(dialog_->input_linespacing, false);
398 fl_set_input (dialog_->input_space_above, "");
400 setEnabled(dialog_->input_space_above, false);
401 setEnabled(dialog_->choice_value_space_above, false);
402 switch (par_->params().spaceTop().kind()) {
404 fl_set_choice (dialog_->choice_space_above, 1);
406 case VSpace::DEFSKIP:
407 fl_set_choice (dialog_->choice_space_above, 2);
409 case VSpace::SMALLSKIP:
410 fl_set_choice (dialog_->choice_space_above, 3);
412 case VSpace::MEDSKIP:
413 fl_set_choice (dialog_->choice_space_above, 4);
415 case VSpace::BIGSKIP:
416 fl_set_choice (dialog_->choice_space_above, 5);
419 fl_set_choice (dialog_->choice_space_above, 6);
423 fl_set_choice (dialog_->choice_space_above, 7);
424 setEnabled(dialog_->input_space_above, true);
425 setEnabled(dialog_->choice_value_space_above, true);
426 bool const metric = lyxrc.default_papersize > 3;
427 string const default_unit = metric ? "cm" : "in";
428 string const length = par_->params().spaceTop().length().asString();
429 updateWidgetsFromLengthString(dialog_->input_space_above,
430 dialog_->choice_value_space_above,
431 length, default_unit);
436 fl_set_button (dialog_->check_space_above,
437 par_->params().spaceTop().keep());
438 fl_set_input (dialog_->input_space_below, "");
440 setEnabled(dialog_->input_space_below, false);
441 setEnabled(dialog_->choice_value_space_below, false);
442 switch (par_->params().spaceBottom().kind()) {
444 fl_set_choice (dialog_->choice_space_below, 1);
446 case VSpace::DEFSKIP:
447 fl_set_choice (dialog_->choice_space_below, 2);
449 case VSpace::SMALLSKIP:
450 fl_set_choice (dialog_->choice_space_below, 3);
452 case VSpace::MEDSKIP:
453 fl_set_choice (dialog_->choice_space_below, 4);
455 case VSpace::BIGSKIP:
456 fl_set_choice (dialog_->choice_space_below, 5);
459 fl_set_choice (dialog_->choice_space_below, 6);
463 fl_set_choice (dialog_->choice_space_below, 7);
464 setEnabled(dialog_->input_space_below, true);
465 setEnabled(dialog_->choice_value_space_below, true);
466 bool const metric = lyxrc.default_papersize > 3;
467 string const default_unit = metric ? "cm" : "in";
468 string const length =
469 par_->params().spaceBottom().length().asString();
470 updateWidgetsFromLengthString(dialog_->input_space_below,
471 dialog_->choice_value_space_below,
472 length, default_unit);
477 fl_set_button(dialog_->check_space_below,
478 par_->params().spaceBottom().keep());
479 fl_set_button(dialog_->check_noindent,
480 par_->params().noindent());
484 bool FormParagraph::input(FL_OBJECT * ob, long)
488 fl_hide_object(dialog_->text_warning);
490 // First check the buttons which are exclusive and you have to
491 // check only the actuall de/activated button.
493 // "Synchronize" the choices and input fields, making it
494 // impossible to commit senseless data.
496 if (ob == dialog_->choice_space_above) {
497 if (fl_get_choice (dialog_->choice_space_above) != 7) {
498 fl_set_input (dialog_->input_space_above, "");
499 setEnabled (dialog_->input_space_above, false);
500 setEnabled (dialog_->choice_value_space_above, false);
502 setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
503 setEnabled (dialog_->choice_value_space_above, !lv_->buffer()->isReadonly());
504 bool const metric = lyxrc.default_papersize > 3;
505 int const default_unit = metric ? 8 : 9;
506 if (strip(fl_get_input(dialog_->input_space_above)).empty())
507 fl_set_choice(dialog_->choice_value_space_above,
511 if (ob == dialog_->choice_space_below) {
512 if (fl_get_choice (dialog_->choice_space_below) != 7) {
513 fl_set_input (dialog_->input_space_below, "");
514 setEnabled (dialog_->input_space_below, false);
515 setEnabled (dialog_->choice_value_space_below, false);
517 setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
518 setEnabled (dialog_->choice_value_space_below, !lv_->buffer()->isReadonly());
519 bool const metric = lyxrc.default_papersize > 3;
520 int const default_unit = metric ? 8 : 9;
521 if (strip(fl_get_input(dialog_->input_space_below)).empty())
522 fl_set_choice(dialog_->choice_value_space_below,
528 // warnings if input is senseless
530 string input = fl_get_input (dialog_->input_space_above);
531 bool invalid = false;
533 if (fl_get_choice(dialog_->choice_space_above)==7)
534 invalid = !input.empty() && !isValidGlueLength(input) && !isStrDbl(input);
536 input = fl_get_input (dialog_->input_space_below);
538 if (fl_get_choice(dialog_->choice_space_below)==7)
540 || (!input.empty() && !isValidGlueLength(input) && !isStrDbl(input));
542 if (ob == dialog_->input_space_above || ob == dialog_->input_space_below) {
544 fl_set_object_label(dialog_->text_warning,
545 _("Warning: Invalid Length (valid example: 10mm)"));
546 fl_show_object(dialog_->text_warning);
549 fl_hide_object(dialog_->text_warning);
554 if (fl_get_choice (dialog_->choice_linespacing) == 5)
555 setEnabled (dialog_->input_linespacing, true);
557 setEnabled (dialog_->input_linespacing, false);
558 fl_set_input (dialog_->input_linespacing, "");
561 double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));
563 if (fl_get_choice (dialog_->choice_linespacing) == 5
564 && int(spacing) == 0)