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.
14 #include "FormPrint.h"
15 #include "ControlPrint.h"
16 #include "forms/form_print.h"
18 #include "input_validators.h"
20 #include "xforms_helpers.h"
23 #include "PrinterParams.h"
25 #include "support/lstrings.h"
26 #include "support/tostr.h"
28 #include "lyx_forms.h"
30 using lyx::support::strToInt;
35 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
37 FormPrint::FormPrint()
38 : base_class(_("Print Document"))
42 void FormPrint::build()
44 dialog_.reset(build_print(this));
46 // Manage the ok, apply and cancel/close buttons
47 bcview().setOK(dialog_->button_ok);
48 bcview().setApply(dialog_->button_apply);
49 bcview().setCancel(dialog_->button_close);
51 // trigger an input event for cut&paste with middle mouse button.
52 setPrehandler(dialog_->input_printer);
53 setPrehandler(dialog_->input_file);
54 setPrehandler(dialog_->input_from_page);
55 setPrehandler(dialog_->input_to_page);
57 fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
58 fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
59 fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
60 fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
62 // limit these inputs to unsigned integers
63 fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
64 fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
66 // what limits (if any) make sense for these?
67 fl_set_input_maxchars(dialog_->input_printer, 255);
68 fl_set_input_maxchars(dialog_->input_file, 255);
69 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
70 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
72 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
73 target_.init(dialog_->radio_file, PrinterParams::FILE);
75 all_pages_.init(dialog_->radio_all_pages, true);
76 all_pages_.init(dialog_->radio_from_to, false);
78 // set up the tooltips for Destination
79 string str = _("Select for printer output.");
80 tooltips().init(dialog_->radio_printer, str);
81 str = _("Enter printer command.");
82 tooltips().init(dialog_->input_printer, str);
83 str = _("Select for file output.");
84 tooltips().init(dialog_->radio_file, str);
85 str = _("Enter file name as print destination.");
86 tooltips().init(dialog_->input_file, str);
87 str = _("Browse directories for file name.");
88 tooltips().init(dialog_->button_browse, str);
90 // set up the tooltips for Range
91 str = _("Select for printing all pages.");
92 tooltips().init(dialog_->radio_all_pages, str);
93 str = _("Select for printing a specific page range.");
94 tooltips().init(dialog_->radio_from_to, str);
95 str = _("First page.");
96 tooltips().init(dialog_->input_from_page, str);
97 str = _("Last page.");
98 tooltips().init(dialog_->input_to_page, str);
99 str = _("Print the odd numbered pages.");
100 tooltips().init(dialog_->check_odd_pages, str);
101 str = _("Print the even numbered pages.");
102 tooltips().init(dialog_->check_even_pages, str);
104 // set up the tooltips for Copies
105 str = _("Number of copies to be printed.");
106 tooltips().init(dialog_->counter_copies, str);
107 str = _("Sort the copies.");
108 tooltips().init(dialog_->check_sorted_copies, str);
110 str = _("Reverse the order of the printed pages.");
111 tooltips().init(dialog_->check_reverse_order, str);
115 void FormPrint::apply()
119 pp.target = static_cast<PrinterParams::Target>(target_.get());
120 pp.printer_name = getString(dialog_->input_printer);
121 pp.file_name = getString(dialog_->input_file);
123 pp.all_pages = static_cast<bool>(all_pages_.get());
125 pp.from_page = pp.to_page = 0;
126 if (!getString(dialog_->input_from_page).empty()) {
127 // we have at least one page requested
128 pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
129 if (!getString(dialog_->input_to_page).empty()) {
130 // okay we have a range
131 pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
132 } // else we only print one page.
135 pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
136 pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
138 pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
139 pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
141 pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
143 controller().params() = pp;
147 void FormPrint::update()
149 PrinterParams & pp = controller().params();
151 target_.set(pp.target);
152 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
153 fl_set_input(dialog_->input_file, pp.file_name.c_str());
155 // hmmm... maybe a bit weird but maybe not
156 // we might just be remembering the last time this was printed.
157 all_pages_.set(pp.all_pages);
159 string const from = ( pp.from_page ? tostr(pp.from_page) : string() );
160 string const to = ( pp.to_page ? tostr(pp.to_page) : string() );
161 fl_set_input(dialog_->input_from_page, from.c_str());
162 fl_set_input(dialog_->input_to_page, to.c_str());
164 fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
165 fl_set_button(dialog_->check_even_pages, pp.even_pages);
166 fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
167 fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
169 fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
171 // number of copies only used when output goes to printer
172 bool const enable_counter = pp.target == PrinterParams::PRINTER;
173 setEnabled(dialog_->counter_copies, enable_counter);
175 // sorting only used when printing more than one copy
176 setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
178 // reset input fields to valid input
183 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
185 if (ob == dialog_->button_browse) {
186 // Get the filename from the dialog
187 string const in_name = getString(dialog_->input_file);
188 string const out_name = controller().Browse(in_name);
190 // Save the filename to the dialog
191 if (out_name != in_name && !out_name.empty()) {
192 fl_set_input(dialog_->input_file, out_name.c_str());
195 // select the file radio
196 if (!out_name.empty()) {
197 target_.set(dialog_->radio_file);
200 // if we type input string for file or printer, select that as a target
201 } else if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
202 target_.set(dialog_->radio_file);
204 } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
205 target_.set(dialog_->radio_printer);
207 // if we type into 'from/to' fields, then select 'from/to' radio button
208 } else if ((ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
209 !fl_get_button(dialog_->radio_from_to)) {
210 all_pages_.set(dialog_->radio_from_to);
213 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
215 // disable OK/Apply buttons when file output is selected, but no file name entered
216 if (fl_get_button(dialog_->radio_file) && getString(dialog_->input_file).empty()) {
217 activate = ButtonPolicy::SMI_INVALID;
220 // check 'from' and 'to' fields only when 'from/to' radio button is selected
221 if (fl_get_button(dialog_->radio_from_to)) {
222 char const * from = fl_get_input(dialog_->input_from_page);
223 char const * to = fl_get_input(dialog_->input_to_page);
224 bool const from_input = static_cast<bool>(*from);
225 bool const to_input = static_cast<bool>(*to);
227 setEnabled(dialog_->input_to_page, from_input);
228 if (!from_input || (to_input && strToInt(from) > strToInt(to))) {
229 // Invalid input. Either 'from' is empty, or 'from' > 'to'.
230 // Probably editting these fields, so deactivate OK/Apply until input is valid again.
231 activate = ButtonPolicy::SMI_INVALID;
232 } else if (!to_input || strToInt(from) == strToInt(to)) {
233 // Valid input. Either there's only 'from' input, or 'from' == 'to'.
234 // Deactivate OK/Apply if odd/even selection implies no pages.
235 bool const odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
236 bool const even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
237 bool const odd_only = odd_pages && !even_pages;
238 bool const even_only = even_pages && !odd_pages;
239 bool const from_is_odd = static_cast<bool>(strToInt(from) % 2);
240 if ( (from_is_odd && even_only) || (!from_is_odd && odd_only) ) {
241 activate = ButtonPolicy::SMI_INVALID;
246 // number of copies only used when output goes to printer
247 bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
248 setEnabled(dialog_->counter_copies, enable_counter);
250 // sorting only used when printing more than one copy
251 bool const enable_sorted = enable_counter && fl_get_counter_value(dialog_->counter_copies) > 1;
252 setEnabled(dialog_->check_sorted_copies, enable_sorted);