]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
fix crash with "save as"
[lyx.git] / src / frontends / xforms / FormPrint.C
1 /**
2  * \file xforms/FormPrint.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Allan Rae
7  * \author Angus Leeming 
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "xformsBC.h"
19 #include "ControlPrint.h"
20 #include "FormPrint.h"
21 #include "forms/form_print.h"
22 #include "Tooltips.h"
23
24 #include "PrinterParams.h"
25
26 #include "input_validators.h"
27 #include "xforms_helpers.h"
28
29 #include "support/lstrings.h"
30 #include FORMS_H_LOCATION
31
32
33 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
34
35 FormPrint::FormPrint()
36         : base_class(_("Print"))
37 {}
38
39
40 void FormPrint::build()
41 {
42         dialog_.reset(build_print(this));
43
44         // Manage the ok, apply and cancel/close buttons
45         bc().setOK(dialog_->button_ok);
46         bc().setApply(dialog_->button_apply);
47         bc().setCancel(dialog_->button_close);
48
49         // trigger an input event for cut&paste with middle mouse button.
50         setPrehandler(dialog_->input_printer);
51         setPrehandler(dialog_->input_file);
52         setPrehandler(dialog_->input_from_page);
53         setPrehandler(dialog_->input_to_page);
54
55         fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
56         fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
57         fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
58         fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
59
60         // limit these inputs to unsigned integers
61         fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
62         fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
63
64         // what limits (if any) make sense for these?
65         fl_set_input_maxchars(dialog_->input_printer, 255);
66         fl_set_input_maxchars(dialog_->input_file, 255);
67         fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
68         fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
69
70         target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
71         target_.init(dialog_->radio_file,    PrinterParams::FILE);
72
73         all_pages_.init(dialog_->radio_all_pages, true);
74         all_pages_.init(dialog_->radio_from_to, false);
75
76         // set up the tooltips for Destination
77         string str = _("Select for printer output.");
78         tooltips().init(dialog_->radio_printer, str);
79         str = _("Enter printer command.");
80         tooltips().init(dialog_->input_printer, str);
81         str = _("Select for file output.");
82         tooltips().init(dialog_->radio_file, str);
83         str = _("Enter file name as print destination.");
84         tooltips().init(dialog_->input_file, str);
85         str = _("Browse directories for file name.");
86         tooltips().init(dialog_->button_browse, str);
87
88         // set up the tooltips for Range
89         str = _("Select for printing all pages.");
90         tooltips().init(dialog_->radio_all_pages, str);
91         str = _("Select for printing a specific page range.");
92         tooltips().init(dialog_->radio_from_to, str);
93         str = _("First page.");
94         tooltips().init(dialog_->input_from_page, str);
95         str = _("Last page.");
96         tooltips().init(dialog_->input_to_page, str);
97         str = _("Print the odd numbered pages.");
98         tooltips().init(dialog_->check_odd_pages, str);
99         str = _("Print the even numbered pages.");
100         tooltips().init(dialog_->check_even_pages, str);
101
102         // set up the tooltips for Copies
103         str = _("Number of copies to be printed.");
104         tooltips().init(dialog_->counter_copies, str);
105         str = _("Sort the copies.");
106         tooltips().init(dialog_->check_sorted_copies, str);
107
108         str = _("Reverse the order of the printed pages.");
109         tooltips().init(dialog_->check_reverse_order, str);
110 }
111
112
113 void FormPrint::apply()
114 {
115         PrinterParams pp;
116
117         pp.target = static_cast<PrinterParams::Target>(target_.get());
118         pp.printer_name = getString(dialog_->input_printer);
119         pp.file_name = getString(dialog_->input_file);
120
121         pp.all_pages = static_cast<bool>(all_pages_.get());
122
123         pp.from_page = pp.to_page = 0;
124         if (!getString(dialog_->input_from_page).empty()) {
125                 // we have at least one page requested
126                 pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
127                 if (!getString(dialog_->input_to_page).empty()) {
128                         // okay we have a range
129                         pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
130                 } // else we only print one page.
131         }
132
133         pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
134         pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
135
136         pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
137         pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
138
139         pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
140
141         controller().params() = pp;
142 }
143
144
145 void FormPrint::update()
146 {
147         PrinterParams & pp = controller().params();
148
149         target_.set(pp.target);
150         fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
151         fl_set_input(dialog_->input_file, pp.file_name.c_str());
152
153         // hmmm... maybe a bit weird but maybe not
154         // we might just be remembering the last time this was printed.
155         all_pages_.set(pp.all_pages);
156         
157         string const from = ( pp.from_page ? tostr(pp.from_page) : string() );
158         string const to   = ( pp.to_page   ? tostr(pp.to_page)   : string() );
159         fl_set_input(dialog_->input_from_page, from.c_str());
160         fl_set_input(dialog_->input_to_page, to.c_str());
161
162         fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
163         fl_set_button(dialog_->check_even_pages, pp.even_pages);
164         fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
165         fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
166
167         fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
168
169         // number of copies only used when output goes to printer
170         bool const enable_counter = pp.target == PrinterParams::PRINTER;
171         setEnabled(dialog_->counter_copies, enable_counter);
172
173         // sorting only used when printing more than one copy
174         setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
175
176         // reset input fields to valid input
177         input(0, 0);
178 }
179
180
181 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
182 {
183         if (ob == dialog_->button_browse) {
184                 // Get the filename from the dialog
185                 string const in_name = getString(dialog_->input_file);
186                 string const out_name = controller().Browse(in_name);
187
188                 // Save the filename to the dialog
189                 if (out_name != in_name && !out_name.empty()) {
190                         fl_set_input(dialog_->input_file, out_name.c_str());
191                 }
192
193                 // select the file radio
194                 if (!out_name.empty()) {
195                         target_.set(dialog_->radio_file);
196                 }
197
198         // if we type input string for file or printer, select that as a target
199         } else if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
200                 target_.set(dialog_->radio_file);
201
202         } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
203                 target_.set(dialog_->radio_printer);
204
205         // if we type into 'from/to' fields, then select 'from/to' radio button
206         } else if ((ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
207                         !fl_get_button(dialog_->radio_from_to)) {
208                 all_pages_.set(dialog_->radio_from_to);
209         }
210
211         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
212
213         // disable OK/Apply buttons when file output is selected, but no file name entered
214         if (fl_get_button(dialog_->radio_file) && getString(dialog_->input_file).empty()) {
215                         activate = ButtonPolicy::SMI_INVALID;
216         }
217
218         // check 'from' and 'to' fields only when 'from/to' radio button is selected
219         if (fl_get_button(dialog_->radio_from_to)) {
220                 char const * from = fl_get_input(dialog_->input_from_page);
221                 char const * to = fl_get_input(dialog_->input_to_page);
222                 bool const from_input = static_cast<bool>(*from);
223                 bool const to_input = static_cast<bool>(*to);
224
225                 setEnabled(dialog_->input_to_page, from_input);
226                 if (!from_input || (to_input && strToInt(from) > strToInt(to))) {
227                         // Invalid input. Either 'from' is empty, or 'from' > 'to'.
228                         // Probably editting these fields, so deactivate OK/Apply until input is valid again.
229                         activate = ButtonPolicy::SMI_INVALID;
230                 } else if (!to_input || strToInt(from) == strToInt(to)) {
231                         // Valid input. Either there's only 'from' input, or 'from' == 'to'.
232                         // Deactivate OK/Apply if odd/even selection implies no pages.
233                         bool const odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
234                         bool const even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
235                         bool const odd_only = odd_pages && !even_pages;
236                         bool const even_only = even_pages && !odd_pages;
237                         bool const from_is_odd = static_cast<bool>(strToInt(from) % 2);
238                         if ( (from_is_odd && even_only) || (!from_is_odd && odd_only) ) {
239                                 activate = ButtonPolicy::SMI_INVALID;
240                         }
241                 }
242         }
243
244         // number of copies only used when output goes to printer
245         bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
246         setEnabled(dialog_->counter_copies, enable_counter);
247
248         // sorting only used when printing more than one copy
249         bool const enable_sorted = enable_counter && fl_get_counter_value(dialog_->counter_copies) > 1;
250         setEnabled(dialog_->check_sorted_copies, enable_sorted);
251
252         return activate;
253 }