]> git.lyx.org Git - features.git/blob - src/frontends/xforms/FormPrint.C
6658f77f696464a2b44384a3cda33cc8c1b4101b
[features.git] / src / frontends / xforms / FormPrint.C
1 /*
2  * \file xforms/FormPrint.C
3  * Copyright 2000-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Allan Rae, rae@lyx.org
7  * \author Angus Leeming <leeming@lyx.org>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "xformsBC.h"
17 #include "ControlPrint.h"
18 #include "FormPrint.h"
19 #include "forms/form_print.h"
20
21 #include "PrinterParams.h"
22
23 #include "input_validators.h"
24 #include "xforms_helpers.h"
25
26 #include "support/lstrings.h"
27 #include FORMS_H_LOCATION
28
29 using std::make_pair;
30
31 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
32
33 FormPrint::FormPrint()
34         : base_class(_("Print")),
35           target_(2), order_(2), which_(3)
36 {}
37
38
39 void FormPrint::build()
40 {
41         dialog_.reset(build_print(this));
42
43         // Manage the ok, apply and cancel/close buttons
44         bc().setOK(dialog_->button_ok);
45         bc().setApply(dialog_->button_apply);
46         bc().setCancel(dialog_->button_close);
47
48         // allow controlling of input and ok/apply (de)activation
49         fl_set_input_return(dialog_->input_printer,
50                             FL_RETURN_CHANGED);
51         fl_set_input_return(dialog_->input_file,
52                             FL_RETURN_CHANGED);
53         fl_set_input_return(dialog_->input_from_page,
54                             FL_RETURN_CHANGED);
55         fl_set_input_return(dialog_->input_to_page,
56                             FL_RETURN_CHANGED);
57         fl_set_input_return(dialog_->input_count,
58                             FL_RETURN_CHANGED);
59
60         // limit these inputs to unsigned integers
61         fl_set_input_filter(dialog_->input_from_page,
62                             fl_unsigned_int_filter);
63         fl_set_input_filter(dialog_->input_to_page,
64                             fl_unsigned_int_filter);
65         fl_set_input_filter(dialog_->input_count,
66                             fl_unsigned_int_filter);
67
68         setPrehandler(dialog_->input_printer);
69         setPrehandler(dialog_->input_file);
70         setPrehandler(dialog_->input_from_page);
71         setPrehandler(dialog_->input_to_page);
72         setPrehandler(dialog_->input_count);
73
74         // what limits (if any) make sense for these?
75         fl_set_input_maxchars(dialog_->input_printer, 255);
76         fl_set_input_maxchars(dialog_->input_file, 255);
77         fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
78         fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
79         fl_set_input_maxchars(dialog_->input_count, 4);     // 9999
80
81         target_.reset();
82         target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
83         target_.init(dialog_->radio_file,    PrinterParams::FILE);
84         order_.reset();
85         order_.init(dialog_->radio_order_reverse, true);
86         order_.init(dialog_->radio_order_normal,  false);
87         which_.reset();
88         which_.init(dialog_->radio_odd_pages,  PrinterParams::ODD);
89         which_.init(dialog_->radio_even_pages, PrinterParams::EVEN);
90         which_.init(dialog_->radio_all_pages,  PrinterParams::ALL);
91 }
92
93
94 void FormPrint::apply()
95 {
96         PrinterParams::WhichPages
97                 wp(static_cast<PrinterParams::WhichPages>(which_.get()));
98
99         string from;
100         int to(0);
101         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
102                 // we have at least one page requested
103                 from = fl_get_input(dialog_->input_from_page);
104                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
105                         // okay we have a range
106                         to = strToInt(fl_get_input(dialog_->input_to_page));
107                 } // else we only print one page.
108         }
109
110         PrinterParams::Target
111                 t(static_cast<PrinterParams::Target>(target_.get()));
112
113         PrinterParams const pp(t,
114                                string(fl_get_input(dialog_->input_printer)),
115                                string(fl_get_input(dialog_->input_file)),
116                                wp, from, to,
117                                static_cast<bool>(order_.get()),
118                                !static_cast<bool>(fl_get_button(dialog_->check_collated)),
119                                strToInt(fl_get_input(dialog_->input_count)));
120
121         controller().params() = pp;
122 }
123
124
125 void FormPrint::update()
126 {
127         PrinterParams & pp = controller().params();
128
129         fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
130         fl_set_input(dialog_->input_file, pp.file_name.c_str());
131
132         target_.set(pp.target);
133         order_.set(pp.reverse_order);
134         which_.set(pp.which_pages);
135
136         // hmmm... maybe a bit weird but maybe not
137         // we might just be remembering the last
138         // time this was printed.
139         if (!pp.from_page.empty()) {
140                 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
141
142                 // we only set the "to" page of a range
143                 // if there's a corresponding "from"
144                 fl_activate_object(dialog_->input_to_page);
145                 if (pp.to_page) {
146                         fl_set_input(dialog_->input_to_page,
147                                      tostr(pp.to_page).c_str());
148                 } else {
149                         fl_set_input(dialog_->input_to_page,"");
150                 }
151
152         } else {
153                 fl_deactivate_object(dialog_->input_to_page);
154                 fl_set_input(dialog_->input_to_page,"");
155                 fl_set_input(dialog_->input_from_page,"");
156         }
157
158         fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
159 }
160
161
162 // It would be nice if we checked for cases like:
163 // Print only-odd-pages and from_page == an even number
164 //
165 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
166 {
167         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
168
169         // using a fl_input_filter that only permits numbers no '-' or '+'
170         // and the user cannot enter a negative number even if they try.
171         if (strlen(fl_get_input(dialog_->input_from_page))) {
172                 // using a page range so activate the "to" field
173                 fl_activate_object(dialog_->input_to_page);
174                 if (strlen(fl_get_input(dialog_->input_to_page))
175                     && (strToInt(fl_get_input(dialog_->input_from_page))
176                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
177                         // both from and to have values but from > to
178                         // We could have code to silently swap these
179                         // values but I'll disable the ok/apply until
180                         // the user fixes it since they may be editting
181                         // one of the fields.
182                         activate = ButtonPolicy::SMI_INVALID;
183                         // set both backgrounds to red?
184                 }
185         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
186                 // from is empty but to exists so probably editting from
187                 // therefore deactivate ok and apply until form is valid again
188                 activate = ButtonPolicy::SMI_INVALID;
189         } else {
190                 // both from and to are empty.  This is valid so activate
191                 // ok and apply but deactivate to
192                 fl_deactivate_object(dialog_->input_to_page);
193         }
194
195         if (fl_get_button(dialog_->radio_file)
196             && !strlen(fl_get_input(dialog_->input_file))) {
197                 activate = ButtonPolicy::SMI_INVALID;
198         }
199
200         if (ob == dialog_->button_browse) {
201                 // Get the filename from the dialog
202                 string const in_name = fl_get_input(dialog_->input_file);
203                 string const out_name = controller().Browse(in_name);
204
205                 // Save the filename to the dialog
206                 if (out_name != in_name && !out_name.empty()) {
207                         fl_set_input(dialog_->input_file, out_name.c_str());
208                         input(0, 0);
209                 }
210
211                 // select the file radio
212                 if (!out_name.empty()) {
213                         fl_set_button(dialog_->radio_file, 1);
214                         fl_set_button(dialog_->radio_printer, 0);
215                 }
216         }
217
218         // if we type into file, select that as a target
219         if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer)
220             && strlen(fl_get_input(dialog_->input_file))) {
221                 fl_set_button(dialog_->radio_file, 1);
222                 fl_set_button(dialog_->radio_printer, 0);
223         } else if (ob == dialog_->input_printer) {
224                 fl_set_button(dialog_->radio_file, 0);
225                 fl_set_button(dialog_->radio_printer, 1);
226         }
227
228         return activate;
229 }