2 * FormPrint Interface Class Implementation
6 #include "lyx_gui_misc.h"
8 #include FORMS_H_LOCATION
10 #include "FormPrint.h"
11 #include "form_print.h"
12 #include "xform_macros.h"
13 #include "input_validators.h"
16 #include "support/lstrings.h"
18 #include "PrinterParams.h"
21 #include "BufferView.h"
24 #ifdef SIGC_CXX_NAMESPACES
28 #ifdef CXX_WORKING_NAMESPACES
29 using Liason::printBuffer;
30 using Liason::getPrinterParams;
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)
40 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
41 : dialog_(0), lv_(lv), d_(d), u_(0), h_(0)
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));
50 FormPrint::~FormPrint()
56 void FormPrint::build()
58 dialog_ = build_print();
62 void FormPrint::show()
66 // allow controlling of input and ok/apply (de)activation
67 fl_set_input_return(dialog_->input_printer,
69 fl_set_input_return(dialog_->input_file,
71 fl_set_input_return(dialog_->input_from_page,
73 fl_set_input_return(dialog_->input_to_page,
75 fl_set_input_return(dialog_->input_count,
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);
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
93 fl_set_form_atclose(dialog_->form_print,
94 C_FormPrintWMHideCB, 0);
97 update(); // make sure its up-to-date
99 if (dialog_->form_print->visible) {
100 fl_raise_form(dialog_->form_print);
102 fl_show_form(dialog_->form_print,
103 FL_PLACE_MOUSE | FL_FREE_SIZE,
106 u_ = d_->updateBufferDependent.connect(slot(this,
107 &FormPrint::update));
108 h_ = d_->hideBufferDependent.connect(slot(this,
114 void FormPrint::hide()
117 && dialog_->form_print
118 && dialog_->form_print->visible) {
119 fl_hide_form(dialog_->form_print);
126 void FormPrint::apply()
128 if (!lv_->view()->available()) {
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;
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.
150 PrinterParams::Target t(PrinterParams::PRINTER);
151 if (fl_get_button(dialog_->radio_file)) {
152 t = PrinterParams::FILE;
155 // we really should use the return value here I think.
156 if (!printBuffer(lv_->buffer(),
158 string(fl_get_input(dialog_->input_printer)),
159 string(fl_get_input(dialog_->input_file)),
161 static_cast<bool>(fl_get_button(dialog_->
162 radio_order_reverse)),
163 static_cast<bool>(fl_get_button(dialog_->
165 strToInt(fl_get_input(dialog_->input_count))))) {
166 WriteAlert(_("Error:"),
167 _("Unable to print"),
168 _("Check that your parameters are correct"));
173 void FormPrint::update()
176 && lv_->view()->available()) {
177 PrinterParams pp(getPrinterParams(lv_->buffer()));
179 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
180 fl_set_input(dialog_->input_file, pp.file_name.c_str());
183 case PrinterParams::FILE:
184 fl_set_button(dialog_->radio_printer, 0);
185 fl_set_button(dialog_->radio_file, 1);
188 case PrinterParams::PRINTER:
190 fl_set_button(dialog_->radio_printer, 1);
191 fl_set_button(dialog_->radio_file, 0);
195 switch (pp.reverse_order) {
197 fl_set_button(dialog_->radio_order_normal, 0);
198 fl_set_button(dialog_->radio_order_reverse, 1);
203 fl_set_button(dialog_->radio_order_normal, 1);
204 fl_set_button(dialog_->radio_order_reverse, 0);
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);
217 case PrinterParams::EVEN:
218 fl_set_button(dialog_->radio_even_pages, 1);
221 case PrinterParams::ALL:
223 fl_set_button(dialog_->radio_all_pages, 1);
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);
237 fl_set_input(dialog_->input_to_page,
238 tostr(pp.to_page).c_str());
240 fl_set_input(dialog_->input_to_page,"");
243 fl_deactivate_object(dialog_->input_to_page);
244 fl_set_input(dialog_->input_to_page,"");
245 fl_set_input(dialog_->input_from_page,"");
248 fl_set_input(dialog_->input_count,
249 tostr(pp.count_copies).c_str());
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
266 // It would be nice if we checked for cases like:
267 // Print only-odd-pages and from_page == an even number
269 void FormPrint::input()
271 bool activate = true;
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.
287 // set both backgrounds to red?
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
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);
299 if (fl_get_button(dialog_->radio_file)
300 && !strlen(fl_get_input(dialog_->input_file))) {
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))) {
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);
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);
325 void FormPrint::free()
327 // we don't need to delete u and h here because
328 // hide() does that after disconnecting.
330 if (dialog_->form_print
331 && dialog_->form_print->visible) {
334 fl_free_form(dialog_->form_print);
341 int FormPrint::WMHideCB(FL_FORM * form, void *)
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);
351 void FormPrint::OKCB(FL_OBJECT * ob, long)
353 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
359 void FormPrint::ApplyCB(FL_OBJECT * ob, long)
361 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
366 void FormPrint::CancelCB(FL_OBJECT * ob, long)
368 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
373 void FormPrint::InputCB(FL_OBJECT * ob, long)
375 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);