]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
Added lyx-func tabular-feature for menu structure.
[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 "xform_macros.h"
16 #include "input_validators.h"
17 #include "LyXView.h"
18 #include "Dialogs.h"
19 #include "support/lstrings.h"
20 #include "lyxrc.h"
21 #include "PrinterParams.h"
22 #include "Liason.h"
23 #include "debug.h"
24 #include "BufferView.h"
25 #include "lyx_gui_misc.h"
26 #include "gettext.h"
27
28
29 #ifdef SIGC_CXX_NAMESPACES
30 using SigC::slot;
31 #endif
32
33 #ifdef CXX_WORKING_NAMESPACES
34 using Liason::printBuffer;
35 using Liason::getPrinterParams;
36 #endif
37
38 C_RETURNCB(FormPrint,  WMHideCB)
39 C_GENERICCB(FormPrint, OKCB)
40 C_GENERICCB(FormPrint, ApplyCB)
41 C_GENERICCB(FormPrint, CancelCB)
42 C_GENERICCB(FormPrint, InputCB)
43
44
45 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
46         : dialog_(0), lv_(lv), d_(d), u_(0), h_(0)
47 {
48         // let the dialog be shown
49         // This is a permanent connection so we won't bother
50         // storing a copy because we won't be disconnecting.
51         d->showPrint.connect(slot(this,&FormPrint::show));
52 }
53
54
55 FormPrint::~FormPrint()
56 {
57         free();
58 }
59
60
61 void FormPrint::build()
62 {
63         dialog_ = build_print();
64 }
65
66
67 void FormPrint::show()
68 {
69         if (!dialog_) {
70                 build();
71                 // allow controlling of input and ok/apply (de)activation
72                 fl_set_input_return(dialog_->input_printer,
73                                     FL_RETURN_CHANGED);
74                 fl_set_input_return(dialog_->input_file,
75                                     FL_RETURN_CHANGED);
76                 fl_set_input_return(dialog_->input_from_page,
77                                     FL_RETURN_CHANGED);
78                 fl_set_input_return(dialog_->input_to_page,
79                                     FL_RETURN_CHANGED);
80                 fl_set_input_return(dialog_->input_count,
81                                     FL_RETURN_CHANGED);
82
83                 // limit these inputs to unsigned integers
84                 fl_set_input_filter(dialog_->input_from_page,
85                                     fl_unsigned_int_filter);
86                 fl_set_input_filter(dialog_->input_to_page,
87                                     fl_unsigned_int_filter);
88                 fl_set_input_filter(dialog_->input_count,
89                                     fl_unsigned_int_filter);
90
91                 // what limits (if any) make sense for these?
92                 fl_set_input_maxchars(dialog_->input_printer, 255);
93                 fl_set_input_maxchars(dialog_->input_file, 255);
94                 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
95                 fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
96                 fl_set_input_maxchars(dialog_->input_count, 4);     // 9999
97                 
98                 fl_set_form_atclose(dialog_->form_print,
99                                     C_FormPrintWMHideCB, 0);
100         }
101
102         update();  // make sure its up-to-date
103
104         if (dialog_->form_print->visible) {
105                 fl_raise_form(dialog_->form_print);
106         } else {
107                 fl_show_form(dialog_->form_print,
108                              FL_PLACE_MOUSE | FL_FREE_SIZE,
109                              FL_FULLBORDER,
110                              _("Print"));
111                 u_ = d_->updateBufferDependent.connect(slot(this,
112                                                             &FormPrint::update));
113                 h_ = d_->hideBufferDependent.connect(slot(this,
114                                                           &FormPrint::hide));
115         }
116 }
117
118
119 void FormPrint::hide()
120 {
121         if (dialog_
122             && dialog_->form_print
123             && dialog_->form_print->visible) {
124                 fl_hide_form(dialog_->form_print);
125                 u_.disconnect();
126                 h_.disconnect();
127         }
128 }
129
130
131 void FormPrint::apply()
132 {
133         if (!lv_->view()->available()) {
134                 return;
135         }
136
137         PrinterParams::WhichPages wp(PrinterParams::ALL);
138         if (fl_get_button(dialog_->radio_even_pages)) {
139                 wp = PrinterParams::EVEN;
140         } else if (fl_get_button(dialog_->radio_odd_pages)) {
141                 wp = PrinterParams::ODD;
142         }
143
144         string from;
145         int to(0);
146         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
147                 // we have at least one page requested
148                 from = fl_get_input(dialog_->input_from_page);
149                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
150                         // okay we have a range
151                         to = strToInt(fl_get_input(dialog_->input_to_page));
152                 } // else we only print one page.
153         }
154
155         PrinterParams::Target t(PrinterParams::PRINTER);
156         if (fl_get_button(dialog_->radio_file)) {
157                 t = PrinterParams::FILE;
158         }
159
160         // we really should use the return value here I think.
161         if (!printBuffer(lv_->buffer(),
162                          PrinterParams(t,
163                                        string(fl_get_input(dialog_->input_printer)),
164                                        string(fl_get_input(dialog_->input_file)),
165                                        wp, from, to,
166                                        static_cast<bool>(fl_get_button(dialog_->
167                                                                        radio_order_reverse)),
168                                        static_cast<bool>(fl_get_button(dialog_->
169                                                                        radio_unsorted)),
170                                        strToInt(fl_get_input(dialog_->input_count))))) {
171                 WriteAlert(_("Error:"),
172                            _("Unable to print"),
173                            _("Check that your parameters are correct"));
174         }
175 }
176
177
178 void FormPrint::update()
179 {
180         if (dialog_
181             && lv_->view()->available()) {
182                 PrinterParams pp(getPrinterParams(lv_->buffer()));
183
184                 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
185                 fl_set_input(dialog_->input_file, pp.file_name.c_str());
186
187                 switch (pp.target) {
188                 case PrinterParams::FILE:
189                         fl_set_button(dialog_->radio_printer, 0);
190                         fl_set_button(dialog_->radio_file, 1);
191                         break;
192
193                 case PrinterParams::PRINTER:
194                 default:
195                         fl_set_button(dialog_->radio_printer, 1);
196                         fl_set_button(dialog_->radio_file, 0);
197                         break;
198                 }
199
200                 switch (pp.reverse_order) {
201                 case true:
202                         fl_set_button(dialog_->radio_order_normal, 0);
203                         fl_set_button(dialog_->radio_order_reverse, 1);
204                         break;
205
206                 case false:
207                 default:
208                         fl_set_button(dialog_->radio_order_normal, 1);
209                         fl_set_button(dialog_->radio_order_reverse, 0);
210                         break;
211                 }
212 // should be able to remove the various set_button 0 and rely on radio button
213 // action.  Provided xforms is smart enough :D
214                 fl_set_button(dialog_->radio_all_pages, 0);
215                 fl_set_button(dialog_->radio_odd_pages, 0);
216                 fl_set_button(dialog_->radio_even_pages, 0);
217                 switch (pp.which_pages) {
218                 case PrinterParams::ODD:
219                         fl_set_button(dialog_->radio_odd_pages, 1);
220                         break;
221
222                 case PrinterParams::EVEN:
223                         fl_set_button(dialog_->radio_even_pages, 1);
224                         break;
225
226                 case PrinterParams::ALL:
227                 default:
228                         fl_set_button(dialog_->radio_all_pages, 1);
229                         break;
230                 }
231
232                 // hmmm... maybe a bit weird but maybe not
233                 // we might just be remembering the last
234                 // time this was printed.
235                 if (!pp.from_page.empty()) {
236                         fl_set_input(dialog_->input_from_page,
237                                      pp.from_page.c_str());
238                         // we only set the "to" page of a range
239                         // if there's a corresponding "from"
240                         fl_activate_object(dialog_->input_to_page);
241                         if (pp.to_page) {
242                                 fl_set_input(dialog_->input_to_page,
243                                              tostr(pp.to_page).c_str());
244                         } else {
245                                 fl_set_input(dialog_->input_to_page,"");
246                         }
247                 } else {
248                         fl_deactivate_object(dialog_->input_to_page);
249                         fl_set_input(dialog_->input_to_page,"");
250                         fl_set_input(dialog_->input_from_page,"");
251                 }
252
253                 fl_set_input(dialog_->input_count,
254                              tostr(pp.count_copies).c_str());
255
256                 // Even readonly docs can be printed
257                 // these 4 activations are probably superfluous but I'm
258                 // being explicit for a reason.
259                 // They can probably be removed soon along with a few more
260                 // of the de/activations above once input() is a bit smarter.
261                 fl_activate_object(dialog_->input_count);
262                 fl_activate_object(dialog_->input_file);
263                 fl_activate_object(dialog_->input_from_page);
264                 fl_activate_object(dialog_->input_printer);
265                 // and we should always be in a working state upon exit
266                 input();
267         }
268 }
269
270
271 // It would be nice if we checked for cases like:
272 // Print only-odd-pages and from_page == an even number
273 //
274 void FormPrint::input()
275 {
276         bool activate = true;
277
278         // using a fl_input_filter that only permits numbers no '-' or '+'
279         // and the user cannot enter a negative number even if they try.
280         if (strlen(fl_get_input(dialog_->input_from_page))) {
281                 // using a page range so activate the "to" field
282                 fl_activate_object(dialog_->input_to_page);
283                 if (strlen(fl_get_input(dialog_->input_to_page))
284                     && (strToInt(fl_get_input(dialog_->input_from_page))
285                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
286                         // both from and to have values but from > to
287                         // We could have code to silently swap these
288                         // values but I'll disable the ok/apply until
289                         // the user fixes it since they may be editting
290                         // one of the fields.
291                         activate = false;
292                         // set both backgrounds to red?
293                 }
294         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
295                 // from is empty but to exists so probably editting from
296                 // therefore deactivate ok and apply until form is valid again
297                 activate = false;
298         } else {
299                 // both from and to are empty.  This is valid so activate
300                 // ok and apply but deactivate to
301                 fl_deactivate_object(dialog_->input_to_page);
302         }
303
304         if (fl_get_button(dialog_->radio_file)
305             && !strlen(fl_get_input(dialog_->input_file))) {
306                 activate = false;
307         }
308
309         // it is probably legal to have no printer name since the system will
310         // have a default printer set.  Or should have.
311 //      if (fl_get_button(dialog_->radio_printer)
312 //          && !strlen(fl_get_input(dialog_->input_printer))) {
313 //              activate = false;
314 //      }
315
316         if (activate) {
317                 fl_activate_object(dialog_->button_ok);
318                 fl_activate_object(dialog_->button_apply);
319                 fl_set_object_lcol(dialog_->button_ok, FL_BLACK);
320                 fl_set_object_lcol(dialog_->button_apply, FL_BLACK);
321         } else {
322                 fl_deactivate_object(dialog_->button_ok);
323                 fl_deactivate_object(dialog_->button_apply);
324                 fl_set_object_lcol(dialog_->button_ok, FL_INACTIVE);
325                 fl_set_object_lcol(dialog_->button_apply, FL_INACTIVE);
326         }
327 }
328
329
330 void FormPrint::free()
331 {
332         // we don't need to delete u and h here because
333         // hide() does that after disconnecting.
334         if (dialog_) {
335                 if (dialog_->form_print
336                     && dialog_->form_print->visible) {
337                         hide();
338                 }
339                 fl_free_form(dialog_->form_print);
340                 delete dialog_;
341                 dialog_ = 0;
342         }
343 }
344
345
346 int FormPrint::WMHideCB(FL_FORM * form, void *)
347 {
348         // Ensure that the signals (u and h) are disconnected even if the
349         // window manager is used to close the dialog.
350         FormPrint * pre = static_cast<FormPrint*>(form->u_vdata);
351         pre->hide();
352         return FL_CANCEL;
353 }
354
355
356 void FormPrint::OKCB(FL_OBJECT * ob, long)
357 {
358         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
359         pre->apply();
360         pre->hide();
361 }
362
363
364 void FormPrint::ApplyCB(FL_OBJECT * ob, long)
365 {
366         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
367         pre->apply();
368 }
369
370
371 void FormPrint::CancelCB(FL_OBJECT * ob, long)
372 {
373         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
374         pre->hide();
375 }
376
377
378 void FormPrint::InputCB(FL_OBJECT * ob, long)
379 {
380         FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
381         pre->input();
382 }