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