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);
59 // allow controlling of input and ok/apply (de)activation
60 fl_set_input_return(dialog_->input_printer,
62 fl_set_input_return(dialog_->input_file,
64 fl_set_input_return(dialog_->input_from_page,
66 fl_set_input_return(dialog_->input_to_page,
68 fl_set_input_return(dialog_->input_count,
71 // limit these inputs to unsigned integers
72 fl_set_input_filter(dialog_->input_from_page,
73 fl_unsigned_int_filter);
74 fl_set_input_filter(dialog_->input_to_page,
75 fl_unsigned_int_filter);
76 fl_set_input_filter(dialog_->input_count,
77 fl_unsigned_int_filter);
79 // what limits (if any) make sense for these?
80 fl_set_input_maxchars(dialog_->input_printer, 255);
81 fl_set_input_maxchars(dialog_->input_file, 255);
82 fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
83 fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
84 fl_set_input_maxchars(dialog_->input_count, 4); // 9999
87 target_.registerRadioButton(dialog_->radio_printer,
88 PrinterParams::PRINTER);
89 target_.registerRadioButton(dialog_->radio_file,
92 order_.registerRadioButton(dialog_->radio_order_reverse,
94 order_.registerRadioButton(dialog_->radio_order_normal,
97 which_.registerRadioButton(dialog_->radio_odd_pages,
99 which_.registerRadioButton(dialog_->radio_even_pages,
100 PrinterParams::EVEN);
101 which_.registerRadioButton(dialog_->radio_all_pages,
106 void FormPrint::apply()
108 PrinterParams::WhichPages
109 wp(static_cast<PrinterParams::WhichPages>(which_.getButton()));
113 if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
114 // we have at least one page requested
115 from = fl_get_input(dialog_->input_from_page);
116 if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
117 // okay we have a range
118 to = strToInt(fl_get_input(dialog_->input_to_page));
119 } // else we only print one page.
122 PrinterParams::Target
123 t(static_cast<PrinterParams::Target>(target_.getButton()));
125 PrinterParams const pp(t,
126 string(fl_get_input(dialog_->input_printer)),
127 string(fl_get_input(dialog_->input_file)),
129 static_cast<bool>(order_.getButton()),
130 !static_cast<bool>(fl_get_button(dialog_->radio_collated)),
131 strToInt(fl_get_input(dialog_->input_count)));
133 controller().params() = pp;
137 void FormPrint::update()
139 PrinterParams & pp = controller().params();
141 fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
142 fl_set_input(dialog_->input_file, pp.file_name.c_str());
144 target_.setButton(pp.target);
145 order_.setButton(pp.reverse_order);
146 which_.setButton(pp.which_pages);
148 // hmmm... maybe a bit weird but maybe not
149 // we might just be remembering the last
150 // time this was printed.
151 if (!pp.from_page.empty()) {
152 fl_set_input(dialog_->input_from_page, pp.from_page.c_str());
154 // we only set the "to" page of a range
155 // if there's a corresponding "from"
156 fl_activate_object(dialog_->input_to_page);
158 fl_set_input(dialog_->input_to_page,
159 tostr(pp.to_page).c_str());
161 fl_set_input(dialog_->input_to_page,"");
165 fl_deactivate_object(dialog_->input_to_page);
166 fl_set_input(dialog_->input_to_page,"");
167 fl_set_input(dialog_->input_from_page,"");
170 fl_set_input(dialog_->input_count, tostr(pp.count_copies).c_str());
174 // It would be nice if we checked for cases like:
175 // Print only-odd-pages and from_page == an even number
177 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
179 ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
181 // using a fl_input_filter that only permits numbers no '-' or '+'
182 // and the user cannot enter a negative number even if they try.
183 if (strlen(fl_get_input(dialog_->input_from_page))) {
184 // using a page range so activate the "to" field
185 fl_activate_object(dialog_->input_to_page);
186 if (strlen(fl_get_input(dialog_->input_to_page))
187 && (strToInt(fl_get_input(dialog_->input_from_page))
188 > strToInt(fl_get_input(dialog_->input_to_page)))) {
189 // both from and to have values but from > to
190 // We could have code to silently swap these
191 // values but I'll disable the ok/apply until
192 // the user fixes it since they may be editting
193 // one of the fields.
194 activate = ButtonPolicy::SMI_INVALID;
195 // set both backgrounds to red?
197 } else if (strlen(fl_get_input(dialog_->input_to_page))) {
198 // from is empty but to exists so probably editting from
199 // therefore deactivate ok and apply until form is valid again
200 activate = ButtonPolicy::SMI_INVALID;
202 // both from and to are empty. This is valid so activate
203 // ok and apply but deactivate to
204 fl_deactivate_object(dialog_->input_to_page);
207 if (fl_get_button(dialog_->radio_file)
208 && !strlen(fl_get_input(dialog_->input_file))) {
209 activate = ButtonPolicy::SMI_INVALID;
212 if (ob == dialog_->button_browse) {
213 // Get the filename from the dialog
214 string const in_name = fl_get_input(dialog_->input_file);
215 string const out_name = controller().Browse(in_name);
217 // Save the filename to the dialog
218 if (out_name != in_name && !out_name.empty()) {
219 fl_set_input(dialog_->input_file, out_name.c_str());
224 // it is probably legal to have no printer name since the system will
225 // have a default printer set. Or should have.
226 // if (fl_get_button(dialog_->radio_printer)
227 // && !strlen(fl_get_input(dialog_->input_printer))) {
228 // activate = ButtonPolicy::SMI_INVALID;