]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
4ae24cd23093dc61d848568b09a8741ce256b329
[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         : FormBase(lv, d, _("Print"),
38                    BUFFER_DEPENDENT, HIDE, new OkApplyCancelPolicy),
39           dialog_(0), target_(2), order_(2), which_(3)
40 {
41         // let the dialog be shown
42         // This is a permanent connection so we won't bother
43         // storing a copy because we won't be disconnecting.
44         d->showPrint.connect(slot(this, &FormPrint::show));
45 }
46
47
48 FormPrint::~FormPrint()
49 {
50         delete dialog_;
51 }
52
53
54 void FormPrint::build()
55 {
56         dialog_ = build_print();
57
58         // manage the ok, apply and cancel/close buttons
59         bc_.setOK(dialog_->button_ok);
60         bc_.setApply(dialog_->button_apply);
61         bc_.setCancel(dialog_->button_cancel);
62         bc_.refresh();
63
64         // allow controlling of input and ok/apply (de)activation
65         fl_set_input_return(dialog_->input_printer,
66                             FL_RETURN_CHANGED);
67         fl_set_input_return(dialog_->input_file,
68                             FL_RETURN_CHANGED);
69         fl_set_input_return(dialog_->input_from_page,
70                             FL_RETURN_CHANGED);
71         fl_set_input_return(dialog_->input_to_page,
72                             FL_RETURN_CHANGED);
73         fl_set_input_return(dialog_->input_count,
74                             FL_RETURN_CHANGED);
75
76         // limit these inputs to unsigned integers
77         fl_set_input_filter(dialog_->input_from_page,
78                             fl_unsigned_int_filter);
79         fl_set_input_filter(dialog_->input_to_page,
80                             fl_unsigned_int_filter);
81         fl_set_input_filter(dialog_->input_count,
82                             fl_unsigned_int_filter);
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::connect()
112 {
113         FormBase::connect();
114         fl_set_form_minsize(dialog_->form,
115                             dialog_->form->w,
116                             dialog_->form->h);
117 }
118
119
120 FL_FORM * FormPrint::form() const
121 {
122         if (dialog_) return dialog_->form;
123         return 0;
124 }
125
126
127 void FormPrint::apply()
128 {
129         if (!lv_->view()->available()) {
130                 return;
131         }
132
133         PrinterParams::WhichPages
134                 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
135
136         string from;
137         int to(0);
138         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
139                 // we have at least one page requested
140                 from = fl_get_input(dialog_->input_from_page);
141                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
142                         // okay we have a range
143                         to = strToInt(fl_get_input(dialog_->input_to_page));
144                 } // else we only print one page.
145         }
146
147         PrinterParams::Target
148                 t(static_cast<PrinterParams::Target>(target_.getButton()));
149
150         // we really should use the return value here I think.
151         if (!printBuffer(lv_->buffer(),
152                          PrinterParams(t,
153                                        string(fl_get_input(dialog_->input_printer)),
154                                        string(fl_get_input(dialog_->input_file)),
155                                        wp, from, to,
156                                        static_cast<bool>(order_.getButton()),
157                                        !static_cast<bool>(fl_get_button(dialog_->
158                                                                         radio_collated)),
159                                        strToInt(fl_get_input(dialog_->input_count))))) {
160                 WriteAlert(_("Error:"),
161                            _("Unable to print"),
162                            _("Check that your parameters are correct"));
163         }
164 }
165
166
167 void FormPrint::update()
168 {
169         if (dialog_
170             && lv_->view()->available()) {
171                 PrinterParams pp(getPrinterParams(lv_->buffer()));
172
173                 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
174                 fl_set_input(dialog_->input_file, pp.file_name.c_str());
175
176                 target_.setButton(pp.target);
177                 order_.setButton(pp.reverse_order);
178                 which_.setButton(pp.which_pages);
179
180                 // hmmm... maybe a bit weird but maybe not
181                 // we might just be remembering the last
182                 // time this was printed.
183                 if (!pp.from_page.empty()) {
184                         fl_set_input(dialog_->input_from_page,
185                                      pp.from_page.c_str());
186                         // we only set the "to" page of a range
187                         // if there's a corresponding "from"
188                         fl_activate_object(dialog_->input_to_page);
189                         if (pp.to_page) {
190                                 fl_set_input(dialog_->input_to_page,
191                                              tostr(pp.to_page).c_str());
192                         } else {
193                                 fl_set_input(dialog_->input_to_page,"");
194                         }
195                 } else {
196                         fl_deactivate_object(dialog_->input_to_page);
197                         fl_set_input(dialog_->input_to_page,"");
198                         fl_set_input(dialog_->input_from_page,"");
199                 }
200
201                 fl_set_input(dialog_->input_count,
202                              tostr(pp.count_copies).c_str());
203         }
204 }
205
206
207 // It would be nice if we checked for cases like:
208 // Print only-odd-pages and from_page == an even number
209 //
210 bool FormPrint::input(FL_OBJECT *, long)
211 {
212         bool activate = true;
213
214         // using a fl_input_filter that only permits numbers no '-' or '+'
215         // and the user cannot enter a negative number even if they try.
216         if (strlen(fl_get_input(dialog_->input_from_page))) {
217                 // using a page range so activate the "to" field
218                 fl_activate_object(dialog_->input_to_page);
219                 if (strlen(fl_get_input(dialog_->input_to_page))
220                     && (strToInt(fl_get_input(dialog_->input_from_page))
221                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
222                         // both from and to have values but from > to
223                         // We could have code to silently swap these
224                         // values but I'll disable the ok/apply until
225                         // the user fixes it since they may be editting
226                         // one of the fields.
227                         activate = false;
228                         // set both backgrounds to red?
229                 }
230         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
231                 // from is empty but to exists so probably editting from
232                 // therefore deactivate ok and apply until form is valid again
233                 activate = false;
234         } else {
235                 // both from and to are empty.  This is valid so activate
236                 // ok and apply but deactivate to
237                 fl_deactivate_object(dialog_->input_to_page);
238         }
239
240         if (fl_get_button(dialog_->radio_file)
241             && !strlen(fl_get_input(dialog_->input_file))) {
242                 activate = false;
243         }
244
245         // it is probably legal to have no printer name since the system will
246         // have a default printer set.  Or should have.
247 //      if (fl_get_button(dialog_->radio_printer)
248 //          && !strlen(fl_get_input(dialog_->input_printer))) {
249 //              activate = false;
250 //      }
251         return activate;
252 }