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
33 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
35 FormPrint::FormPrint()
36 : base_class(_("Print"))
40 void FormPrint::build()
42 dialog_.reset(build_print(this));
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 // trigger an input event for cut&paste with middle mouse button.
50 setPrehandler(dialog_->input_printer);
51 setPrehandler(dialog_->input_file);
52 setPrehandler(dialog_->input_from_page);
53 setPrehandler(dialog_->input_to_page);
55 fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
56 fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
57 fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
58 fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
60 // limit these inputs to unsigned integers
61 fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
62 fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
64 // what limits (if any) make sense for these?
65 fl_set_input_maxchars(dialog_->input_printer, 255);
66 fl_set_input_maxchars(dialog_->input_file, 255);
67 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
68 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
70 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
71 target_.init(dialog_->radio_file, PrinterParams::FILE);
73 all_pages_.init(dialog_->radio_all_pages, true);
74 all_pages_.init(dialog_->radio_from_to, false);
76 // set up the tooltips for Destination
77 string str = _("Select for printer output.");
78 tooltips().init(dialog_->radio_printer, str);
79 str = _("Enter printer command.");
80 tooltips().init(dialog_->input_printer, str);
81 str = _("Select for file output.");
82 tooltips().init(dialog_->radio_file, str);
83 str = _("Enter file name as print destination.");
84 tooltips().init(dialog_->input_file, str);
85 str = _("Browse directories for file name.");
86 tooltips().init(dialog_->button_browse, str);
88 // set up the tooltips for Range
89 str = _("Select for printing all pages.");
90 tooltips().init(dialog_->radio_all_pages, str);
91 str = _("Select for printing a specific page range.");
92 tooltips().init(dialog_->radio_from_to, str);
93 str = _("First page.");
94 tooltips().init(dialog_->input_from_page, str);
95 str = _("Last page.");
96 tooltips().init(dialog_->input_to_page, str);
97 str = _("Print the odd numbered pages.");
98 tooltips().init(dialog_->check_odd_pages, str);
99 str = _("Print the even numbered pages.");
100 tooltips().init(dialog_->check_even_pages, str);
102 // set up the tooltips for Copies
103 str = _("Number of copies to be printed.");
104 tooltips().init(dialog_->counter_copies, str);
105 str = _("Sort the copies.");
106 tooltips().init(dialog_->check_sorted_copies, str);
108 str = _("Reverse the order of the printed pages.");
109 tooltips().init(dialog_->check_reverse_order, str);
113 void FormPrint::apply()
117 pp.target = static_cast<PrinterParams::Target>(target_.get());
118 pp.printer_name = getString(dialog_->input_printer);
119 pp.file_name = getString(dialog_->input_file);
121 pp.all_pages = static_cast<bool>(all_pages_.get());
123 pp.from_page = pp.to_page = 0;
124 if (!getString(dialog_->input_from_page).empty()) {
125 // we have at least one page requested
126 pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
127 if (!getString(dialog_->input_to_page).empty()) {
128 // okay we have a range
129 pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
130 } // else we only print one page.
133 pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
134 pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
136 pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
137 pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
139 pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
141 controller().params() = pp;
145 void FormPrint::update()
147 PrinterParams & pp = controller().params();
149 target_.set(pp.target);
150 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
151 fl_set_input(dialog_->input_file, pp.file_name.c_str());
153 // hmmm... maybe a bit weird but maybe not
154 // we might just be remembering the last time this was printed.
155 all_pages_.set(pp.all_pages);
157 string const from = ( pp.from_page ? tostr(pp.from_page) : string() );
158 string const to = ( pp.to_page ? tostr(pp.to_page) : string() );
159 fl_set_input(dialog_->input_from_page, from.c_str());
160 fl_set_input(dialog_->input_to_page, to.c_str());
162 fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
163 fl_set_button(dialog_->check_even_pages, pp.even_pages);
164 fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
165 fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
167 fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
169 // number of copies only used when output goes to printer
170 bool const enable_counter = pp.target == PrinterParams::PRINTER;
171 setEnabled(dialog_->counter_copies, enable_counter);
173 // sorting only used when printing more than one copy
174 setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
176 // reset input fields to valid input
181 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
183 if (ob == dialog_->button_browse) {
184 // Get the filename from the dialog
185 string const in_name = getString(dialog_->input_file);
186 string const out_name = controller().Browse(in_name);
188 // Save the filename to the dialog
189 if (out_name != in_name && !out_name.empty()) {
190 fl_set_input(dialog_->input_file, out_name.c_str());
193 // select the file radio
194 if (!out_name.empty()) {
195 target_.set(dialog_->radio_file);
198 // if we type input string for file or printer, select that as a target
199 } else if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
200 target_.set(dialog_->radio_file);
202 } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
203 target_.set(dialog_->radio_printer);
205 // if we type into 'from/to' fields, then select 'from/to' radio button
206 } else if ((ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
207 !fl_get_button(dialog_->radio_from_to)) {
208 all_pages_.set(dialog_->radio_from_to);
211 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
213 // disable OK/Apply buttons when file output is selected, but no file name entered
214 if (fl_get_button(dialog_->radio_file) && getString(dialog_->input_file).empty()) {
215 activate = ButtonPolicy::SMI_INVALID;
218 // check 'from' and 'to' fields only when 'from/to' radio button is selected
219 if (fl_get_button(dialog_->radio_from_to)) {
220 char const * from = fl_get_input(dialog_->input_from_page);
221 char const * to = fl_get_input(dialog_->input_to_page);
222 bool const from_input = static_cast<bool>(*from);
223 bool const to_input = static_cast<bool>(*to);
225 setEnabled(dialog_->input_to_page, from_input);
226 if (!from_input || (to_input && strToInt(from) > strToInt(to))) {
227 // Invalid input. Either 'from' is empty, or 'from' > 'to'.
228 // Probably editting these fields, so deactivate OK/Apply until input is valid again.
229 activate = ButtonPolicy::SMI_INVALID;
230 } else if (!to_input || strToInt(from) == strToInt(to)) {
231 // Valid input. Either there's only 'from' input, or 'from' == 'to'.
232 // Deactivate OK/Apply if odd/even selection implies no pages.
233 bool const odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
234 bool const even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
235 bool const odd_only = odd_pages && !even_pages;
236 bool const even_only = even_pages && !odd_pages;
237 bool const from_is_odd = static_cast<bool>(strToInt(from) % 2);
238 if ( (from_is_odd && even_only) || (!from_is_odd && odd_only) ) {
239 activate = ButtonPolicy::SMI_INVALID;
244 // number of copies only used when output goes to printer
245 bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
246 setEnabled(dialog_->counter_copies, enable_counter);
248 // sorting only used when printing more than one copy
249 bool const enable_sorted = enable_counter && fl_get_counter_value(dialog_->counter_copies) > 1;
250 setEnabled(dialog_->check_sorted_copies, enable_sorted);