]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
(Rob Lahaye): small clean-up of the clean-ups already applied.
[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 {}
39
40
41 void FormPrint::build()
42 {
43         dialog_.reset(build_print(this));
44
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);
49
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);
55
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);
59
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
65
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);
71
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);
76         
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);
88
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);
102
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);
108
109         str = _("Reverse the order of the printed pages.");
110         tooltips().init(dialog_->check_reverse_order, str);
111 }
112
113
114 void FormPrint::apply()
115 {
116         PrinterParams pp;
117
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);
121
122         pp.all_pages = which_pages_.get();
123         pp.from_page = 0;
124         pp.to_page = 0;
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.
132         }
133
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));
136
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));
139
140         pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
141
142         controller().params() = pp;
143 }
144
145
146 void FormPrint::update()
147 {
148         PrinterParams & pp = controller().params();
149
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());
153
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);
157         
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());
162
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);
167
168         fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
169
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);
173
174         // sorting only used when printing more than one copy
175         setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
176 }
177
178
179 // It would be nice if we checked for cases like:
180 // Print only-odd-pages and from_page == an even number
181 //
182 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
183 {
184         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
185
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?
201                 }
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;
206         } else {
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);
210         }
211
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);
215
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);
220
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;
224         }
225
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);
230
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());
234                         input(0, 0);
235                 }
236
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);
241                 }
242         }
243
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);
256         }
257
258         return activate;
259 }