]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
Yet more dialog tweaking from Rob.
[lyx.git] / src / frontends / xforms / FormPrint.C
1 /**
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.
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 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "xformsBC.h"
19 #include "ControlPrint.h"
20 #include "FormPrint.h"
21 #include "forms/form_print.h"
22 #include "Tooltips.h"
23
24 #include "PrinterParams.h"
25
26 #include "input_validators.h"
27 #include "xforms_helpers.h"
28
29 #include "support/lstrings.h"
30 #include FORMS_H_LOCATION
31
32 using std::make_pair;
33
34 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
35
36 FormPrint::FormPrint()
37         : base_class(_("Print")),
38           target_(2), which_pages_(2)
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         bc().setOK(dialog_->button_ok);
48         bc().setApply(dialog_->button_apply);
49         bc().setCancel(dialog_->button_close);
50
51         // allow controlling of input and ok/apply (de)activation
52         fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
53         fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
54         fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
55         fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
56
57         // limit these inputs to unsigned integers
58         fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
59         fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
60
61         // what limits (if any) make sense for these?
62         fl_set_input_maxchars(dialog_->input_printer, 255);
63         fl_set_input_maxchars(dialog_->input_file, 255);
64         fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
65         fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
66
67         bc().addReadOnly(dialog_->button_browse);   
68         bc().addReadOnly(dialog_->check_odd_pages);   
69         bc().addReadOnly(dialog_->check_even_pages);   
70         bc().addReadOnly(dialog_->check_sorted_copies);
71         bc().addReadOnly(dialog_->check_reverse_order);
72
73         target_.reset();
74         target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
75         target_.init(dialog_->radio_file,    PrinterParams::FILE);
76         which_pages_.reset();
77         which_pages_.init(dialog_->radio_all_pages, true);
78         which_pages_.init(dialog_->radio_from_to,   false);
79         
80         // set up the tooltips for Destination
81         string str = _("Select for printer output.");
82         tooltips().init(dialog_->radio_printer, str);
83         str = _("Enter printer command.");
84         tooltips().init(dialog_->input_printer, str);
85         str = _("Select for file output.");
86         tooltips().init(dialog_->radio_file, str);
87         str = _("Enter file name as print destination.");
88         tooltips().init(dialog_->input_file, str);
89         str = _("Browse directories for file name.");
90         tooltips().init(dialog_->button_browse, str);
91
92         // set up the tooltips for Range
93         str = _("Select for printing all pages.");
94         tooltips().init(dialog_->radio_all_pages, str);
95         str = _("Select for printing a specific page range.");
96         tooltips().init(dialog_->radio_from_to, str);
97         str = _("First page.");
98         tooltips().init(dialog_->input_from_page, str);
99         str = _("Last page.");
100         tooltips().init(dialog_->input_to_page, str);
101         str = _("Print the odd numbered pages.");
102         tooltips().init(dialog_->check_odd_pages, str);
103         str = _("Print the even numbered pages.");
104         tooltips().init(dialog_->check_even_pages, str);
105
106         // set up the tooltips for Copies
107         str = _("Number of copies to be printed.");
108         tooltips().init(dialog_->counter_copies, str);
109         str = _("Sort the copies.");
110         tooltips().init(dialog_->check_sorted_copies, str);
111
112         str = _("Reverse the order of the printed pages.");
113         tooltips().init(dialog_->check_reverse_order, str);
114 }
115
116
117 void FormPrint::apply()
118 {
119         PrinterParams pp;
120
121         pp.target = static_cast<PrinterParams::Target>(target_.get());
122         pp.printer_name = fl_get_input(dialog_->input_printer);
123         pp.file_name = fl_get_input(dialog_->input_file);
124
125         pp.all_pages = which_pages_.get();
126         pp.from_page = 0;
127         pp.to_page = 0;
128         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
129                 // we have at least one page requested
130                 pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
131                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
132                         // okay we have a range
133                         pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
134                 } // else we only print one page.
135         }
136
137         pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
138         pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
139
140         pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
141         pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
142
143         pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
144
145         controller().params() = pp;
146 }
147
148
149 void FormPrint::update()
150 {
151         PrinterParams & pp = controller().params();
152
153         target_.set(pp.target);
154         fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
155         fl_set_input(dialog_->input_file, pp.file_name.c_str());
156
157         // hmmm... maybe a bit weird but maybe not
158         // we might just be remembering the last time this was printed.
159         which_pages_.set(pp.all_pages);
160         
161         string const from = ( pp.from_page ? tostr(pp.from_page) : "");
162         string const to   = ( pp.to_page   ? tostr(pp.to_page)   : "");
163         fl_set_input(dialog_->input_from_page, from.c_str());
164         fl_set_input(dialog_->input_to_page, to.c_str());
165
166         fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
167         fl_set_button(dialog_->check_even_pages, pp.even_pages);
168         fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
169         fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
170
171         fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
172
173         // number of copies only used when output goes to printer
174         bool const enable_counter = pp.target == PrinterParams::PRINTER;
175         setEnabled(dialog_->counter_copies, enable_counter);
176
177         // sorting only used when printing more than one copy
178         setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
179 }
180
181
182 // It would be nice if we checked for cases like:
183 // Print only-odd-pages and from_page == an even number
184 //
185 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
186 {
187         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
188
189         // using a fl_input_filter that only permits numbers no '-' or '+'
190         // and the user cannot enter a negative number even if they try.
191         if (strlen(fl_get_input(dialog_->input_from_page))) {
192                 // using a page range so activate the "to" field
193                 fl_activate_object(dialog_->input_to_page);
194                 if (strlen(fl_get_input(dialog_->input_to_page))
195                     && (strToInt(fl_get_input(dialog_->input_from_page))
196                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
197                         // both from and to have values but from > to
198                         // We could have code to silently swap these
199                         // values but I'll disable the ok/apply until
200                         // the user fixes it since they may be editting
201                         // one of the fields.
202                         activate = ButtonPolicy::SMI_INVALID;
203                         // set both backgrounds to red?
204                 }
205         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
206                 // from is empty but to exists, so probably editting from
207                 // therefore deactivate ok and apply until form is valid again
208                 activate = ButtonPolicy::SMI_INVALID;
209         } else {
210                 // both from and to are empty.  This is valid so activate
211                 // ok and apply but deactivate to
212                 fl_deactivate_object(dialog_->input_to_page);
213         }
214
215         // number of copies only used when output goes to printer
216         bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
217         setEnabled(dialog_->counter_copies, enable_counter);
218
219         // sorting only used when printing more than one copy
220         bool const enable_sorted = enable_counter
221                         && static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies)) > 1;
222         setEnabled(dialog_->check_sorted_copies, enable_sorted);
223
224         // disable OK/Apply buttons when file output is selected, but no file name entered.
225         if (fl_get_button(dialog_->radio_file) && !strlen(fl_get_input(dialog_->input_file))) {
226                         activate = ButtonPolicy::SMI_INVALID;
227         }
228
229         if (ob == dialog_->button_browse) {
230                 // Get the filename from the dialog
231                 string const in_name = fl_get_input(dialog_->input_file);
232                 string const out_name = controller().Browse(in_name);
233
234                 // Save the filename to the dialog
235                 if (out_name != in_name && !out_name.empty()) {
236                         fl_set_input(dialog_->input_file, out_name.c_str());
237                         input(0, 0);
238                 }
239
240                 // select the file radio
241                 if (!out_name.empty()) {
242                         fl_set_button(dialog_->radio_file, 1);
243                         fl_set_button(dialog_->radio_printer, 0);
244                 }
245         }
246
247         // if we type input string for file or printer, select that as a target
248         if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
249                 fl_set_button(dialog_->radio_printer, 0);
250                 fl_set_button(dialog_->radio_file, 1);
251         } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
252                 fl_set_button(dialog_->radio_printer, 1);
253                 fl_set_button(dialog_->radio_file, 0);
254         // if we type intput string for from/to, select from/to radio button
255         } else if ( (ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
256                         !fl_get_button(dialog_->radio_from_to)) {
257                 fl_set_button(dialog_->radio_from_to, 1);
258                 fl_set_button(dialog_->radio_all_pages, 0);
259         }
260
261         return activate;
262 }