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/path.h"
30 #include "support/systemcall.h"
34 using lyx::support::bformat;
35 using lyx::support::ChangeExtension;
36 using lyx::support::IsDirWriteable;
37 using lyx::support::MakeAbsPath;
38 using lyx::support::MakeDisplayPath;
39 using lyx::support::Path;
40 using lyx::support::QuoteName;
41 using lyx::support::Systemcall;
46 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
47 : ControlDialogBD(lv, d),
52 PrinterParams & ControlPrint::params() const
54 BOOST_ASSERT(params_);
59 void ControlPrint::setParams()
61 if (params_) delete params_;
63 /// get global printer parameters
64 string const name = ChangeExtension(buffer()->fileName(),
65 lyxrc.print_file_extension);
66 params_ = new PrinterParams (PrinterParams::PRINTER,
69 bc().valid(); // so that the user can press Ok
73 void ControlPrint::clearParams()
82 string const ControlPrint::Browse(string const & in_name)
84 string const title = _("Print to file");
85 string const pattern = "*.ps";
87 // Show the file browser dialog
88 return browseRelFile(in_name, buffer()->filePath(),
89 title, pattern, true);
95 void showPrintError(string const & name)
97 string str = bformat(_("Could not print the document %1$s.\n"
98 "Check that your printer is set up correctly."),
99 MakeDisplayPath(name, 50));
100 Alert::error(_("Print document failed"), str);
106 /// print the current buffer
107 void ControlPrint::apply()
109 if (!bufferIsAvailable())
114 PrinterParams const pp = params();
115 string command(lyxrc.print_command + ' ');
117 if (pp.target == PrinterParams::PRINTER
118 && lyxrc.print_adapt_output // dvips wants a printer name
119 && !pp.printer_name.empty()) {// printer name given
120 command += lyxrc.print_to_printer
125 if (!pp.all_pages && pp.from_page) {
126 command += lyxrc.print_pagerange_flag + ' ';
127 command += tostr(pp.from_page);
129 // we have a range "from-to"
136 // If both are, or both are not selected, then skip the odd/even printing
137 if (pp.odd_pages != pp.even_pages) {
139 command += lyxrc.print_oddpage_flag + ' ';
140 } else if (pp.even_pages) {
141 command += lyxrc.print_evenpage_flag + ' ';
145 if (pp.count_copies > 1) {
146 if (pp.sorted_copies) {
147 command += lyxrc.print_collcopies_flag;
149 command += lyxrc.print_copies_flag;
152 + tostr(pp.count_copies)
156 if (pp.reverse_order) {
157 command += lyxrc.print_reverse_flag + ' ';
160 if (!lyxrc.print_extra_options.empty()) {
161 command += lyxrc.print_extra_options + ' ';
164 command += buffer()->params().dvips_options() + ' ';
166 if (!Exporter::Export(buffer(), "dvi", true)) {
167 showPrintError(buffer()->fileName());
171 // Push directory path.
172 string path = buffer()->filePath();
173 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
174 path = buffer()->temppath();
178 // there are three cases here:
179 // 1. we print to a file
180 // 2. we print directly to a printer
181 // 3. we print using a spool command (print to file first)
184 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
186 case PrinterParams::PRINTER:
187 if (!lyxrc.print_spool_command.empty()) {
188 // case 3: print using a spool
189 string const psname = ChangeExtension(dviname, ".ps");
190 command += lyxrc.print_to_file
193 + QuoteName(dviname);
195 string command2 = lyxrc.print_spool_command + ' ';
196 if (!pp.printer_name.empty()) {
197 command2 += lyxrc.print_spool_printerprefix
201 command2 += QuoteName(psname);
203 // If successful, then spool command
204 res = one.startscript(Systemcall::Wait, command);
206 res = one.startscript(Systemcall::DontWait,
209 // case 2: print directly to a printer
210 res = one.startscript(Systemcall::DontWait,
211 command + QuoteName(dviname));
215 case PrinterParams::FILE:
216 // case 1: print to a file
217 command += lyxrc.print_to_file
218 + QuoteName(MakeAbsPath(pp.file_name, path))
220 + QuoteName(dviname);
221 res = one.startscript(Systemcall::DontWait, command);
225 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
226 << command << '"' << endl;
229 showPrintError(buffer()->fileName());