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/convert.h"
28 #include "lyx_forms.h"
36 typedef FormController<ControlPrint, FormView<FD_print> > base_class;
38 FormPrint::FormPrint(Dialog & parent)
39 : base_class(parent, _("Print Document"))
43 void FormPrint::build()
45 dialog_.reset(build_print(this));
47 // Manage the ok, apply and cancel/close buttons
48 bcview().setOK(dialog_->button_ok);
49 bcview().setApply(dialog_->button_apply);
50 bcview().setCancel(dialog_->button_close);
52 // trigger an input event for cut&paste with middle mouse button.
53 setPrehandler(dialog_->input_printer);
54 setPrehandler(dialog_->input_file);
55 setPrehandler(dialog_->input_from_page);
56 setPrehandler(dialog_->input_to_page);
58 fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
59 fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
60 fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
61 fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
63 // limit these inputs to unsigned integers
64 fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
65 fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
67 // what limits (if any) make sense for these?
68 fl_set_input_maxchars(dialog_->input_printer, 255);
69 fl_set_input_maxchars(dialog_->input_file, 255);
70 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
71 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
73 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
74 target_.init(dialog_->radio_file, PrinterParams::FILE);
76 all_pages_.init(dialog_->radio_all_pages, true);
77 all_pages_.init(dialog_->radio_from_to, false);
79 // set up the tooltips for Destination
80 string str = _("Select for printer output.");
81 tooltips().init(dialog_->radio_printer, str);
82 str = _("Enter printer command.");
83 tooltips().init(dialog_->input_printer, str);
84 str = _("Select for file output.");
85 tooltips().init(dialog_->radio_file, str);
86 str = _("Enter file name as print destination.");
87 tooltips().init(dialog_->input_file, str);
88 str = _("Browse directories for file name.");
89 tooltips().init(dialog_->button_browse, str);
91 // set up the tooltips for Range
92 str = _("Select for printing all pages.");
93 tooltips().init(dialog_->radio_all_pages, str);
94 str = _("Select for printing a specific page range.");
95 tooltips().init(dialog_->radio_from_to, str);
96 str = _("First page.");
97 tooltips().init(dialog_->input_from_page, str);
98 str = _("Last page.");
99 tooltips().init(dialog_->input_to_page, str);
100 str = _("Print the odd numbered pages.");
101 tooltips().init(dialog_->check_odd_pages, str);
102 str = _("Print the even numbered pages.");
103 tooltips().init(dialog_->check_even_pages, str);
105 // set up the tooltips for Copies
106 str = _("Number of copies to be printed.");
107 tooltips().init(dialog_->counter_copies, str);
108 str = _("Sort the copies.");
109 tooltips().init(dialog_->check_sorted_copies, str);
111 str = _("Reverse the order of the printed pages.");
112 tooltips().init(dialog_->check_reverse_order, str);
116 void FormPrint::apply()
120 pp.target = static_cast<PrinterParams::Target>(target_.get());
121 pp.printer_name = getString(dialog_->input_printer);
122 pp.file_name = getString(dialog_->input_file);
124 pp.all_pages = static_cast<bool>(all_pages_.get());
126 pp.from_page = pp.to_page = 0;
127 if (!getString(dialog_->input_from_page).empty()) {
128 // we have at least one page requested
129 pp.from_page = convert<int>(fl_get_input(dialog_->input_from_page));
130 if (!getString(dialog_->input_to_page).empty()) {
131 // okay we have a range
132 pp.to_page = convert<int>(fl_get_input(dialog_->input_to_page));
133 } // else we only print one page.
136 pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
137 pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
139 pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
140 pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
142 pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
144 controller().params() = pp;
148 void FormPrint::update()
150 PrinterParams & pp = controller().params();
152 target_.set(pp.target);
153 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
154 fl_set_input(dialog_->input_file, pp.file_name.c_str());
156 // hmmm... maybe a bit weird but maybe not
157 // we might just be remembering the last time this was printed.
158 all_pages_.set(pp.all_pages);
160 string const from = ( pp.from_page ? convert<string>(pp.from_page) : string() );
161 string const to = ( pp.to_page ? convert<string>(pp.to_page) : string() );
162 fl_set_input(dialog_->input_from_page, from.c_str());
163 fl_set_input(dialog_->input_to_page, to.c_str());
165 fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
166 fl_set_button(dialog_->check_even_pages, pp.even_pages);
167 fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
168 fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
170 fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
172 // number of copies only used when output goes to printer
173 bool const enable_counter = pp.target == PrinterParams::PRINTER;
174 setEnabled(dialog_->counter_copies, enable_counter);
176 // sorting only used when printing more than one copy
177 setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
179 // reset input fields to valid input
184 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
186 if (ob == dialog_->button_browse) {
187 // Get the filename from the dialog
188 string const in_name = getString(dialog_->input_file);
189 string const out_name = controller().browse(in_name);
191 // Save the filename to the dialog
192 if (out_name != in_name && !out_name.empty()) {
193 fl_set_input(dialog_->input_file, out_name.c_str());
196 // select the file radio
197 if (!out_name.empty()) {
198 target_.set(dialog_->radio_file);
201 // if we type input string for file or printer, select that as a target
202 } else if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
203 target_.set(dialog_->radio_file);
205 } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
206 target_.set(dialog_->radio_printer);
208 // if we type into 'from/to' fields, then select 'from/to' radio button
209 } else if ((ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
210 !fl_get_button(dialog_->radio_from_to)) {
211 all_pages_.set(dialog_->radio_from_to);
214 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
216 // disable OK/Apply buttons when file output is selected, but no file name entered
217 if (fl_get_button(dialog_->radio_file) && getString(dialog_->input_file).empty()) {
218 activate = ButtonPolicy::SMI_INVALID;
221 // check 'from' and 'to' fields only when 'from/to' radio button is selected
222 if (fl_get_button(dialog_->radio_from_to)) {
223 char const * from = fl_get_input(dialog_->input_from_page);
224 char const * to = fl_get_input(dialog_->input_to_page);
225 bool const from_input = static_cast<bool>(*from);
226 bool const to_input = static_cast<bool>(*to);
228 setEnabled(dialog_->input_to_page, from_input);
229 if (!from_input || (to_input && convert<int>(from) > convert<int>(to))) {
230 // Invalid input. Either 'from' is empty, or 'from' > 'to'.
231 // Probably editting these fields, so deactivate OK/Apply until input is valid again.
232 activate = ButtonPolicy::SMI_INVALID;
233 } else if (!to_input || convert<int>(from) == convert<int>(to)) {
234 // Valid input. Either there's only 'from' input, or 'from' == 'to'.
235 // Deactivate OK/Apply if odd/even selection implies no pages.
236 bool const odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
237 bool const even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
238 bool const odd_only = odd_pages && !even_pages;
239 bool const even_only = even_pages && !odd_pages;
240 bool const from_is_odd = static_cast<bool>(convert<int>(from) % 2);
241 if ( (from_is_odd && even_only) || (!from_is_odd && odd_only) ) {
242 activate = ButtonPolicy::SMI_INVALID;
247 // number of copies only used when output goes to printer
248 bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
249 setEnabled(dialog_->counter_copies, enable_counter);
251 // sorting only used when printing more than one copy
252 bool const enable_sorted = enable_counter && fl_get_counter_value(dialog_->counter_copies) > 1;
253 setEnabled(dialog_->check_sorted_copies, enable_sorted);
258 } // namespace frontend