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 "xform_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"), new OkApplyCancelPolicy),
40 dialog_(0), 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 FormPrint::~FormPrint()
55 void FormPrint::build()
57 dialog_ = build_print();
59 // Workaround dumb xforms sizing bug
63 // Manage the ok, apply and cancel/close buttons
64 bc_.setOK(dialog_->button_ok);
65 bc_.setApply(dialog_->button_apply);
66 bc_.setCancel(dialog_->button_cancel);
69 // allow controlling of input and ok/apply (de)activation
70 fl_set_input_return(dialog_->input_printer,
72 fl_set_input_return(dialog_->input_file,
74 fl_set_input_return(dialog_->input_from_page,
76 fl_set_input_return(dialog_->input_to_page,
78 fl_set_input_return(dialog_->input_count,
81 // limit these inputs to unsigned integers
82 fl_set_input_filter(dialog_->input_from_page,
83 fl_unsigned_int_filter);
84 fl_set_input_filter(dialog_->input_to_page,
85 fl_unsigned_int_filter);
86 fl_set_input_filter(dialog_->input_count,
87 fl_unsigned_int_filter);
89 // what limits (if any) make sense for these?
90 fl_set_input_maxchars(dialog_->input_printer, 255);
91 fl_set_input_maxchars(dialog_->input_file, 255);
92 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
93 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
94 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
97 target_.registerRadioButton(dialog_->radio_printer,
98 PrinterParams::PRINTER);
99 target_.registerRadioButton(dialog_->radio_file,
100 PrinterParams::FILE);
102 order_.registerRadioButton(dialog_->radio_order_reverse,
104 order_.registerRadioButton(dialog_->radio_order_normal,
107 which_.registerRadioButton(dialog_->radio_odd_pages,
109 which_.registerRadioButton(dialog_->radio_even_pages,
110 PrinterParams::EVEN);
111 which_.registerRadioButton(dialog_->radio_all_pages,
116 FL_FORM * FormPrint::form() const
118 if (dialog_) return dialog_->form;
123 void FormPrint::apply()
125 if (!lv_->view()->available()) {
129 PrinterParams::WhichPages
130 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
134 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
135 // we have at least one page requested
136 from = fl_get_input(dialog_->input_from_page);
137 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
138 // okay we have a range
139 to = strToInt(fl_get_input(dialog_->input_to_page));
140 } // else we only print one page.
143 PrinterParams::Target
144 t(static_cast<PrinterParams::Target>(target_.getButton()));
146 // we really should use the return value here I think.
147 if (!printBuffer(lv_->buffer(),
149 string(fl_get_input(dialog_->input_printer)),
150 string(fl_get_input(dialog_->input_file)),
152 static_cast<bool>(order_.getButton()),
153 !static_cast<bool>(fl_get_button(dialog_->
155 strToInt(fl_get_input(dialog_->input_count))))) {
156 WriteAlert(_("Error:"),
157 _("Unable to print"),
158 _("Check that your parameters are correct"));
163 void FormPrint::update()
166 && lv_->view()->available()) {
167 PrinterParams pp(getPrinterParams(lv_->buffer()));
169 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
170 fl_set_input(dialog_->input_file, pp.file_name.c_str());
172 target_.setButton(pp.target);
173 order_.setButton(pp.reverse_order);
174 which_.setButton(pp.which_pages);
176 // hmmm... maybe a bit weird but maybe not
177 // we might just be remembering the last
178 // time this was printed.
179 if (!pp.from_page.empty()) {
180 fl_set_input(dialog_->input_from_page,
181 pp.from_page.c_str());
182 // we only set the "to" page of a range
183 // if there's a corresponding "from"
184 fl_activate_object(dialog_->input_to_page);
186 fl_set_input(dialog_->input_to_page,
187 tostr(pp.to_page).c_str());
189 fl_set_input(dialog_->input_to_page,"");
192 fl_deactivate_object(dialog_->input_to_page);
193 fl_set_input(dialog_->input_to_page,"");
194 fl_set_input(dialog_->input_from_page,"");
197 fl_set_input(dialog_->input_count,
198 tostr(pp.count_copies).c_str());
204 // It would be nice if we checked for cases like:
205 // Print only-odd-pages and from_page == an even number
207 bool FormPrint::input(FL_OBJECT * ob, long)
209 bool activate = true;
211 // using a fl_input_filter that only permits numbers no '-' or '+'
212 // and the user cannot enter a negative number even if they try.
213 if (strlen(fl_get_input(dialog_->input_from_page))) {
214 // using a page range so activate the "to" field
215 fl_activate_object(dialog_->input_to_page);
216 if (strlen(fl_get_input(dialog_->input_to_page))
217 && (strToInt(fl_get_input(dialog_->input_from_page))
218 > strToInt(fl_get_input(dialog_->input_to_page)))) {
219 // both from and to have values but from > to
220 // We could have code to silently swap these
221 // values but I'll disable the ok/apply until
222 // the user fixes it since they may be editting
223 // one of the fields.
225 // set both backgrounds to red?
227 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
228 // from is empty but to exists so probably editting from
229 // therefore deactivate ok and apply until form is valid again
232 // both from and to are empty. This is valid so activate
233 // ok and apply but deactivate to
234 fl_deactivate_object(dialog_->input_to_page);
237 if (fl_get_button(dialog_->radio_file)
238 && !strlen(fl_get_input(dialog_->input_file))) {
242 if (ob == dialog_->button_browse) {
246 // it is probably legal to have no printer name since the system will
247 // have a default printer set. Or should have.
248 // if (fl_get_button(dialog_->radio_printer)
249 // && !strlen(fl_get_input(dialog_->input_printer))) {
256 void FormPrint::browse()
258 // Get the filename from the dialog
259 string const filename = fl_get_input(dialog_->input_file);
261 string const title = N_("Print to file");
262 string const pattern = "*.ps";
264 // Show the file browser dialog
265 string const new_filename =
266 browseFile(filename, title, pattern,
267 make_pair(string(), string()),
268 make_pair(string(), string()));
270 // Save the filename to the dialog
271 if (new_filename != filename && !new_filename.empty()) {
272 fl_set_input(dialog_->input_file, new_filename.c_str());