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
16 #include "ControlPrint.h"
17 #include "FormPrint.h"
18 #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
30 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
32 FormPrint::FormPrint()
33 : base_class(_("Print"))
37 void FormPrint::build()
39 dialog_.reset(build_print(this));
41 // Manage the ok, apply and cancel/close buttons
42 bc().setOK(dialog_->button_ok);
43 bc().setApply(dialog_->button_apply);
44 bc().setCancel(dialog_->button_close);
46 // trigger an input event for cut&paste with middle mouse button.
47 setPrehandler(dialog_->input_printer);
48 setPrehandler(dialog_->input_file);
49 setPrehandler(dialog_->input_from_page);
50 setPrehandler(dialog_->input_to_page);
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 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
68 target_.init(dialog_->radio_file, PrinterParams::FILE);
70 all_pages_.init(dialog_->radio_all_pages, true);
71 all_pages_.init(dialog_->radio_from_to, false);
73 // set up the tooltips for Destination
74 string str = _("Select for printer output.");
75 tooltips().init(dialog_->radio_printer, str);
76 str = _("Enter printer command.");
77 tooltips().init(dialog_->input_printer, str);
78 str = _("Select for file output.");
79 tooltips().init(dialog_->radio_file, str);
80 str = _("Enter file name as print destination.");
81 tooltips().init(dialog_->input_file, str);
82 str = _("Browse directories for file name.");
83 tooltips().init(dialog_->button_browse, str);
85 // set up the tooltips for Range
86 str = _("Select for printing all pages.");
87 tooltips().init(dialog_->radio_all_pages, str);
88 str = _("Select for printing a specific page range.");
89 tooltips().init(dialog_->radio_from_to, str);
90 str = _("First page.");
91 tooltips().init(dialog_->input_from_page, str);
92 str = _("Last page.");
93 tooltips().init(dialog_->input_to_page, str);
94 str = _("Print the odd numbered pages.");
95 tooltips().init(dialog_->check_odd_pages, str);
96 str = _("Print the even numbered pages.");
97 tooltips().init(dialog_->check_even_pages, str);
99 // set up the tooltips for Copies
100 str = _("Number of copies to be printed.");
101 tooltips().init(dialog_->counter_copies, str);
102 str = _("Sort the copies.");
103 tooltips().init(dialog_->check_sorted_copies, str);
105 str = _("Reverse the order of the printed pages.");
106 tooltips().init(dialog_->check_reverse_order, str);
110 void FormPrint::apply()
114 pp.target = static_cast<PrinterParams::Target>(target_.get());
115 pp.printer_name = getString(dialog_->input_printer);
116 pp.file_name = getString(dialog_->input_file);
118 pp.all_pages = static_cast<bool>(all_pages_.get());
120 pp.from_page = pp.to_page = 0;
121 if (!getString(dialog_->input_from_page).empty()) {
122 // we have at least one page requested
123 pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
124 if (!getString(dialog_->input_to_page).empty()) {
125 // okay we have a range
126 pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
127 } // else we only print one page.
130 pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
131 pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
133 pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
134 pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
136 pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
138 controller().params() = pp;
142 void FormPrint::update()
144 PrinterParams & pp = controller().params();
146 target_.set(pp.target);
147 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
148 fl_set_input(dialog_->input_file, pp.file_name.c_str());
150 // hmmm... maybe a bit weird but maybe not
151 // we might just be remembering the last time this was printed.
152 all_pages_.set(pp.all_pages);
154 string const from = ( pp.from_page ? tostr(pp.from_page) : string() );
155 string const to = ( pp.to_page ? tostr(pp.to_page) : string() );
156 fl_set_input(dialog_->input_from_page, from.c_str());
157 fl_set_input(dialog_->input_to_page, to.c_str());
159 fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
160 fl_set_button(dialog_->check_even_pages, pp.even_pages);
161 fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
162 fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
164 fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
166 // number of copies only used when output goes to printer
167 bool const enable_counter = pp.target == PrinterParams::PRINTER;
168 setEnabled(dialog_->counter_copies, enable_counter);
170 // sorting only used when printing more than one copy
171 setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
173 // reset input fields to valid input
178 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
180 if (ob == dialog_->button_browse) {
181 // Get the filename from the dialog
182 string const in_name = getString(dialog_->input_file);
183 string const out_name = controller().Browse(in_name);
185 // Save the filename to the dialog
186 if (out_name != in_name && !out_name.empty()) {
187 fl_set_input(dialog_->input_file, out_name.c_str());
190 // select the file radio
191 if (!out_name.empty()) {
192 target_.set(dialog_->radio_file);
195 // if we type input string for file or printer, select that as a target
196 } else if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
197 target_.set(dialog_->radio_file);
199 } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
200 target_.set(dialog_->radio_printer);
202 // if we type into 'from/to' fields, then select 'from/to' radio button
203 } else if ((ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
204 !fl_get_button(dialog_->radio_from_to)) {
205 all_pages_.set(dialog_->radio_from_to);
208 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
210 // disable OK/Apply buttons when file output is selected, but no file name entered
211 if (fl_get_button(dialog_->radio_file) && getString(dialog_->input_file).empty()) {
212 activate = ButtonPolicy::SMI_INVALID;
215 // check 'from' and 'to' fields only when 'from/to' radio button is selected
216 if (fl_get_button(dialog_->radio_from_to)) {
217 char const * from = fl_get_input(dialog_->input_from_page);
218 char const * to = fl_get_input(dialog_->input_to_page);
219 bool const from_input = static_cast<bool>(*from);
220 bool const to_input = static_cast<bool>(*to);
222 setEnabled(dialog_->input_to_page, from_input);
223 if (!from_input || (to_input && strToInt(from) > strToInt(to))) {
224 // Invalid input. Either 'from' is empty, or 'from' > 'to'.
225 // Probably editting these fields, so deactivate OK/Apply until input is valid again.
226 activate = ButtonPolicy::SMI_INVALID;
227 } else if (!to_input || strToInt(from) == strToInt(to)) {
228 // Valid input. Either there's only 'from' input, or 'from' == 'to'.
229 // Deactivate OK/Apply if odd/even selection implies no pages.
230 bool const odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
231 bool const even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
232 bool const odd_only = odd_pages && !even_pages;
233 bool const even_only = even_pages && !odd_pages;
234 bool const from_is_odd = static_cast<bool>(strToInt(from) % 2);
235 if ( (from_is_odd && even_only) || (!from_is_odd && odd_only) ) {
236 activate = ButtonPolicy::SMI_INVALID;
241 // number of copies only used when output goes to printer
242 bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
243 setEnabled(dialog_->counter_copies, enable_counter);
245 // sorting only used when printing more than one copy
246 bool const enable_sorted = enable_counter && fl_get_counter_value(dialog_->counter_copies) > 1;
247 setEnabled(dialog_->check_sorted_copies, enable_sorted);