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"
23 #include "support/lstrings.h"
28 #include "xforms_helpers.h"
29 #include "BufferView.h"
31 #include "ParagraphParameters.h"
32 #include "input_validators.h"
33 #include "helper_funcs.h"
35 using Liason::setMinibuffer;
39 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
40 : FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
42 // let the dialog be shown
43 // This is a permanent connection so we won't bother
44 // storing a copy because we won't be disconnecting.
45 d->showParagraph.connect(slot(this, &FormParagraph::show));
49 void FormParagraph::connect()
51 cp_ = d_->updateParagraph
52 .connect(slot(this, &FormParagraph::changedParagraph));
53 FormBaseBD::connect();
57 void FormParagraph::disconnect()
60 FormBaseBD::disconnect();
64 Paragraph const * FormParagraph::getCurrentParagraph() const
68 if (lv_->view()->theLockingInset())
69 text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
71 text = lv_->view()->text;
72 return text->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,
84 // just activate the Apply button
89 void FormParagraph::redraw()
91 if( form() && form()->visible )
92 fl_redraw_form( form() );
96 FL_FORM * FormParagraph::form() const
98 if (dialog_.get()) return dialog_->form;
103 void FormParagraph::build()
106 dialog_.reset(build_paragraph());
108 fl_addto_choice(dialog_->choice_space_above,
109 _(" None | Defskip | Smallskip "
110 "| Medskip | Bigskip | VFill | Length "));
111 fl_addto_choice(dialog_->choice_space_below,
112 _(" None | Defskip | Smallskip "
113 "| Medskip | Bigskip | VFill | Length "));
115 fl_addto_choice(dialog_->choice_linespacing,
116 _(" Default | Single | OneHalf | Double | Other "));
118 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
119 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
120 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
121 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
122 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
123 fl_set_input_filter(dialog_->input_space_above, fl_unsigned_float_filter);
124 fl_set_input_filter(dialog_->input_space_below, fl_unsigned_float_filter);
126 // Create the contents of the unit choices
127 // Don't include the "%" terms...
128 std::vector<string> units_vec = getLatexUnits();
129 for (std::vector<string>::iterator it = units_vec.begin();
130 it != units_vec.end(); ++it) {
131 if (contains(*it, "%"))
132 it = units_vec.erase(it, it+1) - 1;
134 string units = getStringFromVector(units_vec, "|");
136 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
137 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
139 // Manage the ok, apply, restore and cancel/close buttons
140 bc_.setOK(dialog_->button_ok);
141 bc_.setApply(dialog_->button_apply);
142 bc_.setCancel(dialog_->button_cancel);
143 bc_.setRestore(dialog_->button_restore);
145 bc_.addReadOnly (dialog_->group_radio_alignment);
146 // bc_.addReadOnly (dialog_->radio_align_right);
147 // bc_.addReadOnly (dialog_->radio_align_left);
148 // bc_.addReadOnly (dialog_->radio_align_block);
149 // bc_.addReadOnly (dialog_->radio_align_center);
150 bc_.addReadOnly (dialog_->check_lines_top);
151 bc_.addReadOnly (dialog_->check_lines_bottom);
152 bc_.addReadOnly (dialog_->check_pagebreaks_top);
153 bc_.addReadOnly (dialog_->check_pagebreaks_bottom);
154 bc_.addReadOnly (dialog_->choice_space_above);
155 bc_.addReadOnly (dialog_->input_space_above);
156 bc_.addReadOnly (dialog_->check_space_above);
157 bc_.addReadOnly (dialog_->choice_space_below);
158 bc_.addReadOnly (dialog_->input_space_below);
159 bc_.addReadOnly (dialog_->check_space_below);
160 bc_.addReadOnly (dialog_->choice_linespacing);
161 bc_.addReadOnly (dialog_->input_linespacing);
162 bc_.addReadOnly (dialog_->check_noindent);
163 bc_.addReadOnly (dialog_->input_labelwidth);
167 void FormParagraph::apply()
169 if (!lv_->view()->available() || !dialog_.get())
172 VSpace space_top, space_bottom;
174 string labelwidthstring;
177 // If a vspace kind is "Length" but there's no text in
178 // the input field, reset the kind to "None".
179 if ((fl_get_choice (dialog_->choice_space_above) == 7) &&
180 !*(fl_get_input (dialog_->input_space_above)))
182 fl_set_choice (dialog_->choice_space_above, 1);
184 if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
185 !*(fl_get_input (dialog_->input_space_below)))
187 fl_set_choice (dialog_->choice_space_below, 1);
190 bool line_top = fl_get_button(dialog_->check_lines_top);
191 bool line_bottom = fl_get_button(dialog_->check_lines_bottom);
192 bool pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
193 bool pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
195 switch (fl_get_choice (dialog_->choice_space_above)) {
197 space_top = VSpace(VSpace::NONE);
200 space_top = VSpace(VSpace::DEFSKIP);
203 space_top = VSpace(VSpace::SMALLSKIP);
206 space_top = VSpace(VSpace::MEDSKIP);
209 space_top = VSpace(VSpace::BIGSKIP);
212 space_top = VSpace(VSpace::VFILL);
216 string const length =
217 getLengthFromWidgets(dialog_->input_space_above,
218 dialog_->choice_value_space_above);
220 VSpace(LyXGlueLength(length));
225 if (fl_get_button (dialog_->check_space_above))
226 space_top.setKeep (true);
227 switch (fl_get_choice (dialog_->choice_space_below)) {
229 space_bottom = VSpace(VSpace::NONE);
232 space_bottom = VSpace(VSpace::DEFSKIP);
235 space_bottom = VSpace(VSpace::SMALLSKIP);
238 space_bottom = VSpace(VSpace::MEDSKIP);
241 space_bottom = VSpace(VSpace::BIGSKIP);
244 space_bottom = VSpace(VSpace::VFILL);
248 VSpace(LyXGlueLength(fl_get_input(dialog_->input_space_below)));
251 if (fl_get_button (dialog_->check_space_below))
252 space_bottom.setKeep (true);
254 if (fl_get_button(dialog_->radio_align_left))
255 align = LYX_ALIGN_LEFT;
256 else if (fl_get_button(dialog_->radio_align_right))
257 align = LYX_ALIGN_RIGHT;
258 else if (fl_get_button(dialog_->radio_align_center))
259 align = LYX_ALIGN_CENTER;
261 align = LYX_ALIGN_BLOCK;
263 labelwidthstring = fl_get_input(dialog_->input_labelwidth);
264 noindent = fl_get_button(dialog_->check_noindent);
265 Spacing::Space linespacing = Spacing::Default;
266 string other_linespacing;
267 switch (fl_get_choice(dialog_->choice_linespacing)) {
268 case 1: linespacing = Spacing::Default; break;
269 case 2: linespacing = Spacing::Single; break;
270 case 3: linespacing = Spacing::Onehalf; break;
271 case 4: linespacing = Spacing::Double; break;
273 linespacing = Spacing::Other;
274 other_linespacing = fl_get_input(dialog_->input_linespacing);
278 Spacing const spacing(linespacing, other_linespacing);
280 if (lv_->view()->theLockingInset())
281 text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
283 text = lv_->view()->text;
284 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
285 pagebreak_bottom, space_top, space_bottom, spacing,
286 align, labelwidthstring, noindent);
289 // Actually apply these settings
290 lv_->view()->update(lv_->view()->text,
291 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
292 lv_->buffer()->markDirty();
293 setMinibuffer(lv_, _("Paragraph layout set"));
297 void FormParagraph::update()
302 // Do this first; some objects may be de/activated subsequently.
303 bc_.readOnly(lv_->buffer()->isReadonly());
305 Buffer * buf = lv_->view()->buffer();
307 /// Record the paragraph
308 par_ = getCurrentParagraph();
310 fl_set_input(dialog_->input_labelwidth,
311 par_->getLabelWidthString().c_str());
312 setEnabled(dialog_->input_labelwidth,
313 (par_->getLabelWidthString() != _("Senseless with this layout!")));
315 fl_set_button(dialog_->radio_align_right, 0);
316 fl_set_button(dialog_->radio_align_left, 0);
317 fl_set_button(dialog_->radio_align_center, 0);
318 fl_set_button(dialog_->radio_align_block, 0);
320 int align = par_->getAlign();
321 if (align == LYX_ALIGN_LAYOUT)
322 align = textclasslist.Style(buf->params.textclass,
323 par_->getLayout()).align;
326 case LYX_ALIGN_RIGHT:
327 fl_set_button(dialog_->radio_align_right, 1);
330 fl_set_button(dialog_->radio_align_left, 1);
332 case LYX_ALIGN_CENTER:
333 fl_set_button(dialog_->radio_align_center, 1);
336 fl_set_button(dialog_->radio_align_block, 1);
340 LyXAlignment alignpos =
341 textclasslist.Style(buf->params.textclass,
342 par_->getLayout()).alignpossible;
344 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
345 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
346 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
347 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
349 fl_set_button(dialog_->check_lines_top,
350 par_->params().lineTop());
351 fl_set_button(dialog_->check_lines_bottom,
352 par_->params().lineBottom());
353 fl_set_button(dialog_->check_pagebreaks_top,
354 par_->params().pagebreakTop());
355 fl_set_button(dialog_->check_pagebreaks_bottom,
356 par_->params().pagebreakBottom());
357 fl_set_button(dialog_->check_noindent,
358 par_->params().noindent());
361 Spacing const space = par_->params().spacing();
363 switch (space.getSpace()) {
364 default: linespacing = 1; break;
365 case Spacing::Single: linespacing = 2; break;
366 case Spacing::Onehalf: linespacing = 3; break;
367 case Spacing::Double: linespacing = 4; break;
368 case Spacing::Other: linespacing = 5; break;
371 fl_set_choice(dialog_->choice_linespacing, linespacing);
372 if (space.getSpace() == Spacing::Other) {
373 string const sp = tostr(space.getValue());
374 fl_set_input(dialog_->input_linespacing, sp.c_str());
375 setEnabled(dialog_->input_linespacing, true);
377 fl_set_input(dialog_->input_linespacing, "");
378 setEnabled(dialog_->input_linespacing, false);
381 fl_set_input (dialog_->input_space_above, "");
383 setEnabled(dialog_->input_space_above, false);
384 setEnabled(dialog_->choice_value_space_above, false);
385 switch (par_->params().spaceTop().kind()) {
387 fl_set_choice (dialog_->choice_space_above, 1);
389 case VSpace::DEFSKIP:
390 fl_set_choice (dialog_->choice_space_above, 2);
392 case VSpace::SMALLSKIP:
393 fl_set_choice (dialog_->choice_space_above, 3);
395 case VSpace::MEDSKIP:
396 fl_set_choice (dialog_->choice_space_above, 4);
398 case VSpace::BIGSKIP:
399 fl_set_choice (dialog_->choice_space_above, 5);
402 fl_set_choice (dialog_->choice_space_above, 6);
406 setEnabled(dialog_->input_space_above, true);
407 setEnabled(dialog_->choice_value_space_above, true);
408 string const default_unit = "cm";
409 string const length = par_->params().spaceTop().length().asString();
410 updateWidgetsFromLengthString(dialog_->input_space_above,
411 dialog_->choice_value_space_above,
412 length, default_unit);
417 fl_set_button (dialog_->check_space_above,
418 par_->params().spaceTop().keep());
419 fl_set_input (dialog_->input_space_below, "");
421 setEnabled(dialog_->input_space_below, false);
422 setEnabled(dialog_->choice_value_space_below, false);
423 switch (par_->params().spaceBottom().kind()) {
425 fl_set_choice (dialog_->choice_space_below, 1);
427 case VSpace::DEFSKIP:
428 fl_set_choice (dialog_->choice_space_below, 2);
430 case VSpace::SMALLSKIP:
431 fl_set_choice (dialog_->choice_space_below, 3);
433 case VSpace::MEDSKIP:
434 fl_set_choice (dialog_->choice_space_below, 4);
436 case VSpace::BIGSKIP:
437 fl_set_choice (dialog_->choice_space_below, 5);
440 fl_set_choice (dialog_->choice_space_below, 6);
444 setEnabled(dialog_->input_space_below, true);
445 setEnabled(dialog_->choice_value_space_below, true);
446 string const default_unit = "cm";
447 string const length =
448 par_->params().spaceBottom().length().asString();
449 updateWidgetsFromLengthString(dialog_->input_space_below,
450 dialog_->choice_value_space_below,
451 length, default_unit);
456 fl_set_button(dialog_->check_space_below,
457 par_->params().spaceBottom().keep());
458 fl_set_button(dialog_->check_noindent,
459 par_->params().noindent());
463 bool FormParagraph::input(FL_OBJECT * ob, long)
467 fl_hide_object(dialog_->text_warning);
469 // First check the buttons which are exclusive and you have to
470 // check only the actuall de/activated button.
472 // "Synchronize" the choices and input fields, making it
473 // impossible to commit senseless data.
475 if (ob == dialog_->choice_space_above) {
476 if (fl_get_choice (dialog_->choice_space_above) != 7) {
477 fl_set_input (dialog_->input_space_above, "");
478 setEnabled (dialog_->input_space_above, false);
479 setEnabled (dialog_->choice_value_space_above, false);
481 setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
482 setEnabled (dialog_->choice_value_space_above, !lv_->buffer()->isReadonly());
483 int const default_unit = 8;
484 if (strip(fl_get_input(dialog_->input_space_above)).empty())
485 fl_set_choice(dialog_->choice_value_space_above,
489 if (ob == dialog_->choice_space_below) {
490 if (fl_get_choice (dialog_->choice_space_below) != 7) {
491 fl_set_input (dialog_->input_space_below, "");
492 setEnabled (dialog_->input_space_below, false);
493 setEnabled (dialog_->choice_value_space_below, false);
495 setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
496 setEnabled (dialog_->choice_value_space_below, !lv_->buffer()->isReadonly());
497 int const default_unit = 8;
498 if (strip(fl_get_input(dialog_->input_space_below)).empty())
499 fl_set_choice(dialog_->choice_value_space_below,
504 if (fl_get_choice (dialog_->choice_linespacing) == 5)
505 setEnabled (dialog_->input_linespacing, true);
507 setEnabled (dialog_->input_linespacing, false);
508 fl_set_input (dialog_->input_linespacing, "");
511 double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));
513 if (fl_get_choice (dialog_->choice_linespacing) == 5
514 && int(spacing) == 0)