]> git.lyx.org Git - lyx.git/blob - src/ParagraphExtra.C
Angus insetindex patch + protect patch from Dekel
[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
22 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
31 void ActivateParagraphExtraButtons ()
32 {
33         fl_activate_object(fd_form_paragraph_extra->button_ok);
34         fl_activate_object(fd_form_paragraph_extra->button_apply);
35         fl_set_object_lcol(fd_form_paragraph_extra->button_ok, FL_BLACK);
36         fl_set_object_lcol(fd_form_paragraph_extra->button_apply, FL_BLACK);
37 }
38
39 inline
40 void DisableParagraphExtra ()
41 {
42         DeactivateParagraphExtraButtons();
43         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
44         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
45         fl_deactivate_object(fd_form_paragraph_extra->group_alignment);
46         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
47         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
48         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
49         fl_deactivate_object(fd_form_paragraph_extra->text_warning);
50         fl_deactivate_object(fd_form_paragraph_extra->group_extraopt);
51         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_indent);
52         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_minipage);
53         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_floatflt);
54         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_hfill);
55         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_startmp);
56 }       
57
58 inline
59 void EnableParagraphExtra ()
60 {
61         ActivateParagraphExtraButtons();
62         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
63         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
64         fl_activate_object(fd_form_paragraph_extra->group_alignment);
65         fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
66         fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
67         fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
68         fl_activate_object(fd_form_paragraph_extra->text_warning);
69         fl_activate_object(fd_form_paragraph_extra->group_extraopt);
70         fl_activate_object(fd_form_paragraph_extra->radio_pextra_indent);
71         fl_activate_object(fd_form_paragraph_extra->radio_pextra_minipage);
72         fl_activate_object(fd_form_paragraph_extra->radio_pextra_floatflt);
73         fl_activate_object(fd_form_paragraph_extra->radio_pextra_hfill);
74         fl_activate_object(fd_form_paragraph_extra->radio_pextra_startmp);
75 }       
76
77
78 bool UpdateParagraphExtra()
79 {
80         bool update = false;
81         if (current_view->available()) {
82                 update = true;
83                 LyXParagraph * par = current_view->text->cursor.par();
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->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                 LyXText * text = current_view->text;
179                 int type = LyXParagraph::PEXTRA_NONE;
180                 LyXParagraph::MINIPAGE_ALIGNMENT alignment =
181                         LyXParagraph::MINIPAGE_ALIGN_TOP;
182                 bool
183                         hfill = false,
184                         start_minipage = false;
185
186                 if (fl_get_button(fd_form_paragraph_extra->radio_pextra_indent)) {
187                         type = LyXParagraph::PEXTRA_INDENT;
188                 } else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_minipage)) {
189                         type = LyXParagraph::PEXTRA_MINIPAGE;
190                         hfill = fl_get_button(fd_form_paragraph_extra->radio_pextra_hfill);
191                         start_minipage = 
192                                 fl_get_button(fd_form_paragraph_extra->radio_pextra_startmp);
193                         if (fl_get_button(fd_form_paragraph_extra->radio_pextra_top))
194                                 alignment = LyXParagraph::MINIPAGE_ALIGN_TOP;
195                         else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_middle))
196                                 alignment = LyXParagraph::MINIPAGE_ALIGN_MIDDLE;
197                         else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_bottom))
198                                 alignment = LyXParagraph::MINIPAGE_ALIGN_BOTTOM;
199                 } else if (fl_get_button(fd_form_paragraph_extra->radio_pextra_floatflt)) {
200                         type = LyXParagraph::PEXTRA_FLOATFLT;
201                 }
202                 text->SetParagraphExtraOpt(current_view, type, width, widthp, alignment, hfill,
203                                            start_minipage);
204                 current_view->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
205                 current_view->owner()->getMiniBuffer()->
206                         Set(_("ParagraphExtra layout set"));
207         }
208         return;
209 }
210
211
212 void ParagraphExtraCancelCB(FL_OBJECT *, long)
213 {
214         fl_hide_form(fd_form_paragraph_extra->form_paragraph_extra);
215 }
216
217
218 void ParagraphExtraOKCB(FL_OBJECT * ob, long data)
219 {
220         ParagraphExtraCancelCB(ob, data);
221         ParagraphExtraApplyCB(ob, data);
222 }
223
224
225 void CheckPExtraOptCB(FL_OBJECT * ob, long)
226 {
227         ActivateParagraphExtraButtons();
228         CheckInputWidth();
229         if (ob == fd_form_paragraph_extra->radio_pextra_indent) {
230                 int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_indent);
231                 if (n) {
232                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
233                         fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
234                         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
235                         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
236                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
237                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
238                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
239                 } else {
240                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
241                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
242                         ActivateParagraphExtraButtons();
243                 }
244         } else if (ob == fd_form_paragraph_extra->radio_pextra_minipage) {
245                 int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_minipage);
246                 if (n) {
247                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
248                         fl_set_button(fd_form_paragraph_extra->radio_pextra_floatflt, 0);
249                         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
250                         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
251                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
252                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
253                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
254                 } else {
255                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
256                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
257                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
258                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
259                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
260                         ActivateParagraphExtraButtons();
261                 }
262         } else if (ob == fd_form_paragraph_extra->radio_pextra_floatflt) {
263                 int n = fl_get_button(fd_form_paragraph_extra->radio_pextra_floatflt);
264                 if (n) {
265                         fl_set_button(fd_form_paragraph_extra->radio_pextra_indent, 0);
266                         fl_set_button(fd_form_paragraph_extra->radio_pextra_minipage, 0);
267                         fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
268                         fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
269                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_top);
270                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_middle);
271                         fl_activate_object(fd_form_paragraph_extra->radio_pextra_bottom);
272                 } else {
273                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
274                         fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
275                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_top);
276                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_middle);
277                         fl_deactivate_object(fd_form_paragraph_extra->radio_pextra_bottom);
278                         ActivateParagraphExtraButtons();
279                 }
280         }
281         return;
282 }
283
284
285 static
286 bool CheckInputWidth()
287 {
288         string s1 = fl_get_input(fd_form_paragraph_extra->input_pextra_width);
289         string s2 = fl_get_input(fd_form_paragraph_extra->input_pextra_widthp);
290         if (s1.empty() && s2.empty()) {
291                 fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
292                 fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
293                 fl_hide_object(fd_form_paragraph_extra->text_warning);
294                 DeactivateParagraphExtraButtons();
295                 return false;
296         }
297         if (!s1.empty()) { // LyXLength parameter
298                 fl_activate_object(fd_form_paragraph_extra->input_pextra_width);
299                 fl_deactivate_object(fd_form_paragraph_extra->input_pextra_widthp);
300                 if (!isValidLength(s1)) {
301                         DeactivateParagraphExtraButtons();
302                         fl_set_object_label(fd_form_paragraph_extra->text_warning,
303                                             _("Warning: Invalid Length (valid example: 10mm)"));
304                         fl_show_object(fd_form_paragraph_extra->text_warning);
305                         return false;
306                 }
307         } else { // !s2.empty() % parameter
308                 fl_deactivate_object(fd_form_paragraph_extra->input_pextra_width);
309                 fl_activate_object(fd_form_paragraph_extra->input_pextra_widthp);
310                 if ((atoi(s2.c_str()) < 0 ) || (atoi(s2.c_str()) > 100)) {
311                         DeactivateParagraphExtraButtons();
312                         fl_set_object_label(fd_form_paragraph_extra->text_warning,
313                                             _("Warning: Invalid percent value (0-100)"));
314                         fl_show_object(fd_form_paragraph_extra->text_warning);
315                         return false;
316                 }
317         }
318         fl_hide_object(fd_form_paragraph_extra->text_warning);
319         return true;
320 }