]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormPrint.C
fix tooltips in toolbar
[lyx.git] / src / frontends / xforms / FormPrint.C
1 /*
2  * \file xforms/FormPrint.C
3  * Copyright 2000-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Allan Rae, rae@lyx.org
7  * \author Angus Leeming, a.leeming@ic.ac.uk
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "xformsBC.h"
17 #include "ControlPrint.h"
18 #include "FormPrint.h"
19 #include "forms/form_print.h"
20
21 #include "frontends/LyXView.h"
22
23 #include "PrinterParams.h"
24
25 #include "input_validators.h"
26 #include "xforms_helpers.h"
27
28 #include "support/lstrings.h"
29 #include FORMS_H_LOCATION
30
31 using std::make_pair;
32
33 typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
34
35 FormPrint::FormPrint(ControlPrint & c, Dialogs & d)
36         : base_class(c, d, _("Print")),
37           target_(2), order_(2), which_(3)
38 {}
39
40
41 void FormPrint::build()
42 {
43         dialog_.reset(build_print(this));
44
45         // Manage the ok, apply and cancel/close buttons
46         bc().setOK(dialog_->button_ok);
47         bc().setApply(dialog_->button_apply);
48         bc().setCancel(dialog_->button_close);
49
50         // allow controlling of input and ok/apply (de)activation
51         fl_set_input_return(dialog_->input_printer,
52                             FL_RETURN_CHANGED);
53         fl_set_input_return(dialog_->input_file,
54                             FL_RETURN_CHANGED);
55         fl_set_input_return(dialog_->input_from_page,
56                             FL_RETURN_CHANGED);
57         fl_set_input_return(dialog_->input_to_page,
58                             FL_RETURN_CHANGED);
59         fl_set_input_return(dialog_->input_count,
60                             FL_RETURN_CHANGED);
61
62         // limit these inputs to unsigned integers
63         fl_set_input_filter(dialog_->input_from_page,
64                             fl_unsigned_int_filter);
65         fl_set_input_filter(dialog_->input_to_page,
66                             fl_unsigned_int_filter);
67         fl_set_input_filter(dialog_->input_count,
68                             fl_unsigned_int_filter);
69
70         setPrehandler(dialog_->input_printer);
71         setPrehandler(dialog_->input_file);
72         setPrehandler(dialog_->input_from_page);
73         setPrehandler(dialog_->input_to_page);
74         setPrehandler(dialog_->input_count);
75
76         // what limits (if any) make sense for these?
77         fl_set_input_maxchars(dialog_->input_printer, 255);
78         fl_set_input_maxchars(dialog_->input_file, 255);
79         fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
80         fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
81         fl_set_input_maxchars(dialog_->input_count, 4);     // 9999
82
83         target_.reset();
84         target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
85         target_.init(dialog_->radio_file,    PrinterParams::FILE);
86         order_.reset();
87         order_.init(dialog_->radio_order_reverse, true);
88         order_.init(dialog_->radio_order_normal,  false);
89         which_.reset();
90         which_.init(dialog_->radio_odd_pages,  PrinterParams::ODD);
91         which_.init(dialog_->radio_even_pages, PrinterParams::EVEN);
92         which_.init(dialog_->radio_all_pages,  PrinterParams::ALL);
93 }
94
95
96 void FormPrint::apply()
97 {
98         PrinterParams::WhichPages
99                 wp(static_cast<PrinterParams::WhichPages>(which_.get()));
100
101         string from;
102         int to(0);
103         if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
104                 // we have at least one page requested
105                 from = fl_get_input(dialog_->input_from_page);
106                 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
107                         // okay we have a range
108                         to = strToInt(fl_get_input(dialog_->input_to_page));
109                 } // else we only print one page.
110         }
111
112         PrinterParams::Target
113                 t(static_cast<PrinterParams::Target>(target_.get()));
114
115         PrinterParams const pp(t,
116                                string(fl_get_input(dialog_->input_printer)),
117                                string(fl_get_input(dialog_->input_file)),
118                                wp, from, to,
119                                static_cast<bool>(order_.get()),
120                                !static_cast<bool>(fl_get_button(dialog_->check_collated)),
121                                strToInt(fl_get_input(dialog_->input_count)));
122
123         controller().params() = pp;
124 }
125
126
127 void FormPrint::update()
128 {
129         PrinterParams & pp = controller().params();
130
131         fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
132         fl_set_input(dialog_->input_file, pp.file_name.c_str());
133
134         target_.set(pp.target);
135         order_.set(pp.reverse_order);
136         which_.set(pp.which_pages);
137
138         // hmmm... maybe a bit weird but maybe not
139         // we might just be remembering the last
140         // time this was printed.
141         if (!pp.from_page.empty()) {
142                 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
143
144                 // we only set the "to" page of a range
145                 // if there's a corresponding "from"
146                 fl_activate_object(dialog_->input_to_page);
147                 if (pp.to_page) {
148                         fl_set_input(dialog_->input_to_page,
149                                      tostr(pp.to_page).c_str());
150                 } else {
151                         fl_set_input(dialog_->input_to_page,"");
152                 }
153
154         } else {
155                 fl_deactivate_object(dialog_->input_to_page);
156                 fl_set_input(dialog_->input_to_page,"");
157                 fl_set_input(dialog_->input_from_page,"");
158         }
159
160         fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
161 }
162
163
164 // It would be nice if we checked for cases like:
165 // Print only-odd-pages and from_page == an even number
166 //
167 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
168 {
169         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
170
171         // using a fl_input_filter that only permits numbers no '-' or '+'
172         // and the user cannot enter a negative number even if they try.
173         if (strlen(fl_get_input(dialog_->input_from_page))) {
174                 // using a page range so activate the "to" field
175                 fl_activate_object(dialog_->input_to_page);
176                 if (strlen(fl_get_input(dialog_->input_to_page))
177                     && (strToInt(fl_get_input(dialog_->input_from_page))
178                         > strToInt(fl_get_input(dialog_->input_to_page)))) {
179                         // both from and to have values but from > to
180                         // We could have code to silently swap these
181                         // values but I'll disable the ok/apply until
182                         // the user fixes it since they may be editting
183                         // one of the fields.
184                         activate = ButtonPolicy::SMI_INVALID;
185                         // set both backgrounds to red?
186                 }
187         } else if (strlen(fl_get_input(dialog_->input_to_page))) {
188                 // from is empty but to exists so probably editting from
189                 // therefore deactivate ok and apply until form is valid again
190                 activate = ButtonPolicy::SMI_INVALID;
191         } else {
192                 // both from and to are empty.  This is valid so activate
193                 // ok and apply but deactivate to
194                 fl_deactivate_object(dialog_->input_to_page);
195         }
196
197         if (fl_get_button(dialog_->radio_file)
198             && !strlen(fl_get_input(dialog_->input_file))) {
199                 activate = ButtonPolicy::SMI_INVALID;
200         }
201
202         if (ob == dialog_->button_browse) {
203                 // Get the filename from the dialog
204                 string const in_name = fl_get_input(dialog_->input_file);
205                 string const out_name = controller().Browse(in_name);
206
207                 // Save the filename to the dialog
208                 if (out_name != in_name && !out_name.empty()) {
209                         fl_set_input(dialog_->input_file, out_name.c_str());
210                         input(0, 0);
211                 }
212
213                 // select the file radio
214                 if (!out_name.empty()) {
215                         fl_set_button(dialog_->radio_file, 1);
216                         fl_set_button(dialog_->radio_printer, 0);
217                 }
218         }
219
220         // if we type into file, select that as a target
221         if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer)
222             && strlen(fl_get_input(dialog_->input_file))) {
223                 fl_set_button(dialog_->radio_file, 1);
224                 fl_set_button(dialog_->radio_printer, 0);
225         } else if (ob == dialog_->input_printer) {
226                 fl_set_button(dialog_->radio_file, 0);
227                 fl_set_button(dialog_->radio_printer, 1);
228         }
229
230         return activate;
231 }