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"
34 using Liason::setMinibuffer;
38 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
39 : FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
41 // let the dialog be shown
42 // This is a permanent connection so we won't bother
43 // storing a copy because we won't be disconnecting.
44 d->showParagraph.connect(slot(this, &FormParagraph::show));
48 void FormParagraph::connect()
50 cp_ = d_->updateParagraph
51 .connect(slot(this, &FormParagraph::changedParagraph));
52 FormBaseBD::connect();
56 void FormParagraph::disconnect()
59 FormBaseBD::disconnect();
63 Paragraph const * FormParagraph::getCurrentParagraph() const
67 if (lv_->view()->theLockingInset())
68 text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
70 text = lv_->view()->text;
71 return text->cursor.par();
75 void FormParagraph::changedParagraph()
77 /// Record the paragraph
78 Paragraph const * const p = getCurrentParagraph();
79 if (p == 0 || p == par_)
82 // For now don't bother checking if the params are different,
83 // just activate the Apply button
88 void FormParagraph::redraw()
90 if( form() && form()->visible )
91 fl_redraw_form( form() );
95 FL_FORM * FormParagraph::form() const
97 if (dialog_.get()) return dialog_->form;
102 void FormParagraph::build()
105 dialog_.reset(build_paragraph());
107 fl_addto_choice(dialog_->choice_space_above,
108 _(" None | Defskip | Smallskip "
109 "| Medskip | Bigskip | VFill | Length "));
110 fl_addto_choice(dialog_->choice_space_below,
111 _(" None | Defskip | Smallskip "
112 "| Medskip | Bigskip | VFill | Length "));
114 fl_addto_choice(dialog_->choice_linespacing,
115 _(" Default | Single | OneHalf | Double | Other "));
117 fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
118 fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
119 fl_set_input_return(dialog_->input_labelwidth, FL_RETURN_CHANGED);
120 fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
121 fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
123 // Manage the ok, apply, restore and cancel/close buttons
124 bc_.setOK(dialog_->button_ok);
125 bc_.setApply(dialog_->button_apply);
126 bc_.setCancel(dialog_->button_cancel);
127 bc_.setRestore(dialog_->button_restore);
129 bc_.addReadOnly (dialog_->group_radio_alignment);
130 // bc_.addReadOnly (dialog_->radio_align_right);
131 // bc_.addReadOnly (dialog_->radio_align_left);
132 // bc_.addReadOnly (dialog_->radio_align_block);
133 // bc_.addReadOnly (dialog_->radio_align_center);
134 bc_.addReadOnly (dialog_->check_lines_top);
135 bc_.addReadOnly (dialog_->check_lines_bottom);
136 bc_.addReadOnly (dialog_->check_pagebreaks_top);
137 bc_.addReadOnly (dialog_->check_pagebreaks_bottom);
138 bc_.addReadOnly (dialog_->choice_space_above);
139 bc_.addReadOnly (dialog_->input_space_above);
140 bc_.addReadOnly (dialog_->check_space_above);
141 bc_.addReadOnly (dialog_->choice_space_below);
142 bc_.addReadOnly (dialog_->input_space_below);
143 bc_.addReadOnly (dialog_->check_space_below);
144 bc_.addReadOnly (dialog_->choice_linespacing);
145 bc_.addReadOnly (dialog_->input_linespacing);
146 bc_.addReadOnly (dialog_->check_noindent);
147 bc_.addReadOnly (dialog_->input_labelwidth);
151 void FormParagraph::apply()
153 if (!lv_->view()->available() || !dialog_.get())
156 VSpace space_top, space_bottom;
158 string labelwidthstring;
161 // If a vspace kind is "Length" but there's no text in
162 // the input field, reset the kind to "None".
163 if ((fl_get_choice (dialog_->choice_space_above) == 7) &&
164 !*(fl_get_input (dialog_->input_space_above)))
166 fl_set_choice (dialog_->choice_space_above, 1);
168 if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
169 !*(fl_get_input (dialog_->input_space_below)))
171 fl_set_choice (dialog_->choice_space_below, 1);
174 bool line_top = fl_get_button(dialog_->check_lines_top);
175 bool line_bottom = fl_get_button(dialog_->check_lines_bottom);
176 bool pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
177 bool pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
179 switch (fl_get_choice (dialog_->choice_space_above)) {
181 space_top = VSpace(VSpace::NONE);
184 space_top = VSpace(VSpace::DEFSKIP);
187 space_top = VSpace(VSpace::SMALLSKIP);
190 space_top = VSpace(VSpace::MEDSKIP);
193 space_top = VSpace(VSpace::BIGSKIP);
196 space_top = VSpace(VSpace::VFILL);
200 VSpace(LyXGlueLength(fl_get_input(dialog_->input_space_above)));
203 if (fl_get_button (dialog_->check_space_above))
204 space_top.setKeep (true);
205 switch (fl_get_choice (dialog_->choice_space_below)) {
207 space_bottom = VSpace(VSpace::NONE);
210 space_bottom = VSpace(VSpace::DEFSKIP);
213 space_bottom = VSpace(VSpace::SMALLSKIP);
216 space_bottom = VSpace(VSpace::MEDSKIP);
219 space_bottom = VSpace(VSpace::BIGSKIP);
222 space_bottom = VSpace(VSpace::VFILL);
226 VSpace(LyXGlueLength(fl_get_input(dialog_->input_space_below)));
229 if (fl_get_button (dialog_->check_space_below))
230 space_bottom.setKeep (true);
232 if (fl_get_button(dialog_->radio_align_left))
233 align = LYX_ALIGN_LEFT;
234 else if (fl_get_button(dialog_->radio_align_right))
235 align = LYX_ALIGN_RIGHT;
236 else if (fl_get_button(dialog_->radio_align_center))
237 align = LYX_ALIGN_CENTER;
239 align = LYX_ALIGN_BLOCK;
241 labelwidthstring = fl_get_input(dialog_->input_labelwidth);
242 noindent = fl_get_button(dialog_->check_noindent);
243 Spacing::Space linespacing;
244 string other_linespacing;
245 switch (fl_get_choice(dialog_->choice_linespacing)) {
246 case 1: linespacing = Spacing::Default; break;
247 case 2: linespacing = Spacing::Single; break;
248 case 3: linespacing = Spacing::Onehalf; break;
249 case 4: linespacing = Spacing::Double; break;
251 linespacing = Spacing::Other;
252 other_linespacing = fl_get_input(dialog_->input_linespacing);
256 Spacing const spacing(linespacing, other_linespacing);
258 if (lv_->view()->theLockingInset())
259 text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
261 text = lv_->view()->text;
262 text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
263 pagebreak_bottom, space_top, space_bottom, spacing,
264 align, labelwidthstring, noindent);
267 // Actually apply these settings
268 lv_->view()->update(lv_->view()->text,
269 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
270 lv_->buffer()->markDirty();
271 setMinibuffer(lv_, _("Paragraph layout set"));
275 void FormParagraph::update()
280 // Do this first; some objects may be de/activated subsequently.
281 bc_.readOnly(lv_->buffer()->isReadonly());
283 Buffer * buf = lv_->view()->buffer();
285 /// Record the paragraph
286 par_ = getCurrentParagraph();
288 fl_set_input(dialog_->input_labelwidth,
289 par_->getLabelWidthString().c_str());
290 fl_set_button(dialog_->radio_align_right, 0);
291 fl_set_button(dialog_->radio_align_left, 0);
292 fl_set_button(dialog_->radio_align_center, 0);
293 fl_set_button(dialog_->radio_align_block, 0);
295 int align = par_->getAlign();
296 if (align == LYX_ALIGN_LAYOUT)
297 align = textclasslist.Style(buf->params.textclass,
298 par_->getLayout()).align;
301 case LYX_ALIGN_RIGHT:
302 fl_set_button(dialog_->radio_align_right, 1);
305 fl_set_button(dialog_->radio_align_left, 1);
307 case LYX_ALIGN_CENTER:
308 fl_set_button(dialog_->radio_align_center, 1);
311 fl_set_button(dialog_->radio_align_block, 1);
315 LyXAlignment alignpos =
316 textclasslist.Style(buf->params.textclass,
317 par_->getLayout()).alignpossible;
319 setEnabled(dialog_->radio_align_block, bool(alignpos & LYX_ALIGN_BLOCK));
320 setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
321 setEnabled(dialog_->radio_align_left, bool(alignpos & LYX_ALIGN_LEFT));
322 setEnabled(dialog_->radio_align_right, bool(alignpos & LYX_ALIGN_RIGHT));
324 fl_set_button(dialog_->check_lines_top,
325 par_->params().lineTop());
326 fl_set_button(dialog_->check_lines_bottom,
327 par_->params().lineBottom());
328 fl_set_button(dialog_->check_pagebreaks_top,
329 par_->params().pagebreakTop());
330 fl_set_button(dialog_->check_pagebreaks_bottom,
331 par_->params().pagebreakBottom());
332 fl_set_button(dialog_->check_noindent,
333 par_->params().noindent());
336 Spacing space = par_->params().spacing();
339 switch (lv_->buffer()->params.spacing.getSpace()) {
340 case Spacing::Default: linespacing = 1; break;
341 case Spacing::Single: linespacing = 2; break;
342 case Spacing::Onehalf: linespacing = 3; break;
343 case Spacing::Double: linespacing = 4; break;
344 case Spacing::Other: linespacing = 5; break;
348 switch (space.getSpace()) {
349 case Spacing::Default: linespacing = 1; break;
350 case Spacing::Single: linespacing = 2; break;
351 case Spacing::Onehalf: linespacing = 3; break;
352 case Spacing::Double: linespacing = 4; break;
353 case Spacing::Other: linespacing = 5; break;
355 fl_set_choice(dialog_->choice_linespacing, linespacing);
356 if (space.getSpace() == Spacing::Other) {
359 if (space.getSpace() == Spacing::Default)
360 sp = tostr(lv_->buffer()->params.spacing.getValue());
362 sp = tostr(space.getValue());
363 fl_set_input(dialog_->input_linespacing, sp.c_str());
364 setEnabled(dialog_->input_linespacing, true);
366 fl_set_input(dialog_->input_linespacing, "");
367 setEnabled(dialog_->input_linespacing, false);
370 fl_set_input (dialog_->input_space_above, "");
372 setEnabled(dialog_->input_space_above, false);
373 switch (par_->params().spaceTop().kind()) {
375 fl_set_choice (dialog_->choice_space_above, 1);
377 case VSpace::DEFSKIP:
378 fl_set_choice (dialog_->choice_space_above, 2);
380 case VSpace::SMALLSKIP:
381 fl_set_choice (dialog_->choice_space_above, 3);
383 case VSpace::MEDSKIP:
384 fl_set_choice (dialog_->choice_space_above, 4);
386 case VSpace::BIGSKIP:
387 fl_set_choice (dialog_->choice_space_above, 5);
390 fl_set_choice (dialog_->choice_space_above, 6);
393 setEnabled(dialog_->input_space_above, true);
394 fl_set_choice (dialog_->choice_space_above, 7);
395 fl_set_input(dialog_->input_space_above, par_->
396 params().spaceTop().length().asString().c_str());
400 fl_set_button (dialog_->check_space_above,
401 par_->params().spaceTop().keep());
402 fl_set_input (dialog_->input_space_below, "");
404 setEnabled(dialog_->input_space_below, false);
405 switch (par_->params().spaceBottom().kind()) {
407 fl_set_choice (dialog_->choice_space_below, 1);
409 case VSpace::DEFSKIP:
410 fl_set_choice (dialog_->choice_space_below, 2);
412 case VSpace::SMALLSKIP:
413 fl_set_choice (dialog_->choice_space_below, 3);
415 case VSpace::MEDSKIP:
416 fl_set_choice (dialog_->choice_space_below, 4);
418 case VSpace::BIGSKIP:
419 fl_set_choice (dialog_->choice_space_below, 5);
422 fl_set_choice (dialog_->choice_space_below, 6);
425 setEnabled(dialog_->input_space_below, true);
426 fl_set_choice (dialog_->choice_space_below, 7);
427 fl_set_input(dialog_->input_space_below, par_->
428 params().spaceBottom().length().asString().c_str());
431 fl_set_button(dialog_->check_space_below,
432 par_->params().spaceBottom().keep());
433 fl_set_button(dialog_->check_noindent,
434 par_->params().noindent());
438 bool FormParagraph::input(FL_OBJECT * ob, long)
442 fl_hide_object(dialog_->text_warning);
444 // First check the buttons which are exclusive and you have to
445 // check only the actuall de/activated button.
447 // "Synchronize" the choices and input fields, making it
448 // impossible to commit senseless data.
450 if (ob == dialog_->choice_space_above) {
451 if (fl_get_choice (dialog_->choice_space_above) != 7) {
452 fl_set_input (dialog_->input_space_above, "");
453 setEnabled (dialog_->input_space_above, false);
455 setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
458 if (ob == dialog_->choice_space_below) {
459 if (fl_get_choice (dialog_->choice_space_below) != 7) {
460 fl_set_input (dialog_->input_space_below, "");
461 setEnabled (dialog_->input_space_below, false);
463 setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
467 if (fl_get_choice (dialog_->choice_linespacing) == 5)
468 setEnabled (dialog_->input_linespacing, true);
470 setEnabled (dialog_->input_linespacing, false);
471 fl_set_input (dialog_->input_linespacing, "");
474 string input = fl_get_input (dialog_->input_space_above);
476 if (fl_get_choice(dialog_->choice_space_above)==7 &&
477 input.empty() || !isValidGlueLength(input))
480 if (ob == dialog_->input_space_above) {
481 if (!isValidGlueLength(input)) {
482 fl_set_object_label(dialog_->text_warning,
483 _("Warning: Invalid Length (valid example: 10mm)"));
484 fl_show_object(dialog_->text_warning);
487 fl_hide_object(dialog_->text_warning);
490 input = fl_get_input (dialog_->input_space_below);
492 if (fl_get_choice(dialog_->choice_space_below)==7 &&
493 input.empty() || !isValidGlueLength(input))
496 if (ob == dialog_->input_space_below) {
497 if (!isValidGlueLength(input)) {
498 fl_set_object_label(dialog_->text_warning,
499 _("Warning: Invalid Length (valid example: 10mm)"));
500 fl_show_object(dialog_->text_warning);
503 fl_hide_object(dialog_->text_warning);
506 double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));
508 if (fl_get_choice (dialog_->choice_linespacing) == 5
509 && int(spacing) == 0)