]> git.lyx.org Git - features.git/blob - src/ParagraphExtra.C
68556bd83ceecc5f3e49aa97ffd3e04dda43ee77
[features.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
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 #ifdef MOVE_TEXT
80         LyXParagraph * par = current_view->text->cursor.par;
81 #else
82         LyXParagraph * par = current_view->buffer()->text->cursor.par;
83 #endif
84
85         EnableParagraphExtra();
86
87         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
88         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
89         fl_set_input(fd_form_paragraph_extra->input_pextra_width,
90                      par->pextra_width.c_str());
91         fl_set_input(fd_form_paragraph_extra->input_pextra_widthp,
92                      par->pextra_widthp.c_str());
93         switch(par->pextra_alignment) {
94           case LyXParagraph::MINIPAGE_ALIGN_TOP:
95             fl_set_button(fd_form_paragraph_extra->radio_pextra_top, 1);
96             break;
97           case LyXParagraph::MINIPAGE_ALIGN_MIDDLE:
98             fl_set_button(fd_form_paragraph_extra->radio_pextra_middle, 1);
99             break;
100           case LyXParagraph::MINIPAGE_ALIGN_BOTTOM:
101             fl_set_button(fd_form_paragraph_extra->radio_pextra_bottom, 1);
102             break;
103         }
104         fl_set_button(fd_form_paragraph_extra->radio_pextra_hfill,
105                       par->pextra_hfill);
106         fl_set_button(fd_form_paragraph_extra->radio_pextra_startmp,
107                       par->pextra_start_minipage);
108         CheckInputWidth();
109         if (par->pextra_type == LyXParagraph::PEXTRA_INDENT) {
110             fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 1);
111             fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
112             fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
113             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
114             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
115             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
116         } else if (par->pextra_type == LyXParagraph::PEXTRA_MINIPAGE) {
117             fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
118             fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 1);
119             fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
120             fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
121             fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
122             fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
123         } else if (par->pextra_type == LyXParagraph::PEXTRA_FLOATFLT) {
124             fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
125             fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
126             fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 1);
127             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
128             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
129             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
130         } else {
131             fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
132             fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
133             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
134             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
135             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
136             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
137             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
138         }
139         if (par->pextra_type == LyXParagraph::PEXTRA_NONE)
140             ActivateParagraphExtraButtons();
141     }
142     fl_hide_object(fd_form_paragraph_extra->text_warning);
143
144     if (current_view->buffer()->isReadonly()) {
145       DisableParagraphExtra();
146       fl_set_object_label(fd_form_paragraph_extra->text_warning,
147                           _("Document is read-only. "
148                             "No changes to layout permitted."));
149       fl_show_object(fd_form_paragraph_extra->text_warning);
150     }
151     if (!update && fd_form_paragraph_extra->form_paragraph_extra->visible) {
152       fl_hide_form(fd_form_paragraph_extra->form_paragraph_extra);
153     }
154     return update;
155 }
156
157
158 void ParagraphExtraOpen(FL_OBJECT *, long)
159 {
160     if (UpdateParagraphExtra()) {
161         if (fd_form_paragraph_extra->form_paragraph_extra->visible) {
162             fl_raise_form(fd_form_paragraph_extra->form_paragraph_extra);
163         } else {
164             fl_show_form(fd_form_paragraph_extra->form_paragraph_extra,
165                          FL_PLACE_MOUSE, FL_FULLBORDER,
166                          _("ParagraphExtra Layout"));
167         }
168     }
169 }
170
171
172 void ParagraphExtraApplyCB(FL_OBJECT *, long)
173 {
174     if (current_view->getScreen() && current_view->available()) {
175         FD_form_paragraph_extra const * fd = fd_form_paragraph_extra;
176         char const * width = fl_get_input(fd->input_pextra_width);
177         char const * widthp = fl_get_input(fd->input_pextra_widthp);
178 #ifdef MOVE_TEXT
179         LyXText * text = current_view->text;
180 #else
181         LyXText * text = current_view->buffer()->text;
182 #endif
183         int type = LyXParagraph::PEXTRA_NONE;
184         LyXParagraph::MINIPAGE_ALIGNMENT alignment =
185                 LyXParagraph::MINIPAGE_ALIGN_TOP;
186         bool
187             hfill = false,
188             start_minipage = false;
189
190         if (fl_get_button(fd_form_paragraph_extra->radio_pextra_indent)) {
191             type = LyXParagraph::PEXTRA_INDENT;
192         } else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_minipage)) {
193             type = LyXParagraph::PEXTRA_MINIPAGE;
194             hfill = fl_get_button(fd_form_paragraph_extra->radio_pextra_hfill);
195             start_minipage = 
196                 fl_get_button(fd_form_paragraph_extra->radio_pextra_startmp);
197             if (fl_get_button(fd_form_paragraph_extra->radio_pextra_top))
198                 alignment = LyXParagraph::MINIPAGE_ALIGN_TOP;
199             else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_middle))
200                 alignment = LyXParagraph::MINIPAGE_ALIGN_MIDDLE;
201             else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_bottom))
202                 alignment = LyXParagraph::MINIPAGE_ALIGN_BOTTOM;
203         } else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_floatflt)) {
204             type = LyXParagraph::PEXTRA_FLOATFLT;
205         }
206         text->SetParagraphExtraOpt(type, width, widthp, alignment, hfill,
207                                    start_minipage);
208 #ifdef MOVE_TEXT
209         current_view->update(1);
210 #else
211         current_view->buffer()->update(1);
212 #endif
213         minibuffer->Set(_("ParagraphExtra layout set"));
214     }
215     return;
216 }
217
218
219 void ParagraphExtraCancelCB(FL_OBJECT *, long)
220 {
221     fl_hide_form(fd_form_paragraph_extra->form_paragraph_extra);
222 }
223
224
225 void ParagraphExtraOKCB(FL_OBJECT * ob, long data)
226 {
227     ParagraphExtraCancelCB(ob, data);
228     ParagraphExtraApplyCB(ob, data);
229 }
230
231
232 void CheckPExtraOptCB(FL_OBJECT * ob, long)
233 {
234     ActivateParagraphExtraButtons();
235     CheckInputWidth();
236     if (ob == fd_form_paragraph_extra->radio_pextra_indent) {
237         int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_indent);
238         if (n) {
239             fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
240             fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
241             fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
242             fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
243             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
244             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
245             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
246         } else {
247             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
248             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
249             ActivateParagraphExtraButtons();
250         }
251     } else if (ob == fd_form_paragraph_extra->radio_pextra_minipage) {
252         int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_minipage);
253         if (n) {
254             fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
255             fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
256             fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
257             fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
258             fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
259             fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
260             fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
261         } else {
262             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
263             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
264             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
265             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
266             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
267             ActivateParagraphExtraButtons();
268         }
269     } else if (ob == fd_form_paragraph_extra->radio_pextra_floatflt) {
270         int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_floatflt);
271         if (n) {
272             fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
273             fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
274             fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
275             fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
276             fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
277             fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
278             fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
279         } else {
280             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
281             fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
282             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
283             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
284             fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
285             ActivateParagraphExtraButtons();
286         }
287     }
288     return;
289 }
290
291
292 static bool CheckInputWidth()
293 {
294     string s1 = fl_get_input(fd_form_paragraph_extra->input_pextra_width);
295     string s2 = fl_get_input(fd_form_paragraph_extra->input_pextra_widthp);
296     if (s1.empty() && s2.empty()) {
297         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
298         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
299         fl_hide_object(fd_form_paragraph_extra->text_warning);
300         DeactivateParagraphExtraButtons();
301         return false;
302     }
303     if (!s1.empty()) { // LyXLength parameter
304         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
305         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
306         if (!isValidLength(s1)) {
307             DeactivateParagraphExtraButtons();
308             fl_set_object_label(fd_form_paragraph_extra->text_warning,
309                                 _("Warning: Invalid Length (valid example: 10mm)"));
310             fl_show_object(fd_form_paragraph_extra->text_warning);
311             return false;
312         }
313     } else { // !s2.empty() % parameter
314         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
315         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
316         if ((atoi(s2.c_str()) < 0 ) || (atoi(s2.c_str()) > 100)) {
317             DeactivateParagraphExtraButtons();
318             fl_set_object_label(fd_form_paragraph_extra->text_warning,
319                                 _("Warning: Invalid percent value (0-100)"));
320             fl_show_object(fd_form_paragraph_extra->text_warning);
321             return false;
322         }
323     }
324     fl_hide_object(fd_form_paragraph_extra->text_warning);
325     return true;
326 }