2 * FormPrint Interface Class Implementation
6 #include "lyx_gui_misc.h"
8 #include FORMS_H_LOCATION
10 #include "FormPrint.h"
11 #include "xform_macros.h"
12 #include "input_validators.h"
15 #include "support/lstrings.h"
17 #include "PrinterParams.h"
20 #include "BufferView.h"
23 #ifdef SIGC_CXX_NAMESPACES
27 #ifdef CXX_WORKING_NAMESPACES
28 using Liason::printBuffer;
29 using Liason::getPrinterParams;
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)
39 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
40 : dialog_(0), lv_(lv), d_(d), u_(0), h_(0)
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));
49 FormPrint::~FormPrint()
55 void FormPrint::build()
57 dialog_ = build_print();
61 void FormPrint::show()
65 // allow controlling of input and ok/apply (de)activation
66 fl_set_input_return(dialog_->input_printer,
68 fl_set_input_return(dialog_->input_file,
70 fl_set_input_return(dialog_->input_from_page,
72 fl_set_input_return(dialog_->input_to_page,
74 fl_set_input_return(dialog_->input_count,
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);
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
92 fl_set_form_atclose(dialog_->form_print,
93 C_FormPrintWMHideCB, 0);
96 update(); // make sure its up-to-date
98 if (dialog_->form_print->visible) {
99 fl_raise_form(dialog_->form_print);
101 fl_show_form(dialog_->form_print,
102 FL_PLACE_MOUSE | FL_FREE_SIZE,
105 u_ = d_->updateBufferDependent.connect(slot(this,
106 &FormPrint::update));
107 h_ = d_->hideBufferDependent.connect(slot(this,
113 void FormPrint::hide()
116 && dialog_->form_print
117 && dialog_->form_print->visible) {
118 fl_hide_form(dialog_->form_print);
125 void FormPrint::apply()
127 if (!lv_->view()->available()) {
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;
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.
149 PrinterParams::Target t(PrinterParams::PRINTER);
150 if (fl_get_button(dialog_->radio_file)) {
151 t = PrinterParams::FILE;
154 // we really should use the return value here I think.
155 if (!printBuffer(lv_->buffer(),
157 string(fl_get_input(dialog_->input_printer)),
158 string(fl_get_input(dialog_->input_file)),
160 static_cast<bool>(fl_get_button(dialog_->
161 radio_order_reverse)),
162 static_cast<bool>(fl_get_button(dialog_->
164 strToInt(fl_get_input(dialog_->input_count))))) {
165 WriteAlert(_("Error:"),
166 _("Unable to print"),
167 _("Check that your parameters are correct"));
172 void FormPrint::update()
175 && lv_->view()->available()) {
176 PrinterParams pp(getPrinterParams(lv_->buffer()));
178 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
179 fl_set_input(dialog_->input_file, pp.file_name.c_str());
182 case PrinterParams::FILE:
183 fl_set_button(dialog_->radio_printer, 0);
184 fl_set_button(dialog_->radio_file, 1);
187 case PrinterParams::PRINTER:
189 fl_set_button(dialog_->radio_printer, 1);
190 fl_set_button(dialog_->radio_file, 0);
194 switch (pp.reverse_order) {
196 fl_set_button(dialog_->radio_order_normal, 0);
197 fl_set_button(dialog_->radio_order_reverse, 1);
202 fl_set_button(dialog_->radio_order_normal, 1);
203 fl_set_button(dialog_->radio_order_reverse, 0);
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);
216 case PrinterParams::EVEN:
217 fl_set_button(dialog_->radio_even_pages, 1);
220 case PrinterParams::ALL:
222 fl_set_button(dialog_->radio_all_pages, 1);
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);
236 fl_set_input(dialog_->input_to_page,
237 tostr(pp.to_page).c_str());
239 fl_set_input(dialog_->input_to_page,"");
242 fl_deactivate_object(dialog_->input_to_page);
243 fl_set_input(dialog_->input_to_page,"");
244 fl_set_input(dialog_->input_from_page,"");
247 fl_set_input(dialog_->input_count,
248 tostr(pp.count_copies).c_str());
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
265 // It would be nice if we checked for cases like:
266 // Print only-odd-pages and from_page == an even number
268 void FormPrint::input()
270 bool activate = true;
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.
286 // set both backgrounds to red?
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
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);
298 if (fl_get_button(dialog_->radio_file)
299 && !strlen(fl_get_input(dialog_->input_file))) {
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))) {
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);
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);
324 void FormPrint::free()
326 // we don't need to delete u and h here because
327 // hide() does that after disconnecting.
329 if (dialog_->form_print
330 && dialog_->form_print->visible) {
333 fl_free_form(dialog_->form_print);
340 int FormPrint::WMHideCB(FL_FORM * form, void *)
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);
350 void FormPrint::OKCB(FL_OBJECT * ob, long)
352 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
358 void FormPrint::ApplyCB(FL_OBJECT * ob, long)
360 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
365 void FormPrint::CancelCB(FL_OBJECT * ob, long)
367 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
372 void FormPrint::InputCB(FL_OBJECT * ob, long)
374 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);