]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
cxx compile fixes ; several patches from John
[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@.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         // what limits (if any) make sense for these?
79         fl_set_input_maxchars(dialog_->input_printer, 255);
80         fl_set_input_maxchars(dialog_->input_file, 255);
81         fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
82         fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
83         fl_set_input_maxchars(dialog_->input_count, 4);     // 9999
84
85         target_.reset();
86         target_.registerRadioButton(dialog_->radio_printer,
87                                     PrinterParams::PRINTER);
88         target_.registerRadioButton(dialog_->radio_file,
89                                     PrinterParams::FILE);
90         order_.reset();
91         order_.registerRadioButton(dialog_->radio_order_reverse,
92                                    true);
93         order_.registerRadioButton(dialog_->radio_order_normal,
94                                    false);
95         which_.reset();
96         which_.registerRadioButton(dialog_->radio_odd_pages,
97                                    PrinterParams::ODD);
98         which_.registerRadioButton(dialog_->radio_even_pages,
99                                    PrinterParams::EVEN);
100         which_.registerRadioButton(dialog_->radio_all_pages,
101                                    PrinterParams::ALL);
102 }
103
104
105 void FormPrint::apply()
106 {
107         PrinterParams::WhichPages
108                 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
109
110         string from;
111         int to(0);
112         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
113                 // we have at least one page requested
114                 from = fl_get_input(dialog_->input_from_page);
115                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
116                         // okay we have a range
117                         to = strToInt(fl_get_input(dialog_->input_to_page));
118                 } // else we only print one page.
119         }
120
121         PrinterParams::Target
122                 t(static_cast<PrinterParams::Target>(target_.getButton()));
123
124         PrinterParams const pp(t,
125                                string(fl_get_input(dialog_->input_printer)),
126                                string(fl_get_input(dialog_->input_file)),
127                                wp, from, to,
128                                static_cast<bool>(order_.getButton()),
129                                !static_cast<bool>(fl_get_button(dialog_->radio_collated)),
130                                strToInt(fl_get_input(dialog_->input_count)));
131
132         controller().params() = pp;
133 }
134
135
136 void FormPrint::update()
137 {
138         PrinterParams & pp = controller().params();
139
140         fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
141         fl_set_input(dialog_->input_file, pp.file_name.c_str());
142
143         target_.setButton(pp.target);
144         order_.setButton(pp.reverse_order);
145         which_.setButton(pp.which_pages);
146
147         // hmmm... maybe a bit weird but maybe not
148         // we might just be remembering the last
149         // time this was printed.
150         if (!pp.from_page.empty()) {
151                 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
152
153                 // we only set the "to" page of a range
154                 // if there's a corresponding "from"
155                 fl_activate_object(dialog_->input_to_page);
156                 if (pp.to_page) {
157                         fl_set_input(dialog_->input_to_page,
158                                      tostr(pp.to_page).c_str());
159                 } else {
160                         fl_set_input(dialog_->input_to_page,"");
161                 }
162
163         } else {
164                 fl_deactivate_object(dialog_->input_to_page);
165                 fl_set_input(dialog_->input_to_page,"");
166                 fl_set_input(dialog_->input_from_page,"");
167         }
168
169         fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
170 }
171
172
173 // It would be nice if we checked for cases like:
174 // Print only-odd-pages and from_page == an even number
175 //
176 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
177 {
178         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
179
180         // using a fl_input_filter that only permits numbers no '-' or '+'
181         // and the user cannot enter a negative number even if they try.
182         if (strlen(fl_get_input(dialog_->input_from_page))) {
183                 // using a page range so activate the "to" field
184                 fl_activate_object(dialog_->input_to_page);
185                 if (strlen(fl_get_input(dialog_->input_to_page))
186                     && (strToInt(fl_get_input(dialog_->input_from_page))
187                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
188                         // both from and to have values but from > to
189                         // We could have code to silently swap these
190                         // values but I'll disable the ok/apply until
191                         // the user fixes it since they may be editting
192                         // one of the fields.
193                         activate = ButtonPolicy::SMI_INVALID;
194                         // set both backgrounds to red?
195                 }
196         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
197                 // from is empty but to exists so probably editting from
198                 // therefore deactivate ok and apply until form is valid again
199                 activate = ButtonPolicy::SMI_INVALID;
200         } else {
201                 // both from and to are empty.  This is valid so activate
202                 // ok and apply but deactivate to
203                 fl_deactivate_object(dialog_->input_to_page);
204         }
205
206         if (fl_get_button(dialog_->radio_file)
207             && !strlen(fl_get_input(dialog_->input_file))) {
208                 activate = ButtonPolicy::SMI_INVALID;
209         }
210
211         if (ob == dialog_->button_browse) {
212                 // Get the filename from the dialog
213                 string const in_name = fl_get_input(dialog_->input_file);
214                 string const out_name = controller().Browse(in_name);
215
216                 // Save the filename to the dialog
217                 if (out_name != in_name && !out_name.empty()) {
218                         fl_set_input(dialog_->input_file, out_name.c_str());
219                         input(0, 0);
220                 }
221
222                 // select the file radio
223                 if (!out_name.empty()) {
224                         fl_set_button(dialog_->radio_file, 1);
225                         fl_set_button(dialog_->radio_printer, 0);
226                 }
227         }
228                 
229         // if we type into file, select that as a target
230         if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer) 
231                 && strlen(fl_get_input(dialog_->input_file))) {
232                 fl_set_button(dialog_->radio_file, 1);
233                 fl_set_button(dialog_->radio_printer, 0);
234         } else if (ob == dialog_->input_printer) {
235                 fl_set_button(dialog_->radio_file, 0);
236                 fl_set_button(dialog_->radio_printer, 1);
237         }
238  
239         return activate;
240 }