2 * FormPrint Interface Class Implementation
7 #include FORMS_H_LOCATION
10 #pragma implementation
13 #include "FormPrint.h"
14 #include "form_print.h"
15 #include "input_validators.h"
18 #include "support/lstrings.h"
20 #include "PrinterParams.h"
23 #include "BufferView.h"
24 #include "lyx_gui_misc.h" // WriteAlert
25 #include "xforms_helpers.h" // for browseFile
27 using Liason::printBuffer;
28 using Liason::getPrinterParams;
31 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
32 : FormBaseBD(lv, d, _("Print")),
33 target_(2), order_(2), which_(3)
35 // let the dialog be shown
36 // This is a permanent connection so we won't bother
37 // storing a copy because we won't be disconnecting.
38 d->showPrint.connect(slot(this, &FormPrint::show));
42 void FormPrint::build()
44 dialog_.reset(build_print());
46 // Workaround dumb xforms sizing bug
50 // Manage the ok, apply and cancel/close buttons
51 bc().setOK(dialog_->button_ok);
52 bc().setApply(dialog_->button_apply);
53 bc().setCancel(dialog_->button_cancel);
56 // allow controlling of input and ok/apply (de)activation
57 fl_set_input_return(dialog_->input_printer,
59 fl_set_input_return(dialog_->input_file,
61 fl_set_input_return(dialog_->input_from_page,
63 fl_set_input_return(dialog_->input_to_page,
65 fl_set_input_return(dialog_->input_count,
68 // limit these inputs to unsigned integers
69 fl_set_input_filter(dialog_->input_from_page,
70 fl_unsigned_int_filter);
71 fl_set_input_filter(dialog_->input_to_page,
72 fl_unsigned_int_filter);
73 fl_set_input_filter(dialog_->input_count,
74 fl_unsigned_int_filter);
76 // what limits (if any) make sense for these?
77 fl_set_input_maxchars(dialog_->input_printer, 255);
78 fl_set_input_maxchars(dialog_->input_file, 255);
79 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
80 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
81 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
84 target_.registerRadioButton(dialog_->radio_printer,
85 PrinterParams::PRINTER);
86 target_.registerRadioButton(dialog_->radio_file,
89 order_.registerRadioButton(dialog_->radio_order_reverse,
91 order_.registerRadioButton(dialog_->radio_order_normal,
94 which_.registerRadioButton(dialog_->radio_odd_pages,
96 which_.registerRadioButton(dialog_->radio_even_pages,
98 which_.registerRadioButton(dialog_->radio_all_pages,
103 FL_FORM * FormPrint::form() const
106 return dialog_->form;
111 void FormPrint::apply()
113 if (!lv_->view()->available()) {
117 PrinterParams::WhichPages
118 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
122 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
123 // we have at least one page requested
124 from = fl_get_input(dialog_->input_from_page);
125 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
126 // okay we have a range
127 to = strToInt(fl_get_input(dialog_->input_to_page));
128 } // else we only print one page.
131 PrinterParams::Target
132 t(static_cast<PrinterParams::Target>(target_.getButton()));
134 // we really should use the return value here I think.
135 if (!printBuffer(lv_->buffer(),
137 string(fl_get_input(dialog_->input_printer)),
138 string(fl_get_input(dialog_->input_file)),
140 static_cast<bool>(order_.getButton()),
141 !static_cast<bool>(fl_get_button(dialog_->
143 strToInt(fl_get_input(dialog_->input_count))))) {
144 WriteAlert(_("Error:"),
145 _("Unable to print"),
146 _("Check that your parameters are correct"));
151 void FormPrint::update()
154 && lv_->view()->available()) {
155 PrinterParams pp(getPrinterParams(lv_->buffer()));
157 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
158 fl_set_input(dialog_->input_file, pp.file_name.c_str());
160 target_.setButton(pp.target);
161 order_.setButton(pp.reverse_order);
162 which_.setButton(pp.which_pages);
164 // hmmm... maybe a bit weird but maybe not
165 // we might just be remembering the last
166 // time this was printed.
167 if (!pp.from_page.empty()) {
168 fl_set_input(dialog_->input_from_page,
169 pp.from_page.c_str());
170 // we only set the "to" page of a range
171 // if there's a corresponding "from"
172 fl_activate_object(dialog_->input_to_page);
174 fl_set_input(dialog_->input_to_page,
175 tostr(pp.to_page).c_str());
177 fl_set_input(dialog_->input_to_page,"");
180 fl_deactivate_object(dialog_->input_to_page);
181 fl_set_input(dialog_->input_to_page,"");
182 fl_set_input(dialog_->input_from_page,"");
185 fl_set_input(dialog_->input_count,
186 tostr(pp.count_copies).c_str());
192 // It would be nice if we checked for cases like:
193 // Print only-odd-pages and from_page == an even number
195 bool FormPrint::input(FL_OBJECT * ob, long)
197 bool activate = true;
199 // using a fl_input_filter that only permits numbers no '-' or '+'
200 // and the user cannot enter a negative number even if they try.
201 if (strlen(fl_get_input(dialog_->input_from_page))) {
202 // using a page range so activate the "to" field
203 fl_activate_object(dialog_->input_to_page);
204 if (strlen(fl_get_input(dialog_->input_to_page))
205 && (strToInt(fl_get_input(dialog_->input_from_page))
206 > strToInt(fl_get_input(dialog_->input_to_page)))) {
207 // both from and to have values but from > to
208 // We could have code to silently swap these
209 // values but I'll disable the ok/apply until
210 // the user fixes it since they may be editting
211 // one of the fields.
213 // set both backgrounds to red?
215 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
216 // from is empty but to exists so probably editting from
217 // therefore deactivate ok and apply until form is valid again
220 // both from and to are empty. This is valid so activate
221 // ok and apply but deactivate to
222 fl_deactivate_object(dialog_->input_to_page);
225 if (fl_get_button(dialog_->radio_file)
226 && !strlen(fl_get_input(dialog_->input_file))) {
230 if (ob == dialog_->button_browse) {
234 // it is probably legal to have no printer name since the system will
235 // have a default printer set. Or should have.
236 // if (fl_get_button(dialog_->radio_printer)
237 // && !strlen(fl_get_input(dialog_->input_printer))) {
244 void FormPrint::browse()
246 // Get the filename from the dialog
247 string const filename = fl_get_input(dialog_->input_file);
249 string const title = N_("Print to file");
250 string const pattern = "*.ps";
252 // Show the file browser dialog
253 string const new_filename =
254 browseFile(lv_, filename, title, pattern,
255 make_pair(string(), string()),
256 make_pair(string(), string()));
258 // Save the filename to the dialog
259 if (new_filename != filename && !new_filename.empty()) {
260 fl_set_input(dialog_->input_file, new_filename.c_str());