]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormParagraph.C
Squash compiler warnings.
[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/convert.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 typedef FormController<ControlParagraph, FormView<FD_paragraph> > base_class;
53
54 FormParagraph::FormParagraph(Dialog & parent)
55         : base_class(parent, _("Paragraph Settings"))
56 {}
57
58
59 void FormParagraph::build()
60 {
61         // the tabbed folder
62         dialog_.reset(build_paragraph(this));
63
64         // Manage the ok, apply, restore and cancel/close buttons
65         bcview().setOK(dialog_->button_ok);
66         bcview().setApply(dialog_->button_apply);
67         bcview().setCancel(dialog_->button_close);
68         bcview().setRestore(dialog_->button_restore);
69
70         // disable for read-only documents
71         bcview().addReadOnly(dialog_->check_noindent);
72         bcview().addReadOnly(dialog_->choice_linespacing);
73         bcview().addReadOnly(dialog_->input_linespacing);
74
75         bcview().addReadOnly(dialog_->input_labelwidth);
76
77         // trigger an input event for cut&paste with middle mouse button.
78         setPrehandler(dialog_->input_linespacing);
79         setPrehandler(dialog_->input_labelwidth);
80
81         fl_set_input_return(dialog_->input_labelwidth,  FL_RETURN_CHANGED);
82         fl_set_input_return(dialog_->input_linespacing, FL_RETURN_CHANGED);
83
84         // limit these inputs to unsigned floats
85         fl_set_input_filter(dialog_->input_linespacing, fl_unsigned_float_filter);
86
87         // add alignment radio buttons
88         alignment_.init(dialog_->radio_align_left,   LYX_ALIGN_LEFT);
89         alignment_.init(dialog_->radio_align_right,  LYX_ALIGN_RIGHT);
90         alignment_.init(dialog_->radio_align_block,  LYX_ALIGN_BLOCK);
91         alignment_.init(dialog_->radio_align_center, LYX_ALIGN_CENTER);
92
93         string const linespacing = _("Default|Single|OneHalf|Double|Custom");
94         fl_addto_choice(dialog_->choice_linespacing, linespacing.c_str());
95
96         // Create the contents of the unit choices; don't include the "%" terms.
97         vector<string> units_vec = getLatexUnits();
98         vector<string>::iterator del =
99                 remove_if(units_vec.begin(), units_vec.end(),
100                           bind(contains<char>, _1, '%'));
101         units_vec.erase(del, units_vec.end());
102 }
103
104
105 void FormParagraph::apply()
106 {
107         if (!form())
108                 return;
109
110         // alignment
111         LyXAlignment const alignment =
112                 static_cast<LyXAlignment>(alignment_.get());
113         controller().params().align(alignment);
114
115         // label width
116         string const labelwidthstring =
117                 getString(dialog_->input_labelwidth);
118         controller().params().labelWidthString(labelwidthstring);
119
120         // indendation
121         bool const noindent = fl_get_button(dialog_->check_noindent);
122         controller().params().noindent(noindent);
123
124         // get spacing
125         Spacing::Space linespacing = Spacing::Default;
126         string other;
127         switch (fl_get_choice(dialog_->choice_linespacing)) {
128         case 1:
129                 linespacing = Spacing::Default;
130                 break;
131         case 2:
132                 linespacing = Spacing::Single;
133                 break;
134         case 3:
135                 linespacing = Spacing::Onehalf;
136                 break;
137         case 4:
138                 linespacing = Spacing::Double;
139                 break;
140         case 5:
141                 // reset to default if input is empty
142                 other = getString(dialog_->input_linespacing);
143                 if (!other.empty()) {
144                         linespacing = Spacing::Other;
145                 } else {
146                         linespacing = Spacing::Default;
147                         fl_set_choice(dialog_->choice_linespacing, 1);
148                 }
149                 break;
150         }
151         Spacing const spacing(linespacing, other);
152         controller().params().spacing(spacing);
153 }
154
155
156 void FormParagraph::update()
157 {
158         if (!dialog_.get())
159                 return;
160
161         // label width
162         string const labelwidth = controller().params().labelWidthString();
163         fl_set_input(dialog_->input_labelwidth, labelwidth.c_str());
164         setEnabled(dialog_->input_labelwidth,
165                    labelwidth != _("Senseless with this layout!"));
166
167         // alignment
168         alignment_.set(controller().params().align());
169
170         // mark default alignment
171         LyXAlignment const default_alignment = controller().alignDefault();
172
173         string label = _("Block");
174         if (default_alignment == LYX_ALIGN_BLOCK) {
175                 label += _(" (default)");
176         }
177         fl_set_object_label(dialog_->radio_align_block, label.c_str());
178         fl_set_button_shortcut(dialog_->radio_align_block, "#B", 1);
179
180         label = _("Center");
181         if (default_alignment == LYX_ALIGN_CENTER) {
182                 label += _(" (default)");
183         }
184         fl_set_object_label(dialog_->radio_align_center, label.c_str());
185         fl_set_button_shortcut(dialog_->radio_align_center, "#C", 1);
186
187         label = _("Left");
188         if (default_alignment == LYX_ALIGN_LEFT) {
189                 label += _(" (default)");
190         }
191         fl_set_object_label(dialog_->radio_align_left, label.c_str());
192         fl_set_button_shortcut(dialog_->radio_align_left, "#L", 1);
193
194         label = _("Right");
195         if (default_alignment == LYX_ALIGN_RIGHT) {
196                 label = _(" (default)");
197         }
198         fl_set_object_label(dialog_->radio_align_right, label.c_str());
199         fl_set_button_shortcut(dialog_->radio_align_right, "#R", 1);
200
201         // Ensure that there's no crud left on the screen from this change
202         // of labels.
203         fl_redraw_form(form());
204
205         LyXAlignment alignpos = controller().alignPossible();
206         setEnabled(dialog_->radio_align_block,
207                    bool(alignpos & LYX_ALIGN_BLOCK));
208         setEnabled(dialog_->radio_align_center,
209                    bool(alignpos & LYX_ALIGN_CENTER));
210         setEnabled(dialog_->radio_align_left,
211                    bool(alignpos & LYX_ALIGN_LEFT));
212         setEnabled(dialog_->radio_align_right,
213                    bool(alignpos & LYX_ALIGN_RIGHT));
214
215         // lines, pagebreaks and indent
216         fl_set_button(dialog_->check_noindent,
217                       controller().params().noindent());
218
219         // linespacing
220         Spacing const space = controller().params().spacing();
221
222         int pos;
223         switch (space.getSpace()) {
224         case Spacing::Other:
225                 pos = 5;
226                 break;
227         case Spacing::Double:
228                 pos = 4;
229                 break;
230         case Spacing::Onehalf:
231                 pos = 3;
232                 break;
233         case Spacing::Single:
234                 pos = 2;
235                 break;
236         case Spacing::Default:
237         default:
238                 pos = 1;
239                 break;
240         }
241         fl_set_choice(dialog_->choice_linespacing, pos);
242
243         bool const spacing_other = space.getSpace() == Spacing::Other;
244         setEnabled(dialog_->input_linespacing, spacing_other);
245         if (spacing_other) {
246                 fl_set_input(dialog_->input_linespacing,
247                         space.getValueAsString().c_str());
248         } else {
249                 fl_set_input(dialog_->input_linespacing, "");
250         }
251
252         // no indent
253         fl_set_button(dialog_->check_noindent,
254                       controller().params().noindent());
255 }
256
257
258 ButtonPolicy::SMInput FormParagraph::input(FL_OBJECT * ob, long)
259 {
260         // Enable input when custum length is choosen,
261         // disable 'keep' when no space is choosen
262         if (ob == dialog_->choice_linespacing) {
263                 bool const custom_spacing =
264                         fl_get_choice(dialog_->choice_linespacing) == 5;
265                 setEnabled(dialog_->input_linespacing, custom_spacing);
266         }
267
268         return ButtonPolicy::SMI_VALID;
269 }
270
271 } // namespace frontend
272 } // namespace lyx