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 #ifdef SIGC_CXX_NAMESPACES
31 #ifdef CXX_WORKING_NAMESPACES
32 using Liason::printBuffer;
33 using Liason::getPrinterParams;
38 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
39 : FormBaseBD(lv, d, _("Print")),
40 target_(2), order_(2), which_(3)
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 void FormPrint::build()
51 dialog_.reset(build_print());
53 // Workaround dumb xforms sizing bug
57 // Manage the ok, apply and cancel/close buttons
58 bc().setOK(dialog_->button_ok);
59 bc().setApply(dialog_->button_apply);
60 bc().setCancel(dialog_->button_cancel);
63 // allow controlling of input and ok/apply (de)activation
64 fl_set_input_return(dialog_->input_printer,
66 fl_set_input_return(dialog_->input_file,
68 fl_set_input_return(dialog_->input_from_page,
70 fl_set_input_return(dialog_->input_to_page,
72 fl_set_input_return(dialog_->input_count,
75 // limit these inputs to unsigned integers
76 fl_set_input_filter(dialog_->input_from_page,
77 fl_unsigned_int_filter);
78 fl_set_input_filter(dialog_->input_to_page,
79 fl_unsigned_int_filter);
80 fl_set_input_filter(dialog_->input_count,
81 fl_unsigned_int_filter);
83 // what limits (if any) make sense for these?
84 fl_set_input_maxchars(dialog_->input_printer, 255);
85 fl_set_input_maxchars(dialog_->input_file, 255);
86 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
87 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
88 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
91 target_.registerRadioButton(dialog_->radio_printer,
92 PrinterParams::PRINTER);
93 target_.registerRadioButton(dialog_->radio_file,
96 order_.registerRadioButton(dialog_->radio_order_reverse,
98 order_.registerRadioButton(dialog_->radio_order_normal,
101 which_.registerRadioButton(dialog_->radio_odd_pages,
103 which_.registerRadioButton(dialog_->radio_even_pages,
104 PrinterParams::EVEN);
105 which_.registerRadioButton(dialog_->radio_all_pages,
110 FL_FORM * FormPrint::form() const
113 return dialog_->form;
118 void FormPrint::apply()
120 if (!lv_->view()->available()) {
124 PrinterParams::WhichPages
125 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
129 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
130 // we have at least one page requested
131 from = fl_get_input(dialog_->input_from_page);
132 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
133 // okay we have a range
134 to = strToInt(fl_get_input(dialog_->input_to_page));
135 } // else we only print one page.
138 PrinterParams::Target
139 t(static_cast<PrinterParams::Target>(target_.getButton()));
141 // we really should use the return value here I think.
142 if (!printBuffer(lv_->buffer(),
144 string(fl_get_input(dialog_->input_printer)),
145 string(fl_get_input(dialog_->input_file)),
147 static_cast<bool>(order_.getButton()),
148 !static_cast<bool>(fl_get_button(dialog_->
150 strToInt(fl_get_input(dialog_->input_count))))) {
151 WriteAlert(_("Error:"),
152 _("Unable to print"),
153 _("Check that your parameters are correct"));
158 void FormPrint::update()
161 && lv_->view()->available()) {
162 PrinterParams pp(getPrinterParams(lv_->buffer()));
164 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
165 fl_set_input(dialog_->input_file, pp.file_name.c_str());
167 target_.setButton(pp.target);
168 order_.setButton(pp.reverse_order);
169 which_.setButton(pp.which_pages);
171 // hmmm... maybe a bit weird but maybe not
172 // we might just be remembering the last
173 // time this was printed.
174 if (!pp.from_page.empty()) {
175 fl_set_input(dialog_->input_from_page,
176 pp.from_page.c_str());
177 // we only set the "to" page of a range
178 // if there's a corresponding "from"
179 fl_activate_object(dialog_->input_to_page);
181 fl_set_input(dialog_->input_to_page,
182 tostr(pp.to_page).c_str());
184 fl_set_input(dialog_->input_to_page,"");
187 fl_deactivate_object(dialog_->input_to_page);
188 fl_set_input(dialog_->input_to_page,"");
189 fl_set_input(dialog_->input_from_page,"");
192 fl_set_input(dialog_->input_count,
193 tostr(pp.count_copies).c_str());
199 // It would be nice if we checked for cases like:
200 // Print only-odd-pages and from_page == an even number
202 bool FormPrint::input(FL_OBJECT * ob, long)
204 bool activate = true;
206 // using a fl_input_filter that only permits numbers no '-' or '+'
207 // and the user cannot enter a negative number even if they try.
208 if (strlen(fl_get_input(dialog_->input_from_page))) {
209 // using a page range so activate the "to" field
210 fl_activate_object(dialog_->input_to_page);
211 if (strlen(fl_get_input(dialog_->input_to_page))
212 && (strToInt(fl_get_input(dialog_->input_from_page))
213 > strToInt(fl_get_input(dialog_->input_to_page)))) {
214 // both from and to have values but from > to
215 // We could have code to silently swap these
216 // values but I'll disable the ok/apply until
217 // the user fixes it since they may be editting
218 // one of the fields.
220 // set both backgrounds to red?
222 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
223 // from is empty but to exists so probably editting from
224 // therefore deactivate ok and apply until form is valid again
227 // both from and to are empty. This is valid so activate
228 // ok and apply but deactivate to
229 fl_deactivate_object(dialog_->input_to_page);
232 if (fl_get_button(dialog_->radio_file)
233 && !strlen(fl_get_input(dialog_->input_file))) {
237 if (ob == dialog_->button_browse) {
241 // it is probably legal to have no printer name since the system will
242 // have a default printer set. Or should have.
243 // if (fl_get_button(dialog_->radio_printer)
244 // && !strlen(fl_get_input(dialog_->input_printer))) {
251 void FormPrint::browse()
253 // Get the filename from the dialog
254 string const filename = fl_get_input(dialog_->input_file);
256 string const title = N_("Print to file");
257 string const pattern = "*.ps";
259 // Show the file browser dialog
260 string const new_filename =
261 browseFile(lv_, filename, title, pattern,
262 make_pair(string(), string()),
263 make_pair(string(), string()));
265 // Save the filename to the dialog
266 if (new_filename != filename && !new_filename.empty()) {
267 fl_set_input(dialog_->input_file, new_filename.c_str());