]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
70894485c563fcd82a75a754493870e27a7a2303
[lyx.git] / src / frontends / xforms / FormPrint.C
1 /* FormPrint.C
2  * FormPrint Interface Class Implementation
3  */
4
5 #include <config.h>
6 #include "lyx_gui_misc.h"
7 #include "gettext.h"
8 #include FORMS_H_LOCATION
9
10 #include "FormPrint.h"
11 #include "form_print.h"
12 #include "xform_macros.h"
13 #include "input_validators.h"
14 #include "LyXView.h"
15 #include "Dialogs.h"
16 #include "support/lstrings.h"
17 #include "lyxrc.h"
18 #include "PrinterParams.h"
19 #include "Liason.h"
20 #include "debug.h"
21 #include "BufferView.h"
22
23
24 #ifdef SIGC_CXX_NAMESPACES
25 using SigC::slot;
26 #endif
27
28 #ifdef CXX_WORKING_NAMESPACES
29 using Liason::printBuffer;
30 using Liason::getPrinterParams;
31 #endif
32
33 C_RETURNCB(FormPrint,  WMHideCB)
34 C_GENERICCB(FormPrint, OKCB)
35 C_GENERICCB(FormPrint, ApplyCB)
36 C_GENERICCB(FormPrint, CancelCB)
37 C_GENERICCB(FormPrint, InputCB)
38
39
40 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
41         : dialog_(0), lv_(lv), d_(d), u_(0), h_(0)
42 {
43         // let the dialog be shown
44         // This is a permanent connection so we won't bother
45         // storing a copy because we won't be disconnecting.
46         d->showPrint.connect(slot(this,&FormPrint::show));
47 }
48
49
50 FormPrint::~FormPrint()
51 {
52         free();
53 }
54
55
56 void FormPrint::build()
57 {
58         dialog_ = build_print();
59 }
60
61
62 void FormPrint::show()
63 {
64         if (!dialog_) {
65                 build();
66                 // allow controlling of input and ok/apply (de)activation
67                 fl_set_input_return(dialog_->input_printer,
68                                     FL_RETURN_CHANGED);
69                 fl_set_input_return(dialog_->input_file,
70                                     FL_RETURN_CHANGED);
71                 fl_set_input_return(dialog_->input_from_page,
72                                     FL_RETURN_CHANGED);
73                 fl_set_input_return(dialog_->input_to_page,
74                                     FL_RETURN_CHANGED);
75                 fl_set_input_return(dialog_->input_count,
76                                     FL_RETURN_CHANGED);
77
78                 // limit these inputs to unsigned integers
79                 fl_set_input_filter(dialog_->input_from_page,
80                                     fl_unsigned_int_filter);
81                 fl_set_input_filter(dialog_->input_to_page,
82                                     fl_unsigned_int_filter);
83                 fl_set_input_filter(dialog_->input_count,
84                                     fl_unsigned_int_filter);
85
86                 // what limits (if any) make sense for these?
87                 fl_set_input_maxchars(dialog_->input_printer, 255);
88                 fl_set_input_maxchars(dialog_->input_file, 255);
89                 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
90                 fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
91                 fl_set_input_maxchars(dialog_->input_count, 4);     // 9999
92                 
93                 fl_set_form_atclose(dialog_->form_print,
94                                     C_FormPrintWMHideCB, 0);
95         }
96
97         update();  // make sure its up-to-date
98
99         if (dialog_->form_print->visible) {
100                 fl_raise_form(dialog_->form_print);
101         } else {
102                 fl_show_form(dialog_->form_print,
103                              FL_PLACE_MOUSE | FL_FREE_SIZE,
104                              FL_FULLBORDER,
105                              _("Print"));
106                 u_ = d_->updateBufferDependent.connect(slot(this,
107                                                             &FormPrint::update));
108                 h_ = d_->hideBufferDependent.connect(slot(this,
109                                                           &FormPrint::hide));
110         }
111 }
112
113
114 void FormPrint::hide()
115 {
116         if (dialog_
117             && dialog_->form_print
118             && dialog_->form_print->visible) {
119                 fl_hide_form(dialog_->form_print);
120                 u_.disconnect();
121                 h_.disconnect();
122         }
123 }
124
125
126 void FormPrint::apply()
127 {
128         if (!lv_->view()->available()) {
129                 return;
130         }
131
132         PrinterParams::WhichPages wp(PrinterParams::ALL);
133         if (fl_get_button(dialog_->radio_even_pages)) {
134                 wp = PrinterParams::EVEN;
135         } else if (fl_get_button(dialog_->radio_odd_pages)) {
136                 wp = PrinterParams::ODD;
137         }
138
139         string from;
140         int to(0);
141         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
142                 // we have at least one page requested
143                 from = fl_get_input(dialog_->input_from_page);
144                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
145                         // okay we have a range
146                         to = strToInt(fl_get_input(dialog_->input_to_page));
147                 } // else we only print one page.
148         }
149
150         PrinterParams::Target t(PrinterParams::PRINTER);
151         if (fl_get_button(dialog_->radio_file)) {
152                 t = PrinterParams::FILE;
153         }
154
155         // we really should use the return value here I think.
156         if (!printBuffer(lv_->buffer(),
157                          PrinterParams(t,
158                                        string(fl_get_input(dialog_->input_printer)),
159                                        string(fl_get_input(dialog_->input_file)),
160                                        wp, from, to,
161                                        static_cast<bool>(fl_get_button(dialog_->
162                                                                        radio_order_reverse)),
163                                        static_cast<bool>(fl_get_button(dialog_->
164                                                                        radio_unsorted)),
165                                        strToInt(fl_get_input(dialog_->input_count))))) {
166                 WriteAlert(_("Error:"),
167                            _("Unable to print"),
168                            _("Check that your parameters are correct"));
169         }
170 }
171
172
173 void FormPrint::update()
174 {
175         if (dialog_
176             && lv_->view()->available()) {
177                 PrinterParams pp(getPrinterParams(lv_->buffer()));
178
179                 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
180                 fl_set_input(dialog_->input_file, pp.file_name.c_str());
181
182                 switch (pp.target) {
183                 case PrinterParams::FILE:
184                         fl_set_button(dialog_->radio_printer, 0);
185                         fl_set_button(dialog_->radio_file, 1);
186                         break;
187
188                 case PrinterParams::PRINTER:
189                 default:
190                         fl_set_button(dialog_->radio_printer, 1);
191                         fl_set_button(dialog_->radio_file, 0);
192                         break;
193                 }
194
195                 switch (pp.reverse_order) {
196                 case true:
197                         fl_set_button(dialog_->radio_order_normal, 0);
198                         fl_set_button(dialog_->radio_order_reverse, 1);
199                         break;
200
201                 case false:
202                 default:
203                         fl_set_button(dialog_->radio_order_normal, 1);
204                         fl_set_button(dialog_->radio_order_reverse, 0);
205                         break;
206                 }
207 // should be able to remove the various set_button 0 and rely on radio button
208 // action.  Provided xforms is smart enough :D
209                 fl_set_button(dialog_->radio_all_pages, 0);
210                 fl_set_button(dialog_->radio_odd_pages, 0);
211                 fl_set_button(dialog_->radio_even_pages, 0);
212                 switch (pp.which_pages) {
213                 case PrinterParams::ODD:
214                         fl_set_button(dialog_->radio_odd_pages, 1);
215                         break;
216
217                 case PrinterParams::EVEN:
218                         fl_set_button(dialog_->radio_even_pages, 1);
219                         break;
220
221                 case PrinterParams::ALL:
222                 default:
223                         fl_set_button(dialog_->radio_all_pages, 1);
224                         break;
225                 }
226
227                 // hmmm... maybe a bit weird but maybe not
228                 // we might just be remembering the last
229                 // time this was printed.
230                 if (!pp.from_page.empty()) {
231                         fl_set_input(dialog_->input_from_page,
232                                      pp.from_page.c_str());
233                         // we only set the "to" page of a range
234                         // if there's a corresponding "from"
235                         fl_activate_object(dialog_->input_to_page);
236                         if (pp.to_page) {
237                                 fl_set_input(dialog_->input_to_page,
238                                              tostr(pp.to_page).c_str());
239                         } else {
240                                 fl_set_input(dialog_->input_to_page,"");
241                         }
242                 } else {
243                         fl_deactivate_object(dialog_->input_to_page);
244                         fl_set_input(dialog_->input_to_page,"");
245                         fl_set_input(dialog_->input_from_page,"");
246                 }
247
248                 fl_set_input(dialog_->input_count,
249                              tostr(pp.count_copies).c_str());
250
251                 // Even readonly docs can be printed
252                 // these 4 activations are probably superfluous but I'm
253                 // being explicit for a reason.
254                 // They can probably be removed soon along with a few more
255                 // of the de/activations above once input() is a bit smarter.
256                 fl_activate_object(dialog_->input_count);
257                 fl_activate_object(dialog_->input_file);
258                 fl_activate_object(dialog_->input_from_page);
259                 fl_activate_object(dialog_->input_printer);
260                 // and we should always be in a working state upon exit
261                 input();
262         }
263 }
264
265
266 // It would be nice if we checked for cases like:
267 // Print only-odd-pages and from_page == an even number
268 //
269 void FormPrint::input()
270 {
271         bool activate = true;
272
273         // using a fl_input_filter that only permits numbers no '-' or '+'
274         // and the user cannot enter a negative number even if they try.
275         if (strlen(fl_get_input(dialog_->input_from_page))) {
276                 // using a page range so activate the "to" field
277                 fl_activate_object(dialog_->input_to_page);
278                 if (strlen(fl_get_input(dialog_->input_to_page))
279                     && (strToInt(fl_get_input(dialog_->input_from_page))
280                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
281                         // both from and to have values but from > to
282                         // We could have code to silently swap these
283                         // values but I'll disable the ok/apply until
284                         // the user fixes it since they may be editting
285                         // one of the fields.
286                         activate = false;
287                         // set both backgrounds to red?
288                 }
289         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
290                 // from is empty but to exists so probably editting from
291                 // therefore deactivate ok and apply until form is valid again
292                 activate = false;
293         } else {
294                 // both from and to are empty.  This is valid so activate
295                 // ok and apply but deactivate to
296                 fl_deactivate_object(dialog_->input_to_page);
297         }
298
299         if (fl_get_button(dialog_->radio_file)
300             && !strlen(fl_get_input(dialog_->input_file))) {
301                 activate = false;
302         }
303
304         // it is probably legal to have no printer name since the system will
305         // have a default printer set.  Or should have.
306 //      if (fl_get_button(dialog_->radio_printer)
307 //          && !strlen(fl_get_input(dialog_->input_printer))) {
308 //              activate = false;
309 //      }
310
311         if (activate) {
312                 fl_activate_object(dialog_->button_ok);
313                 fl_activate_object(dialog_->button_apply);
314                 fl_set_object_lcol(dialog_->button_ok, FL_BLACK);
315                 fl_set_object_lcol(dialog_->button_apply, FL_BLACK);
316         } else {
317                 fl_deactivate_object(dialog_->button_ok);
318                 fl_deactivate_object(dialog_->button_apply);
319                 fl_set_object_lcol(dialog_->button_ok, FL_INACTIVE);
320                 fl_set_object_lcol(dialog_->button_apply, FL_INACTIVE);
321         }
322 }
323
324
325 void FormPrint::free()
326 {
327         // we don't need to delete u and h here because
328         // hide() does that after disconnecting.
329         if (dialog_) {
330                 if (dialog_->form_print
331                     && dialog_->form_print->visible) {
332                         hide();
333                 }
334                 fl_free_form(dialog_->form_print);
335                 delete dialog_;
336                 dialog_ = 0;
337         }
338 }
339
340
341 int FormPrint::WMHideCB(FL_FORM * form, void *)
342 {
343         // Ensure that the signals (u and h) are disconnected even if the
344         // window manager is used to close the dialog.
345         FormPrint * pre = static_cast<FormPrint*>(form->u_vdata);
346         pre->hide();
347         return FL_CANCEL;
348 }
349
350
351 void FormPrint::OKCB(FL_OBJECT * ob, long)
352 {
353         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
354         pre->apply();
355         pre->hide();
356 }
357
358
359 void FormPrint::ApplyCB(FL_OBJECT * ob, long)
360 {
361         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
362         pre->apply();
363 }
364
365
366 void FormPrint::CancelCB(FL_OBJECT * ob, long)
367 {
368         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
369         pre->hide();
370 }
371
372
373 void FormPrint::InputCB(FL_OBJECT * ob, long)
374 {
375         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
376         pre->input();
377 }