]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/xforms/FormPrint.C
Introduce LFUN_PRINT.
[lyx.git] / src / frontends / xforms / FormPrint.C
index 8d095390092a33312b5a1606d5249c5bb0ceea3e..a3ea1756ea58d693198c918c3b353e46a50331ab 100644 (file)
@@ -1,41 +1,41 @@
 /**
- * \file xforms/FormPrint.C
+ * \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 
+ * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
-#include "xformsBC.h"
-#include "ControlPrint.h"
 #include "FormPrint.h"
+#include "ControlPrint.h"
 #include "forms/form_print.h"
-#include "Tooltips.h"
-
-#include "PrinterParams.h"
 
 #include "input_validators.h"
+#include "Tooltips.h"
 #include "xforms_helpers.h"
+#include "xformsBC.h"
+
+#include "PrinterParams.h"
 
 #include "support/lstrings.h"
-#include FORMS_H_LOCATION
+#include "support/tostr.h"
+
+#include "lyx_forms.h"
+
+using lyx::support::strToInt;
+
+using std::string;
 
-using std::make_pair;
 
-typedef FormCB<ControlPrint, FormDB<FD_print> > base_class;
+typedef FormController<ControlPrint, FormView<FD_print> > base_class;
 
-FormPrint::FormPrint()
-       : base_class(_("Print")),
-         target_(2), which_pages_(2)
+FormPrint::FormPrint(Dialog & parent)
+       : base_class(parent, _("Print Document"))
 {}
 
 
@@ -44,11 +44,16 @@ void FormPrint::build()
        dialog_.reset(build_print(this));
 
        // Manage the ok, apply and cancel/close buttons
-       bc().setOK(dialog_->button_ok);
-       bc().setApply(dialog_->button_apply);
-       bc().setCancel(dialog_->button_close);
+       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);
 
-       // 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);
@@ -64,19 +69,12 @@ void FormPrint::build()
        fl_set_input_maxchars(dialog_->input_from_page, 4); // 9999
        fl_set_input_maxchars(dialog_->input_to_page, 4);   // 9999
 
-       bc().addReadOnly(dialog_->button_browse);   
-       bc().addReadOnly(dialog_->check_odd_pages);   
-       bc().addReadOnly(dialog_->check_even_pages);   
-       bc().addReadOnly(dialog_->check_sorted_copies);
-       bc().addReadOnly(dialog_->check_reverse_order);
-
-       target_.reset();
        target_.init(dialog_->radio_printer, PrinterParams::PRINTER);
        target_.init(dialog_->radio_file,    PrinterParams::FILE);
-       which_pages_.reset();
-       which_pages_.init(dialog_->radio_all_pages, true);
-       which_pages_.init(dialog_->radio_from_to,   false);
-       
+
+       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);
@@ -119,16 +117,16 @@ void FormPrint::apply()
        PrinterParams pp;
 
        pp.target = static_cast<PrinterParams::Target>(target_.get());
-       pp.printer_name = fl_get_input(dialog_->input_printer);
-       pp.file_name = fl_get_input(dialog_->input_file);
+       pp.printer_name = getString(dialog_->input_printer);
+       pp.file_name = getString(dialog_->input_file);
 
-       pp.all_pages = which_pages_.get();
-       pp.from_page = 0;
-       pp.to_page = 0;
-       if (strlen(fl_get_input(dialog_->input_from_page)) > 0) {
+       pp.all_pages = static_cast<bool>(all_pages_.get());
+
+       pp.from_page = pp.to_page = 0;
+       if (!getString(dialog_->input_from_page).empty()) {
                // we have at least one page requested
                pp.from_page = strToInt(fl_get_input(dialog_->input_from_page));
-               if (strlen(fl_get_input(dialog_->input_to_page)) > 0) {
+               if (!getString(dialog_->input_to_page).empty()) {
                        // okay we have a range
                        pp.to_page = strToInt(fl_get_input(dialog_->input_to_page));
                } // else we only print one page.
@@ -156,10 +154,10 @@ void FormPrint::update()
 
        // hmmm... maybe a bit weird but maybe not
        // we might just be remembering the last time this was printed.
-       which_pages_.set(pp.all_pages);
-       
-       string const from = ( pp.from_page ? tostr(pp.from_page) : "");
-       string const to   = ( pp.to_page   ? tostr(pp.to_page)   : "");
+       all_pages_.set(pp.all_pages);
+
+       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());
 
@@ -176,87 +174,82 @@ void FormPrint::update()
 
        // 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);
 }
 
 
-// It would be nice if we checked for cases like:
-// Print only-odd-pages and from_page == an even number
-//
 ButtonPolicy::SMInput FormPrint::input(FL_OBJECT * ob, long)
 {
-       ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
-
-       // 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 = ButtonPolicy::SMI_INVALID;
-                       // 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 = ButtonPolicy::SMI_INVALID;
-       } 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);
-       }
-
-       // 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
-                       && static_cast<unsigned int>(fl_get_counter_value(dialog_->counter_copies)) > 1;
-       setEnabled(dialog_->check_sorted_copies, enable_sorted);
-
-       // disable OK/Apply buttons when file output is selected, but no file name entered.
-       if (fl_get_button(dialog_->radio_file) && !strlen(fl_get_input(dialog_->input_file))) {
-                       activate = ButtonPolicy::SMI_INVALID;
-       }
-
        if (ob == dialog_->button_browse) {
                // Get the filename from the dialog
-               string const in_name = fl_get_input(dialog_->input_file);
-               string const out_name = controller().Browse(in_name);
+               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());
-                       input(0, 0);
                }
 
                // select the file radio
                if (!out_name.empty()) {
-                       fl_set_button(dialog_->radio_file, 1);
-                       fl_set_button(dialog_->radio_printer, 0);
+                       target_.set(dialog_->radio_file);
                }
-       }
 
        // if we type input string for file or printer, select that as a target
-       if (ob == dialog_->input_file && !fl_get_button(dialog_->radio_file)) {
-               fl_set_button(dialog_->radio_printer, 0);
-               fl_set_button(dialog_->radio_file, 1);
+       } 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)) {
-               fl_set_button(dialog_->radio_printer, 1);
-               fl_set_button(dialog_->radio_file, 0);
-       // if we type intput string for from/to, select from/to radio button
-       } else if ( (ob == dialog_->input_from_page || ob == dialog_->input_to_page) &&
+               target_.set(dialog_->radio_printer);
+
+       // 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)) {
-               fl_set_button(dialog_->radio_from_to, 1);
-               fl_set_button(dialog_->radio_all_pages, 0);
+               all_pages_.set(dialog_->radio_from_to);
+       }
+
+       ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
+
+       // 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;
+                       }
+               }
        }
 
+       // 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);
+
        return activate;
 }