]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormParagraph.C
Make more use of the feedback controller routines.
[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 } // namespace anon 
238
239
240 void FormParagraph::apply()
241 {
242         if (!lv_->view()->available() || !dialog_.get())
243                 return;
244
245         // If a vspace kind is "Length" but there's no text in
246         // the input field, reset the kind to "None". 
247         if ((fl_get_choice(dialog_->choice_space_above) == 7) &&
248             !*(fl_get_input(dialog_->input_space_above)))
249         {
250                 fl_set_choice(dialog_->choice_space_above, 1);
251         }
252
253         if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
254             !*(fl_get_input (dialog_->input_space_below)))
255         {
256                 fl_set_choice(dialog_->choice_space_below, 1);
257         }
258
259         bool const line_top = fl_get_button(dialog_->check_lines_top);
260         bool const line_bottom = fl_get_button(dialog_->check_lines_bottom);
261         bool const pagebreak_top = fl_get_button(dialog_->check_pagebreaks_top);
262         bool const pagebreak_bottom = fl_get_button(dialog_->check_pagebreaks_bottom);
263
264         VSpace const space_top =
265                 setVSpaceFromWidgets(dialog_->choice_space_above,
266                                      dialog_->input_space_above,
267                                      dialog_->choice_value_space_above,
268                                      dialog_->check_space_above);
269
270         VSpace const space_bottom =
271                 setVSpaceFromWidgets(dialog_->choice_space_below,
272                                      dialog_->input_space_below,
273                                      dialog_->choice_value_space_below,
274                                      dialog_->check_space_below);
275
276         LyXAlignment align;
277         if (fl_get_button(dialog_->radio_align_left))
278                 align = LYX_ALIGN_LEFT;
279         else if (fl_get_button(dialog_->radio_align_right))
280                 align = LYX_ALIGN_RIGHT;
281         else if (fl_get_button(dialog_->radio_align_center))
282                 align = LYX_ALIGN_CENTER;
283         else 
284                 align = LYX_ALIGN_BLOCK;
285
286         string const labelwidthstring =
287                 getStringFromInput(dialog_->input_labelwidth);
288
289         bool const noindent = fl_get_button(dialog_->check_noindent);
290
291         Spacing::Space linespacing = Spacing::Default;
292         string other_linespacing;
293         switch (fl_get_choice(dialog_->choice_linespacing)) {
294                 case 1: linespacing = Spacing::Default; break;
295                 case 2: linespacing = Spacing::Single; break;
296                 case 3: linespacing = Spacing::Onehalf; break;
297                 case 4: linespacing = Spacing::Double; break;
298                 case 5:
299                         linespacing = Spacing::Other;
300                         other_linespacing = fl_get_input(dialog_->input_linespacing);
301                         break;
302         }
303
304         Spacing const spacing(linespacing, other_linespacing);
305
306         LyXText * text(lv_->view()->getLyXText());
307         text->setParagraph(lv_->view(), line_top, line_bottom, pagebreak_top,
308                            pagebreak_bottom, space_top, space_bottom, spacing,
309                            align, labelwidthstring, noindent);
310
311         // Actually apply these settings
312         lv_->view()->update(text, 
313         BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
314         lv_->buffer()->markDirty();
315         setMinibuffer(lv_, _("Paragraph layout set"));
316 }
317
318
319 namespace {
320
321 void setWidgetsFromVSpace(VSpace const & space,
322                           FL_OBJECT * choice_type,
323                           FL_OBJECT * input_length,
324                           FL_OBJECT * choice_length,
325                           FL_OBJECT * check_keep)
326 {
327         // Paranoia check!
328         lyx::Assert(choice_type   && choice_type->objclass   == FL_CHOICE &&
329                     input_length  && input_length->objclass  == FL_INPUT &&
330                     choice_length && choice_length->objclass == FL_CHOICE &&
331                     check_keep    && check_keep->objclass    == FL_CHECKBUTTON);
332
333         fl_set_input(input_length, "");
334         setEnabled(input_length,  false);
335         setEnabled(choice_length, false);
336
337         switch (space.kind()) {
338         case VSpace::NONE:
339                 fl_set_choice(choice_type, 1);
340                 break;
341         case VSpace::DEFSKIP:
342                 fl_set_choice(choice_type, 2);
343                 break;
344         case VSpace::SMALLSKIP:
345                 fl_set_choice(choice_type, 3);
346                 break;
347         case VSpace::MEDSKIP:
348                 fl_set_choice(choice_type, 4);
349                 break;
350         case VSpace::BIGSKIP:
351                 fl_set_choice(choice_type, 5);
352                 break;
353         case VSpace::VFILL:
354                 fl_set_choice(choice_type, 6);
355                 break;
356         case VSpace::LENGTH:
357         {
358                 fl_set_choice(choice_type, 7);
359
360                 setEnabled(input_length,  true);
361                 setEnabled(choice_length, true);
362
363                 bool const metric = lyxrc.default_papersize > 3;
364                 string const default_unit = metric ? "cm" : "in";
365                 string const length = space.length().asString();
366
367                 updateWidgetsFromLengthString(input_length, choice_length,
368                                               length, default_unit);
369                 break;
370         }
371         }
372
373         fl_set_button(check_keep, space.keep());
374 }
375
376 } // namespace anon 
377
378
379 void FormParagraph::update()
380 {
381         if (!dialog_.get())
382                 return;
383
384         // Do this first; some objects may be de/activated subsequently.
385         bc_.readOnly(lv_->buffer()->isReadonly());
386
387         /// Record the paragraph
388         par_ = getCurrentParagraph();
389
390         fl_set_input(dialog_->input_labelwidth,
391                      par_->getLabelWidthString().c_str());
392         setEnabled(dialog_->input_labelwidth,
393                    (par_->getLabelWidthString() != _("Senseless with this layout!")));
394
395         fl_set_button(dialog_->radio_align_right, 0);
396         fl_set_button(dialog_->radio_align_left, 0);
397         fl_set_button(dialog_->radio_align_center, 0);
398         fl_set_button(dialog_->radio_align_block, 0);
399
400         LyXTextClass const & tclass =
401                 textclasslist[lv_->view()->buffer()->params.textclass];
402
403         int align = par_->getAlign();
404         if (align == LYX_ALIGN_LAYOUT)
405                 align = tclass[par_->layout()].align;
406
407         switch (align) {
408                 case LYX_ALIGN_RIGHT:
409                         fl_set_button(dialog_->radio_align_right, 1);
410                         break;
411                 case LYX_ALIGN_LEFT:
412                         fl_set_button(dialog_->radio_align_left, 1);
413                         break;
414                 case LYX_ALIGN_CENTER:
415                         fl_set_button(dialog_->radio_align_center, 1);
416                         break;
417                 default:
418                         fl_set_button(dialog_->radio_align_block, 1);
419                         break;
420         }
421
422         LyXAlignment alignpos = tclass[par_->layout()].alignpossible;
423
424         setEnabled(dialog_->radio_align_block,  bool(alignpos & LYX_ALIGN_BLOCK));
425         setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
426         setEnabled(dialog_->radio_align_left,   bool(alignpos & LYX_ALIGN_LEFT));
427         setEnabled(dialog_->radio_align_right,  bool(alignpos & LYX_ALIGN_RIGHT));
428
429         // no inset-text-owned paragraph may have pagebreaks
430         setEnabled(dialog_->check_pagebreaks_top, !par_->inInset());
431         setEnabled(dialog_->check_pagebreaks_bottom, !par_->inInset());
432
433         fl_set_button(dialog_->check_lines_top,
434                       par_->params().lineTop());
435         fl_set_button(dialog_->check_lines_bottom,
436                       par_->params().lineBottom());
437         fl_set_button(dialog_->check_pagebreaks_top,
438                       par_->params().pagebreakTop());
439         fl_set_button(dialog_->check_pagebreaks_bottom,
440                       par_->params().pagebreakBottom());
441         fl_set_button(dialog_->check_noindent,
442                       par_->params().noindent());
443
444         int linespacing;
445         Spacing const space = par_->params().spacing();
446
447         switch (space.getSpace()) {
448                 default: linespacing = 1; break;
449                 case Spacing::Single: linespacing = 2; break;
450                 case Spacing::Onehalf: linespacing = 3; break;
451                 case Spacing::Double: linespacing = 4; break;
452                 case Spacing::Other: linespacing = 5; break;
453         }
454
455         fl_set_choice(dialog_->choice_linespacing, linespacing);
456         if (space.getSpace() == Spacing::Other) {
457                 string const sp = tostr(space.getValue());
458                 fl_set_input(dialog_->input_linespacing, sp.c_str());
459                 setEnabled(dialog_->input_linespacing, true);
460         } else {
461                 fl_set_input(dialog_->input_linespacing, "");
462                 setEnabled(dialog_->input_linespacing, false);
463         }
464
465         setWidgetsFromVSpace(par_->params().spaceTop(),
466                              dialog_->choice_space_above,
467                              dialog_->input_space_above,
468                              dialog_->choice_value_space_above,
469                              dialog_->check_space_above);
470                              
471         setWidgetsFromVSpace(par_->params().spaceBottom(),
472                              dialog_->choice_space_below,
473                              dialog_->input_space_below,
474                              dialog_->choice_value_space_below,
475                              dialog_->check_space_below);
476                      
477         fl_set_button(dialog_->check_noindent,
478                       par_->params().noindent());
479 }
480
481
482 namespace {
483
484 void synchronizeSpaceWidgets(FL_OBJECT * choice_type,
485                              FL_OBJECT * input_length,
486                              FL_OBJECT * choice_length,
487                              bool readonly)
488 {
489         // Paranoia check!
490         lyx::Assert(choice_type   && choice_type->objclass   == FL_CHOICE &&
491                     input_length  && input_length->objclass  == FL_INPUT &&
492                     choice_length && choice_length->objclass == FL_CHOICE);
493
494         if (fl_get_choice(choice_type) != 7) {
495                 fl_set_input(input_length, "");
496                 setEnabled(input_length, false);
497                 setEnabled(choice_length, false);
498
499         } else {
500                 setEnabled(input_length,  !readonly);
501                 setEnabled(choice_length, !readonly);
502
503                 string const length = getStringFromInput(input_length);
504
505                 if (strip(length).empty()) {
506                         bool const metric = lyxrc.default_papersize > 3;
507                         int const default_unit = metric ? 8 : 9;
508
509                         fl_set_choice(choice_length, default_unit);
510                 }
511         }
512 }
513  
514 bool validSpaceWidgets(FL_OBJECT * choice_type, FL_OBJECT * input_length)
515 {
516         // Paranoia check!
517         lyx::Assert(choice_type  && choice_type->objclass   == FL_CHOICE &&
518                     input_length && input_length->objclass  == FL_INPUT);
519
520         if (fl_get_choice(choice_type) != 7)
521                 return true;
522
523         string const input = getStringFromInput(input_length);
524         return (input.empty() ||
525                 isValidGlueLength(input) ||
526                 isStrDbl(input));
527 }
528
529 } // namespace anon
530
531  
532 bool FormParagraph::input(FL_OBJECT * ob, long)
533 {
534         clearMessage();
535
536         // First check the buttons which are exclusive and you have to
537         // check only the actuall de/activated button.
538         //
539         // "Synchronize" the choices and input fields, making it
540         // impossible to commit senseless data.
541
542         if (ob == dialog_->choice_space_above) {
543                 synchronizeSpaceWidgets(dialog_->choice_space_above,
544                                         dialog_->input_space_above,
545                                         dialog_->choice_value_space_above,
546                                         lv_->buffer()->isReadonly());
547         }
548
549         if (ob == dialog_->choice_space_below) {
550                 synchronizeSpaceWidgets(dialog_->choice_space_below,
551                                         dialog_->input_space_below,
552                                         dialog_->choice_value_space_below,
553                                         lv_->buffer()->isReadonly());
554         }
555
556         // Display a warning if the input is senseless
557         bool valid = (validSpaceWidgets(dialog_->choice_space_above,
558                                         dialog_->input_space_above) &&
559                       validSpaceWidgets(dialog_->choice_space_below,
560                                         dialog_->input_space_below));
561
562         if (!valid) {
563                 postWarning(_("Invalid Length (valid example: 10mm)"));
564         }
565
566         int const choice_spacing = fl_get_choice(dialog_->choice_linespacing);
567
568         if (choice_spacing == 5)
569                 setEnabled(dialog_->input_linespacing, true);
570         else {
571                 fl_set_input(dialog_->input_linespacing, "");
572                 setEnabled(dialog_->input_linespacing, false);
573         }
574
575         double const spacing =
576                 strToDbl(getStringFromInput(dialog_->input_linespacing));
577
578         if (choice_spacing == 5 && int(spacing) == 0)
579                 valid = false;
580
581         return valid;
582 }