]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
Swap two printer related help messages.
[lyx.git] / src / frontends / xforms / FormPrint.C
1 /* FormPrint.C
2  * FormPrint Interface Class Implementation
3  */
4
5 #include <config.h>
6
7 #include FORMS_H_LOCATION
8
9 #ifdef __GNUG__
10 #pragma implementation
11 #endif
12
13 #include "FormPrint.h"
14 #include "form_print.h"
15 #include "input_validators.h"
16 #include "LyXView.h"
17 #include "Dialogs.h"
18 #include "support/lstrings.h"
19 #include "lyxrc.h"
20 #include "PrinterParams.h"
21 #include "Liason.h"
22 #include "debug.h"
23 #include "BufferView.h"
24 #include "lyx_gui_misc.h"       // WriteAlert
25
26 #ifdef SIGC_CXX_NAMESPACES
27 using SigC::slot;
28 #endif
29
30 #ifdef CXX_WORKING_NAMESPACES
31 using Liason::printBuffer;
32 using Liason::getPrinterParams;
33 #endif
34
35
36 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
37         : FormBaseBD(lv, d, _("Print"), new OkApplyCancelPolicy),
38           dialog_(0), target_(2), order_(2), which_(3)
39 {
40         // let the dialog be shown
41         // This is a permanent connection so we won't bother
42         // storing a copy because we won't be disconnecting.
43         d->showPrint.connect(slot(this, &FormPrint::show));
44 }
45
46
47 FormPrint::~FormPrint()
48 {
49         delete dialog_;
50 }
51
52
53 void FormPrint::build()
54 {
55         dialog_ = build_print();
56
57         // Workaround dumb xforms sizing bug
58         minw_ = form()->w;
59         minh_ = form()->h;
60
61         // Manage the ok, apply and cancel/close buttons
62         bc_.setOK(dialog_->button_ok);
63         bc_.setApply(dialog_->button_apply);
64         bc_.setCancel(dialog_->button_cancel);
65         bc_.refresh();
66
67         // allow controlling of input and ok/apply (de)activation
68         fl_set_input_return(dialog_->input_printer,
69                             FL_RETURN_CHANGED);
70         fl_set_input_return(dialog_->input_file,
71                             FL_RETURN_CHANGED);
72         fl_set_input_return(dialog_->input_from_page,
73                             FL_RETURN_CHANGED);
74         fl_set_input_return(dialog_->input_to_page,
75                             FL_RETURN_CHANGED);
76         fl_set_input_return(dialog_->input_count,
77                             FL_RETURN_CHANGED);
78
79         // limit these inputs to unsigned integers
80         fl_set_input_filter(dialog_->input_from_page,
81                             fl_unsigned_int_filter);
82         fl_set_input_filter(dialog_->input_to_page,
83                             fl_unsigned_int_filter);
84         fl_set_input_filter(dialog_->input_count,
85                             fl_unsigned_int_filter);
86
87         // what limits (if any) make sense for these?
88         fl_set_input_maxchars(dialog_->input_printer, 255);
89         fl_set_input_maxchars(dialog_->input_file, 255);
90         fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
91         fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
92         fl_set_input_maxchars(dialog_->input_count, 4);     // 9999
93
94         target_.reset();
95         target_.registerRadioButton(dialog_->radio_printer,
96                                     PrinterParams::PRINTER);
97         target_.registerRadioButton(dialog_->radio_file,
98                                     PrinterParams::FILE);
99         order_.reset();
100         order_.registerRadioButton(dialog_->radio_order_reverse,
101                                    true);
102         order_.registerRadioButton(dialog_->radio_order_normal,
103                                    false);
104         which_.reset();
105         which_.registerRadioButton(dialog_->radio_odd_pages,
106                                    PrinterParams::ODD);
107         which_.registerRadioButton(dialog_->radio_even_pages,
108                                    PrinterParams::EVEN);
109         which_.registerRadioButton(dialog_->radio_all_pages,
110                                    PrinterParams::ALL);
111 }
112
113
114 FL_FORM * FormPrint::form() const
115 {
116         if (dialog_) return dialog_->form;
117         return 0;
118 }
119
120
121 void FormPrint::apply()
122 {
123         if (!lv_->view()->available()) {
124                 return;
125         }
126
127         PrinterParams::WhichPages
128                 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
129
130         string from;
131         int to(0);
132         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
133                 // we have at least one page requested
134                 from = fl_get_input(dialog_->input_from_page);
135                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
136                         // okay we have a range
137                         to = strToInt(fl_get_input(dialog_->input_to_page));
138                 } // else we only print one page.
139         }
140
141         PrinterParams::Target
142                 t(static_cast<PrinterParams::Target>(target_.getButton()));
143
144         // we really should use the return value here I think.
145         if (!printBuffer(lv_->buffer(),
146                          PrinterParams(t,
147                                        string(fl_get_input(dialog_->input_printer)),
148                                        string(fl_get_input(dialog_->input_file)),
149                                        wp, from, to,
150                                        static_cast<bool>(order_.getButton()),
151                                        !static_cast<bool>(fl_get_button(dialog_->
152                                                                         radio_collated)),
153                                        strToInt(fl_get_input(dialog_->input_count))))) {
154                 WriteAlert(_("Error:"),
155                            _("Unable to print"),
156                            _("Check that your parameters are correct"));
157         }
158 }
159
160
161 void FormPrint::update()
162 {
163         if (dialog_
164             && lv_->view()->available()) {
165                 PrinterParams pp(getPrinterParams(lv_->buffer()));
166
167                 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
168                 fl_set_input(dialog_->input_file, pp.file_name.c_str());
169
170                 target_.setButton(pp.target);
171                 order_.setButton(pp.reverse_order);
172                 which_.setButton(pp.which_pages);
173
174                 // hmmm... maybe a bit weird but maybe not
175                 // we might just be remembering the last
176                 // time this was printed.
177                 if (!pp.from_page.empty()) {
178                         fl_set_input(dialog_->input_from_page,
179                                      pp.from_page.c_str());
180                         // we only set the "to" page of a range
181                         // if there's a corresponding "from"
182                         fl_activate_object(dialog_->input_to_page);
183                         if (pp.to_page) {
184                                 fl_set_input(dialog_->input_to_page,
185                                              tostr(pp.to_page).c_str());
186                         } else {
187                                 fl_set_input(dialog_->input_to_page,"");
188                         }
189                 } else {
190                         fl_deactivate_object(dialog_->input_to_page);
191                         fl_set_input(dialog_->input_to_page,"");
192                         fl_set_input(dialog_->input_from_page,"");
193                 }
194
195                 fl_set_input(dialog_->input_count,
196                              tostr(pp.count_copies).c_str());
197                 bc_.valid();
198         }
199 }
200
201
202 // It would be nice if we checked for cases like:
203 // Print only-odd-pages and from_page == an even number
204 //
205 bool FormPrint::input(FL_OBJECT *, long)
206 {
207         bool activate = true;
208
209         // using a fl_input_filter that only permits numbers no '-' or '+'
210         // and the user cannot enter a negative number even if they try.
211         if (strlen(fl_get_input(dialog_->input_from_page))) {
212                 // using a page range so activate the "to" field
213                 fl_activate_object(dialog_->input_to_page);
214                 if (strlen(fl_get_input(dialog_->input_to_page))
215                     && (strToInt(fl_get_input(dialog_->input_from_page))
216                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
217                         // both from and to have values but from > to
218                         // We could have code to silently swap these
219                         // values but I'll disable the ok/apply until
220                         // the user fixes it since they may be editting
221                         // one of the fields.
222                         activate = false;
223                         // set both backgrounds to red?
224                 }
225         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
226                 // from is empty but to exists so probably editting from
227                 // therefore deactivate ok and apply until form is valid again
228                 activate = false;
229         } else {
230                 // both from and to are empty.  This is valid so activate
231                 // ok and apply but deactivate to
232                 fl_deactivate_object(dialog_->input_to_page);
233         }
234
235         if (fl_get_button(dialog_->radio_file)
236             && !strlen(fl_get_input(dialog_->input_file))) {
237                 activate = false;
238         }
239
240         // it is probably legal to have no printer name since the system will
241         // have a default printer set.  Or should have.
242 //      if (fl_get_button(dialog_->radio_printer)
243 //          && !strlen(fl_get_input(dialog_->input_printer))) {
244 //              activate = false;
245 //      }
246         return activate;
247 }