]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlPrint.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlPrint.C
index 0845ec8648b00a951ca2fff0065d82db982a2b95..18718d24d9ee40f0dfaeca32e68f95960ea9d7e5 100644 (file)
-/* This file is part of
- * ======================================================
- *
- *           LyX, The Document Processor
- *
- *          Copyright 2001 The LyX Team.
+/**
+ * \file ControlPrint.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *======================================================
+ * \author Angus Leeming
  *
- * \file ControlPrint.C
- * \author Angus Leeming, a.leeming@.ac.uk
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "ControlPrint.h"
-#include "ViewBase.h"
-#include "ButtonControllerBase.h"
-#include "buffer.h"
-#include "PrinterParams.h"
-#include "Liason.h"
+
+#include "ButtonController.h"
 #include "helper_funcs.h"
-#include "frontends/Alert.h"
+
+#include "buffer.h"
+#include "bufferparams.h"
+#include "funcrequest.h"
 #include "gettext.h"
-#include "support/LAssert.h"
+#include "PrinterParams.h"
+
+#include "support/convert.h"
+#include "support/filefilterlist.h"
+#include "support/filetools.h"
+
+using std::string;
+
+namespace lyx {
 
-using Liason::printBuffer;
-using Liason::getPrinterParams;
+using support::ChangeExtension;
+using support::FileFilterList;
 
+namespace frontend {
 
-ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
-       : ControlDialogBD(lv, d),
+
+ControlPrint::ControlPrint(Dialog & parent)
+       : Dialog::Controller(parent),
          params_(0)
 {}
 
 
-void ControlPrint::apply()
+bool ControlPrint::initialiseParams(std::string const &)
 {
-       if (!bufferIsAvailable())
-               return;
+       /// get global printer parameters
+       string const name =  ChangeExtension(kernel().buffer().fileName(),
+                                       lyxrc.print_file_extension);
+       params_.reset(new PrinterParams (PrinterParams::PRINTER,
+                                        lyxrc.printer, name));
+
+       dialog().bc().valid(); // so that the user can press Ok
+       return true;
+}
 
-       view().apply();
 
-       if (!printBuffer(buffer(), params())) {
-               Alert::alert(_("Error:"),
-                          _("Unable to print"),
-                          _("Check that your parameters are correct"));
-       }
+void ControlPrint::clearParams()
+{
+       params_.reset();
 }
 
 
 PrinterParams & ControlPrint::params() const
 {
-       lyx::Assert(params_);
+       BOOST_ASSERT(params_.get());
        return *params_;
 }
 
 
-void ControlPrint::setParams()
+string const ControlPrint::browse(string const & in_name) const
 {
-       if (params_) delete params_;
-       params_ = new PrinterParams(getPrinterParams(buffer()));
-
-       bc().valid(); // so that the user can press Ok
+       return browseRelFile(in_name, kernel().buffer().filePath(),
+                            _("Print to file"),
+                            FileFilterList(_("PostScript files (*.ps)")),
+                            true);
 }
 
 
-void ControlPrint::clearParams()
+/// print the current buffer
+void ControlPrint::dispatchParams()
 {
-       if (params_) {
-               delete params_;
-               params_ = 0;
+       PrinterParams const pp = params();
+       string command(lyxrc.print_command + ' ');
+
+       if (pp.target == PrinterParams::PRINTER
+           && lyxrc.print_adapt_output  // dvips wants a printer name
+           && !pp.printer_name.empty()) {// printer name given
+               command += lyxrc.print_to_printer
+                       + pp.printer_name
+                       + ' ';
        }
-}
 
+       if (!pp.all_pages && pp.from_page) {
+               command += lyxrc.print_pagerange_flag + ' ';
+               command += convert<string>(pp.from_page);
+               if (pp.to_page) {
+                       // we have a range "from-to"
+                       command += '-'
+                               + convert<string>(pp.to_page);
+               }
+               command += ' ';
+       }
 
-string const ControlPrint::Browse(string const & in_name)
-{
-       string const title = _("Print to file");
-       string const pattern = "*.ps";
+       // If both are, or both are not selected, then skip the odd/even printing
+       if (pp.odd_pages != pp.even_pages) {
+               if (pp.odd_pages) {
+                       command += lyxrc.print_oddpage_flag + ' ';
+               } else if (pp.even_pages) {
+                       command += lyxrc.print_evenpage_flag + ' ';
+               }
+       }
+
+       if (pp.count_copies > 1) {
+               if (pp.sorted_copies) {
+                       command += lyxrc.print_collcopies_flag;
+               } else {
+                       command += lyxrc.print_copies_flag;
+               }
+               command += ' '
+                       + convert<string>(pp.count_copies)
+                       + ' ';
+       }
 
-       // Show the file browser dialog
-       return browseRelFile(&lv_, in_name, buffer()->filePath(),
-                            title, pattern);
+       if (pp.reverse_order) {
+               command += lyxrc.print_reverse_flag + ' ';
+       }
+
+       if (!lyxrc.print_extra_options.empty()) {
+               command += lyxrc.print_extra_options + ' ';
+       }
+
+       command += kernel().buffer().params().dvips_options() + ' ';
+
+       string const target = (pp.target == PrinterParams::PRINTER) ?
+               "printer" : "file";
+
+       string const target_name = (pp.target == PrinterParams::PRINTER) ?
+               (pp.printer_name.empty() ? "default" : pp.printer_name) :
+               pp.file_name;
+
+       string const data = target + " " + target_name + " " + command;
+       kernel().dispatch(FuncRequest(getLfun(), data));
 }
+
+} // namespace frontend
+} // namespace lyx