2 * \file xforms/FormPrint.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS
15 #pragma implementation
19 #include "ControlPrint.h"
20 #include "FormPrint.h"
21 #include "forms/form_print.h"
24 #include "PrinterParams.h"
26 #include "input_validators.h"
27 #include "xforms_helpers.h"
29 #include "support/lstrings.h"
30 #include FORMS_H_LOCATION
34 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
36 FormPrint::FormPrint()
37 : base_class(_("Print")),
38 target_(2), which_pages_(2)
42 void FormPrint::build()
44 dialog_.reset(build_print(this));
46 // Manage the ok, apply and cancel/close buttons
47 bc().setOK(dialog_->button_ok);
48 bc().setApply(dialog_->button_apply);
49 bc().setCancel(dialog_->button_close);
51 // allow controlling of input and ok/apply (de)activation
52 fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
53 fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
54 fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
55 fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
57 // limit these inputs to unsigned integers
58 fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
59 fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
61 // what limits (if any) make sense for these?
62 fl_set_input_maxchars(dialog_->input_printer, 255);
63 fl_set_input_maxchars(dialog_->input_file, 255);
64 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
65 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
67 bc().addReadOnly(dialog_->button_browse);
68 bc().addReadOnly(dialog_->check_odd_pages);
69 bc().addReadOnly(dialog_->check_even_pages);
70 bc().addReadOnly(dialog_->check_sorted_copies);
71 bc().addReadOnly(dialog_->check_reverse_order);
74 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
75 target_.init(dialog_->radio_file, PrinterParams::FILE);
77 which_pages_.init(dialog_->radio_all_pages, true);
78 which_pages_.init(dialog_->radio_from_to, false);
80 // set up the tooltips for Destination
81 string str = _("Select for printer output.");
82 tooltips().init(dialog_->radio_printer, str);
83 str = _("Enter printer command.");
84 tooltips().init(dialog_->input_printer, str);
85 str = _("Select for file output.");
86 tooltips().init(dialog_->radio_file, str);
87 str = _("Enter file name as print destination.");
88 tooltips().init(dialog_->input_file, str);
89 str = _("Browse directories for file name.");
90 tooltips().init(dialog_->button_browse, str);
92 // set up the tooltips for Range
93 str = _("Select for printing all pages.");
94 tooltips().init(dialog_->radio_all_pages, str);
95 str = _("Select for printing a specific page range.");
96 tooltips().init(dialog_->radio_from_to, str);
97 str = _("First page.");
98 tooltips().init(dialog_->input_from_page, str);
99 str = _("Last page.");
100 tooltips().init(dialog_->input_to_page, str);
101 str = _("Print the odd numbered pages.");
102 tooltips().init(dialog_->check_odd_pages, str);
103 str = _("Print the even numbered pages.");
104 tooltips().init(dialog_->check_even_pages, str);
106 // set up the tooltips for Copies
107 str = _("Number of copies to be printed.");
108 tooltips().init(dialog_->counter_copies, str);
109 str = _("Sort the copies.");
110 tooltips().init(dialog_->check_sorted_copies, str);
112 str = _("Reverse the order of the printed pages.");
113 tooltips().init(dialog_->check_reverse_order, str);
117 void FormPrint::apply()
121 pp.target = static_cast<PrinterParams::Target>(target_.get());
122 pp.printer_name = fl_get_input(dialog_->input_printer);
123 pp.file_name = fl_get_input(dialog_->input_file);
125 pp.all_pages = which_pages_.get();
128 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
129 // we have at least one page requested
130 pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
131 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
132 // okay we have a range
133 pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
134 } // else we only print one page.
137 pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
138 pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
140 pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
141 pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
143 pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
145 controller().params() = pp;
149 void FormPrint::update()
151 PrinterParams & pp = controller().params();
153 target_.set(pp.target);
154 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
155 fl_set_input(dialog_->input_file, pp.file_name.c_str());
157 // hmmm... maybe a bit weird but maybe not
158 // we might just be remembering the last time this was printed.
159 which_pages_.set(pp.all_pages);
161 string const from = ( pp.from_page ? tostr(pp.from_page) : "");
162 string const to = ( pp.to_page ? tostr(pp.to_page) : "");
163 fl_set_input(dialog_->input_from_page, from.c_str());
164 fl_set_input(dialog_->input_to_page, to.c_str());
166 fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
167 fl_set_button(dialog_->check_even_pages, pp.even_pages);
168 fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
169 fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
171 fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
173 // number of copies only used when output goes to printer
174 bool const enable_counter = pp.target == PrinterParams::PRINTER;
175 setEnabled(dialog_->counter_copies, enable_counter);
177 // sorting only used when printing more than one copy
178 setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
182 // It would be nice if we checked for cases like:
183 // Print only-odd-pages and from_page == an even number
185 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
187 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
189 // using a fl_input_filter that only permits numbers no '-' or '+'
190 // and the user cannot enter a negative number even if they try.
191 if (strlen(fl_get_input(dialog_->input_from_page))) {
192 // using a page range so activate the "to" field
193 fl_activate_object(dialog_->input_to_page);
194 if (strlen(fl_get_input(dialog_->input_to_page))
195 && (strToInt(fl_get_input(dialog_->input_from_page))
196 > strToInt(fl_get_input(dialog_->input_to_page)))) {
197 // both from and to have values but from > to
198 // We could have code to silently swap these
199 // values but I'll disable the ok/apply until
200 // the user fixes it since they may be editting
201 // one of the fields.
202 activate = ButtonPolicy::SMI_INVALID;
203 // set both backgrounds to red?
205 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
206 // from is empty but to exists, so probably editting from
207 // therefore deactivate ok and apply until form is valid again
208 activate = ButtonPolicy::SMI_INVALID;
210 // both from and to are empty. This is valid so activate
211 // ok and apply but deactivate to
212 fl_deactivate_object(dialog_->input_to_page);
215 // number of copies only used when output goes to printer
216 bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
217 setEnabled(dialog_->counter_copies, enable_counter);
219 // sorting only used when printing more than one copy
220 bool const enable_sorted = enable_counter
221 && static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies)) > 1;
222 setEnabled(dialog_->check_sorted_copies, enable_sorted);
224 // disable OK/Apply buttons when file output is selected, but no file name entered.
225 if (fl_get_button(dialog_->radio_file) && !strlen(fl_get_input(dialog_->input_file))) {
226 activate = ButtonPolicy::SMI_INVALID;
229 if (ob == dialog_->button_browse) {
230 // Get the filename from the dialog
231 string const in_name = fl_get_input(dialog_->input_file);
232 string const out_name = controller().Browse(in_name);
234 // Save the filename to the dialog
235 if (out_name != in_name && !out_name.empty()) {
236 fl_set_input(dialog_->input_file, out_name.c_str());
240 // select the file radio
241 if (!out_name.empty()) {
242 fl_set_button(dialog_->radio_file, 1);
243 fl_set_button(dialog_->radio_printer, 0);
247 // if we type input string for file or printer, select that as a target
248 if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
249 fl_set_button(dialog_->radio_printer, 0);
250 fl_set_button(dialog_->radio_file, 1);
251 } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
252 fl_set_button(dialog_->radio_printer, 1);
253 fl_set_button(dialog_->radio_file, 0);
254 // if we type intput string for from/to, select from/to radio button
255 } else if ( (ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
256 !fl_get_button(dialog_->radio_from_to)) {
257 fl_set_button(dialog_->radio_from_to, 1);
258 fl_set_button(dialog_->radio_all_pages, 0);