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