-/* FormPrint.C
- * FormPrint Interface Class Implementation
+/**
+ * \file FormPrint.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Allan Rae
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS.
*/
#include <config.h>
-#include "lyx_gui_misc.h"
-#include "gettext.h"
-#include FORMS_H_LOCATION
#include "FormPrint.h"
-#include "xform_macros.h"
+#include "ControlPrint.h"
+#include "forms/form_print.h"
+
#include "input_validators.h"
-#include "LyXView.h"
-#include "Dialogs.h"
-#include "support/lstrings.h"
-#include "lyxrc.h"
+#include "Tooltips.h"
+#include "xforms_helpers.h"
+#include "xformsBC.h"
+
#include "PrinterParams.h"
-#include "Liason.h"
-#include "debug.h"
-#include "BufferView.h"
+#include "support/lstrings.h"
+#include "support/tostr.h"
-#ifdef SIGC_CXX_NAMESPACES
-using SigC::slot;
-#endif
+#include "lyx_forms.h"
-#ifdef CXX_WORKING_NAMESPACES
-using Liason::printBuffer;
-using Liason::getPrinterParams;
-#endif
+using lyx::support::strToInt;
-C_RETURNCB(FormPrint, WMHideCB)
-C_GENERICCB(FormPrint, OKCB)
-C_GENERICCB(FormPrint, ApplyCB)
-C_GENERICCB(FormPrint, CancelCB)
-C_GENERICCB(FormPrint, InputCB)
+using std::string;
-FormPrint::FormPrint(LyXView * lv, Dialogs * d)
- : dialog_(0), lv_(lv), d_(d), u_(0), h_(0)
-{
- // let the dialog be shown
- // This is a permanent connection so we won't bother
- // storing a copy because we won't be disconnecting.
- d->showPrint.connect(slot(this,&FormPrint::show));
-}
+typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
-
-FormPrint::~FormPrint()
-{
- free();
-}
+FormPrint::FormPrint()
+ : base_class(_("Print Document"))
+{}
void FormPrint::build()
{
- dialog_ = build_print();
-}
-
-
-void FormPrint::show()
-{
- if (!dialog_) {
- build();
- // allow controlling of input and ok/apply (de)activation
- fl_set_input_return(dialog_->input_printer,
- FL_RETURN_CHANGED);
- fl_set_input_return(dialog_->input_file,
- FL_RETURN_CHANGED);
- fl_set_input_return(dialog_->input_from_page,
- FL_RETURN_CHANGED);
- fl_set_input_return(dialog_->input_to_page,
- FL_RETURN_CHANGED);
- fl_set_input_return(dialog_->input_count,
- FL_RETURN_CHANGED);
-
- // limit these inputs to unsigned integers
- fl_set_input_filter(dialog_->input_from_page,
- fl_unsigned_int_filter);
- fl_set_input_filter(dialog_->input_to_page,
- fl_unsigned_int_filter);
- fl_set_input_filter(dialog_->input_count,
- fl_unsigned_int_filter);
-
- // what limits (if any) make sense for these?
- fl_set_input_maxchars(dialog_->input_printer, 255);
- fl_set_input_maxchars(dialog_->input_file, 255);
- fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
- fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
- fl_set_input_maxchars(dialog_->input_count, 4); // 9999
-
- fl_set_form_atclose(dialog_->form_print,
- C_FormPrintWMHideCB, 0);
- }
-
- update(); // make sure its up-to-date
-
- if (dialog_->form_print->visible) {
- fl_raise_form(dialog_->form_print);
- } else {
- fl_show_form(dialog_->form_print,
- FL_PLACE_MOUSE | FL_FREE_SIZE,
- FL_FULLBORDER,
- _("Print"));
- u_ = d_->updateBufferDependent.connect(slot(this,
- &FormPrint::update));
- h_ = d_->hideBufferDependent.connect(slot(this,
- &FormPrint::hide));
- }
-}
-
-
-void FormPrint::hide()
-{
- if (dialog_
- && dialog_->form_print
- && dialog_->form_print->visible) {
- fl_hide_form(dialog_->form_print);
- u_.disconnect();
- h_.disconnect();
- }
+ dialog_.reset(build_print(this));
+
+ // Manage the ok, apply and cancel/close buttons
+ bcview().setOK(dialog_->button_ok);
+ bcview().setApply(dialog_->button_apply);
+ bcview().setCancel(dialog_->button_close);
+
+ // trigger an input event for cut&paste with middle mouse button.
+ setPrehandler(dialog_->input_printer);
+ setPrehandler(dialog_->input_file);
+ setPrehandler(dialog_->input_from_page);
+ setPrehandler(dialog_->input_to_page);
+
+ fl_set_input_return(dialog_->input_printer, FL_RETURN_CHANGED);
+ fl_set_input_return(dialog_->input_file, FL_RETURN_CHANGED);
+ fl_set_input_return(dialog_->input_from_page, FL_RETURN_CHANGED);
+ fl_set_input_return(dialog_->input_to_page, FL_RETURN_CHANGED);
+
+ // limit these inputs to unsigned integers
+ fl_set_input_filter(dialog_->input_from_page, fl_unsigned_int_filter);
+ fl_set_input_filter(dialog_->input_to_page, fl_unsigned_int_filter);
+
+ // what limits (if any) make sense for these?
+ fl_set_input_maxchars(dialog_->input_printer, 255);
+ fl_set_input_maxchars(dialog_->input_file, 255);
+ fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
+ fl_set_input_maxchars(dialog_->input_to_page, 4); // 9999
+
+ target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
+ target_.init(dialog_->radio_file, PrinterParams::FILE);
+
+ all_pages_.init(dialog_->radio_all_pages, true);
+ all_pages_.init(dialog_->radio_from_to, false);
+
+ // set up the tooltips for Destination
+ string str = _("Select for printer output.");
+ tooltips().init(dialog_->radio_printer, str);
+ str = _("Enter printer command.");
+ tooltips().init(dialog_->input_printer, str);
+ str = _("Select for file output.");
+ tooltips().init(dialog_->radio_file, str);
+ str = _("Enter file name as print destination.");
+ tooltips().init(dialog_->input_file, str);
+ str = _("Browse directories for file name.");
+ tooltips().init(dialog_->button_browse, str);
+
+ // set up the tooltips for Range
+ str = _("Select for printing all pages.");
+ tooltips().init(dialog_->radio_all_pages, str);
+ str = _("Select for printing a specific page range.");
+ tooltips().init(dialog_->radio_from_to, str);
+ str = _("First page.");
+ tooltips().init(dialog_->input_from_page, str);
+ str = _("Last page.");
+ tooltips().init(dialog_->input_to_page, str);
+ str = _("Print the odd numbered pages.");
+ tooltips().init(dialog_->check_odd_pages, str);
+ str = _("Print the even numbered pages.");
+ tooltips().init(dialog_->check_even_pages, str);
+
+ // set up the tooltips for Copies
+ str = _("Number of copies to be printed.");
+ tooltips().init(dialog_->counter_copies, str);
+ str = _("Sort the copies.");
+ tooltips().init(dialog_->check_sorted_copies, str);
+
+ str = _("Reverse the order of the printed pages.");
+ tooltips().init(dialog_->check_reverse_order, str);
}
void FormPrint::apply()
{
- if (!lv_->view()->available()) {
- return;
- }
+ PrinterParams pp;
- PrinterParams::WhichPages wp(PrinterParams::ALL);
- if (fl_get_button(dialog_->radio_even_pages)) {
- wp = PrinterParams::EVEN;
- } else if (fl_get_button(dialog_->radio_odd_pages)) {
- wp = PrinterParams::ODD;
- }
+ pp.target = static_cast<PrinterParams::Target>(target_.get());
+ pp.printer_name = getString(dialog_->input_printer);
+ pp.file_name = getString(dialog_->input_file);
+
+ pp.all_pages = static_cast<bool>(all_pages_.get());
- string from;
- int to(0);
- if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
+ pp.from_page = pp.to_page = 0;
+ if (!getString(dialog_->input_from_page).empty()) {
// we have at least one page requested
- from = fl_get_input(dialog_->input_from_page);
- if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
+ pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
+ if (!getString(dialog_->input_to_page).empty()) {
// okay we have a range
- to = strToInt(fl_get_input(dialog_->input_to_page));
+ pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
} // else we only print one page.
}
- PrinterParams::Target t(PrinterParams::PRINTER);
- if (fl_get_button(dialog_->radio_file)) {
- t = PrinterParams::FILE;
- }
+ pp.odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
+ pp.even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
- // we really should use the return value here I think.
- if (!printBuffer(lv_->buffer(),
- PrinterParams(t,
- string(fl_get_input(dialog_->input_printer)),
- string(fl_get_input(dialog_->input_file)),
- wp, from, to,
- static_cast<bool>(fl_get_button(dialog_->
- radio_order_reverse)),
- static_cast<bool>(fl_get_button(dialog_->
- radio_unsorted)),
- strToInt(fl_get_input(dialog_->input_count))))) {
- WriteAlert(_("Error:"),
- _("Unable to print"),
- _("Check that your parameters are correct"));
- }
+ pp.count_copies = static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies));
+ pp.sorted_copies = static_cast<bool>(fl_get_button(dialog_->check_sorted_copies));
+
+ pp.reverse_order = static_cast<bool>(fl_get_button(dialog_->check_reverse_order));
+
+ controller().params() = pp;
}
void FormPrint::update()
{
- if (dialog_
- && lv_->view()->available()) {
- PrinterParams pp(getPrinterParams(lv_->buffer()));
-
- fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
- fl_set_input(dialog_->input_file, pp.file_name.c_str());
-
- switch (pp.target) {
- case PrinterParams::FILE:
- fl_set_button(dialog_->radio_printer, 0);
- fl_set_button(dialog_->radio_file, 1);
- break;
-
- case PrinterParams::PRINTER:
- default:
- fl_set_button(dialog_->radio_printer, 1);
- fl_set_button(dialog_->radio_file, 0);
- break;
- }
+ PrinterParams & pp = controller().params();
- switch (pp.reverse_order) {
- case true:
- fl_set_button(dialog_->radio_order_normal, 0);
- fl_set_button(dialog_->radio_order_reverse, 1);
- break;
-
- case false:
- default:
- fl_set_button(dialog_->radio_order_normal, 1);
- fl_set_button(dialog_->radio_order_reverse, 0);
- break;
- }
-// should be able to remove the various set_button 0 and rely on radio button
-// action. Provided xforms is smart enough :D
- fl_set_button(dialog_->radio_all_pages, 0);
- fl_set_button(dialog_->radio_odd_pages, 0);
- fl_set_button(dialog_->radio_even_pages, 0);
- switch (pp.which_pages) {
- case PrinterParams::ODD:
- fl_set_button(dialog_->radio_odd_pages, 1);
- break;
-
- case PrinterParams::EVEN:
- fl_set_button(dialog_->radio_even_pages, 1);
- break;
-
- case PrinterParams::ALL:
- default:
- fl_set_button(dialog_->radio_all_pages, 1);
- break;
- }
+ target_.set(pp.target);
+ fl_set_input(dialog_->input_printer, pp.printer_name.c_str());
+ fl_set_input(dialog_->input_file, pp.file_name.c_str());
- // hmmm... maybe a bit weird but maybe not
- // we might just be remembering the last
- // time this was printed.
- if (!pp.from_page.empty()) {
- fl_set_input(dialog_->input_from_page,
- pp.from_page.c_str());
- // we only set the "to" page of a range
- // if there's a corresponding "from"
- fl_activate_object(dialog_->input_to_page);
- if (pp.to_page) {
- fl_set_input(dialog_->input_to_page,
- tostr(pp.to_page).c_str());
- } else {
- fl_set_input(dialog_->input_to_page,"");
- }
- } else {
- fl_deactivate_object(dialog_->input_to_page);
- fl_set_input(dialog_->input_to_page,"");
- fl_set_input(dialog_->input_from_page,"");
- }
+ // hmmm... maybe a bit weird but maybe not
+ // we might just be remembering the last time this was printed.
+ all_pages_.set(pp.all_pages);
- fl_set_input(dialog_->input_count,
- tostr(pp.count_copies).c_str());
-
- // Even readonly docs can be printed
- // these 4 activations are probably superfluous but I'm
- // being explicit for a reason.
- // They can probably be removed soon along with a few more
- // of the de/activations above once input() is a bit smarter.
- fl_activate_object(dialog_->input_count);
- fl_activate_object(dialog_->input_file);
- fl_activate_object(dialog_->input_from_page);
- fl_activate_object(dialog_->input_printer);
- // and we should always be in a working state upon exit
- input();
- }
-}
+ string const from = ( pp.from_page ? tostr(pp.from_page) : string() );
+ string const to = ( pp.to_page ? tostr(pp.to_page) : string() );
+ fl_set_input(dialog_->input_from_page, from.c_str());
+ fl_set_input(dialog_->input_to_page, to.c_str());
+ fl_set_button(dialog_->check_odd_pages, pp.odd_pages);
+ fl_set_button(dialog_->check_even_pages, pp.even_pages);
+ fl_set_button(dialog_->check_reverse_order, pp.reverse_order);
+ fl_set_button(dialog_->check_sorted_copies, pp.sorted_copies);
-// It would be nice if we checked for cases like:
-// Print only-odd-pages and from_page == an even number
-//
-void FormPrint::input()
-{
- bool activate = true;
-
- // using a fl_input_filter that only permits numbers no '-' or '+'
- // and the user cannot enter a negative number even if they try.
- if (strlen(fl_get_input(dialog_->input_from_page))) {
- // using a page range so activate the "to" field
- fl_activate_object(dialog_->input_to_page);
- if (strlen(fl_get_input(dialog_->input_to_page))
- && (strToInt(fl_get_input(dialog_->input_from_page))
- > strToInt(fl_get_input(dialog_->input_to_page)))) {
- // both from and to have values but from > to
- // We could have code to silently swap these
- // values but I'll disable the ok/apply until
- // the user fixes it since they may be editting
- // one of the fields.
- activate = false;
- // set both backgrounds to red?
- }
- } else if (strlen(fl_get_input(dialog_->input_to_page))) {
- // from is empty but to exists so probably editting from
- // therefore deactivate ok and apply until form is valid again
- activate = false;
- } else {
- // both from and to are empty. This is valid so activate
- // ok and apply but deactivate to
- fl_deactivate_object(dialog_->input_to_page);
- }
+ fl_set_counter_value(dialog_->counter_copies, pp.count_copies);
- if (fl_get_button(dialog_->radio_file)
- && !strlen(fl_get_input(dialog_->input_file))) {
- activate = false;
- }
+ // number of copies only used when output goes to printer
+ bool const enable_counter = pp.target == PrinterParams::PRINTER;
+ setEnabled(dialog_->counter_copies, enable_counter);
- // it is probably legal to have no printer name since the system will
- // have a default printer set. Or should have.
-// if (fl_get_button(dialog_->radio_printer)
-// && !strlen(fl_get_input(dialog_->input_printer))) {
-// activate = false;
-// }
-
- if (activate) {
- fl_activate_object(dialog_->button_ok);
- fl_activate_object(dialog_->button_apply);
- fl_set_object_lcol(dialog_->button_ok, FL_BLACK);
- fl_set_object_lcol(dialog_->button_apply, FL_BLACK);
- } else {
- fl_deactivate_object(dialog_->button_ok);
- fl_deactivate_object(dialog_->button_apply);
- fl_set_object_lcol(dialog_->button_ok, FL_INACTIVE);
- fl_set_object_lcol(dialog_->button_apply, FL_INACTIVE);
- }
+ // sorting only used when printing more than one copy
+ setEnabled(dialog_->check_sorted_copies, enable_counter && pp.count_copies > 1);
+
+ // reset input fields to valid input
+ input(0, 0);
}
-void FormPrint::free()
+ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
{
- // we don't need to delete u and h here because
- // hide() does that after disconnecting.
- if (dialog_) {
- if (dialog_->form_print
- && dialog_->form_print->visible) {
- hide();
+ if (ob == dialog_->button_browse) {
+ // Get the filename from the dialog
+ string const in_name = getString(dialog_->input_file);
+ string const out_name = controller().Browse(in_name);
+
+ // Save the filename to the dialog
+ if (out_name != in_name && !out_name.empty()) {
+ fl_set_input(dialog_->input_file, out_name.c_str());
}
- fl_free_form(dialog_->form_print);
- delete dialog_;
- dialog_ = 0;
- }
-}
+ // select the file radio
+ if (!out_name.empty()) {
+ target_.set(dialog_->radio_file);
+ }
-int FormPrint::WMHideCB(FL_FORM * form, void *)
-{
- // Ensure that the signals (u and h) are disconnected even if the
- // window manager is used to close the dialog.
- FormPrint * pre = static_cast<FormPrint*>(form->u_vdata);
- pre->hide();
- return FL_CANCEL;
-}
+ // if we type input string for file or printer, select that as a target
+ } else if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
+ target_.set(dialog_->radio_file);
+ } else if (ob == dialog_->input_printer && !fl_get_button(dialog_->radio_printer)) {
+ target_.set(dialog_->radio_printer);
-void FormPrint::OKCB(FL_OBJECT * ob, long)
-{
- FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
- pre->apply();
- pre->hide();
-}
+ // if we type into 'from/to' fields, then select 'from/to' radio button
+ } else if ((ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
+ !fl_get_button(dialog_->radio_from_to)) {
+ all_pages_.set(dialog_->radio_from_to);
+ }
+ ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
-void FormPrint::ApplyCB(FL_OBJECT * ob, long)
-{
- FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
- pre->apply();
-}
+ // disable OK/Apply buttons when file output is selected, but no file name entered
+ if (fl_get_button(dialog_->radio_file) && getString(dialog_->input_file).empty()) {
+ activate = ButtonPolicy::SMI_INVALID;
+ }
+ // check 'from' and 'to' fields only when 'from/to' radio button is selected
+ if (fl_get_button(dialog_->radio_from_to)) {
+ char const * from = fl_get_input(dialog_->input_from_page);
+ char const * to = fl_get_input(dialog_->input_to_page);
+ bool const from_input = static_cast<bool>(*from);
+ bool const to_input = static_cast<bool>(*to);
+
+ setEnabled(dialog_->input_to_page, from_input);
+ if (!from_input || (to_input && strToInt(from) > strToInt(to))) {
+ // Invalid input. Either 'from' is empty, or 'from' > 'to'.
+ // Probably editting these fields, so deactivate OK/Apply until input is valid again.
+ activate = ButtonPolicy::SMI_INVALID;
+ } else if (!to_input || strToInt(from) == strToInt(to)) {
+ // Valid input. Either there's only 'from' input, or 'from' == 'to'.
+ // Deactivate OK/Apply if odd/even selection implies no pages.
+ bool const odd_pages = static_cast<bool>(fl_get_button(dialog_->check_odd_pages));
+ bool const even_pages = static_cast<bool>(fl_get_button(dialog_->check_even_pages));
+ bool const odd_only = odd_pages && !even_pages;
+ bool const even_only = even_pages && !odd_pages;
+ bool const from_is_odd = static_cast<bool>(strToInt(from) % 2);
+ if ( (from_is_odd && even_only) || (!from_is_odd && odd_only) ) {
+ activate = ButtonPolicy::SMI_INVALID;
+ }
+ }
+ }
-void FormPrint::CancelCB(FL_OBJECT * ob, long)
-{
- FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
- pre->hide();
-}
+ // number of copies only used when output goes to printer
+ bool const enable_counter = static_cast<bool>(fl_get_button(dialog_->radio_printer));
+ setEnabled(dialog_->counter_copies, enable_counter);
+ // sorting only used when printing more than one copy
+ bool const enable_sorted = enable_counter && fl_get_counter_value(dialog_->counter_copies) > 1;
+ setEnabled(dialog_->check_sorted_copies, enable_sorted);
-void FormPrint::InputCB(FL_OBJECT * ob, long)
-{
- FormPrint * pre = static_cast<FormPrint*>(ob->form->u_vdata);
- pre->input();
+ return activate;
}