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