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