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 <leeming@lyx.org>
13 #pragma implementation
17 #include "ControlPrint.h"
18 #include "FormPrint.h"
19 #include "forms/form_print.h"
21 #include "PrinterParams.h"
23 #include "input_validators.h"
24 #include "xforms_helpers.h"
26 #include "support/lstrings.h"
27 #include FORMS_H_LOCATION
31 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
33 FormPrint::FormPrint()
34 : base_class(_("Print")),
35 target_(2), order_(2), which_(3)
39 void FormPrint::build()
41 dialog_.reset(build_print(this));
43 // Manage the ok, apply and cancel/close buttons
44 bc().setOK(dialog_->button_ok);
45 bc().setApply(dialog_->button_apply);
46 bc().setCancel(dialog_->button_close);
48 // allow controlling of input and ok/apply (de)activation
49 fl_set_input_return(dialog_->input_printer,
51 fl_set_input_return(dialog_->input_file,
53 fl_set_input_return(dialog_->input_from_page,
55 fl_set_input_return(dialog_->input_to_page,
57 fl_set_input_return(dialog_->input_count,
60 // limit these inputs to unsigned integers
61 fl_set_input_filter(dialog_->input_from_page,
62 fl_unsigned_int_filter);
63 fl_set_input_filter(dialog_->input_to_page,
64 fl_unsigned_int_filter);
65 fl_set_input_filter(dialog_->input_count,
66 fl_unsigned_int_filter);
68 setPrehandler(dialog_->input_printer);
69 setPrehandler(dialog_->input_file);
70 setPrehandler(dialog_->input_from_page);
71 setPrehandler(dialog_->input_to_page);
72 setPrehandler(dialog_->input_count);
74 // what limits (if any) make sense for these?
75 fl_set_input_maxchars(dialog_->input_printer, 255);
76 fl_set_input_maxchars(dialog_->input_file, 255);
77 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
78 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
79 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
82 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
83 target_.init(dialog_->radio_file, PrinterParams::FILE);
85 order_.init(dialog_->radio_order_reverse, true);
86 order_.init(dialog_->radio_order_normal, false);
88 which_.init(dialog_->radio_odd_pages, PrinterParams::ODD);
89 which_.init(dialog_->radio_even_pages, PrinterParams::EVEN);
90 which_.init(dialog_->radio_all_pages, PrinterParams::ALL);
94 void FormPrint::apply()
96 PrinterParams::WhichPages
97 wp(static_cast<PrinterParams::WhichPages>(which_.get()));
101 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
102 // we have at least one page requested
103 from = fl_get_input(dialog_->input_from_page);
104 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
105 // okay we have a range
106 to = strToInt(fl_get_input(dialog_->input_to_page));
107 } // else we only print one page.
110 PrinterParams::Target
111 t(static_cast<PrinterParams::Target>(target_.get()));
113 PrinterParams const pp(t,
114 string(fl_get_input(dialog_->input_printer)),
115 string(fl_get_input(dialog_->input_file)),
117 static_cast<bool>(order_.get()),
118 !static_cast<bool>(fl_get_button(dialog_->check_collated)),
119 strToInt(fl_get_input(dialog_->input_count)));
121 controller().params() = pp;
125 void FormPrint::update()
127 PrinterParams & pp = controller().params();
129 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
130 fl_set_input(dialog_->input_file, pp.file_name.c_str());
132 target_.set(pp.target);
133 order_.set(pp.reverse_order);
134 which_.set(pp.which_pages);
136 // hmmm... maybe a bit weird but maybe not
137 // we might just be remembering the last
138 // time this was printed.
139 if (!pp.from_page.empty()) {
140 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
142 // we only set the "to" page of a range
143 // if there's a corresponding "from"
144 fl_activate_object(dialog_->input_to_page);
146 fl_set_input(dialog_->input_to_page,
147 tostr(pp.to_page).c_str());
149 fl_set_input(dialog_->input_to_page,"");
153 fl_deactivate_object(dialog_->input_to_page);
154 fl_set_input(dialog_->input_to_page,"");
155 fl_set_input(dialog_->input_from_page,"");
158 fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
162 // It would be nice if we checked for cases like:
163 // Print only-odd-pages and from_page == an even number
165 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
167 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
169 // using a fl_input_filter that only permits numbers no '-' or '+'
170 // and the user cannot enter a negative number even if they try.
171 if (strlen(fl_get_input(dialog_->input_from_page))) {
172 // using a page range so activate the "to" field
173 fl_activate_object(dialog_->input_to_page);
174 if (strlen(fl_get_input(dialog_->input_to_page))
175 && (strToInt(fl_get_input(dialog_->input_from_page))
176 > strToInt(fl_get_input(dialog_->input_to_page)))) {
177 // both from and to have values but from > to
178 // We could have code to silently swap these
179 // values but I'll disable the ok/apply until
180 // the user fixes it since they may be editting
181 // one of the fields.
182 activate = ButtonPolicy::SMI_INVALID;
183 // set both backgrounds to red?
185 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
186 // from is empty but to exists so probably editting from
187 // therefore deactivate ok and apply until form is valid again
188 activate = ButtonPolicy::SMI_INVALID;
190 // both from and to are empty. This is valid so activate
191 // ok and apply but deactivate to
192 fl_deactivate_object(dialog_->input_to_page);
195 if (fl_get_button(dialog_->radio_file)
196 && !strlen(fl_get_input(dialog_->input_file))) {
197 activate = ButtonPolicy::SMI_INVALID;
200 if (ob == dialog_->button_browse) {
201 // Get the filename from the dialog
202 string const in_name = fl_get_input(dialog_->input_file);
203 string const out_name = controller().Browse(in_name);
205 // Save the filename to the dialog
206 if (out_name != in_name && !out_name.empty()) {
207 fl_set_input(dialog_->input_file, out_name.c_str());
211 // select the file radio
212 if (!out_name.empty()) {
213 fl_set_button(dialog_->radio_file, 1);
214 fl_set_button(dialog_->radio_printer, 0);
218 // if we type into file, select that as a target
219 if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer)
220 && strlen(fl_get_input(dialog_->input_file))) {
221 fl_set_button(dialog_->radio_file, 1);
222 fl_set_button(dialog_->radio_printer, 0);
223 } else if (ob == dialog_->input_printer) {
224 fl_set_button(dialog_->radio_file, 0);
225 fl_set_button(dialog_->radio_printer, 1);