3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
13 #include "ControlPrint.h"
16 #include "ButtonController.h"
19 #include "bufferparams.h"
21 #include "helper_funcs.h"
22 #include "PrinterParams.h"
25 #include "frontends/Alert.h"
27 #include "support/tostr.h"
28 #include "support/filetools.h"
29 #include "support/globbing.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
35 using lyx::support::bformat;
36 using lyx::support::ChangeExtension;
37 using lyx::support::FileFilterList;
38 using lyx::support::IsDirWriteable;
39 using lyx::support::MakeAbsPath;
40 using lyx::support::MakeDisplayPath;
41 using lyx::support::Path;
42 using lyx::support::QuoteName;
43 using lyx::support::Systemcall;
49 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
50 : ControlDialogBD(lv, d),
55 PrinterParams & ControlPrint::params() const
57 BOOST_ASSERT(params_);
62 void ControlPrint::setParams()
64 if (params_) delete params_;
66 /// get global printer parameters
67 string const name = ChangeExtension(buffer()->fileName(),
68 lyxrc.print_file_extension);
69 params_ = new PrinterParams (PrinterParams::PRINTER,
72 bc().valid(); // so that the user can press Ok
76 void ControlPrint::clearParams()
85 string const ControlPrint::browse(string const & in_name) const
87 return browseRelFile(in_name, buffer().filePath(),
89 FileFilterList("PostScript files (*.ps)"),
96 void showPrintError(string const & name)
98 string str = bformat(_("Could not print the document %1$s.\n"
99 "Check that your printer is set up correctly."),
100 MakeDisplayPath(name, 50));
101 Alert::error(_("Print document failed"), str);
107 /// print the current buffer
108 void ControlPrint::apply()
110 if (!bufferIsAvailable())
115 PrinterParams const pp = params();
116 string command(lyxrc.print_command + ' ');
118 if (pp.target == PrinterParams::PRINTER
119 && lyxrc.print_adapt_output // dvips wants a printer name
120 && !pp.printer_name.empty()) {// printer name given
121 command += lyxrc.print_to_printer
126 if (!pp.all_pages && pp.from_page) {
127 command += lyxrc.print_pagerange_flag + ' ';
128 command += tostr(pp.from_page);
130 // we have a range "from-to"
137 // If both are, or both are not selected, then skip the odd/even printing
138 if (pp.odd_pages != pp.even_pages) {
140 command += lyxrc.print_oddpage_flag + ' ';
141 } else if (pp.even_pages) {
142 command += lyxrc.print_evenpage_flag + ' ';
146 if (pp.count_copies > 1) {
147 if (pp.sorted_copies) {
148 command += lyxrc.print_collcopies_flag;
150 command += lyxrc.print_copies_flag;
153 + tostr(pp.count_copies)
157 if (pp.reverse_order) {
158 command += lyxrc.print_reverse_flag + ' ';
161 if (!lyxrc.print_extra_options.empty()) {
162 command += lyxrc.print_extra_options + ' ';
165 command += buffer()->params().dvips_options() + ' ';
167 if (!Exporter::Export(buffer(), "dvi", true)) {
168 showPrintError(buffer()->fileName());
172 // Push directory path.
173 string path = buffer()->filePath();
174 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
175 path = buffer()->temppath();
179 // there are three cases here:
180 // 1. we print to a file
181 // 2. we print directly to a printer
182 // 3. we print using a spool command (print to file first)
185 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
187 case PrinterParams::PRINTER:
188 if (!lyxrc.print_spool_command.empty()) {
189 // case 3: print using a spool
190 string const psname = ChangeExtension(dviname, ".ps");
191 command += lyxrc.print_to_file
194 + QuoteName(dviname);
196 string command2 = lyxrc.print_spool_command + ' ';
197 if (!pp.printer_name.empty()) {
198 command2 += lyxrc.print_spool_printerprefix
202 command2 += QuoteName(psname);
204 // If successful, then spool command
205 res = one.startscript(Systemcall::Wait, command);
207 res = one.startscript(Systemcall::DontWait,
210 // case 2: print directly to a printer
211 res = one.startscript(Systemcall::DontWait,
212 command + QuoteName(dviname));
216 case PrinterParams::FILE:
217 // case 1: print to a file
218 command += lyxrc.print_to_file
219 + QuoteName(MakeAbsPath(pp.file_name, path))
221 + QuoteName(dviname);
222 res = one.startscript(Systemcall::DontWait, command);
226 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
227 << command << '"' << endl;
230 showPrintError(buffer()->fileName());