]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormParagraph.C
ws cleanup
[lyx.git] / src / frontends / xforms / FormParagraph.C
1 /**
2  * \file FormParagraph.C
3  * Copyright 2000-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Jürgen Vigna, jug@sad.it
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG_
12 #pragma implementation
13 #endif
14
15 #include FORMS_H_LOCATION
16
17 #include "FormParagraph.h"
18 #include "form_paragraph.h"
19 #include "Dialogs.h"
20 #include "Liason.h"
21 #include "LyXView.h"
22 #include "buffer.h"
23 #include "lyxtext.h"
24 #include "xforms_helpers.h"
25 #include "lyxrc.h" // to set the deafult length values
26 #include "BufferView.h"
27 #include "lyxtextclasslist.h"
28 #include "Spacing.h"
29 #include "ParagraphParameters.h"
30 #include "input_validators.h"
31 #include "helper_funcs.h"
32
33 #include "support/lstrings.h"
34 #include "support/LAssert.h"
35
36 #include <functional>
37
38 using Liason::setMinibuffer;
39 using SigC::slot;
40 using std::vector;
41 using std::bind2nd;
42 using std::remove_if;
43
44
45 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
46         : FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
47 {
48         // let the dialog be shown
49         // This is a permanent connection so we won't bother
50         // storing a copy because we won't be disconnecting.
51         d->showParagraph.connect(slot(this, &FormParagraph::show));
52 }
53
54
55 void FormParagraph::connect()
56 {
57         cp_ = d_->updateParagraph
58                 .connect(slot(this, &FormParagraph::changedParagraph));
59         FormBaseBD::connect();
60 }
61
62
63 void FormParagraph::disconnect()
64 {
65         cp_.disconnect();
66         FormBaseBD::disconnect();
67 }
68
69
70 Paragraph const * FormParagraph::getCurrentParagraph() const
71 {
72         return lv_->view()->getLyXText()->cursor.par();
73 }
74
75
76 void FormParagraph::changedParagraph()
77 {
78         /// Record the paragraph
79         Paragraph const * const p = getCurrentParagraph();
80         if (p == 0 || p == par_)
81                 return;
82
83         // OBS FIX LOOK HERE
84
85         // shouldn't we chage the par_ pointer too?
86         // anyway for me the below function does just nothing!
87         // (Jug 20020108)
88
89         // For now don't bother checking if the params are different,
90         // just activate the Apply button
91         bc().valid();
92 }
93
94
95 void FormParagraph::redraw()
96 {
97         if (form() && form()->visible)
98                 fl_redraw_form(form());
99 }
100
101
102 FL_FORM * FormParagraph::form() const
103 {
104         if (dialog_.get())
105                 return dialog_->form;
106         return 0;
107 }
108
109
110 void FormParagraph::build()
111 {
112         // the tabbed folder
113         dialog_.reset(build_paragraph());
114
115         // Allow the base class to control messages
116         setMessageWidget(dialog_->text_warning);
117
118         fl_addto_choice(dialog_->choice_space_above,
119                         _(" None | Defskip | Smallskip "
120                           "| Medskip | Bigskip | VFill | Length "));
121         fl_addto_choice(dialog_->choice_space_below,
122                         _(" None | Defskip | Smallskip "
123                           "| Medskip | Bigskip | VFill | Length "));
124
125         fl_addto_choice(dialog_->choice_linespacing,
126                         _(" Default | Single | OneHalf | Double | Other "));
127
128         fl_set_input_return(dialog_->input_space_above, FL_RETURN_CHANGED);
129         fl_set_input_return(dialog_->input_space_below, FL_RETURN_CHANGED);
130         fl_set_input_return(dialog_->input_labelwidth,  FL_RETURN_CHANGED);
131         fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
132         fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
133
134         setPrehandler(dialog_->input_space_above);
135         setPrehandler(dialog_->input_space_below);
136         setPrehandler(dialog_->input_labelwidth);
137         setPrehandler(dialog_->input_linespacing);
138
139         // Create the contents of the unit choices
140         // Don't include the "%" terms...
141         vector<string> units_vec = getLatexUnits();
142 #if 0
143         for (vector<string>::iterator it = units_vec.begin();
144              it != units_vec.end(); ++it) {
145                 if (contains(*it, "%"))
146                         it = units_vec.erase(it, it+1) - 1;
147         }
148 #else
149         // Something similar to this is a better way to erase
150         vector<string>::iterator del =
151                 remove_if(units_vec.begin(), units_vec.end(),
152                           bind2nd(contains_functor(), "%"));
153         units_vec.erase(del, units_vec.end());
154 #endif
155
156         string units = getStringFromVector(units_vec, "|");
157
158         fl_addto_choice(dialog_->choice_value_space_above, units.c_str());
159         fl_addto_choice(dialog_->choice_value_space_below, units.c_str());
160
161         // Manage the ok, apply, restore and cancel/close buttons
162         bc_.setOK(dialog_->button_ok);
163         bc_.setApply(dialog_->button_apply);
164         bc_.setCancel(dialog_->button_close);
165         bc_.setRestore(dialog_->button_restore);
166
167         bc_.addReadOnly(dialog_->radio_align_right);
168         bc_.addReadOnly(dialog_->radio_align_left);
169         bc_.addReadOnly(dialog_->radio_align_block);
170         bc_.addReadOnly(dialog_->radio_align_center);
171         bc_.addReadOnly(dialog_->check_lines_top);
172         bc_.addReadOnly(dialog_->check_lines_bottom);
173         bc_.addReadOnly(dialog_->check_pagebreaks_top);
174         bc_.addReadOnly(dialog_->check_pagebreaks_bottom);
175         bc_.addReadOnly(dialog_->choice_space_above);
176         bc_.addReadOnly(dialog_->input_space_above);
177         bc_.addReadOnly(dialog_->check_space_above);
178         bc_.addReadOnly(dialog_->choice_space_below);
179         bc_.addReadOnly(dialog_->input_space_below);
180         bc_.addReadOnly(dialog_->check_space_below);
181         bc_.addReadOnly(dialog_->choice_linespacing);
182         bc_.addReadOnly(dialog_->input_linespacing);
183         bc_.addReadOnly(dialog_->check_noindent);
184         bc_.addReadOnly(dialog_->input_labelwidth);
185 }
186
187
188 namespace {
189
190 VSpace setVSpaceFromWidgets(FL_OBJECT * choice_type,
191                             FL_OBJECT * input_length,
192                             FL_OBJECT * choice_length,
193                             FL_OBJECT * check_keep)
194 {
195         // Paranoia check!
196         lyx::Assert(choice_type   && choice_type->objclass   == FL_CHOICE &&
197                     input_length  && input_length->objclass  == FL_INPUT &&
198                     choice_length && choice_length->objclass == FL_CHOICE &&
199                     check_keep    && check_keep->objclass    == FL_CHECKBUTTON);
200
201         VSpace space;
202
203         switch (fl_get_choice(choice_type)) {
204         case 1:
205                 space = VSpace(VSpace::NONE);
206                 break;
207         case 2:
208                 space = VSpace(VSpace::DEFSKIP);
209                 break;
210         case 3:
211                 space = VSpace(VSpace::SMALLSKIP);
212                 break;
213         case 4:
214                 space = VSpace(VSpace::MEDSKIP);
215                 break;
216         case 5:
217                 space = VSpace(VSpace::BIGSKIP);
218                 break;
219         case 6:
220                 space = VSpace(VSpace::VFILL);
221                 break;
222         case 7:
223         {
224                 string const length =
225                         getLengthFromWidgets(input_length, choice_length);
226                 space = VSpace(LyXGlueLength(length));
227                 break;
228         }
229         }
230
231         if (fl_get_button(check_keep))
232                 space.setKeep(true);
233
234         return space;
235 }
236
237
238 void validateVSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
239 {
240         // Paranoia check!
241         lyx::Assert(choice_type  && choice_type->objclass   == FL_CHOICE &&
242                     input_length && input_length->objclass  == FL_INPUT);
243
244         if (fl_get_choice(choice_type) != 7)
245                 return;
246
247         // If a vspace kind is "Length" but there's no text in
248         // the input field, reset the kind to "None".
249         string const input = strip(getStringFromInput(input_length));
250         if (input.empty())
251                 fl_set_choice(choice_type, 1);
252 }
253
254 } // namespace anon
255
256
257 void FormParagraph::apply()
258 {
259         if (!lv_->view()->available() || !dialog_.get())
260                 return;
261
262         // If a vspace kind is "Length" but there's no text in
263         // the input field, reset the kind to "None".
264         validateVSpaceWidgets(dialog_->choice_space_above,
265                               dialog_->input_space_above);
266         validateVSpaceWidgets(dialog_->choice_space_below,
267                               dialog_->input_space_below);
268
269         bool const line_top = fl_get_button(dialog_->check_lines_top);
270         bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
271         bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
272         bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
273
274         VSpace const space_top =
275                 setVSpaceFromWidgets(dialog_->choice_space_above,
276                                      dialog_->input_space_above,
277                                      dialog_->choice_value_space_above,
278                                      dialog_->check_space_above);
279
280         VSpace const space_bottom =
281                 setVSpaceFromWidgets(dialog_->choice_space_below,
282                                      dialog_->input_space_below,
283                                      dialog_->choice_value_space_below,
284                                      dialog_->check_space_below);
285
286         LyXAlignment align;
287         if (fl_get_button(dialog_->radio_align_left))
288                 align = LYX_ALIGN_LEFT;
289         else if (fl_get_button(dialog_->radio_align_right))
290                 align = LYX_ALIGN_RIGHT;
291         else if (fl_get_button(dialog_->radio_align_center))
292                 align = LYX_ALIGN_CENTER;
293         else
294                 align = LYX_ALIGN_BLOCK;
295
296         string const labelwidthstring =
297                 getStringFromInput(dialog_->input_labelwidth);
298
299         bool const noindent = fl_get_button(dialog_->check_noindent);
300
301         Spacing::Space linespacing = Spacing::Default;
302         string other;
303         switch (fl_get_choice(dialog_->choice_linespacing)) {
304         case 1:
305                 linespacing = Spacing::Default;
306                 break;
307         case 2:
308                 linespacing = Spacing::Single;
309                 break;
310         case 3:
311                 linespacing = Spacing::Onehalf;
312                 break;
313         case 4:
314                 linespacing = Spacing::Double;
315                 break;
316         case 5:
317                 linespacing = Spacing::Other;
318                 other = getStringFromInput(dialog_->input_linespacing);
319                 break;
320         }
321
322         Spacing const spacing(linespacing, other);
323
324         LyXText * text(lv_->view()->getLyXText());
325         text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
326                            pagebreak_bottom, space_top, space_bottom, spacing,
327                            align, labelwidthstring, noindent);
328
329         // Actually apply these settings
330         lv_->view()->update(text,
331                             BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
332         lv_->buffer()->markDirty();
333         setMinibuffer(lv_, _("Paragraph layout set"));
334 }
335
336
337 namespace {
338
339 void setWidgetsFromVSpace(VSpace const & space,
340                           FL_OBJECT * choice_type,
341                           FL_OBJECT * input_length,
342                           FL_OBJECT * choice_length,
343                           FL_OBJECT * check_keep)
344 {
345         // Paranoia check!
346         lyx::Assert(choice_type   && choice_type->objclass   == FL_CHOICE &&
347                     input_length  && input_length->objclass  == FL_INPUT &&
348                     choice_length && choice_length->objclass == FL_CHOICE &&
349                     check_keep    && check_keep->objclass    == FL_CHECKBUTTON);
350
351         fl_set_input(input_length, "");
352         setEnabled(input_length,  false);
353         setEnabled(choice_length, false);
354
355         switch (space.kind()) {
356         case VSpace::NONE:
357                 fl_set_choice(choice_type, 1);
358                 break;
359         case VSpace::DEFSKIP:
360                 fl_set_choice(choice_type, 2);
361                 break;
362         case VSpace::SMALLSKIP:
363                 fl_set_choice(choice_type, 3);
364                 break;
365         case VSpace::MEDSKIP:
366                 fl_set_choice(choice_type, 4);
367                 break;
368         case VSpace::BIGSKIP:
369                 fl_set_choice(choice_type, 5);
370                 break;
371         case VSpace::VFILL:
372                 fl_set_choice(choice_type, 6);
373                 break;
374         case VSpace::LENGTH:
375         {
376                 fl_set_choice(choice_type, 7);
377
378                 setEnabled(input_length,  true);
379                 setEnabled(choice_length, true);
380
381                 bool const metric = lyxrc.default_papersize > 3;
382                 string const default_unit = metric ? "cm" : "in";
383                 string const length = space.length().asString();
384
385                 updateWidgetsFromLengthString(input_length, choice_length,
386                                               length, default_unit);
387                 break;
388         }
389         }
390
391         fl_set_button(check_keep, space.keep());
392 }
393
394 } // namespace anon
395
396
397 void FormParagraph::update()
398 {
399         if (!dialog_.get())
400                 return;
401
402         // Do this first; some objects may be de/activated subsequently.
403         bc_.readOnly(lv_->buffer()->isReadonly());
404
405         /// Record the paragraph
406         par_ = getCurrentParagraph();
407
408         fl_set_input(dialog_->input_labelwidth,
409                      par_->getLabelWidthString().c_str());
410         setEnabled(dialog_->input_labelwidth,
411                    (par_->getLabelWidthString() != _("Senseless with this layout!")));
412
413         fl_set_button(dialog_->radio_align_right, 0);
414         fl_set_button(dialog_->radio_align_left, 0);
415         fl_set_button(dialog_->radio_align_center, 0);
416         fl_set_button(dialog_->radio_align_block, 0);
417
418         LyXTextClass const & tclass =
419                 textclasslist[lv_->view()->buffer()->params.textclass];
420
421         int align = par_->getAlign();
422         if (align == LYX_ALIGN_LAYOUT)
423                 align = tclass[par_->layout()].align;
424
425         switch (align) {
426         case LYX_ALIGN_RIGHT:
427                 fl_set_button(dialog_->radio_align_right, 1);
428                 break;
429         case LYX_ALIGN_LEFT:
430                 fl_set_button(dialog_->radio_align_left, 1);
431                 break;
432         case LYX_ALIGN_CENTER:
433                 fl_set_button(dialog_->radio_align_center, 1);
434                 break;
435         default:
436                 fl_set_button(dialog_->radio_align_block, 1);
437                 break;
438         }
439
440         LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
441
442         setEnabled(dialog_->radio_align_block,  bool(alignpos & LYX_ALIGN_BLOCK));
443         setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
444         setEnabled(dialog_->radio_align_left,   bool(alignpos & LYX_ALIGN_LEFT));
445         setEnabled(dialog_->radio_align_right,  bool(alignpos & LYX_ALIGN_RIGHT));
446
447         // no inset-text-owned paragraph may have pagebreaks
448         setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
449         setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
450
451         fl_set_button(dialog_->check_lines_top,
452                       par_->params().lineTop());
453         fl_set_button(dialog_->check_lines_bottom,
454                       par_->params().lineBottom());
455         fl_set_button(dialog_->check_pagebreaks_top,
456                       par_->params().pagebreakTop());
457         fl_set_button(dialog_->check_pagebreaks_bottom,
458                       par_->params().pagebreakBottom());
459         fl_set_button(dialog_->check_noindent,
460                       par_->params().noindent());
461
462         int linespacing;
463         Spacing const space = par_->params().spacing();
464
465         switch (space.getSpace()) {
466         default: linespacing = 1; break;
467         case Spacing::Single: linespacing = 2; break;
468         case Spacing::Onehalf: linespacing = 3; break;
469         case Spacing::Double: linespacing = 4; break;
470         case Spacing::Other: linespacing = 5; break;
471         }
472
473         fl_set_choice(dialog_->choice_linespacing, linespacing);
474         if (space.getSpace() == Spacing::Other) {
475                 string const sp = tostr(space.getValue());
476                 fl_set_input(dialog_->input_linespacing, sp.c_str());
477                 setEnabled(dialog_->input_linespacing, true);
478         } else {
479                 fl_set_input(dialog_->input_linespacing, "");
480                 setEnabled(dialog_->input_linespacing, false);
481         }
482
483         setWidgetsFromVSpace(par_->params().spaceTop(),
484                              dialog_->choice_space_above,
485                              dialog_->input_space_above,
486                              dialog_->choice_value_space_above,
487                              dialog_->check_space_above);
488
489         setWidgetsFromVSpace(par_->params().spaceBottom(),
490                              dialog_->choice_space_below,
491                              dialog_->input_space_below,
492                              dialog_->choice_value_space_below,
493                              dialog_->check_space_below);
494
495         fl_set_button(dialog_->check_noindent,
496                       par_->params().noindent());
497 }
498
499
500 namespace {
501
502 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
503                              FL_OBJECT * input_length,
504                              FL_OBJECT * choice_length,
505                              bool readonly)
506 {
507         // Paranoia check!
508         lyx::Assert(choice_type   && choice_type->objclass   == FL_CHOICE &&
509                     input_length  && input_length->objclass  == FL_INPUT &&
510                     choice_length && choice_length->objclass == FL_CHOICE);
511
512         if (fl_get_choice(choice_type) != 7) {
513                 fl_set_input(input_length, "");
514                 setEnabled(input_length, false);
515                 setEnabled(choice_length, false);
516
517         } else {
518                 setEnabled(input_length,  !readonly);
519                 setEnabled(choice_length, !readonly);
520
521                 string const length = getStringFromInput(input_length);
522
523                 if (strip(length).empty()) {
524                         bool const metric = lyxrc.default_papersize > 3;
525                         int const default_unit = metric ? 8 : 9;
526
527                         fl_set_choice(choice_length, default_unit);
528                 }
529         }
530 }
531
532 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
533 {
534         // Paranoia check!
535         lyx::Assert(choice_type  && choice_type->objclass   == FL_CHOICE &&
536                     input_length && input_length->objclass  == FL_INPUT);
537
538         if (fl_get_choice(choice_type) != 7)
539                 return true;
540
541         string const input = getStringFromInput(input_length);
542         return (input.empty() ||
543                 isValidGlueLength(input) ||
544                 isStrDbl(input));
545 }
546
547 } // namespace anon
548
549
550 bool FormParagraph::input(FL_OBJECT * ob, long)
551 {
552         clearMessage();
553
554         // First check the buttons which are exclusive and you have to
555         // check only the actuall de/activated button.
556         //
557         // "Synchronize" the choices and input fields, making it
558         // impossible to commit senseless data.
559
560         if (ob == dialog_->choice_space_above) {
561                 synchronizeSpaceWidgets(dialog_->choice_space_above,
562                                         dialog_->input_space_above,
563                                         dialog_->choice_value_space_above,
564                                         lv_->buffer()->isReadonly());
565         }
566
567         if (ob == dialog_->choice_space_below) {
568                 synchronizeSpaceWidgets(dialog_->choice_space_below,
569                                         dialog_->input_space_below,
570                                         dialog_->choice_value_space_below,
571                                         lv_->buffer()->isReadonly());
572         }
573
574         // Display a warning if the input is senseless
575         bool valid = (validSpaceWidgets(dialog_->choice_space_above,
576                                         dialog_->input_space_above) &&
577                       validSpaceWidgets(dialog_->choice_space_below,
578                                         dialog_->input_space_below));
579
580         if (!valid) {
581                 postWarning(_("Invalid Length (valid example: 10mm)"));
582         }
583
584         int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
585
586         if (choice_spacing == 5)
587                 setEnabled(dialog_->input_linespacing, true);
588         else {
589                 fl_set_input(dialog_->input_linespacing, "");
590                 setEnabled(dialog_->input_linespacing, false);
591         }
592
593         double const spacing =
594                 strToDbl(getStringFromInput(dialog_->input_linespacing));
595
596         if (choice_spacing == 5 && int(spacing) == 0)
597                 valid = false;
598
599         return valid;
600 }