]> 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 a329ff71dbe888e74a3ec323e1275280ef3d7e4b..18718d24d9ee40f0dfaeca32e68f95960ea9d7e5 100644 (file)
@@ -5,93 +5,80 @@
  *
  * \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 "ControlPrint.h"
 
-#include "ViewBase.h"
-#include "ButtonControllerBase.h"
+#include "ButtonController.h"
+#include "helper_funcs.h"
 
 #include "buffer.h"
+#include "bufferparams.h"
+#include "funcrequest.h"
 #include "gettext.h"
-#include "helper_funcs.h"
 #include "PrinterParams.h"
-#include "exporter.h"
-#include "converter.h"
-
-#include "frontends/Alert.h"
 
-#include "support/LAssert.h"
+#include "support/convert.h"
+#include "support/filefilterlist.h"
 #include "support/filetools.h"
-#include "support/path.h"
-#include "support/systemcall.h"
 
-#include "debug.h" // for lyxerr
+using std::string;
 
-using std::endl;
+namespace lyx {
 
-ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
-       : ControlDialogBD(lv, d),
-         params_(0)
-{}
+using support::ChangeExtension;
+using support::FileFilterList;
 
+namespace frontend {
 
-PrinterParams & ControlPrint::params() const
-{
-       lyx::Assert(params_);
-       return *params_;
-}
 
+ControlPrint::ControlPrint(Dialog & parent)
+       : Dialog::Controller(parent),
+         params_(0)
+{}
 
-void ControlPrint::setParams()
-{
-       if (params_) delete params_;
 
+bool ControlPrint::initialiseParams(std::string const &)
+{
        /// get global printer parameters
-       string const name =  ChangeExtension(buffer()->fileName(),
+       string const name =  ChangeExtension(kernel().buffer().fileName(),
                                        lyxrc.print_file_extension);
-       params_ = new PrinterParams (PrinterParams::PRINTER,
-                                       lyxrc.printer, name);
+       params_.reset(new PrinterParams (PrinterParams::PRINTER,
+                                        lyxrc.printer, name));
 
-       bc().valid(); // so that the user can press Ok
+       dialog().bc().valid(); // so that the user can press Ok
+       return true;
 }
 
 
 void ControlPrint::clearParams()
 {
-       if (params_) {
-               delete params_;
-               params_ = 0;
-       }
+       params_.reset();
 }
 
 
-string const ControlPrint::Browse(string const & in_name)
+PrinterParams & ControlPrint::params() const
 {
-       string const title = _("Print to file");
-       string const pattern = "*.ps";
-
-       // Show the file browser dialog
-       return browseRelFile(&lv_, in_name, buffer()->filePath(),
-                            title, pattern, true);
+       BOOST_ASSERT(params_.get());
+       return *params_;
 }
 
 
-/// print the current buffer
-void ControlPrint::apply()
+string const ControlPrint::browse(string const & in_name) const
 {
-       if (!bufferIsAvailable())
-               return;
+       return browseRelFile(in_name, kernel().buffer().filePath(),
+                            _("Print to file"),
+                            FileFilterList(_("PostScript files (*.ps)")),
+                            true);
+}
 
-       view().apply();
 
+/// print the current buffer
+void ControlPrint::dispatchParams()
+{
        PrinterParams const pp = params();
        string command(lyxrc.print_command + ' ');
 
@@ -105,11 +92,11 @@ void ControlPrint::apply()
 
        if (!pp.all_pages && pp.from_page) {
                command += lyxrc.print_pagerange_flag + ' ';
-               command += tostr(pp.from_page);
+               command += convert<string>(pp.from_page);
                if (pp.to_page) {
                        // we have a range "from-to"
                        command += '-'
-                               + tostr(pp.to_page);
+                               + convert<string>(pp.to_page);
                }
                command += ' ';
        }
@@ -130,7 +117,7 @@ void ControlPrint::apply()
                        command += lyxrc.print_copies_flag;
                }
                command += ' '
-                       + tostr(pp.count_copies)
+                       + convert<string>(pp.count_copies)
                        + ' ';
        }
 
@@ -142,75 +129,18 @@ void ControlPrint::apply()
                command += lyxrc.print_extra_options + ' ';
        }
 
-       command += converters.dvips_options(buffer()) + ' ';
+       command += kernel().buffer().params().dvips_options() + ' ';
 
-       if (!Exporter::Export(buffer(), "dvi", true)) {
-               Alert::alert(_("Error:"),
-                          _("Unable to print"),
-                          _("Check that your parameters are correct"));
-               return;
-       }
+       string const target = (pp.target == PrinterParams::PRINTER) ?
+               "printer" : "file";
 
-       // Push directory path.
-       string path = buffer()->filePath();
-       if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
-               path = buffer()->tmppath;
-       }
-       Path p(path);
-
-       // there are three cases here:
-       // 1. we print to a file
-       // 2. we print directly to a printer
-       // 3. we print using a spool command (print to file first)
-       Systemcall one;
-       int res = 0;
-       string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
-       switch (pp.target) {
-       case PrinterParams::PRINTER:
-               if (!lyxrc.print_spool_command.empty()) {
-                       // case 3: print using a spool
-                       string const psname = ChangeExtension(dviname, ".ps");
-                       command += lyxrc.print_to_file
-                               + QuoteName(psname)
-                               + ' '
-                               + QuoteName(dviname);
-
-                       string command2 = lyxrc.print_spool_command + ' ';
-                       if (!pp.printer_name.empty()) {
-                               command2 += lyxrc.print_spool_printerprefix
-                                       + pp.printer_name
-                                       + ' ';
-                       }
-                       command2 += QuoteName(psname);
-                       // First run dvips.
-                       // If successful, then spool command
-                       res = one.startscript(Systemcall::Wait, command);
-                       if (res == 0)
-                               res = one.startscript(Systemcall::DontWait,
-                                                     command2);
-               } else {
-                       // case 2: print directly to a printer
-                       res = one.startscript(Systemcall::DontWait,
-                                             command + QuoteName(dviname));
-               }
-               break;
-
-       case PrinterParams::FILE:
-               // case 1: print to a file
-               command += lyxrc.print_to_file
-                       + QuoteName(MakeAbsPath(pp.file_name, path))
-                       + ' '
-                       + QuoteName(dviname);
-               res = one.startscript(Systemcall::DontWait, command);
-               break;
-       }
+       string const target_name = (pp.target == PrinterParams::PRINTER) ?
+               (pp.printer_name.empty() ? "default" : pp.printer_name) :
+               pp.file_name;
 
-       lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
-                            << command << '"' << endl;
-
-       if (res != 0) {
-               Alert::alert(_("Error:"),
-                          _("Unable to print"),
-                          _("Check that your parameters are correct"));
-       }
+       string const data = target + " " + target_name + " " + command;
+       kernel().dispatch(FuncRequest(getLfun(), data));
 }
+
+} // namespace frontend
+} // namespace lyx