]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
2b3db0e10da5daf6ed228a1877ded7f0851f6ef2
[lyx.git] / src / frontends / xforms / FormPrint.C
1 /*
2  * \file 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 #include "input_validators.h"
21 #include "support/lstrings.h"
22
23 #include "lyxrc.h" // needed by PrinterParams
24 #include "PrinterParams.h"
25
26 #include "LyXView.h"
27 #include "xforms_helpers.h"     // for browseFile
28
29 /*
30 #include "LyXView.h"
31 #include "Dialogs.h"
32 #include "Liason.h"
33 #include "debug.h"
34 #include "BufferView.h"
35 */
36
37 //using Liason::printBuffer;
38 //using Liason::getPrinterParams;
39 using std::make_pair;
40
41 typedef FormCB<ControlPrint, FormDB<FD_form_print> > base_class;
42
43 FormPrint::FormPrint(ControlPrint & c)
44         : base_class(c, _("Print")),
45           target_(2), order_(2), which_(3)
46 {}
47
48
49 void FormPrint::build()
50 {
51         dialog_.reset(build_print());
52
53         // Manage the ok, apply and cancel/close buttons
54         bc().setOK(dialog_->button_ok);
55         bc().setApply(dialog_->button_apply);
56         bc().setCancel(dialog_->button_cancel);
57
58         // allow controlling of input and ok/apply (de)activation
59         fl_set_input_return(dialog_->input_printer,
60                             FL_RETURN_CHANGED);
61         fl_set_input_return(dialog_->input_file,
62                             FL_RETURN_CHANGED);
63         fl_set_input_return(dialog_->input_from_page,
64                             FL_RETURN_CHANGED);
65         fl_set_input_return(dialog_->input_to_page,
66                             FL_RETURN_CHANGED);
67         fl_set_input_return(dialog_->input_count,
68                             FL_RETURN_CHANGED);
69
70         // limit these inputs to unsigned integers
71         fl_set_input_filter(dialog_->input_from_page,
72                             fl_unsigned_int_filter);
73         fl_set_input_filter(dialog_->input_to_page,
74                             fl_unsigned_int_filter);
75         fl_set_input_filter(dialog_->input_count,
76                             fl_unsigned_int_filter);
77
78         setPrehandler(dialog_->input_printer);
79         setPrehandler(dialog_->input_file);
80         setPrehandler(dialog_->input_from_page);
81         setPrehandler(dialog_->input_to_page);
82         setPrehandler(dialog_->input_count);
83
84         // what limits (if any) make sense for these?
85         fl_set_input_maxchars(dialog_->input_printer, 255);
86         fl_set_input_maxchars(dialog_->input_file, 255);
87         fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
88         fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
89         fl_set_input_maxchars(dialog_->input_count, 4);     // 9999
90
91         target_.reset();
92         target_.registerRadioButton(dialog_->radio_printer,
93                                     PrinterParams::PRINTER);
94         target_.registerRadioButton(dialog_->radio_file,
95                                     PrinterParams::FILE);
96         order_.reset();
97         order_.registerRadioButton(dialog_->radio_order_reverse,
98                                    true);
99         order_.registerRadioButton(dialog_->radio_order_normal,
100                                    false);
101         which_.reset();
102         which_.registerRadioButton(dialog_->radio_odd_pages,
103                                    PrinterParams::ODD);
104         which_.registerRadioButton(dialog_->radio_even_pages,
105                                    PrinterParams::EVEN);
106         which_.registerRadioButton(dialog_->radio_all_pages,
107                                    PrinterParams::ALL);
108 }
109
110
111 void FormPrint::apply()
112 {
113         PrinterParams::WhichPages
114                 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
115
116         string from;
117         int to(0);
118         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
119                 // we have at least one page requested
120                 from = fl_get_input(dialog_->input_from_page);
121                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
122                         // okay we have a range
123                         to = strToInt(fl_get_input(dialog_->input_to_page));
124                 } // else we only print one page.
125         }
126
127         PrinterParams::Target
128                 t(static_cast<PrinterParams::Target>(target_.getButton()));
129
130         PrinterParams const pp(t,
131                                string(fl_get_input(dialog_->input_printer)),
132                                string(fl_get_input(dialog_->input_file)),
133                                wp, from, to,
134                                static_cast<bool>(order_.getButton()),
135                                !static_cast<bool>(fl_get_button(dialog_->radio_collated)),
136                                strToInt(fl_get_input(dialog_->input_count)));
137
138         controller().params() = pp;
139 }
140
141
142 void FormPrint::update()
143 {
144         PrinterParams & pp = controller().params();
145
146         fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
147         fl_set_input(dialog_->input_file, pp.file_name.c_str());
148
149         target_.setButton(pp.target);
150         order_.setButton(pp.reverse_order);
151         which_.setButton(pp.which_pages);
152
153         // hmmm... maybe a bit weird but maybe not
154         // we might just be remembering the last
155         // time this was printed.
156         if (!pp.from_page.empty()) {
157                 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
158
159                 // we only set the "to" page of a range
160                 // if there's a corresponding "from"
161                 fl_activate_object(dialog_->input_to_page);
162                 if (pp.to_page) {
163                         fl_set_input(dialog_->input_to_page,
164                                      tostr(pp.to_page).c_str());
165                 } else {
166                         fl_set_input(dialog_->input_to_page,"");
167                 }
168
169         } else {
170                 fl_deactivate_object(dialog_->input_to_page);
171                 fl_set_input(dialog_->input_to_page,"");
172                 fl_set_input(dialog_->input_from_page,"");
173         }
174
175         fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
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         if (fl_get_button(dialog_->radio_file)
213             && !strlen(fl_get_input(dialog_->input_file))) {
214                 activate = ButtonPolicy::SMI_INVALID;
215         }
216
217         if (ob == dialog_->button_browse) {
218                 // Get the filename from the dialog
219                 string const in_name = fl_get_input(dialog_->input_file);
220                 string const out_name = controller().Browse(in_name);
221
222                 // Save the filename to the dialog
223                 if (out_name != in_name && !out_name.empty()) {
224                         fl_set_input(dialog_->input_file, out_name.c_str());
225                         input(0, 0);
226                 }
227
228                 // select the file radio
229                 if (!out_name.empty()) {
230                         fl_set_button(dialog_->radio_file, 1);
231                         fl_set_button(dialog_->radio_printer, 0);
232                 }
233         }
234                 
235         // if we type into file, select that as a target
236         if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer) 
237                 && strlen(fl_get_input(dialog_->input_file))) {
238                 fl_set_button(dialog_->radio_file, 1);
239                 fl_set_button(dialog_->radio_printer, 0);
240         } else if (ob == dialog_->input_printer) {
241                 fl_set_button(dialog_->radio_file, 0);
242                 fl_set_button(dialog_->radio_printer, 1);
243         }
244  
245         return activate;
246 }