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