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
66 return lv_->view()->getLyXText()->cursor.par();
70 void FormParagraph::changedParagraph()
72 /// Record the paragraph
73 Paragraph const * const p = getCurrentParagraph();
74 if (p == 0 || p == par_)
77 // For now don't bother checking if the params are different,
78 // just activate the Apply button
83 void FormParagraph::redraw()
85 if( form() && form()->visible )
86 fl_redraw_form( form() );
90 FL_FORM * FormParagraph::form() const
92 if (dialog_.get()) return dialog_->form;
97 void FormParagraph::build()
100 dialog_.reset(build_paragraph());
102 fl_addto_choice(dialog_->choice_space_above,
103 _(" None | Defskip | Smallskip "
104 "| Medskip | Bigskip | VFill | Length "));
105 fl_addto_choice(dialog_->choice_space_below,
106 _(" None | Defskip | Smallskip "
107 "| Medskip | Bigskip | VFill | Length "));
109 fl_addto_choice(dialog_->choice_linespacing,
110 _(" Default | Single | OneHalf | Double | Other "));
112 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
113 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
114 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
115 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
116 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
117 fl_set_input_filter(dialog_->input_space_above, fl_float_filter);
118 fl_set_input_filter(dialog_->input_space_below, fl_float_filter);
120 // Create the contents of the unit choices
121 // Don't include the "%" terms...
122 std::vector<string> units_vec = getLatexUnits();
123 for (std::vector<string>::iterator it = units_vec.begin();
124 it != units_vec.end(); ++it) {
125 if (contains(*it, "%"))
126 it = units_vec.erase(it, it+1) - 1;
128 string units = getStringFromVector(units_vec, "|");
130 fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
131 fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
133 // Manage the ok, apply, restore and cancel/close buttons
134 bc_.setOK(dialog_->button_ok);
135 bc_.setApply(dialog_->button_apply);
136 bc_.setCancel(dialog_->button_cancel);
137 bc_.setRestore(dialog_->button_restore);
139 bc_.addReadOnly (dialog_->group_radio_alignment);
140 // bc_.addReadOnly (dialog_->radio_align_right);
141 // bc_.addReadOnly (dialog_->radio_align_left);
142 // bc_.addReadOnly (dialog_->radio_align_block);
143 // bc_.addReadOnly (dialog_->radio_align_center);
144 bc_.addReadOnly (dialog_->check_lines_top);
145 bc_.addReadOnly (dialog_->check_lines_bottom);
146 bc_.addReadOnly (dialog_->check_pagebreaks_top);
147 bc_.addReadOnly (dialog_->check_pagebreaks_bottom);
148 bc_.addReadOnly (dialog_->choice_space_above);
149 bc_.addReadOnly (dialog_->input_space_above);
150 bc_.addReadOnly (dialog_->check_space_above);
151 bc_.addReadOnly (dialog_->choice_space_below);
152 bc_.addReadOnly (dialog_->input_space_below);
153 bc_.addReadOnly (dialog_->check_space_below);
154 bc_.addReadOnly (dialog_->choice_linespacing);
155 bc_.addReadOnly (dialog_->input_linespacing);
156 bc_.addReadOnly (dialog_->check_noindent);
157 bc_.addReadOnly (dialog_->input_labelwidth);
161 void FormParagraph::apply()
163 if (!lv_->view()->available() || !dialog_.get())
166 VSpace space_top, space_bottom;
168 string labelwidthstring;
171 // If a vspace kind is "Length" but there's no text in
172 // the input field, reset the kind to "None".
173 if ((fl_get_choice (dialog_->choice_space_above) == 7) &&
174 !*(fl_get_input (dialog_->input_space_above)))
176 fl_set_choice (dialog_->choice_space_above, 1);
178 if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
179 !*(fl_get_input (dialog_->input_space_below)))
181 fl_set_choice (dialog_->choice_space_below, 1);
184 bool line_top = fl_get_button(dialog_->check_lines_top);
185 bool line_bottom = fl_get_button(dialog_->check_lines_bottom);
186 bool pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
187 bool pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
189 switch (fl_get_choice (dialog_->choice_space_above)) {
191 space_top = VSpace(VSpace::NONE);
194 space_top = VSpace(VSpace::DEFSKIP);
197 space_top = VSpace(VSpace::SMALLSKIP);
200 space_top = VSpace(VSpace::MEDSKIP);
203 space_top = VSpace(VSpace::BIGSKIP);
206 space_top = VSpace(VSpace::VFILL);
210 string const length =
211 getLengthFromWidgets(dialog_->input_space_above,
212 dialog_->choice_value_space_above);
214 VSpace(LyXGlueLength(length));
219 if (fl_get_button (dialog_->check_space_above))
220 space_top.setKeep (true);
221 switch (fl_get_choice (dialog_->choice_space_below)) {
223 space_bottom = VSpace(VSpace::NONE);
226 space_bottom = VSpace(VSpace::DEFSKIP);
229 space_bottom = VSpace(VSpace::SMALLSKIP);
232 space_bottom = VSpace(VSpace::MEDSKIP);
235 space_bottom = VSpace(VSpace::BIGSKIP);
238 space_bottom = VSpace(VSpace::VFILL);
241 string const length =
242 getLengthFromWidgets(dialog_->input_space_below,
243 dialog_->choice_value_space_below);
244 space_bottom = VSpace(LyXGlueLength(length));
247 if (fl_get_button (dialog_->check_space_below))
248 space_bottom.setKeep (true);
250 if (fl_get_button(dialog_->radio_align_left))
251 align = LYX_ALIGN_LEFT;
252 else if (fl_get_button(dialog_->radio_align_right))
253 align = LYX_ALIGN_RIGHT;
254 else if (fl_get_button(dialog_->radio_align_center))
255 align = LYX_ALIGN_CENTER;
257 align = LYX_ALIGN_BLOCK;
259 labelwidthstring = fl_get_input(dialog_->input_labelwidth);
260 noindent = fl_get_button(dialog_->check_noindent);
261 Spacing::Space linespacing = Spacing::Default;
262 string other_linespacing;
263 switch (fl_get_choice(dialog_->choice_linespacing)) {
264 case 1: linespacing = Spacing::Default; break;
265 case 2: linespacing = Spacing::Single; break;
266 case 3: linespacing = Spacing::Onehalf; break;
267 case 4: linespacing = Spacing::Double; break;
269 linespacing = Spacing::Other;
270 other_linespacing = fl_get_input(dialog_->input_linespacing);
274 Spacing const spacing(linespacing, other_linespacing);
275 LyXText * text(lv_->view()->getLyXText());
276 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
277 pagebreak_bottom, space_top, space_bottom, spacing,
278 align, labelwidthstring, noindent);
281 // Actually apply these settings
282 lv_->view()->update(lv_->view()->text,
283 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
284 lv_->buffer()->markDirty();
285 setMinibuffer(lv_, _("Paragraph layout set"));
289 void FormParagraph::update()
294 // Do this first; some objects may be de/activated subsequently.
295 bc_.readOnly(lv_->buffer()->isReadonly());
297 Buffer * buf = lv_->view()->buffer();
299 /// Record the paragraph
300 par_ = getCurrentParagraph();
302 fl_set_input(dialog_->input_labelwidth,
303 par_->getLabelWidthString().c_str());
304 setEnabled(dialog_->input_labelwidth,
305 (par_->getLabelWidthString() != _("Senseless with this layout!")));
307 fl_set_button(dialog_->radio_align_right, 0);
308 fl_set_button(dialog_->radio_align_left, 0);
309 fl_set_button(dialog_->radio_align_center, 0);
310 fl_set_button(dialog_->radio_align_block, 0);
312 int align = par_->getAlign();
313 if (align == LYX_ALIGN_LAYOUT)
314 align = textclasslist.Style(buf->params.textclass,
315 par_->getLayout()).align;
318 case LYX_ALIGN_RIGHT:
319 fl_set_button(dialog_->radio_align_right, 1);
322 fl_set_button(dialog_->radio_align_left, 1);
324 case LYX_ALIGN_CENTER:
325 fl_set_button(dialog_->radio_align_center, 1);
328 fl_set_button(dialog_->radio_align_block, 1);
332 LyXAlignment alignpos =
333 textclasslist.Style(buf->params.textclass,
334 par_->getLayout()).alignpossible;
336 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
337 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
338 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
339 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
341 // no inset-text-owned paragraph may have pagebreaks
342 setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
343 setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
345 fl_set_button(dialog_->check_lines_top,
346 par_->params().lineTop());
347 fl_set_button(dialog_->check_lines_bottom,
348 par_->params().lineBottom());
349 fl_set_button(dialog_->check_pagebreaks_top,
350 par_->params().pagebreakTop());
351 fl_set_button(dialog_->check_pagebreaks_bottom,
352 par_->params().pagebreakBottom());
353 fl_set_button(dialog_->check_noindent,
354 par_->params().noindent());
357 Spacing const space = par_->params().spacing();
359 switch (space.getSpace()) {
360 default: linespacing = 1; break;
361 case Spacing::Single: linespacing = 2; break;
362 case Spacing::Onehalf: linespacing = 3; break;
363 case Spacing::Double: linespacing = 4; break;
364 case Spacing::Other: linespacing = 5; break;
367 fl_set_choice(dialog_->choice_linespacing, linespacing);
368 if (space.getSpace() == Spacing::Other) {
369 string const sp = tostr(space.getValue());
370 fl_set_input(dialog_->input_linespacing, sp.c_str());
371 setEnabled(dialog_->input_linespacing, true);
373 fl_set_input(dialog_->input_linespacing, "");
374 setEnabled(dialog_->input_linespacing, false);
377 fl_set_input (dialog_->input_space_above, "");
379 setEnabled(dialog_->input_space_above, false);
380 setEnabled(dialog_->choice_value_space_above, false);
381 switch (par_->params().spaceTop().kind()) {
383 fl_set_choice (dialog_->choice_space_above, 1);
385 case VSpace::DEFSKIP:
386 fl_set_choice (dialog_->choice_space_above, 2);
388 case VSpace::SMALLSKIP:
389 fl_set_choice (dialog_->choice_space_above, 3);
391 case VSpace::MEDSKIP:
392 fl_set_choice (dialog_->choice_space_above, 4);
394 case VSpace::BIGSKIP:
395 fl_set_choice (dialog_->choice_space_above, 5);
398 fl_set_choice (dialog_->choice_space_above, 6);
402 setEnabled(dialog_->input_space_above, true);
403 setEnabled(dialog_->choice_value_space_above, true);
404 string const default_unit = "cm";
405 string const length = par_->params().spaceTop().length().asString();
406 updateWidgetsFromLengthString(dialog_->input_space_above,
407 dialog_->choice_value_space_above,
408 length, default_unit);
413 fl_set_button (dialog_->check_space_above,
414 par_->params().spaceTop().keep());
415 fl_set_input (dialog_->input_space_below, "");
417 setEnabled(dialog_->input_space_below, false);
418 setEnabled(dialog_->choice_value_space_below, false);
419 switch (par_->params().spaceBottom().kind()) {
421 fl_set_choice (dialog_->choice_space_below, 1);
423 case VSpace::DEFSKIP:
424 fl_set_choice (dialog_->choice_space_below, 2);
426 case VSpace::SMALLSKIP:
427 fl_set_choice (dialog_->choice_space_below, 3);
429 case VSpace::MEDSKIP:
430 fl_set_choice (dialog_->choice_space_below, 4);
432 case VSpace::BIGSKIP:
433 fl_set_choice (dialog_->choice_space_below, 5);
436 fl_set_choice (dialog_->choice_space_below, 6);
440 setEnabled(dialog_->input_space_below, true);
441 setEnabled(dialog_->choice_value_space_below, true);
442 string const default_unit = "cm";
443 string const length =
444 par_->params().spaceBottom().length().asString();
445 updateWidgetsFromLengthString(dialog_->input_space_below,
446 dialog_->choice_value_space_below,
447 length, default_unit);
452 fl_set_button(dialog_->check_space_below,
453 par_->params().spaceBottom().keep());
454 fl_set_button(dialog_->check_noindent,
455 par_->params().noindent());
459 bool FormParagraph::input(FL_OBJECT * ob, long)
463 fl_hide_object(dialog_->text_warning);
465 // First check the buttons which are exclusive and you have to
466 // check only the actuall de/activated button.
468 // "Synchronize" the choices and input fields, making it
469 // impossible to commit senseless data.
471 if (ob == dialog_->choice_space_above) {
472 if (fl_get_choice (dialog_->choice_space_above) != 7) {
473 fl_set_input (dialog_->input_space_above, "");
474 setEnabled (dialog_->input_space_above, false);
475 setEnabled (dialog_->choice_value_space_above, false);
477 setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
478 setEnabled (dialog_->choice_value_space_above, !lv_->buffer()->isReadonly());
479 int const default_unit = 8;
480 if (strip(fl_get_input(dialog_->input_space_above)).empty())
481 fl_set_choice(dialog_->choice_value_space_above,
485 if (ob == dialog_->choice_space_below) {
486 if (fl_get_choice (dialog_->choice_space_below) != 7) {
487 fl_set_input (dialog_->input_space_below, "");
488 setEnabled (dialog_->input_space_below, false);
489 setEnabled (dialog_->choice_value_space_below, false);
491 setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
492 setEnabled (dialog_->choice_value_space_below, !lv_->buffer()->isReadonly());
493 int const default_unit = 8;
494 if (strip(fl_get_input(dialog_->input_space_below)).empty())
495 fl_set_choice(dialog_->choice_value_space_below,
500 if (fl_get_choice (dialog_->choice_linespacing) == 5)
501 setEnabled (dialog_->input_linespacing, true);
503 setEnabled (dialog_->input_linespacing, false);
504 fl_set_input (dialog_->input_linespacing, "");
507 double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));
509 if (fl_get_choice (dialog_->choice_linespacing) == 5
510 && int(spacing) == 0)