]> git.lyx.org Git - lyx.git/blob - src/PaperLayout.C
Small fix.
[lyx.git] / src / PaperLayout.C
1 #include <config.h>
2
3 #include <cstdlib>
4
5 #include FORMS_H_LOCATION
6 #include "layout_forms.h"
7 #include "lyx_main.h"
8 #include "lyxrc.h"
9 #include "LString.h"
10 #include "support/filetools.h"
11 #include "buffer.h"
12 #include "minibuffer.h"
13 #include "vspace.h"
14 #include "lyx_gui_misc.h"
15 #include "BufferView.h"
16 #include "gettext.h"
17 #include "LyXView.h"
18
19 extern FD_form_paper * fd_form_paper;
20 extern BufferView * current_view;
21
22
23 inline
24 void DeactivatePaperButtons ()
25 {
26         fl_deactivate_object (fd_form_paper->button_ok);
27         fl_deactivate_object (fd_form_paper->button_apply);
28         fl_set_object_lcol (fd_form_paper->button_ok, FL_INACTIVE);
29         fl_set_object_lcol (fd_form_paper->button_apply, FL_INACTIVE);
30 }
31
32
33 inline
34 void ActivatePaperButtons ()
35 {
36         fl_activate_object (fd_form_paper->button_ok);
37         fl_activate_object (fd_form_paper->button_apply);
38         fl_set_object_lcol (fd_form_paper->button_ok, FL_BLACK);
39         fl_set_object_lcol (fd_form_paper->button_apply, FL_BLACK);
40 }
41
42
43 inline
44 void DisablePaperLayout()
45 {
46         DeactivatePaperButtons();
47         fl_deactivate_object (fd_form_paper->choice_paperpackage);
48         fl_deactivate_object (fd_form_paper->greoup_radio_orientation);
49         fl_deactivate_object (fd_form_paper->radio_portrait);
50         fl_deactivate_object (fd_form_paper->radio_landscape);
51         fl_deactivate_object (fd_form_paper->choice_papersize2);
52         fl_deactivate_object (fd_form_paper->push_use_geometry);
53         fl_deactivate_object (fd_form_paper->input_custom_width);
54         fl_deactivate_object (fd_form_paper->input_custom_height);
55         fl_deactivate_object (fd_form_paper->input_top_margin);
56         fl_deactivate_object (fd_form_paper->input_bottom_margin);
57         fl_deactivate_object (fd_form_paper->input_left_margin);
58         fl_deactivate_object (fd_form_paper->input_right_margin);
59         fl_deactivate_object (fd_form_paper->input_head_height);
60         fl_deactivate_object (fd_form_paper->input_head_sep);
61         fl_deactivate_object (fd_form_paper->input_foot_skip);
62         fl_deactivate_object (fd_form_paper->text_warning);
63 }
64
65
66 inline
67 void EnablePaperLayout()
68 {
69         ActivatePaperButtons();
70         fl_activate_object (fd_form_paper->choice_paperpackage);
71         fl_activate_object (fd_form_paper->greoup_radio_orientation);
72         fl_activate_object (fd_form_paper->radio_portrait);
73         fl_activate_object (fd_form_paper->radio_landscape);
74         fl_activate_object (fd_form_paper->choice_papersize2);
75         fl_activate_object (fd_form_paper->push_use_geometry);
76         fl_activate_object (fd_form_paper->input_custom_width);
77         fl_activate_object (fd_form_paper->input_custom_height);
78         fl_activate_object (fd_form_paper->input_top_margin);
79         fl_activate_object (fd_form_paper->input_bottom_margin);
80         fl_activate_object (fd_form_paper->input_left_margin);
81         fl_activate_object (fd_form_paper->input_right_margin);
82         fl_activate_object (fd_form_paper->input_head_height);
83         fl_activate_object (fd_form_paper->input_head_sep);
84         fl_activate_object (fd_form_paper->input_foot_skip);
85         fl_activate_object (fd_form_paper->text_warning);
86 }
87
88
89 static
90 void checkMarginValues()
91 {
92         int allEmpty;
93     
94         fl_hide_object(fd_form_paper->text_warning);
95         ActivatePaperButtons();
96
97         allEmpty = (!strlen(fl_get_input(fd_form_paper->input_top_margin)) &&
98                     !strlen(fl_get_input(fd_form_paper->input_bottom_margin)) &&
99                     !strlen(fl_get_input(fd_form_paper->input_left_margin)) &&
100                     !strlen(fl_get_input(fd_form_paper->input_right_margin)) &&
101                     !strlen(fl_get_input(fd_form_paper->input_head_height)) &&
102                     !strlen(fl_get_input(fd_form_paper->input_head_sep)) &&
103                     !strlen(fl_get_input(fd_form_paper->input_foot_skip)) &&
104                     !strlen(fl_get_input(fd_form_paper->input_custom_width)) &&
105                     !strlen(fl_get_input(fd_form_paper->input_custom_height)));
106         if (!allEmpty) {
107                 fl_set_button(fd_form_paper->push_use_geometry, 1);
108         }
109 }
110
111
112 bool UpdateLayoutPaper()
113 {
114         bool update = true;
115         if (!current_view->available())
116                 update = false;
117
118         if (update) {
119                 BufferParams * params = &current_view->buffer()->params;
120
121                 fl_set_choice(fd_form_paper->choice_papersize2,
122                               params->papersize2 + 1);
123                 fl_set_choice(fd_form_paper->choice_paperpackage,
124                               params->paperpackage + 1);
125                 fl_set_button(fd_form_paper->push_use_geometry,
126                               params->use_geometry);
127                 fl_set_button(fd_form_paper->radio_portrait, 0);
128                 fl_set_button(fd_form_paper->radio_landscape, 0);
129                 if (params->orientation == BufferParams::ORIENTATION_LANDSCAPE)
130                         fl_set_button(fd_form_paper->radio_landscape, 1);
131                 else
132                         fl_set_button(fd_form_paper->radio_portrait, 1);
133    
134                 fl_set_input(fd_form_paper->input_custom_width,
135                              params->paperwidth.c_str());
136                 fl_set_input(fd_form_paper->input_custom_height,
137                              params->paperheight.c_str());
138                 fl_set_input(fd_form_paper->input_left_margin,
139                              params->leftmargin.c_str());
140                 fl_set_input(fd_form_paper->input_top_margin,
141                              params->topmargin.c_str());
142                 fl_set_input(fd_form_paper->input_right_margin,
143                              params->rightmargin.c_str());
144                 fl_set_input(fd_form_paper->input_bottom_margin,
145                              params->bottommargin.c_str());
146                 fl_set_input(fd_form_paper->input_head_height,
147                              params->headheight.c_str());
148                 fl_set_input(fd_form_paper->input_head_sep,
149                              params->headsep.c_str());
150                 fl_set_input(fd_form_paper->input_foot_skip,
151                              params->footskip.c_str());
152                 fl_set_focus_object(fd_form_paper->form_paper,
153                                     fd_form_paper->choice_papersize2);
154                 fl_hide_object(fd_form_paper->text_warning);
155                 if (current_view->buffer()->isReadonly()) {
156                         DisablePaperLayout();
157                         fl_set_object_label(fd_form_paper->text_warning,
158                                             _("Document is read-only."
159                                               "  No changes to layout"
160                                               " permitted."));
161                         fl_show_object(fd_form_paper->text_warning);
162                 } else {
163                         EnablePaperLayout();
164                 }
165         } else if (fd_form_paper->form_paper->visible) {
166                 fl_hide_form(fd_form_paper->form_paper);
167         }
168         return update;
169 }
170
171
172 void MenuLayoutPaper()
173 {
174         if (UpdateLayoutPaper()) {
175                 if (fd_form_paper->form_paper->visible) {
176                         fl_raise_form(fd_form_paper->form_paper);
177                 } else {
178                         fl_show_form(fd_form_paper->form_paper,
179                                      FL_PLACE_MOUSE, FL_FULLBORDER,
180                                      _("Paper Layout"));
181                 }
182         }
183 }
184
185
186 void PaperApplyCB(FL_OBJECT *, long)
187 {
188         if (!current_view->available()) 
189                 return;
190         
191         BufferParams * params = &current_view->buffer()->params;
192         
193         FD_form_paper const * fd = fd_form_paper;
194     
195         params->papersize2 = fl_get_choice(fd->choice_papersize2)-1;
196         params->paperpackage = fl_get_choice(fd->choice_paperpackage)-1;
197         params->use_geometry = fl_get_button(fd_form_paper->push_use_geometry);
198         if (fl_get_button(fd_form_paper->radio_landscape))
199                 params->orientation = BufferParams::ORIENTATION_LANDSCAPE;
200         else
201                 params->orientation = BufferParams::ORIENTATION_PORTRAIT;
202         params->paperwidth = fl_get_input(fd->input_custom_width);
203         params->paperheight = fl_get_input(fd->input_custom_height);
204         params->leftmargin = fl_get_input(fd->input_left_margin);
205         params->topmargin = fl_get_input(fd->input_top_margin);
206         params->rightmargin = fl_get_input(fd->input_right_margin);
207         params->bottommargin = fl_get_input(fd->input_bottom_margin);
208         params->headheight = fl_get_input(fd->input_head_height);
209         params->headsep = fl_get_input(fd->input_head_sep);
210         params->footskip = fl_get_input(fd->input_foot_skip);
211         current_view->buffer()->setPaperStuff();
212         current_view->owner()->getMiniBuffer()->Set(_("Paper layout set"));
213         current_view->buffer()->markDirty();
214         
215         return;
216 }
217
218
219 void PaperCancelCB(FL_OBJECT *, long)
220 {
221         fl_hide_form(fd_form_paper->form_paper);
222 }
223
224
225 void PaperOKCB(FL_OBJECT * ob, long data)
226 {
227         PaperCancelCB(ob, data);
228         PaperApplyCB(ob, data);
229 }
230
231
232 void PaperMarginsCB(FL_OBJECT * ob, long)
233 {
234         FD_form_paper const * fd = fd_form_paper;
235         string str;
236         char val;
237     
238         checkMarginValues();
239         if (ob == fd->choice_papersize2) {
240                 val = fl_get_choice(fd->choice_papersize2)-1;
241                 if (val == BufferParams::VM_PAPER_DEFAULT) {
242                         fl_set_button(fd->push_use_geometry, 0);
243                         checkMarginValues();
244                 } else {
245                         if (val != BufferParams::VM_PAPER_USLETTER
246                             && val != BufferParams::VM_PAPER_USLEGAL
247                             && val != BufferParams::VM_PAPER_USEXECUTIVE
248                             && val != BufferParams::VM_PAPER_A4
249                             && val != BufferParams::VM_PAPER_A5
250                             && val != BufferParams::VM_PAPER_B5) {
251                                 fl_set_button(fd->push_use_geometry, 1);
252                         }
253                         fl_set_choice(fd->choice_paperpackage, BufferParams::PACKAGE_NONE + 1);
254                 }
255         } else if (ob == fd->choice_paperpackage) {
256                 val = fl_get_choice(fd->choice_paperpackage)-1;
257                 if (val != BufferParams::PACKAGE_NONE) {
258                         fl_set_choice(fd->choice_papersize2,
259                                       BufferParams::VM_PAPER_DEFAULT + 1);
260                         fl_set_button(fd->push_use_geometry, 0);
261                 }
262         } else if (ob == fd->input_custom_width
263                    || ob == fd->input_custom_height 
264                    || ob == fd->input_left_margin
265                    || ob == fd->input_right_margin 
266                    || ob == fd->input_top_margin
267                    || ob == fd->input_bottom_margin 
268                    || ob == fd->input_head_height
269                    || ob == fd->input_head_sep
270                    || ob == fd->input_foot_skip) {
271                 str = fl_get_input(ob);
272                 if (!str.empty() && !isValidLength(str)) {
273                         fl_set_object_label(fd_form_paper->text_warning,
274                                             _("Warning: Invalid Length"
275                                               " (valid example: 10mm)"));
276                         fl_show_object(fd_form_paper->text_warning);
277                         DeactivatePaperButtons();
278                 } else if (!str.empty())
279                         fl_set_button(fd->push_use_geometry, 1);
280         }
281         return;
282 }