]> git.lyx.org Git - lyx.git/blob - src/ParagraphExtra.C
update libtool, try to make it work for std::streams (be VERY careful I have experien...
[lyx.git] / src / ParagraphExtra.C
1 #include <config.h>
2
3 #include <cstdlib>
4 #include FORMS_H_LOCATION
5
6 #include "layout_forms.h"
7 #include "LString.h"
8 #include "minibuffer.h"
9 #include "vspace.h"
10 #include "buffer.h"
11 #include "BufferView.h"
12 #include "lyxparagraph.h"
13 #include "gettext.h"
14 #include "lyxtext.h"
15 #include "LyXView.h"
16
17 extern FD_form_paragraph_extra * fd_form_paragraph_extra;
18 extern BufferView * current_view;
19 static bool CheckInputWidth();
20
21 inline void DeactivateParagraphExtraButtons ()
22 {
23         fl_deactivate_object(fd_form_paragraph_extra->button_ok);
24         fl_deactivate_object(fd_form_paragraph_extra->button_apply);
25         fl_set_object_lcol(fd_form_paragraph_extra->button_ok, FL_INACTIVE);
26         fl_set_object_lcol(fd_form_paragraph_extra->button_apply, FL_INACTIVE);
27 }
28
29 inline void ActivateParagraphExtraButtons ()
30 {
31         fl_activate_object(fd_form_paragraph_extra->button_ok);
32         fl_activate_object(fd_form_paragraph_extra->button_apply);
33         fl_set_object_lcol(fd_form_paragraph_extra->button_ok, FL_BLACK);
34         fl_set_object_lcol(fd_form_paragraph_extra->button_apply, FL_BLACK);
35 }
36
37 inline void DisableParagraphExtra ()
38 {
39         DeactivateParagraphExtraButtons();
40         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
41         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
42         fl_deactivate_object(fd_form_paragraph_extra->group_alignment);
43         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
44         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
45         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
46         fl_deactivate_object(fd_form_paragraph_extra->text_warning);
47         fl_deactivate_object(fd_form_paragraph_extra->group_extraopt);
48         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_indent);
49         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_minipage);
50         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_floatflt);
51         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_hfill);
52         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_startmp);
53 }       
54
55 inline void EnableParagraphExtra ()
56 {
57         ActivateParagraphExtraButtons();
58         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
59         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
60         fl_activate_object(fd_form_paragraph_extra->group_alignment);
61         fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
62         fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
63         fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
64         fl_activate_object(fd_form_paragraph_extra->text_warning);
65         fl_activate_object(fd_form_paragraph_extra->group_extraopt);
66         fl_activate_object(fd_form_paragraph_extra->radio_pextra_indent);
67         fl_activate_object(fd_form_paragraph_extra->radio_pextra_minipage);
68         fl_activate_object(fd_form_paragraph_extra->radio_pextra_floatflt);
69         fl_activate_object(fd_form_paragraph_extra->radio_pextra_hfill);
70         fl_activate_object(fd_form_paragraph_extra->radio_pextra_startmp);
71 }       
72
73 bool UpdateParagraphExtra()
74 {
75         bool update = false;
76         if (current_view->getScreen() && current_view->available()) {
77                 update = true;
78                 LyXParagraph * par = current_view->text->cursor.par;
79
80                 EnableParagraphExtra();
81
82                 fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
83                 fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
84                 fl_set_input(fd_form_paragraph_extra->input_pextra_width,
85                              par->pextra_width.c_str());
86                 fl_set_input(fd_form_paragraph_extra->input_pextra_widthp,
87                              par->pextra_widthp.c_str());
88                 switch(par->pextra_alignment) {
89                 case LyXParagraph::MINIPAGE_ALIGN_TOP:
90                         fl_set_button(fd_form_paragraph_extra->radio_pextra_top, 1);
91                         break;
92                 case LyXParagraph::MINIPAGE_ALIGN_MIDDLE:
93                         fl_set_button(fd_form_paragraph_extra->radio_pextra_middle, 1);
94                         break;
95                 case LyXParagraph::MINIPAGE_ALIGN_BOTTOM:
96                         fl_set_button(fd_form_paragraph_extra->radio_pextra_bottom, 1);
97                         break;
98                 }
99                 fl_set_button(fd_form_paragraph_extra->radio_pextra_hfill,
100                               par->pextra_hfill);
101                 fl_set_button(fd_form_paragraph_extra->radio_pextra_startmp,
102                               par->pextra_start_minipage);
103                 CheckInputWidth();
104                 if (par->pextra_type == LyXParagraph::PEXTRA_INDENT) {
105                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 1);
106                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
107                         fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
108                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
109                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
110                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
111                 } else if (par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
112                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
113                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 1);
114                         fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
115                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
116                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
117                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
118                 } else if (par->pextra_type == LyXParagraph::PEXTRA_FLOATFLT) {
119                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
120                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
121                         fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 1);
122                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
123                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
124                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
125                 } else {
126                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
127                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
128                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
129                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
130                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
131                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
132                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
133                 }
134                 if (par->pextra_type == LyXParagraph::PEXTRA_NONE)
135                         ActivateParagraphExtraButtons();
136         }
137         fl_hide_object(fd_form_paragraph_extra->text_warning);
138
139         if (current_view->buffer()->isReadonly()) {
140                 DisableParagraphExtra();
141                 fl_set_object_label(fd_form_paragraph_extra->text_warning,
142                                     _("Document is read-only. "
143                                       "No changes to layout permitted."));
144                 fl_show_object(fd_form_paragraph_extra->text_warning);
145         }
146         if (!update && fd_form_paragraph_extra->form_paragraph_extra->visible) {
147                 fl_hide_form(fd_form_paragraph_extra->form_paragraph_extra);
148         }
149         return update;
150 }
151
152
153 void ParagraphExtraOpen(FL_OBJECT *, long)
154 {
155         if (UpdateParagraphExtra()) {
156                 if (fd_form_paragraph_extra->form_paragraph_extra->visible) {
157                         fl_raise_form(fd_form_paragraph_extra->form_paragraph_extra);
158                 } else {
159                         fl_show_form(fd_form_paragraph_extra->form_paragraph_extra,
160                                      FL_PLACE_MOUSE, FL_FULLBORDER,
161                                      _("ParagraphExtra Layout"));
162                 }
163         }
164 }
165
166
167 void ParagraphExtraApplyCB(FL_OBJECT *, long)
168 {
169         if (current_view->getScreen() && current_view->available()) {
170                 FD_form_paragraph_extra const * fd = fd_form_paragraph_extra;
171                 char const * width = fl_get_input(fd->input_pextra_width);
172                 char const * widthp = fl_get_input(fd->input_pextra_widthp);
173                 LyXText * text = current_view->text;
174                 int type = LyXParagraph::PEXTRA_NONE;
175                 LyXParagraph::MINIPAGE_ALIGNMENT alignment =
176                         LyXParagraph::MINIPAGE_ALIGN_TOP;
177                 bool
178                         hfill = false,
179                         start_minipage = false;
180
181                 if (fl_get_button(fd_form_paragraph_extra->radio_pextra_indent)) {
182                         type = LyXParagraph::PEXTRA_INDENT;
183                 } else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_minipage)) {
184                         type = LyXParagraph::PEXTRA_MINIPAGE;
185                         hfill = fl_get_button(fd_form_paragraph_extra->radio_pextra_hfill);
186                         start_minipage = 
187                                 fl_get_button(fd_form_paragraph_extra->radio_pextra_startmp);
188                         if (fl_get_button(fd_form_paragraph_extra->radio_pextra_top))
189                                 alignment = LyXParagraph::MINIPAGE_ALIGN_TOP;
190                         else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_middle))
191                                 alignment = LyXParagraph::MINIPAGE_ALIGN_MIDDLE;
192                         else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_bottom))
193                                 alignment = LyXParagraph::MINIPAGE_ALIGN_BOTTOM;
194                 } else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_floatflt)) {
195                         type = LyXParagraph::PEXTRA_FLOATFLT;
196                 }
197                 text->SetParagraphExtraOpt(type, width, widthp, alignment, hfill,
198                                            start_minipage);
199                 current_view->update(1);
200                 current_view->owner()->getMiniBuffer()->
201                         Set(_("ParagraphExtra layout set"));
202         }
203         return;
204 }
205
206
207 void ParagraphExtraCancelCB(FL_OBJECT *, long)
208 {
209         fl_hide_form(fd_form_paragraph_extra->form_paragraph_extra);
210 }
211
212
213 void ParagraphExtraOKCB(FL_OBJECT * ob, long data)
214 {
215         ParagraphExtraCancelCB(ob, data);
216         ParagraphExtraApplyCB(ob, data);
217 }
218
219
220 void CheckPExtraOptCB(FL_OBJECT * ob, long)
221 {
222         ActivateParagraphExtraButtons();
223         CheckInputWidth();
224         if (ob == fd_form_paragraph_extra->radio_pextra_indent) {
225                 int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_indent);
226                 if (n) {
227                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
228                         fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
229                         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
230                         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
231                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
232                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
233                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
234                 } else {
235                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
236                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
237                         ActivateParagraphExtraButtons();
238                 }
239         } else if (ob == fd_form_paragraph_extra->radio_pextra_minipage) {
240                 int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_minipage);
241                 if (n) {
242                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
243                         fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
244                         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
245                         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
246                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
247                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
248                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
249                 } else {
250                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
251                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
252                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
253                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
254                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
255                         ActivateParagraphExtraButtons();
256                 }
257         } else if (ob == fd_form_paragraph_extra->radio_pextra_floatflt) {
258                 int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_floatflt);
259                 if (n) {
260                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
261                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
262                         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
263                         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
264                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
265                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
266                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
267                 } else {
268                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
269                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
270                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
271                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
272                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
273                         ActivateParagraphExtraButtons();
274                 }
275         }
276         return;
277 }
278
279
280 static bool CheckInputWidth()
281 {
282         string s1 = fl_get_input(fd_form_paragraph_extra->input_pextra_width);
283         string s2 = fl_get_input(fd_form_paragraph_extra->input_pextra_widthp);
284         if (s1.empty() && s2.empty()) {
285                 fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
286                 fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
287                 fl_hide_object(fd_form_paragraph_extra->text_warning);
288                 DeactivateParagraphExtraButtons();
289                 return false;
290         }
291         if (!s1.empty()) { // LyXLength parameter
292                 fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
293                 fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
294                 if (!isValidLength(s1)) {
295                         DeactivateParagraphExtraButtons();
296                         fl_set_object_label(fd_form_paragraph_extra->text_warning,
297                                             _("Warning: Invalid Length (valid example: 10mm)"));
298                         fl_show_object(fd_form_paragraph_extra->text_warning);
299                         return false;
300                 }
301         } else { // !s2.empty() % parameter
302                 fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
303                 fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
304                 if ((atoi(s2.c_str()) < 0 ) || (atoi(s2.c_str()) > 100)) {
305                         DeactivateParagraphExtraButtons();
306                         fl_set_object_label(fd_form_paragraph_extra->text_warning,
307                                             _("Warning: Invalid percent value (0-100)"));
308                         fl_show_object(fd_form_paragraph_extra->text_warning);
309                         return false;
310                 }
311         }
312         fl_hide_object(fd_form_paragraph_extra->text_warning);
313         return true;
314 }