]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormParagraph.C
6224a88ffad024fff257b424d94e75022ed94d39
[lyx.git] / src / frontends / xforms / FormParagraph.C
1 /**
2  * \file FormParagraph.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Rob Lahaye
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "FormParagraph.h"
15 #include "ControlParagraph.h"
16 #include "forms/form_paragraph.h"
17
18 #include "checkedwidgets.h"
19 #include "input_validators.h"
20 #include "Tooltips.h"
21 #include "xforms_helpers.h"
22 #include "xformsBC.h"
23
24 #include "controllers/helper_funcs.h"
25
26 #include "lyxrc.h" // to set the deafult length values
27 #include "ParagraphParameters.h"
28 #include "Spacing.h"
29
30 #include "support/lstrings.h"
31 #include "support/tostr.h"
32
33 #include "lyx_forms.h"
34
35 #include <boost/bind.hpp>
36
37 using boost::bind;
38
39 using std::remove_if;
40
41 using std::vector;
42 using std::string;
43
44 namespace lyx {
45
46 using support::contains;
47 using support::getStringFromVector;
48 using support::rtrim;
49
50 namespace frontend {
51
52 namespace {
53
54 string defaultUnit("cm");
55
56 } // namespace anon
57
58
59 typedef FormController<ControlParagraph, FormView<FD_paragraph> > base_class;
60
61 FormParagraph::FormParagraph(Dialog & parent)
62         : base_class(parent, _("Paragraph Settings"))
63 {}
64
65
66 void FormParagraph::build()
67 {
68         // the tabbed folder
69         dialog_.reset(build_paragraph(this));
70
71         // Manage the ok, apply, restore and cancel/close buttons
72         bcview().setOK(dialog_->button_ok);
73         bcview().setApply(dialog_->button_apply);
74         bcview().setCancel(dialog_->button_close);
75         bcview().setRestore(dialog_->button_restore);
76
77         // disable for read-only documents
78         bcview().addReadOnly(dialog_->check_noindent);
79         bcview().addReadOnly(dialog_->choice_linespacing);
80         bcview().addReadOnly(dialog_->input_linespacing);
81
82         bcview().addReadOnly(dialog_->input_labelwidth);
83
84         // trigger an input event for cut&paste with middle mouse button.
85         setPrehandler(dialog_->input_linespacing);
86         setPrehandler(dialog_->input_labelwidth);
87
88         fl_set_input_return(dialog_->input_labelwidth,  FL_RETURN_CHANGED);
89         fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
90
91         // limit these inputs to unsigned floats
92         fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
93
94         // add alignment radio buttons
95         alignment_.init(dialog_->radio_align_left,   LYX_ALIGN_LEFT);
96         alignment_.init(dialog_->radio_align_right,  LYX_ALIGN_RIGHT);
97         alignment_.init(dialog_->radio_align_block,  LYX_ALIGN_BLOCK);
98         alignment_.init(dialog_->radio_align_center, LYX_ALIGN_CENTER);
99
100         string const linespacing = _("Default|Single|OneHalf|Double|Custom");
101         fl_addto_choice(dialog_->choice_linespacing, linespacing.c_str());
102
103         // Create the contents of the unit choices; don't include the "%" terms.
104         vector<string> units_vec = getLatexUnits();
105         vector<string>::iterator del =
106                 remove_if(units_vec.begin(), units_vec.end(),
107                           bind(contains<char>, _1, '%'));
108         units_vec.erase(del, units_vec.end());
109
110         // set default unit for custom length
111         switch (lyxrc.default_papersize) {
112                 case PAPER_DEFAULT:
113                 case PAPER_USLETTER:
114                 case PAPER_LEGALPAPER:
115                 case PAPER_EXECUTIVEPAPER:
116                         defaultUnit = "in";
117                         break;
118                 case PAPER_A3PAPER:
119                 case PAPER_A4PAPER:
120                 case PAPER_A5PAPER:
121                 case PAPER_B5PAPER:
122                         defaultUnit = "cm";
123                         break;
124         }
125 }
126
127
128 void FormParagraph::apply()
129 {
130         if (!form())
131                 return;
132
133         // alignment
134         LyXAlignment const alignment =
135                 static_cast<LyXAlignment>(alignment_.get());
136         controller().params().align(alignment);
137
138         // label width
139         string const labelwidthstring =
140                 getString(dialog_->input_labelwidth);
141         controller().params().labelWidthString(labelwidthstring);
142
143         // indendation
144         bool const noindent = fl_get_button(dialog_->check_noindent);
145         controller().params().noindent(noindent);
146
147         // get spacing
148         Spacing::Space linespacing = Spacing::Default;
149         string other;
150         switch (fl_get_choice(dialog_->choice_linespacing)) {
151         case 1:
152                 linespacing = Spacing::Default;
153                 break;
154         case 2:
155                 linespacing = Spacing::Single;
156                 break;
157         case 3:
158                 linespacing = Spacing::Onehalf;
159                 break;
160         case 4:
161                 linespacing = Spacing::Double;
162                 break;
163         case 5:
164                 // reset to default if input is empty
165                 other = getString(dialog_->input_linespacing);
166                 if (!other.empty()) {
167                         linespacing = Spacing::Other;
168                 } else {
169                         linespacing = Spacing::Default;
170                         fl_set_choice(dialog_->choice_linespacing, 1);
171                 }
172                 break;
173         }
174         Spacing const spacing(linespacing, other);
175         controller().params().spacing(spacing);
176 }
177
178
179 void FormParagraph::update()
180 {
181         if (!dialog_.get())
182                 return;
183
184         // label width
185         string const labelwidth = controller().params().labelWidthString();
186         fl_set_input(dialog_->input_labelwidth, labelwidth.c_str());
187         setEnabled(dialog_->input_labelwidth,
188                    labelwidth != _("Senseless with this layout!"));
189
190         // alignment
191         alignment_.set(controller().params().align());
192
193         // mark default alignment
194         LyXAlignment const default_alignment = controller().alignDefault();
195
196         string label = _("Block");
197         if (default_alignment == LYX_ALIGN_BLOCK) {
198                 label += _(" (default)");
199         }
200         fl_set_object_label(dialog_->radio_align_block, label.c_str());
201         fl_set_button_shortcut(dialog_->radio_align_block, "#B", 1);
202
203         label = _("Center");
204         if (default_alignment == LYX_ALIGN_CENTER) {
205                 label += _(" (default)");
206         }
207         fl_set_object_label(dialog_->radio_align_center, label.c_str());
208         fl_set_button_shortcut(dialog_->radio_align_center, "#C", 1);
209
210         label = _("Left");
211         if (default_alignment == LYX_ALIGN_LEFT) {
212                 label += _(" (default)");
213         }
214         fl_set_object_label(dialog_->radio_align_left, label.c_str());
215         fl_set_button_shortcut(dialog_->radio_align_left, "#L", 1);
216
217         label = _("Right");
218         if (default_alignment == LYX_ALIGN_RIGHT) {
219                 label = _(" (default)");
220         }
221         fl_set_object_label(dialog_->radio_align_right, label.c_str());
222         fl_set_button_shortcut(dialog_->radio_align_right, "#R", 1);
223
224         // Ensure that there's no crud left on the screen from this change
225         // of labels.
226         fl_redraw_form(form());
227
228         LyXAlignment alignpos = controller().alignPossible();
229         setEnabled(dialog_->radio_align_block,
230                    bool(alignpos & LYX_ALIGN_BLOCK));
231         setEnabled(dialog_->radio_align_center,
232                    bool(alignpos & LYX_ALIGN_CENTER));
233         setEnabled(dialog_->radio_align_left,
234                    bool(alignpos & LYX_ALIGN_LEFT));
235         setEnabled(dialog_->radio_align_right,
236                    bool(alignpos & LYX_ALIGN_RIGHT));
237
238         // lines, pagebreaks and indent
239         fl_set_button(dialog_->check_noindent,
240                       controller().params().noindent());
241
242         // linespacing
243         Spacing const space = controller().params().spacing();
244
245         int pos;
246         switch (space.getSpace()) {
247         case Spacing::Other:
248                 pos = 5;
249                 break;
250         case Spacing::Double:
251                 pos = 4;
252                 break;
253         case Spacing::Onehalf:
254                 pos = 3;
255                 break;
256         case Spacing::Single:
257                 pos = 2;
258                 break;
259         case Spacing::Default:
260         default:
261                 pos = 1;
262                 break;
263         }
264         fl_set_choice(dialog_->choice_linespacing, pos);
265
266         bool const spacing_other = space.getSpace() == Spacing::Other;
267         setEnabled(dialog_->input_linespacing, spacing_other);
268         if (spacing_other) {
269                 string const sp = tostr(space.getValue());
270                 fl_set_input(dialog_->input_linespacing, sp.c_str());
271         } else {
272                 fl_set_input(dialog_->input_linespacing, "");
273         }
274
275         // no indent
276         fl_set_button(dialog_->check_noindent,
277                       controller().params().noindent());
278 }
279
280
281 ButtonPolicy::SMInput FormParagraph::input(FL_OBJECT * ob, long)
282 {
283         // Enable input when custum length is choosen,
284         // disable 'keep' when no space is choosen
285         if (ob == dialog_->choice_linespacing) {
286                 bool const custom_spacing =
287                         fl_get_choice(dialog_->choice_linespacing) == 5;
288                 setEnabled(dialog_->input_linespacing, custom_spacing);
289         }
290
291         return ButtonPolicy::SMI_VALID;
292 }
293
294 } // namespace frontend
295 } // namespace lyx