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