2 * FormPrint Interface Class Implementation
7 #include FORMS_H_LOCATION
10 #pragma implementation
13 #include "FormPrint.h"
14 #include "form_print.h"
15 #include "xform_macros.h"
16 #include "input_validators.h"
19 #include "support/lstrings.h"
21 #include "PrinterParams.h"
24 #include "BufferView.h"
25 #include "lyx_gui_misc.h"
29 #ifdef SIGC_CXX_NAMESPACES
33 #ifdef CXX_WORKING_NAMESPACES
34 using Liason::printBuffer;
35 using Liason::getPrinterParams;
38 C_RETURNCB(FormPrint, WMHideCB)
39 C_GENERICCB(FormPrint, OKCB)
40 C_GENERICCB(FormPrint, ApplyCB)
41 C_GENERICCB(FormPrint, CancelCB)
42 C_GENERICCB(FormPrint, InputCB)
45 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
46 : dialog_(0), lv_(lv), d_(d), u_(0), h_(0)
48 // let the dialog be shown
49 // This is a permanent connection so we won't bother
50 // storing a copy because we won't be disconnecting.
51 d->showPrint.connect(slot(this,&FormPrint::show));
55 FormPrint::~FormPrint()
61 void FormPrint::build()
63 dialog_ = build_print();
67 void FormPrint::show()
71 // allow controlling of input and ok/apply (de)activation
72 fl_set_input_return(dialog_->input_printer,
74 fl_set_input_return(dialog_->input_file,
76 fl_set_input_return(dialog_->input_from_page,
78 fl_set_input_return(dialog_->input_to_page,
80 fl_set_input_return(dialog_->input_count,
83 // limit these inputs to unsigned integers
84 fl_set_input_filter(dialog_->input_from_page,
85 fl_unsigned_int_filter);
86 fl_set_input_filter(dialog_->input_to_page,
87 fl_unsigned_int_filter);
88 fl_set_input_filter(dialog_->input_count,
89 fl_unsigned_int_filter);
91 // what limits (if any) make sense for these?
92 fl_set_input_maxchars(dialog_->input_printer, 255);
93 fl_set_input_maxchars(dialog_->input_file, 255);
94 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
95 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
96 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
98 fl_set_form_atclose(dialog_->form_print,
99 C_FormPrintWMHideCB, 0);
102 update(); // make sure its up-to-date
104 if (dialog_->form_print->visible) {
105 fl_raise_form(dialog_->form_print);
107 fl_show_form(dialog_->form_print,
108 FL_PLACE_MOUSE | FL_FREE_SIZE,
111 u_ = d_->updateBufferDependent.connect(slot(this,
112 &FormPrint::update));
113 h_ = d_->hideBufferDependent.connect(slot(this,
119 void FormPrint::hide()
122 && dialog_->form_print
123 && dialog_->form_print->visible) {
124 fl_hide_form(dialog_->form_print);
131 void FormPrint::apply()
133 if (!lv_->view()->available()) {
137 PrinterParams::WhichPages wp(PrinterParams::ALL);
138 if (fl_get_button(dialog_->radio_even_pages)) {
139 wp = PrinterParams::EVEN;
140 } else if (fl_get_button(dialog_->radio_odd_pages)) {
141 wp = PrinterParams::ODD;
146 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
147 // we have at least one page requested
148 from = fl_get_input(dialog_->input_from_page);
149 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
150 // okay we have a range
151 to = strToInt(fl_get_input(dialog_->input_to_page));
152 } // else we only print one page.
155 PrinterParams::Target t(PrinterParams::PRINTER);
156 if (fl_get_button(dialog_->radio_file)) {
157 t = PrinterParams::FILE;
160 // we really should use the return value here I think.
161 if (!printBuffer(lv_->buffer(),
163 string(fl_get_input(dialog_->input_printer)),
164 string(fl_get_input(dialog_->input_file)),
166 static_cast<bool>(fl_get_button(dialog_->
167 radio_order_reverse)),
168 static_cast<bool>(fl_get_button(dialog_->
170 strToInt(fl_get_input(dialog_->input_count))))) {
171 WriteAlert(_("Error:"),
172 _("Unable to print"),
173 _("Check that your parameters are correct"));
178 void FormPrint::update()
181 && lv_->view()->available()) {
182 PrinterParams pp(getPrinterParams(lv_->buffer()));
184 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
185 fl_set_input(dialog_->input_file, pp.file_name.c_str());
188 case PrinterParams::FILE:
189 fl_set_button(dialog_->radio_printer, 0);
190 fl_set_button(dialog_->radio_file, 1);
193 case PrinterParams::PRINTER:
195 fl_set_button(dialog_->radio_printer, 1);
196 fl_set_button(dialog_->radio_file, 0);
200 switch (pp.reverse_order) {
202 fl_set_button(dialog_->radio_order_normal, 0);
203 fl_set_button(dialog_->radio_order_reverse, 1);
208 fl_set_button(dialog_->radio_order_normal, 1);
209 fl_set_button(dialog_->radio_order_reverse, 0);
212 // should be able to remove the various set_button 0 and rely on radio button
213 // action. Provided xforms is smart enough :D
214 fl_set_button(dialog_->radio_all_pages, 0);
215 fl_set_button(dialog_->radio_odd_pages, 0);
216 fl_set_button(dialog_->radio_even_pages, 0);
217 switch (pp.which_pages) {
218 case PrinterParams::ODD:
219 fl_set_button(dialog_->radio_odd_pages, 1);
222 case PrinterParams::EVEN:
223 fl_set_button(dialog_->radio_even_pages, 1);
226 case PrinterParams::ALL:
228 fl_set_button(dialog_->radio_all_pages, 1);
232 // hmmm... maybe a bit weird but maybe not
233 // we might just be remembering the last
234 // time this was printed.
235 if (!pp.from_page.empty()) {
236 fl_set_input(dialog_->input_from_page,
237 pp.from_page.c_str());
238 // we only set the "to" page of a range
239 // if there's a corresponding "from"
240 fl_activate_object(dialog_->input_to_page);
242 fl_set_input(dialog_->input_to_page,
243 tostr(pp.to_page).c_str());
245 fl_set_input(dialog_->input_to_page,"");
248 fl_deactivate_object(dialog_->input_to_page);
249 fl_set_input(dialog_->input_to_page,"");
250 fl_set_input(dialog_->input_from_page,"");
253 fl_set_input(dialog_->input_count,
254 tostr(pp.count_copies).c_str());
256 // Even readonly docs can be printed
257 // these 4 activations are probably superfluous but I'm
258 // being explicit for a reason.
259 // They can probably be removed soon along with a few more
260 // of the de/activations above once input() is a bit smarter.
261 fl_activate_object(dialog_->input_count);
262 fl_activate_object(dialog_->input_file);
263 fl_activate_object(dialog_->input_from_page);
264 fl_activate_object(dialog_->input_printer);
265 // and we should always be in a working state upon exit
271 // It would be nice if we checked for cases like:
272 // Print only-odd-pages and from_page == an even number
274 void FormPrint::input()
276 bool activate = true;
278 // using a fl_input_filter that only permits numbers no '-' or '+'
279 // and the user cannot enter a negative number even if they try.
280 if (strlen(fl_get_input(dialog_->input_from_page))) {
281 // using a page range so activate the "to" field
282 fl_activate_object(dialog_->input_to_page);
283 if (strlen(fl_get_input(dialog_->input_to_page))
284 && (strToInt(fl_get_input(dialog_->input_from_page))
285 > strToInt(fl_get_input(dialog_->input_to_page)))) {
286 // both from and to have values but from > to
287 // We could have code to silently swap these
288 // values but I'll disable the ok/apply until
289 // the user fixes it since they may be editting
290 // one of the fields.
292 // set both backgrounds to red?
294 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
295 // from is empty but to exists so probably editting from
296 // therefore deactivate ok and apply until form is valid again
299 // both from and to are empty. This is valid so activate
300 // ok and apply but deactivate to
301 fl_deactivate_object(dialog_->input_to_page);
304 if (fl_get_button(dialog_->radio_file)
305 && !strlen(fl_get_input(dialog_->input_file))) {
309 // it is probably legal to have no printer name since the system will
310 // have a default printer set. Or should have.
311 // if (fl_get_button(dialog_->radio_printer)
312 // && !strlen(fl_get_input(dialog_->input_printer))) {
317 fl_activate_object(dialog_->button_ok);
318 fl_activate_object(dialog_->button_apply);
319 fl_set_object_lcol(dialog_->button_ok, FL_BLACK);
320 fl_set_object_lcol(dialog_->button_apply, FL_BLACK);
322 fl_deactivate_object(dialog_->button_ok);
323 fl_deactivate_object(dialog_->button_apply);
324 fl_set_object_lcol(dialog_->button_ok, FL_INACTIVE);
325 fl_set_object_lcol(dialog_->button_apply, FL_INACTIVE);
330 void FormPrint::free()
332 // we don't need to delete u and h here because
333 // hide() does that after disconnecting.
335 if (dialog_->form_print
336 && dialog_->form_print->visible) {
339 fl_free_form(dialog_->form_print);
346 int FormPrint::WMHideCB(FL_FORM * form, void *)
348 // Ensure that the signals (u and h) are disconnected even if the
349 // window manager is used to close the dialog.
350 FormPrint * pre = static_cast<FormPrint*>(form->u_vdata);
356 void FormPrint::OKCB(FL_OBJECT * ob, long)
358 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
364 void FormPrint::ApplyCB(FL_OBJECT * ob, long)
366 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
371 void FormPrint::CancelCB(FL_OBJECT * ob, long)
373 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
378 void FormPrint::InputCB(FL_OBJECT * ob, long)
380 FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);