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"))
41 void FormPrint::build()
43 dialog_.reset(build_print(this));
45 // Manage the ok, apply and cancel/close buttons
46 bc().setOK(dialog_->button_ok);
47 bc().setApply(dialog_->button_apply);
48 bc().setCancel(dialog_->button_close);
50 // allow controlling of input and ok/apply (de)activation
51 fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
52 fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
53 fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
54 fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
56 // limit these inputs to unsigned integers
57 fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
58 fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
60 // what limits (if any) make sense for these?
61 fl_set_input_maxchars(dialog_->input_printer, 255);
62 fl_set_input_maxchars(dialog_->input_file, 255);
63 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
64 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
66 bc().addReadOnly(dialog_->button_browse);
67 bc().addReadOnly(dialog_->check_odd_pages);
68 bc().addReadOnly(dialog_->check_even_pages);
69 bc().addReadOnly(dialog_->check_sorted_copies);
70 bc().addReadOnly(dialog_->check_reverse_order);
72 target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
73 target_.init(dialog_->radio_file, PrinterParams::FILE);
74 which_pages_.init(dialog_->radio_all_pages, true);
75 which_pages_.init(dialog_->radio_from_to, false);
77 // set up the tooltips for Destination
78 string str = _("Select for printer output.");
79 tooltips().init(dialog_->radio_printer, str);
80 str = _("Enter printer command.");
81 tooltips().init(dialog_->input_printer, str);
82 str = _("Select for file output.");
83 tooltips().init(dialog_->radio_file, str);
84 str = _("Enter file name as print destination.");
85 tooltips().init(dialog_->input_file, str);
86 str = _("Browse directories for file name.");
87 tooltips().init(dialog_->button_browse, str);
89 // set up the tooltips for Range
90 str = _("Select for printing all pages.");
91 tooltips().init(dialog_->radio_all_pages, str);
92 str = _("Select for printing a specific page range.");
93 tooltips().init(dialog_->radio_from_to, str);
94 str = _("First page.");
95 tooltips().init(dialog_->input_from_page, str);
96 str = _("Last page.");
97 tooltips().init(dialog_->input_to_page, str);
98 str = _("Print the odd numbered pages.");
99 tooltips().init(dialog_->check_odd_pages, str);
100 str = _("Print the even numbered pages.");
101 tooltips().init(dialog_->check_even_pages, str);
103 // set up the tooltips for Copies
104 str = _("Number of copies to be printed.");
105 tooltips().init(dialog_->counter_copies, str);
106 str = _("Sort the copies.");
107 tooltips().init(dialog_->check_sorted_copies, str);
109 str = _("Reverse the order of the printed pages.");
110 tooltips().init(dialog_->check_reverse_order, str);
114 void FormPrint::apply()
118 pp.target = static_cast<PrinterParams::Target>(target_.get());
119 pp.printer_name = fl_get_input(dialog_->input_printer);
120 pp.file_name = fl_get_input(dialog_->input_file);
122 pp.all_pages = which_pages_.get();
125 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
126 // we have at least one page requested
127 pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
128 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
129 // okay we have a range
130 pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
131 } // else we only print one page.
134 pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
135 pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
137 pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
138 pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
140 pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
142 controller().params() = pp;
146 void FormPrint::update()
148 PrinterParams & pp = controller().params();
150 target_.set(pp.target);
151 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
152 fl_set_input(dialog_->input_file, pp.file_name.c_str());
154 // hmmm... maybe a bit weird but maybe not
155 // we might just be remembering the last time this was printed.
156 which_pages_.set(pp.all_pages);
158 string const from = ( pp.from_page ? tostr(pp.from_page) : "");
159 string const to = ( pp.to_page ? tostr(pp.to_page) : "");
160 fl_set_input(dialog_->input_from_page, from.c_str());
161 fl_set_input(dialog_->input_to_page, to.c_str());
163 fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
164 fl_set_button(dialog_->check_even_pages, pp.even_pages);
165 fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
166 fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
168 fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
170 // number of copies only used when output goes to printer
171 bool const enable_counter = pp.target == PrinterParams::PRINTER;
172 setEnabled(dialog_->counter_copies, enable_counter);
174 // sorting only used when printing more than one copy
175 setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
179 // It would be nice if we checked for cases like:
180 // Print only-odd-pages and from_page == an even number
182 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
184 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
186 // using a fl_input_filter that only permits numbers no '-' or '+'
187 // and the user cannot enter a negative number even if they try.
188 if (strlen(fl_get_input(dialog_->input_from_page))) {
189 // using a page range so activate the "to" field
190 fl_activate_object(dialog_->input_to_page);
191 if (strlen(fl_get_input(dialog_->input_to_page))
192 && (strToInt(fl_get_input(dialog_->input_from_page))
193 > strToInt(fl_get_input(dialog_->input_to_page)))) {
194 // both from and to have values but from > to
195 // We could have code to silently swap these
196 // values but I'll disable the ok/apply until
197 // the user fixes it since they may be editting
198 // one of the fields.
199 activate = ButtonPolicy::SMI_INVALID;
200 // set both backgrounds to red?
202 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
203 // from is empty but to exists, so probably editting from
204 // therefore deactivate ok and apply until form is valid again
205 activate = ButtonPolicy::SMI_INVALID;
207 // both from and to are empty. This is valid so activate
208 // ok and apply but deactivate to
209 fl_deactivate_object(dialog_->input_to_page);
212 // number of copies only used when output goes to printer
213 bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
214 setEnabled(dialog_->counter_copies, enable_counter);
216 // sorting only used when printing more than one copy
217 bool const enable_sorted = enable_counter
218 && static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies)) > 1;
219 setEnabled(dialog_->check_sorted_copies, enable_sorted);
221 // disable OK/Apply buttons when file output is selected, but no file name entered.
222 if (fl_get_button(dialog_->radio_file) && !strlen(fl_get_input(dialog_->input_file))) {
223 activate = ButtonPolicy::SMI_INVALID;
226 if (ob == dialog_->button_browse) {
227 // Get the filename from the dialog
228 string const in_name = fl_get_input(dialog_->input_file);
229 string const out_name = controller().Browse(in_name);
231 // Save the filename to the dialog
232 if (out_name != in_name && !out_name.empty()) {
233 fl_set_input(dialog_->input_file, out_name.c_str());
237 // select the file radio
238 if (!out_name.empty()) {
239 fl_set_button(dialog_->radio_file, 1);
240 fl_set_button(dialog_->radio_printer, 0);
244 // if we type input string for file or printer, select that as a target
245 if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
246 fl_set_button(dialog_->radio_printer, 0);
247 fl_set_button(dialog_->radio_file, 1);
248 } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
249 fl_set_button(dialog_->radio_printer, 1);
250 fl_set_button(dialog_->radio_file, 0);
251 // if we type intput string for from/to, select from/to radio button
252 } else if ( (ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
253 !fl_get_button(dialog_->radio_from_to)) {
254 fl_set_button(dialog_->radio_from_to, 1);
255 fl_set_button(dialog_->radio_all_pages, 0);