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 "forms/form_print.h"
21 #include "frontends/LyXView.h"
23 #include "PrinterParams.h"
25 #include "input_validators.h"
26 #include "xforms_helpers.h"
28 #include "support/lstrings.h"
29 #include FORMS_H_LOCATION
33 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
35 FormPrint::FormPrint(ControlPrint & c, Dialogs & d)
36 : base_class(c, d, _("Print")),
37 target_(2), order_(2), which_(3)
41 void FormPrint::build()
43 dialog_.reset(build_print(this));
45 // Manage the ok, apply and cancel/close buttons
46 bc().setOK(dialog_->button_ok);
47 bc().setApply(dialog_->button_apply);
48 bc().setCancel(dialog_->button_close);
50 // allow controlling of input and ok/apply (de)activation
51 fl_set_input_return(dialog_->input_printer,
53 fl_set_input_return(dialog_->input_file,
55 fl_set_input_return(dialog_->input_from_page,
57 fl_set_input_return(dialog_->input_to_page,
59 fl_set_input_return(dialog_->input_count,
62 // limit these inputs to unsigned integers
63 fl_set_input_filter(dialog_->input_from_page,
64 fl_unsigned_int_filter);
65 fl_set_input_filter(dialog_->input_to_page,
66 fl_unsigned_int_filter);
67 fl_set_input_filter(dialog_->input_count,
68 fl_unsigned_int_filter);
70 setPrehandler(dialog_->input_printer);
71 setPrehandler(dialog_->input_file);
72 setPrehandler(dialog_->input_from_page);
73 setPrehandler(dialog_->input_to_page);
74 setPrehandler(dialog_->input_count);
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_.init(dialog_->radio_printer, PrinterParams::PRINTER);
85 target_.init(dialog_->radio_file, PrinterParams::FILE);
87 order_.init(dialog_->radio_order_reverse, true);
88 order_.init(dialog_->radio_order_normal, false);
90 which_.init(dialog_->radio_odd_pages, PrinterParams::ODD);
91 which_.init(dialog_->radio_even_pages, PrinterParams::EVEN);
92 which_.init(dialog_->radio_all_pages, PrinterParams::ALL);
96 void FormPrint::apply()
98 PrinterParams::WhichPages
99 wp(static_cast<PrinterParams::WhichPages>(which_.get()));
103 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
104 // we have at least one page requested
105 from = fl_get_input(dialog_->input_from_page);
106 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
107 // okay we have a range
108 to = strToInt(fl_get_input(dialog_->input_to_page));
109 } // else we only print one page.
112 PrinterParams::Target
113 t(static_cast<PrinterParams::Target>(target_.get()));
115 PrinterParams const pp(t,
116 string(fl_get_input(dialog_->input_printer)),
117 string(fl_get_input(dialog_->input_file)),
119 static_cast<bool>(order_.get()),
120 !static_cast<bool>(fl_get_button(dialog_->check_collated)),
121 strToInt(fl_get_input(dialog_->input_count)));
123 controller().params() = pp;
127 void FormPrint::update()
129 PrinterParams & pp = controller().params();
131 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
132 fl_set_input(dialog_->input_file, pp.file_name.c_str());
134 target_.set(pp.target);
135 order_.set(pp.reverse_order);
136 which_.set(pp.which_pages);
138 // hmmm... maybe a bit weird but maybe not
139 // we might just be remembering the last
140 // time this was printed.
141 if (!pp.from_page.empty()) {
142 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
144 // we only set the "to" page of a range
145 // if there's a corresponding "from"
146 fl_activate_object(dialog_->input_to_page);
148 fl_set_input(dialog_->input_to_page,
149 tostr(pp.to_page).c_str());
151 fl_set_input(dialog_->input_to_page,"");
155 fl_deactivate_object(dialog_->input_to_page);
156 fl_set_input(dialog_->input_to_page,"");
157 fl_set_input(dialog_->input_from_page,"");
160 fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
164 // It would be nice if we checked for cases like:
165 // Print only-odd-pages and from_page == an even number
167 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
169 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
171 // using a fl_input_filter that only permits numbers no '-' or '+'
172 // and the user cannot enter a negative number even if they try.
173 if (strlen(fl_get_input(dialog_->input_from_page))) {
174 // using a page range so activate the "to" field
175 fl_activate_object(dialog_->input_to_page);
176 if (strlen(fl_get_input(dialog_->input_to_page))
177 && (strToInt(fl_get_input(dialog_->input_from_page))
178 > strToInt(fl_get_input(dialog_->input_to_page)))) {
179 // both from and to have values but from > to
180 // We could have code to silently swap these
181 // values but I'll disable the ok/apply until
182 // the user fixes it since they may be editting
183 // one of the fields.
184 activate = ButtonPolicy::SMI_INVALID;
185 // set both backgrounds to red?
187 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
188 // from is empty but to exists so probably editting from
189 // therefore deactivate ok and apply until form is valid again
190 activate = ButtonPolicy::SMI_INVALID;
192 // both from and to are empty. This is valid so activate
193 // ok and apply but deactivate to
194 fl_deactivate_object(dialog_->input_to_page);
197 if (fl_get_button(dialog_->radio_file)
198 && !strlen(fl_get_input(dialog_->input_file))) {
199 activate = ButtonPolicy::SMI_INVALID;
202 if (ob == dialog_->button_browse) {
203 // Get the filename from the dialog
204 string const in_name = fl_get_input(dialog_->input_file);
205 string const out_name = controller().Browse(in_name);
207 // Save the filename to the dialog
208 if (out_name != in_name && !out_name.empty()) {
209 fl_set_input(dialog_->input_file, out_name.c_str());
213 // select the file radio
214 if (!out_name.empty()) {
215 fl_set_button(dialog_->radio_file, 1);
216 fl_set_button(dialog_->radio_printer, 0);
220 // if we type into file, select that as a target
221 if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer)
222 && strlen(fl_get_input(dialog_->input_file))) {
223 fl_set_button(dialog_->radio_file, 1);
224 fl_set_button(dialog_->radio_printer, 0);
225 } else if (ob == dialog_->input_printer) {
226 fl_set_button(dialog_->radio_file, 0);
227 fl_set_button(dialog_->radio_printer, 1);