1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
10 *======================================================*/
15 #pragma implementation
18 #include FORMS_H_LOCATION
20 #include "FormParagraph.h"
21 #include "form_paragraph.h"
27 #include "xforms_helpers.h"
28 #include "lyxrc.h" // to set the deafult length values
29 #include "BufferView.h"
30 #include "lyxtextclasslist.h"
32 #include "ParagraphParameters.h"
33 #include "input_validators.h"
34 #include "helper_funcs.h"
36 #include "support/lstrings.h"
38 using Liason::setMinibuffer;
42 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
43 : FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
45 // let the dialog be shown
46 // This is a permanent connection so we won't bother
47 // storing a copy because we won't be disconnecting.
48 d->showParagraph.connect(slot(this, &FormParagraph::show));
52 void FormParagraph::connect()
54 cp_ = d_->updateParagraph
55 .connect(slot(this, &FormParagraph::changedParagraph));
56 FormBaseBD::connect();
60 void FormParagraph::disconnect()
63 FormBaseBD::disconnect();
67 Paragraph const * FormParagraph::getCurrentParagraph() const
69 return lv_->view()->getLyXText()->cursor.par();
73 void FormParagraph::changedParagraph()
75 /// Record the paragraph
76 Paragraph const * const p = getCurrentParagraph();
77 if (p == 0 || p == par_)
80 #warning Please someone of you GUII people have a look at this!
82 // shouldn't we chage the par_ pointer too?
83 // anyway for me the below function does just nothing!
86 // For now don't bother checking if the params are different,
87 // just activate the Apply button
92 void FormParagraph::redraw()
94 if( form() && form()->visible )
95 fl_redraw_form( form() );
99 FL_FORM * FormParagraph::form() const
101 if (dialog_.get()) return dialog_->form;
106 void FormParagraph::build()
109 dialog_.reset(build_paragraph());
111 fl_addto_choice(dialog_->choice_space_above,
112 _(" None | Defskip | Smallskip "
113 "| Medskip | Bigskip | VFill | Length "));
114 fl_addto_choice(dialog_->choice_space_below,
115 _(" None | Defskip | Smallskip "
116 "| Medskip | Bigskip | VFill | Length "));
118 fl_addto_choice(dialog_->choice_linespacing,
119 _(" Default | Single | OneHalf | Double | Other "));
121 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
122 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
123 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
124 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
125 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
127 // Create the contents of the unit choices
128 // Don't include the "%" terms...
129 std::vector<string> units_vec = getLatexUnits();
130 for (std::vector<string>::iterator it = units_vec.begin();
131 it != units_vec.end(); ++it) {
132 if (contains(*it, "%"))
133 it = units_vec.erase(it, it+1) - 1;
135 string units = getStringFromVector(units_vec, "|");
137 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
138 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
140 // Manage the ok, apply, restore and cancel/close buttons
141 bc_.setOK(dialog_->button_ok);
142 bc_.setApply(dialog_->button_apply);
143 bc_.setCancel(dialog_->button_cancel);
144 bc_.setRestore(dialog_->button_restore);
146 bc_.addReadOnly (dialog_->group_radio_alignment);
147 // bc_.addReadOnly (dialog_->radio_align_right);
148 // bc_.addReadOnly (dialog_->radio_align_left);
149 // bc_.addReadOnly (dialog_->radio_align_block);
150 // bc_.addReadOnly (dialog_->radio_align_center);
151 bc_.addReadOnly (dialog_->check_lines_top);
152 bc_.addReadOnly (dialog_->check_lines_bottom);
153 bc_.addReadOnly (dialog_->check_pagebreaks_top);
154 bc_.addReadOnly (dialog_->check_pagebreaks_bottom);
155 bc_.addReadOnly (dialog_->choice_space_above);
156 bc_.addReadOnly (dialog_->input_space_above);
157 bc_.addReadOnly (dialog_->check_space_above);
158 bc_.addReadOnly (dialog_->choice_space_below);
159 bc_.addReadOnly (dialog_->input_space_below);
160 bc_.addReadOnly (dialog_->check_space_below);
161 bc_.addReadOnly (dialog_->choice_linespacing);
162 bc_.addReadOnly (dialog_->input_linespacing);
163 bc_.addReadOnly (dialog_->check_noindent);
164 bc_.addReadOnly (dialog_->input_labelwidth);
168 void FormParagraph::apply()
170 if (!lv_->view()->available() || !dialog_.get())
173 VSpace space_top, space_bottom;
175 string labelwidthstring;
178 // If a vspace kind is "Length" but there's no text in
179 // the input field, reset the kind to "None".
180 if ((fl_get_choice (dialog_->choice_space_above) == 7) &&
181 !*(fl_get_input (dialog_->input_space_above)))
183 fl_set_choice (dialog_->choice_space_above, 1);
185 if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
186 !*(fl_get_input (dialog_->input_space_below)))
188 fl_set_choice (dialog_->choice_space_below, 1);
191 bool line_top = fl_get_button(dialog_->check_lines_top);
192 bool line_bottom = fl_get_button(dialog_->check_lines_bottom);
193 bool pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
194 bool pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
196 switch (fl_get_choice (dialog_->choice_space_above)) {
198 space_top = VSpace(VSpace::NONE);
201 space_top = VSpace(VSpace::DEFSKIP);
204 space_top = VSpace(VSpace::SMALLSKIP);
207 space_top = VSpace(VSpace::MEDSKIP);
210 space_top = VSpace(VSpace::BIGSKIP);
213 space_top = VSpace(VSpace::VFILL);
217 string const length =
218 getLengthFromWidgets(dialog_->input_space_above,
219 dialog_->choice_value_space_above);
221 VSpace(LyXGlueLength(length));
226 if (fl_get_button (dialog_->check_space_above))
227 space_top.setKeep (true);
228 switch (fl_get_choice (dialog_->choice_space_below)) {
230 space_bottom = VSpace(VSpace::NONE);
233 space_bottom = VSpace(VSpace::DEFSKIP);
236 space_bottom = VSpace(VSpace::SMALLSKIP);
239 space_bottom = VSpace(VSpace::MEDSKIP);
242 space_bottom = VSpace(VSpace::BIGSKIP);
245 space_bottom = VSpace(VSpace::VFILL);
248 string const length =
249 getLengthFromWidgets(dialog_->input_space_below,
250 dialog_->choice_value_space_below);
251 space_bottom = VSpace(LyXGlueLength(length));
254 if (fl_get_button (dialog_->check_space_below))
255 space_bottom.setKeep (true);
257 if (fl_get_button(dialog_->radio_align_left))
258 align = LYX_ALIGN_LEFT;
259 else if (fl_get_button(dialog_->radio_align_right))
260 align = LYX_ALIGN_RIGHT;
261 else if (fl_get_button(dialog_->radio_align_center))
262 align = LYX_ALIGN_CENTER;
264 align = LYX_ALIGN_BLOCK;
266 labelwidthstring = fl_get_input(dialog_->input_labelwidth);
267 noindent = fl_get_button(dialog_->check_noindent);
268 Spacing::Space linespacing = Spacing::Default;
269 string other_linespacing;
270 switch (fl_get_choice(dialog_->choice_linespacing)) {
271 case 1: linespacing = Spacing::Default; break;
272 case 2: linespacing = Spacing::Single; break;
273 case 3: linespacing = Spacing::Onehalf; break;
274 case 4: linespacing = Spacing::Double; break;
276 linespacing = Spacing::Other;
277 other_linespacing = fl_get_input(dialog_->input_linespacing);
281 Spacing const spacing(linespacing, other_linespacing);
282 LyXText * text(lv_->view()->getLyXText());
283 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
284 pagebreak_bottom, space_top, space_bottom, spacing,
285 align, labelwidthstring, noindent);
288 // Actually apply these settings
289 lv_->view()->update(text,
290 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
291 lv_->buffer()->markDirty();
292 setMinibuffer(lv_, _("Paragraph layout set"));
296 void FormParagraph::update()
301 // Do this first; some objects may be de/activated subsequently.
302 bc_.readOnly(lv_->buffer()->isReadonly());
304 Buffer * buf = lv_->view()->buffer();
306 /// Record the paragraph
307 par_ = getCurrentParagraph();
309 fl_set_input(dialog_->input_labelwidth,
310 par_->getLabelWidthString().c_str());
311 setEnabled(dialog_->input_labelwidth,
312 (par_->getLabelWidthString() != _("Senseless with this layout!")));
314 fl_set_button(dialog_->radio_align_right, 0);
315 fl_set_button(dialog_->radio_align_left, 0);
316 fl_set_button(dialog_->radio_align_center, 0);
317 fl_set_button(dialog_->radio_align_block, 0);
319 int align = par_->getAlign();
320 if (align == LYX_ALIGN_LAYOUT)
321 align = textclasslist.Style(buf->params.textclass,
322 par_->getLayout()).align;
325 case LYX_ALIGN_RIGHT:
326 fl_set_button(dialog_->radio_align_right, 1);
329 fl_set_button(dialog_->radio_align_left, 1);
331 case LYX_ALIGN_CENTER:
332 fl_set_button(dialog_->radio_align_center, 1);
335 fl_set_button(dialog_->radio_align_block, 1);
339 LyXAlignment alignpos =
340 textclasslist.Style(buf->params.textclass,
341 par_->getLayout()).alignpossible;
343 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
344 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
345 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
346 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
348 // no inset-text-owned paragraph may have pagebreaks
349 setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
350 setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
352 fl_set_button(dialog_->check_lines_top,
353 par_->params().lineTop());
354 fl_set_button(dialog_->check_lines_bottom,
355 par_->params().lineBottom());
356 fl_set_button(dialog_->check_pagebreaks_top,
357 par_->params().pagebreakTop());
358 fl_set_button(dialog_->check_pagebreaks_bottom,
359 par_->params().pagebreakBottom());
360 fl_set_button(dialog_->check_noindent,
361 par_->params().noindent());
364 Spacing const space = par_->params().spacing();
366 switch (space.getSpace()) {
367 default: linespacing = 1; break;
368 case Spacing::Single: linespacing = 2; break;
369 case Spacing::Onehalf: linespacing = 3; break;
370 case Spacing::Double: linespacing = 4; break;
371 case Spacing::Other: linespacing = 5; break;
374 fl_set_choice(dialog_->choice_linespacing, linespacing);
375 if (space.getSpace() == Spacing::Other) {
376 string const sp = tostr(space.getValue());
377 fl_set_input(dialog_->input_linespacing, sp.c_str());
378 setEnabled(dialog_->input_linespacing, true);
380 fl_set_input(dialog_->input_linespacing, "");
381 setEnabled(dialog_->input_linespacing, false);
384 fl_set_input (dialog_->input_space_above, "");
386 setEnabled(dialog_->input_space_above, false);
387 setEnabled(dialog_->choice_value_space_above, false);
388 switch (par_->params().spaceTop().kind()) {
390 fl_set_choice (dialog_->choice_space_above, 1);
392 case VSpace::DEFSKIP:
393 fl_set_choice (dialog_->choice_space_above, 2);
395 case VSpace::SMALLSKIP:
396 fl_set_choice (dialog_->choice_space_above, 3);
398 case VSpace::MEDSKIP:
399 fl_set_choice (dialog_->choice_space_above, 4);
401 case VSpace::BIGSKIP:
402 fl_set_choice (dialog_->choice_space_above, 5);
405 fl_set_choice (dialog_->choice_space_above, 6);
409 fl_set_choice (dialog_->choice_space_above, 7);
410 setEnabled(dialog_->input_space_above, true);
411 setEnabled(dialog_->choice_value_space_above, true);
412 bool const metric = lyxrc.default_papersize > 3;
413 string const default_unit = metric ? "cm" : "in";
414 string const length = par_->params().spaceTop().length().asString();
415 updateWidgetsFromLengthString(dialog_->input_space_above,
416 dialog_->choice_value_space_above,
417 length, default_unit);
422 fl_set_button (dialog_->check_space_above,
423 par_->params().spaceTop().keep());
424 fl_set_input (dialog_->input_space_below, "");
426 setEnabled(dialog_->input_space_below, false);
427 setEnabled(dialog_->choice_value_space_below, false);
428 switch (par_->params().spaceBottom().kind()) {
430 fl_set_choice (dialog_->choice_space_below, 1);
432 case VSpace::DEFSKIP:
433 fl_set_choice (dialog_->choice_space_below, 2);
435 case VSpace::SMALLSKIP:
436 fl_set_choice (dialog_->choice_space_below, 3);
438 case VSpace::MEDSKIP:
439 fl_set_choice (dialog_->choice_space_below, 4);
441 case VSpace::BIGSKIP:
442 fl_set_choice (dialog_->choice_space_below, 5);
445 fl_set_choice (dialog_->choice_space_below, 6);
449 fl_set_choice (dialog_->choice_space_below, 7);
450 setEnabled(dialog_->input_space_below, true);
451 setEnabled(dialog_->choice_value_space_below, true);
452 bool const metric = lyxrc.default_papersize > 3;
453 string const default_unit = metric ? "cm" : "in";
454 string const length =
455 par_->params().spaceBottom().length().asString();
456 updateWidgetsFromLengthString(dialog_->input_space_below,
457 dialog_->choice_value_space_below,
458 length, default_unit);
463 fl_set_button(dialog_->check_space_below,
464 par_->params().spaceBottom().keep());
465 fl_set_button(dialog_->check_noindent,
466 par_->params().noindent());
470 bool FormParagraph::input(FL_OBJECT * ob, long)
474 fl_hide_object(dialog_->text_warning);
476 // First check the buttons which are exclusive and you have to
477 // check only the actuall de/activated button.
479 // "Synchronize" the choices and input fields, making it
480 // impossible to commit senseless data.
482 if (ob == dialog_->choice_space_above) {
483 if (fl_get_choice (dialog_->choice_space_above) != 7) {
484 fl_set_input (dialog_->input_space_above, "");
485 setEnabled (dialog_->input_space_above, false);
486 setEnabled (dialog_->choice_value_space_above, false);
488 setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
489 setEnabled (dialog_->choice_value_space_above, !lv_->buffer()->isReadonly());
490 bool const metric = lyxrc.default_papersize > 3;
491 int const default_unit = metric ? 8 : 9;
492 if (strip(fl_get_input(dialog_->input_space_above)).empty())
493 fl_set_choice(dialog_->choice_value_space_above,
497 if (ob == dialog_->choice_space_below) {
498 if (fl_get_choice (dialog_->choice_space_below) != 7) {
499 fl_set_input (dialog_->input_space_below, "");
500 setEnabled (dialog_->input_space_below, false);
501 setEnabled (dialog_->choice_value_space_below, false);
503 setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
504 setEnabled (dialog_->choice_value_space_below, !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_below)).empty())
508 fl_set_choice(dialog_->choice_value_space_below,
514 // warnings if input is senseless
516 string input = fl_get_input (dialog_->input_space_above);
517 bool invalid = false;
519 if (fl_get_choice(dialog_->choice_space_above)==7)
520 invalid = !input.empty() && !isValidGlueLength(input) && !isStrDbl(input);
522 input = fl_get_input (dialog_->input_space_below);
524 if (fl_get_choice(dialog_->choice_space_below)==7)
526 || (!input.empty() && !isValidGlueLength(input) && !isStrDbl(input));
528 if (ob == dialog_->input_space_above || ob == dialog_->input_space_below) {
530 fl_set_object_label(dialog_->text_warning,
531 _("Warning: Invalid Length (valid example: 10mm)"));
532 fl_show_object(dialog_->text_warning);
535 fl_hide_object(dialog_->text_warning);
540 if (fl_get_choice (dialog_->choice_linespacing) == 5)
541 setEnabled (dialog_->input_linespacing, true);
543 setEnabled (dialog_->input_linespacing, false);
544 fl_set_input (dialog_->input_linespacing, "");
547 double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));
549 if (fl_get_choice (dialog_->choice_linespacing) == 5
550 && int(spacing) == 0)