]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
Tiny clean-ups.
[lyx.git] / src / frontends / xforms / FormPrint.C
1 /**
2  * \file FormPrint.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Allan Rae
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "FormPrint.h"
15 #include "ControlPrint.h"
16 #include "forms/form_print.h"
17
18 #include "input_validators.h"
19 #include "Tooltips.h"
20 #include "xforms_helpers.h"
21 #include "xformsBC.h"
22
23 #include "PrinterParams.h"
24
25 #include "support/lstrings.h"
26 #include "support/tostr.h"
27
28 #include "lyx_forms.h"
29
30 using lyx::support::strToInt;
31
32 using std::string;
33
34
35 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
36
37 FormPrint::FormPrint()
38         : base_class(_("Print Document"))
39 {}
40
41
42 void FormPrint::build()
43 {
44         dialog_.reset(build_print(this));
45
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);
50
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);
56
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);
61
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);
65
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
71
72         target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
73         target_.init(dialog_->radio_file,    PrinterParams::FILE);
74
75         all_pages_.init(dialog_->radio_all_pages, true);
76         all_pages_.init(dialog_->radio_from_to, false);
77
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);
89
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);
103
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);
109
110         str = _("Reverse the order of the printed pages.");
111         tooltips().init(dialog_->check_reverse_order, str);
112 }
113
114
115 void FormPrint::apply()
116 {
117         PrinterParams pp;
118
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);
122
123         pp.all_pages = static_cast<bool>(all_pages_.get());
124
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.
133         }
134
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));
137
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));
140
141         pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
142
143         controller().params() = pp;
144 }
145
146
147 void FormPrint::update()
148 {
149         PrinterParams & pp = controller().params();
150
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());
154
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);
158
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());
163
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);
168
169         fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
170
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);
174
175         // sorting only used when printing more than one copy
176         setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
177
178         // reset input fields to valid input
179         input(0, 0);
180 }
181
182
183 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
184 {
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);
189
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());
193                 }
194
195                 // select the file radio
196                 if (!out_name.empty()) {
197                         target_.set(dialog_->radio_file);
198                 }
199
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);
203
204         } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
205                 target_.set(dialog_->radio_printer);
206
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);
211         }
212
213         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
214
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;
218         }
219
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);
226
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;
242                         }
243                 }
244         }
245
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);
249
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);
253
254         return activate;
255 }