]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormParagraph.C
J�rgen Spitzm�ller's tweak of the Paragraph dialog.
[lyx.git] / src / frontends / xforms / FormParagraph.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  *           @author Jürgen Vigna
9  *
10  *======================================================*/
11
12 #include <config.h>
13
14 #ifdef __GNUG_
15 #pragma implementation
16 #endif
17
18 #include FORMS_H_LOCATION
19
20 #include "FormParagraph.h"
21 #include "form_paragraph.h"
22 #include "Dialogs.h"
23 #include "support/lstrings.h" 
24 #include "Liason.h"
25 #include "LyXView.h"
26 #include "buffer.h"
27 #include "lyxtext.h"
28 #include "xforms_helpers.h"
29 #include "BufferView.h"
30 #include "Spacing.h"
31 #include "ParagraphParameters.h"
32 #include "input_validators.h"
33
34 using Liason::setMinibuffer;
35 using SigC::slot;
36
37
38 FormParagraph::FormParagraph(LyXView * lv, Dialogs * d)
39         : FormBaseBD(lv, d, _("Paragraph Layout")), par_(0)
40 {
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));
45 }
46
47
48 void FormParagraph::connect()
49 {
50         cp_ = d_->updateParagraph
51                 .connect(slot(this, &FormParagraph::changedParagraph));
52         FormBaseBD::connect();
53 }
54
55
56 void FormParagraph::disconnect()
57 {
58         cp_.disconnect();
59         FormBaseBD::disconnect();
60 }
61
62
63 Paragraph const * FormParagraph::getCurrentParagraph() const
64 {
65         LyXText * text = 0;
66
67         if (lv_->view()->theLockingInset())
68                 text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
69         if (!text)
70                 text = lv_->view()->text;
71         return text->cursor.par();
72 }
73
74
75 void FormParagraph::changedParagraph()
76 {
77         /// Record the paragraph
78         Paragraph const * const p = getCurrentParagraph();
79         if (p == 0 || p == par_)
80                 return;
81
82         // For now don't bother checking if the params are different,
83         // just activate the Apply button
84         bc().valid();
85 }
86
87
88 void FormParagraph::redraw()
89 {
90         if( form() && form()->visible )
91                 fl_redraw_form( form() );
92 }
93
94
95 FL_FORM * FormParagraph::form() const
96 {
97     if (dialog_.get()) return dialog_->form;
98     return 0;
99 }
100
101
102 void FormParagraph::build()
103 {
104     // the tabbed folder
105     dialog_.reset(build_paragraph());
106
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 ")); 
113
114     fl_addto_choice(dialog_->choice_linespacing,
115                     _(" Default | Single | OneHalf | Double | Other "));
116  
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);
122
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);
128
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);
148 }
149
150
151 void FormParagraph::apply()
152 {
153     if (!lv_->view()->available() || !dialog_.get())
154         return;
155
156     VSpace space_top, space_bottom;
157     LyXAlignment align;
158     string labelwidthstring;
159     bool noindent;
160
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)))
165     {
166         fl_set_choice (dialog_->choice_space_above, 1);
167     }
168     if ((fl_get_choice (dialog_->choice_space_below) == 7) &&
169         !*(fl_get_input (dialog_->input_space_below)))
170     {
171         fl_set_choice (dialog_->choice_space_below, 1);
172     }
173    
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);
178     
179     switch (fl_get_choice (dialog_->choice_space_above)) {
180     case 1:
181         space_top = VSpace(VSpace::NONE);
182         break;
183     case 2:
184         space_top = VSpace(VSpace::DEFSKIP);
185         break;
186     case 3:
187         space_top = VSpace(VSpace::SMALLSKIP);
188         break;
189     case 4:
190         space_top = VSpace(VSpace::MEDSKIP);
191         break;
192     case 5:
193         space_top = VSpace(VSpace::BIGSKIP);
194         break;
195     case 6:
196         space_top = VSpace(VSpace::VFILL);
197         break;
198     case 7:
199         space_top =
200                 VSpace(LyXGlueLength(fl_get_input(dialog_->input_space_above)));
201         break;
202     }
203     if (fl_get_button (dialog_->check_space_above))
204         space_top.setKeep (true);
205     switch (fl_get_choice (dialog_->choice_space_below)) {
206     case 1:
207         space_bottom = VSpace(VSpace::NONE);
208         break;
209     case 2:
210         space_bottom = VSpace(VSpace::DEFSKIP);
211         break;
212     case 3:
213         space_bottom = VSpace(VSpace::SMALLSKIP);
214         break;
215     case 4:
216         space_bottom = VSpace(VSpace::MEDSKIP);
217         break;
218     case 5:
219         space_bottom = VSpace(VSpace::BIGSKIP);
220         break;
221     case 6:
222         space_bottom = VSpace(VSpace::VFILL);
223         break;
224     case 7:
225         space_bottom =
226                 VSpace(LyXGlueLength(fl_get_input(dialog_->input_space_below)));
227         break;
228     }
229     if (fl_get_button (dialog_->check_space_below))
230         space_bottom.setKeep (true);
231
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;
238     else 
239         align = LYX_ALIGN_BLOCK;
240    
241     labelwidthstring = fl_get_input(dialog_->input_labelwidth);
242     noindent = fl_get_button(dialog_->check_noindent);
243     Spacing::Space linespacing = Spacing::Default;
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;
250         case 5:
251             linespacing = Spacing::Other;
252             other_linespacing = fl_get_input(dialog_->input_linespacing);
253             break;
254     }
255
256     Spacing const spacing(linespacing, other_linespacing);
257     LyXText * text = 0;
258     if (lv_->view()->theLockingInset())
259         text = lv_->view()->theLockingInset()->getLyXText(lv_->view());
260     if (!text)
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);
265
266
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"));
272 }
273
274
275 void FormParagraph::update()
276 {
277     if (!dialog_.get())
278         return;
279
280     // Do this first; some objects may be de/activated subsequently.
281     bc_.readOnly(lv_->buffer()->isReadonly());
282
283     Buffer * buf = lv_->view()->buffer();
284
285     /// Record the paragraph
286     par_ = getCurrentParagraph();
287
288     fl_set_input(dialog_->input_labelwidth,
289                  par_->getLabelWidthString().c_str());
290     setEnabled(dialog_->input_labelwidth,
291                (par_->getLabelWidthString() != _("Senseless with this layout!")));
292
293     fl_set_button(dialog_->radio_align_right, 0);
294     fl_set_button(dialog_->radio_align_left, 0);
295     fl_set_button(dialog_->radio_align_center, 0);
296     fl_set_button(dialog_->radio_align_block, 0);
297
298     int align = par_->getAlign();
299     if (align == LYX_ALIGN_LAYOUT)
300         align = textclasslist.Style(buf->params.textclass,
301                                     par_->getLayout()).align;
302
303     switch (align) {
304     case LYX_ALIGN_RIGHT:
305         fl_set_button(dialog_->radio_align_right, 1);
306         break;
307     case LYX_ALIGN_LEFT:
308         fl_set_button(dialog_->radio_align_left, 1);
309         break;
310     case LYX_ALIGN_CENTER:
311         fl_set_button(dialog_->radio_align_center, 1);
312         break;
313     default:
314         fl_set_button(dialog_->radio_align_block, 1);
315         break;
316     }
317
318     LyXAlignment alignpos =
319             textclasslist.Style(buf->params.textclass,
320                                 par_->getLayout()).alignpossible;
321
322     setEnabled(dialog_->radio_align_block,  bool(alignpos & LYX_ALIGN_BLOCK));
323     setEnabled(dialog_->radio_align_center, bool(alignpos & LYX_ALIGN_CENTER));
324     setEnabled(dialog_->radio_align_left,   bool(alignpos & LYX_ALIGN_LEFT));
325     setEnabled(dialog_->radio_align_right,  bool(alignpos & LYX_ALIGN_RIGHT));
326     
327     fl_set_button(dialog_->check_lines_top,
328                   par_->params().lineTop());
329     fl_set_button(dialog_->check_lines_bottom,
330                   par_->params().lineBottom());
331     fl_set_button(dialog_->check_pagebreaks_top,
332                   par_->params().pagebreakTop());
333     fl_set_button(dialog_->check_pagebreaks_bottom,
334                   par_->params().pagebreakBottom());
335     fl_set_button(dialog_->check_noindent,
336                   par_->params().noindent());
337
338     int linespacing;
339     Spacing const space = par_->params().spacing();
340
341     switch (space.getSpace()) {
342         default: linespacing = 1; break;
343         case Spacing::Single: linespacing = 2; break;
344         case Spacing::Onehalf: linespacing = 3; break;
345         case Spacing::Double: linespacing = 4; break;
346         case Spacing::Other: linespacing = 5; break;
347     }
348  
349     fl_set_choice(dialog_->choice_linespacing, linespacing);
350     if (space.getSpace() == Spacing::Other) {
351         string const sp = tostr(space.getValue());
352         fl_set_input(dialog_->input_linespacing, sp.c_str());
353         setEnabled(dialog_->input_linespacing, true);
354     } else {
355         fl_set_input(dialog_->input_linespacing, "");
356         setEnabled(dialog_->input_linespacing, false);
357     }
358
359     fl_set_input (dialog_->input_space_above, "");
360
361     setEnabled(dialog_->input_space_above, false);
362     switch (par_->params().spaceTop().kind()) {
363     case VSpace::NONE:
364         fl_set_choice (dialog_->choice_space_above, 1);
365         break;
366     case VSpace::DEFSKIP:
367         fl_set_choice (dialog_->choice_space_above, 2);
368         break;
369     case VSpace::SMALLSKIP:
370         fl_set_choice (dialog_->choice_space_above, 3);
371         break;
372     case VSpace::MEDSKIP:
373         fl_set_choice (dialog_->choice_space_above, 4);
374         break;
375     case VSpace::BIGSKIP:
376         fl_set_choice (dialog_->choice_space_above, 5);
377         break;
378     case VSpace::VFILL:
379         fl_set_choice (dialog_->choice_space_above, 6);
380         break;
381     case VSpace::LENGTH:
382         setEnabled(dialog_->input_space_above, true);
383         fl_set_choice (dialog_->choice_space_above, 7);
384         fl_set_input(dialog_->input_space_above, par_->
385                      params().spaceTop().length().asString().c_str());
386         break;
387     }
388     
389     fl_set_button (dialog_->check_space_above,
390                    par_->params().spaceTop().keep());
391     fl_set_input (dialog_->input_space_below, "");
392
393     setEnabled(dialog_->input_space_below, false);
394     switch (par_->params().spaceBottom().kind()) {
395     case VSpace::NONE:
396         fl_set_choice (dialog_->choice_space_below, 1);
397         break;
398     case VSpace::DEFSKIP:
399         fl_set_choice (dialog_->choice_space_below, 2);
400         break;
401     case VSpace::SMALLSKIP:
402         fl_set_choice (dialog_->choice_space_below, 3);
403         break;
404     case VSpace::MEDSKIP:
405         fl_set_choice (dialog_->choice_space_below, 4);
406         break;
407     case VSpace::BIGSKIP:
408         fl_set_choice (dialog_->choice_space_below, 5);
409         break;
410     case VSpace::VFILL:
411         fl_set_choice (dialog_->choice_space_below, 6);
412         break;
413     case VSpace::LENGTH:
414         setEnabled(dialog_->input_space_below, true);
415         fl_set_choice (dialog_->choice_space_below, 7);
416         fl_set_input(dialog_->input_space_below, par_->
417                      params().spaceBottom().length().asString().c_str());
418         break;
419     }
420     fl_set_button(dialog_->check_space_below,
421                    par_->params().spaceBottom().keep());
422     fl_set_button(dialog_->check_noindent,
423                   par_->params().noindent());
424 }
425
426
427 bool FormParagraph::input(FL_OBJECT * ob, long)
428 {
429     bool valid = true; 
430
431     fl_hide_object(dialog_->text_warning);
432
433     // First check the buttons which are exclusive and you have to
434     // check only the actuall de/activated button.
435     //
436     // "Synchronize" the choices and input fields, making it
437     // impossible to commit senseless data.
438
439     if (ob == dialog_->choice_space_above) {
440         if (fl_get_choice (dialog_->choice_space_above) != 7) {
441             fl_set_input (dialog_->input_space_above, "");
442             setEnabled (dialog_->input_space_above, false);
443         } else {
444             setEnabled (dialog_->input_space_above, !lv_->buffer()->isReadonly());
445         }
446     }
447     if (ob == dialog_->choice_space_below) {
448         if (fl_get_choice (dialog_->choice_space_below) != 7) {
449             fl_set_input (dialog_->input_space_below, "");
450             setEnabled (dialog_->input_space_below, false);
451         } else {
452             setEnabled (dialog_->input_space_below, !lv_->buffer()->isReadonly());
453         }
454     }
455  
456     if (fl_get_choice (dialog_->choice_linespacing) == 5)
457         setEnabled (dialog_->input_linespacing, true);
458     else {
459         setEnabled (dialog_->input_linespacing, false);
460         fl_set_input (dialog_->input_linespacing, "");
461     }
462
463     string input = fl_get_input (dialog_->input_space_above);
464         
465     if (fl_get_choice(dialog_->choice_space_above)==7 &&
466         input.empty() || !isValidGlueLength(input))
467         valid = false;
468
469     if (ob == dialog_->input_space_above) {
470         if (!isValidGlueLength(input)) {
471             fl_set_object_label(dialog_->text_warning,
472                 _("Warning: Invalid Length (valid example: 10mm)"));
473             fl_show_object(dialog_->text_warning);
474             valid = false;
475         } else
476             fl_hide_object(dialog_->text_warning);
477     }
478
479     input = fl_get_input (dialog_->input_space_below);
480
481     if (fl_get_choice(dialog_->choice_space_below)==7 &&
482         input.empty() || !isValidGlueLength(input))
483         valid = false;
484
485     if (ob == dialog_->input_space_below) {
486         if (!isValidGlueLength(input)) {
487             fl_set_object_label(dialog_->text_warning,
488                 _("Warning: Invalid Length (valid example: 10mm)"));
489             fl_show_object(dialog_->text_warning);
490             valid = false;
491         } else
492             fl_hide_object(dialog_->text_warning);
493     }
494
495     double spacing(strToDbl(fl_get_input(dialog_->input_linespacing)));
496
497     if (fl_get_choice (dialog_->choice_linespacing) == 5
498         && int(spacing) == 0)
499         valid = false;
500
501     return valid;
502 }