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