3 * Copyright 2000-2001 The LyX Team.
4 * See the file COPYING.
6 * \author Allan Rae, rae@lyx.org
7 * \author Angus Leeming, a.leeming@.ac.uk
13 #pragma implementation
17 #include "ControlPrint.h"
18 #include "FormPrint.h"
19 #include "form_print.h"
20 #include "input_validators.h"
21 #include "support/lstrings.h"
23 #include "lyxrc.h" // needed by PrinterParams
24 #include "PrinterParams.h"
27 #include "xforms_helpers.h" // for browseFile
34 #include "BufferView.h"
37 //using Liason::printBuffer;
38 //using Liason::getPrinterParams;
41 typedef FormCB<ControlPrint, FormDB<FD_form_print> > base_class;
43 FormPrint::FormPrint(ControlPrint & c)
44 : base_class(c, _("Print")),
45 target_(2), order_(2), which_(3)
49 void FormPrint::build()
51 dialog_.reset(build_print());
53 // Manage the ok, apply and cancel/close buttons
54 bc().setOK(dialog_->button_ok);
55 bc().setApply(dialog_->button_apply);
56 bc().setCancel(dialog_->button_cancel);
58 // allow controlling of input and ok/apply (de)activation
59 fl_set_input_return(dialog_->input_printer,
61 fl_set_input_return(dialog_->input_file,
63 fl_set_input_return(dialog_->input_from_page,
65 fl_set_input_return(dialog_->input_to_page,
67 fl_set_input_return(dialog_->input_count,
70 // limit these inputs to unsigned integers
71 fl_set_input_filter(dialog_->input_from_page,
72 fl_unsigned_int_filter);
73 fl_set_input_filter(dialog_->input_to_page,
74 fl_unsigned_int_filter);
75 fl_set_input_filter(dialog_->input_count,
76 fl_unsigned_int_filter);
78 // what limits (if any) make sense for these?
79 fl_set_input_maxchars(dialog_->input_printer, 255);
80 fl_set_input_maxchars(dialog_->input_file, 255);
81 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
82 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
83 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
86 target_.registerRadioButton(dialog_->radio_printer,
87 PrinterParams::PRINTER);
88 target_.registerRadioButton(dialog_->radio_file,
91 order_.registerRadioButton(dialog_->radio_order_reverse,
93 order_.registerRadioButton(dialog_->radio_order_normal,
96 which_.registerRadioButton(dialog_->radio_odd_pages,
98 which_.registerRadioButton(dialog_->radio_even_pages,
100 which_.registerRadioButton(dialog_->radio_all_pages,
105 void FormPrint::apply()
107 PrinterParams::WhichPages
108 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
112 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
113 // we have at least one page requested
114 from = fl_get_input(dialog_->input_from_page);
115 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
116 // okay we have a range
117 to = strToInt(fl_get_input(dialog_->input_to_page));
118 } // else we only print one page.
121 PrinterParams::Target
122 t(static_cast<PrinterParams::Target>(target_.getButton()));
124 PrinterParams const pp(t,
125 string(fl_get_input(dialog_->input_printer)),
126 string(fl_get_input(dialog_->input_file)),
128 static_cast<bool>(order_.getButton()),
129 !static_cast<bool>(fl_get_button(dialog_->radio_collated)),
130 strToInt(fl_get_input(dialog_->input_count)));
132 controller().params() = pp;
136 void FormPrint::update()
138 PrinterParams & pp = controller().params();
140 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
141 fl_set_input(dialog_->input_file, pp.file_name.c_str());
143 target_.setButton(pp.target);
144 order_.setButton(pp.reverse_order);
145 which_.setButton(pp.which_pages);
147 // hmmm... maybe a bit weird but maybe not
148 // we might just be remembering the last
149 // time this was printed.
150 if (!pp.from_page.empty()) {
151 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
153 // we only set the "to" page of a range
154 // if there's a corresponding "from"
155 fl_activate_object(dialog_->input_to_page);
157 fl_set_input(dialog_->input_to_page,
158 tostr(pp.to_page).c_str());
160 fl_set_input(dialog_->input_to_page,"");
164 fl_deactivate_object(dialog_->input_to_page);
165 fl_set_input(dialog_->input_to_page,"");
166 fl_set_input(dialog_->input_from_page,"");
169 fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
173 // It would be nice if we checked for cases like:
174 // Print only-odd-pages and from_page == an even number
176 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
178 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
180 // using a fl_input_filter that only permits numbers no '-' or '+'
181 // and the user cannot enter a negative number even if they try.
182 if (strlen(fl_get_input(dialog_->input_from_page))) {
183 // using a page range so activate the "to" field
184 fl_activate_object(dialog_->input_to_page);
185 if (strlen(fl_get_input(dialog_->input_to_page))
186 && (strToInt(fl_get_input(dialog_->input_from_page))
187 > strToInt(fl_get_input(dialog_->input_to_page)))) {
188 // both from and to have values but from > to
189 // We could have code to silently swap these
190 // values but I'll disable the ok/apply until
191 // the user fixes it since they may be editting
192 // one of the fields.
193 activate = ButtonPolicy::SMI_INVALID;
194 // set both backgrounds to red?
196 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
197 // from is empty but to exists so probably editting from
198 // therefore deactivate ok and apply until form is valid again
199 activate = ButtonPolicy::SMI_INVALID;
201 // both from and to are empty. This is valid so activate
202 // ok and apply but deactivate to
203 fl_deactivate_object(dialog_->input_to_page);
206 if (fl_get_button(dialog_->radio_file)
207 && !strlen(fl_get_input(dialog_->input_file))) {
208 activate = ButtonPolicy::SMI_INVALID;
211 if (ob == dialog_->button_browse) {
212 // Get the filename from the dialog
213 string const in_name = fl_get_input(dialog_->input_file);
214 string const out_name = controller().Browse(in_name);
216 // Save the filename to the dialog
217 if (out_name != in_name && !out_name.empty()) {
218 fl_set_input(dialog_->input_file, out_name.c_str());
222 // select the file radio
223 if (!out_name.empty()) {
224 fl_set_button(dialog_->radio_file, 1);
225 fl_set_button(dialog_->radio_printer, 0);
229 // if we type into file, select that as a target
230 if (ob == dialog_->input_file && fl_get_button(dialog_->radio_printer)
231 && strlen(fl_get_input(dialog_->input_file))) {
232 fl_set_button(dialog_->radio_file, 1);
233 fl_set_button(dialog_->radio_printer, 0);
234 } else if (ob == dialog_->input_printer) {
235 fl_set_button(dialog_->radio_file, 0);
236 fl_set_button(dialog_->radio_printer, 1);