2 * \file xforms/FormPrint.C
3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Allan Rae, rae@lyx.org
7 * \author Angus Leeming, a.leeming@ic.ac.uk
13 #pragma implementation
17 #include "ControlPrint.h"
18 #include "FormPrint.h"
19 #include "form_print.h"
23 #include "PrinterParams.h"
25 #include "input_validators.h"
26 #include "xforms_helpers.h"
28 #include "support/lstrings.h"
32 typedef FormCB<ControlPrint, FormDB<FD_form_print> > base_class;
34 FormPrint::FormPrint(ControlPrint & c)
35 : base_class(c, _("Print")),
36 target_(2), order_(2), which_(3)
40 void FormPrint::build()
42 dialog_.reset(build_print());
44 // Manage the ok, apply and cancel/close buttons
45 bc().setOK(dialog_->button_ok);
46 bc().setApply(dialog_->button_apply);
47 bc().setCancel(dialog_->button_close);
49 // allow controlling of input and ok/apply (de)activation
50 fl_set_input_return(dialog_->input_printer,
52 fl_set_input_return(dialog_->input_file,
54 fl_set_input_return(dialog_->input_from_page,
56 fl_set_input_return(dialog_->input_to_page,
58 fl_set_input_return(dialog_->input_count,
61 // limit these inputs to unsigned integers
62 fl_set_input_filter(dialog_->input_from_page,
63 fl_unsigned_int_filter);
64 fl_set_input_filter(dialog_->input_to_page,
65 fl_unsigned_int_filter);
66 fl_set_input_filter(dialog_->input_count,
67 fl_unsigned_int_filter);
69 setPrehandler(dialog_->input_printer);
70 setPrehandler(dialog_->input_file);
71 setPrehandler(dialog_->input_from_page);
72 setPrehandler(dialog_->input_to_page);
73 setPrehandler(dialog_->input_count);
75 // what limits (if any) make sense for these?
76 fl_set_input_maxchars(dialog_->input_printer, 255);
77 fl_set_input_maxchars(dialog_->input_file, 255);
78 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
79 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
80 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
83 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
84 target_.init(dialog_->radio_file, PrinterParams::FILE);
86 order_.init(dialog_->radio_order_reverse, true);
87 order_.init(dialog_->radio_order_normal, false);
89 which_.init(dialog_->radio_odd_pages, PrinterParams::ODD);
90 which_.init(dialog_->radio_even_pages, PrinterParams::EVEN);
91 which_.init(dialog_->radio_all_pages, PrinterParams::ALL);
95 void FormPrint::apply()
97 PrinterParams::WhichPages
98 wp(static_cast<PrinterParams::WhichPages>(which_.get()));
102 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
103 // we have at least one page requested
104 from = fl_get_input(dialog_->input_from_page);
105 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
106 // okay we have a range
107 to = strToInt(fl_get_input(dialog_->input_to_page));
108 } // else we only print one page.
111 PrinterParams::Target
112 t(static_cast<PrinterParams::Target>(target_.get()));
114 PrinterParams const pp(t,
115 string(fl_get_input(dialog_->input_printer)),
116 string(fl_get_input(dialog_->input_file)),
118 static_cast<bool>(order_.get()),
119 !static_cast<bool>(fl_get_button(dialog_->check_collated)),
120 strToInt(fl_get_input(dialog_->input_count)));
122 controller().params() = pp;
126 void FormPrint::update()
128 PrinterParams & pp = controller().params();
130 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
131 fl_set_input(dialog_->input_file, pp.file_name.c_str());
133 target_.set(pp.target);
134 order_.set(pp.reverse_order);
135 which_.set(pp.which_pages);
137 // hmmm... maybe a bit weird but maybe not
138 // we might just be remembering the last
139 // time this was printed.
140 if (!pp.from_page.empty()) {
141 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
143 // we only set the "to" page of a range
144 // if there's a corresponding "from"
145 fl_activate_object(dialog_->input_to_page);
147 fl_set_input(dialog_->input_to_page,
148 tostr(pp.to_page).c_str());
150 fl_set_input(dialog_->input_to_page,"");
154 fl_deactivate_object(dialog_->input_to_page);
155 fl_set_input(dialog_->input_to_page,"");
156 fl_set_input(dialog_->input_from_page,"");
159 fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
163 // It would be nice if we checked for cases like:
164 // Print only-odd-pages and from_page == an even number
166 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
168 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
170 // using a fl_input_filter that only permits numbers no '-' or '+'
171 // and the user cannot enter a negative number even if they try.
172 if (strlen(fl_get_input(dialog_->input_from_page))) {
173 // using a page range so activate the "to" field
174 fl_activate_object(dialog_->input_to_page);
175 if (strlen(fl_get_input(dialog_->input_to_page))
176 && (strToInt(fl_get_input(dialog_->input_from_page))
177 > strToInt(fl_get_input(dialog_->input_to_page)))) {
178 // both from and to have values but from > to
179 // We could have code to silently swap these
180 // values but I'll disable the ok/apply until
181 // the user fixes it since they may be editting
182 // one of the fields.
183 activate = ButtonPolicy::SMI_INVALID;
184 // set both backgrounds to red?
186 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
187 // from is empty but to exists so probably editting from
188 // therefore deactivate ok and apply until form is valid again
189 activate = ButtonPolicy::SMI_INVALID;
191 // both from and to are empty. This is valid so activate
192 // ok and apply but deactivate to
193 fl_deactivate_object(dialog_->input_to_page);
196 if (fl_get_button(dialog_->radio_file)
197 && !strlen(fl_get_input(dialog_->input_file))) {
198 activate = ButtonPolicy::SMI_INVALID;
201 if (ob == dialog_->button_browse) {
202 // Get the filename from the dialog
203 string const in_name = fl_get_input(dialog_->input_file);
204 string const out_name = controller().Browse(in_name);
206 // Save the filename to the dialog
207 if (out_name != in_name && !out_name.empty()) {
208 fl_set_input(dialog_->input_file, out_name.c_str());
212 // select the file radio
213 if (!out_name.empty()) {
214 fl_set_button(dialog_->radio_file, 1);
215 fl_set_button(dialog_->radio_printer, 0);
219 // if we type into file, select that as a target
220 if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer)
221 && strlen(fl_get_input(dialog_->input_file))) {
222 fl_set_button(dialog_->radio_file, 1);
223 fl_set_button(dialog_->radio_printer, 0);
224 } else if (ob == dialog_->input_printer) {
225 fl_set_button(dialog_->radio_file, 0);
226 fl_set_button(dialog_->radio_printer, 1);