]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
Dekel's lyxrc.example; Angus's FormDocument; John's build-listerrors; POTFILES.in...
[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, BUFFER_DEPENDENT, _("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         // 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 void FormPrint::connect()
111 {
112         FormBase::connect();
113         fl_set_form_minsize(dialog_->form,
114                             dialog_->form->w,
115                             dialog_->form->h);
116 }
117
118
119 FL_FORM * FormPrint::form() const
120 {
121         if (dialog_) return dialog_->form;
122         return 0;
123 }
124
125
126 void FormPrint::apply()
127 {
128         if (!lv_->view()->available()) {
129                 return;
130         }
131
132         PrinterParams::WhichPages
133                 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
134
135         string from;
136         int to(0);
137         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
138                 // we have at least one page requested
139                 from = fl_get_input(dialog_->input_from_page);
140                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
141                         // okay we have a range
142                         to = strToInt(fl_get_input(dialog_->input_to_page));
143                 } // else we only print one page.
144         }
145
146         PrinterParams::Target
147                 t(static_cast<PrinterParams::Target>(target_.getButton()));
148
149         // we really should use the return value here I think.
150         if (!printBuffer(lv_->buffer(),
151                          PrinterParams(t,
152                                        string(fl_get_input(dialog_->input_printer)),
153                                        string(fl_get_input(dialog_->input_file)),
154                                        wp, from, to,
155                                        static_cast<bool>(order_.getButton()),
156                                        static_cast<bool>(fl_get_button(dialog_->
157                                                                        radio_unsorted)),
158                                        strToInt(fl_get_input(dialog_->input_count))))) {
159                 WriteAlert(_("Error:"),
160                            _("Unable to print"),
161                            _("Check that your parameters are correct"));
162         }
163 }
164
165
166 void FormPrint::update()
167 {
168         if (dialog_
169             && lv_->view()->available()) {
170                 PrinterParams pp(getPrinterParams(lv_->buffer()));
171
172                 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
173                 fl_set_input(dialog_->input_file, pp.file_name.c_str());
174
175                 target_.setButton(pp.target);
176                 order_.setButton(pp.reverse_order);
177                 which_.setButton(pp.which_pages);
178
179                 // hmmm... maybe a bit weird but maybe not
180                 // we might just be remembering the last
181                 // time this was printed.
182                 if (!pp.from_page.empty()) {
183                         fl_set_input(dialog_->input_from_page,
184                                      pp.from_page.c_str());
185                         // we only set the "to" page of a range
186                         // if there's a corresponding "from"
187                         fl_activate_object(dialog_->input_to_page);
188                         if (pp.to_page) {
189                                 fl_set_input(dialog_->input_to_page,
190                                              tostr(pp.to_page).c_str());
191                         } else {
192                                 fl_set_input(dialog_->input_to_page,"");
193                         }
194                 } else {
195                         fl_deactivate_object(dialog_->input_to_page);
196                         fl_set_input(dialog_->input_to_page,"");
197                         fl_set_input(dialog_->input_from_page,"");
198                 }
199
200                 fl_set_input(dialog_->input_count,
201                              tostr(pp.count_copies).c_str());
202         }
203 }
204
205
206 // It would be nice if we checked for cases like:
207 // Print only-odd-pages and from_page == an even number
208 //
209 bool FormPrint::input(FL_OBJECT *, long)
210 {
211         bool activate = true;
212
213         // using a fl_input_filter that only permits numbers no '-' or '+'
214         // and the user cannot enter a negative number even if they try.
215         if (strlen(fl_get_input(dialog_->input_from_page))) {
216                 // using a page range so activate the "to" field
217                 fl_activate_object(dialog_->input_to_page);
218                 if (strlen(fl_get_input(dialog_->input_to_page))
219                     && (strToInt(fl_get_input(dialog_->input_from_page))
220                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
221                         // both from and to have values but from > to
222                         // We could have code to silently swap these
223                         // values but I'll disable the ok/apply until
224                         // the user fixes it since they may be editting
225                         // one of the fields.
226                         activate = false;
227                         // set both backgrounds to red?
228                 }
229         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
230                 // from is empty but to exists so probably editting from
231                 // therefore deactivate ok and apply until form is valid again
232                 activate = false;
233         } else {
234                 // both from and to are empty.  This is valid so activate
235                 // ok and apply but deactivate to
236                 fl_deactivate_object(dialog_->input_to_page);
237         }
238
239         if (fl_get_button(dialog_->radio_file)
240             && !strlen(fl_get_input(dialog_->input_file))) {
241                 activate = false;
242         }
243
244         // it is probably legal to have no printer name since the system will
245         // have a default printer set.  Or should have.
246 //      if (fl_get_button(dialog_->radio_printer)
247 //          && !strlen(fl_get_input(dialog_->input_printer))) {
248 //              activate = false;
249 //      }
250         return activate;
251 }