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"
20 #include "helper_funcs.h"
21 #include "PrinterParams.h"
23 #include "converter.h"
25 #include "frontends/Alert.h"
27 #include "support/tostr.h"
28 #include "support/LAssert.h"
29 #include "support/filetools.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
35 using namespace lyx::support;
39 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
40 : ControlDialogBD(lv, d),
45 PrinterParams & ControlPrint::params() const
52 void ControlPrint::setParams()
54 if (params_) delete params_;
56 /// get global printer parameters
57 string const name = ChangeExtension(buffer()->fileName(),
58 lyxrc.print_file_extension);
59 params_ = new PrinterParams (PrinterParams::PRINTER,
62 bc().valid(); // so that the user can press Ok
66 void ControlPrint::clearParams()
75 string const ControlPrint::Browse(string const & in_name)
77 string const title = _("Print to file");
78 string const pattern = "*.ps";
80 // Show the file browser dialog
81 return browseRelFile(in_name, buffer()->filePath(),
82 title, pattern, true);
88 void showPrintError(string const & name)
90 string str = bformat(_("Could not print the document %1$s.\n"
91 "Check that your printer is set up correctly."),
92 MakeDisplayPath(name, 50));
93 Alert::error(_("Print document failed"), str);
99 /// print the current buffer
100 void ControlPrint::apply()
102 if (!bufferIsAvailable())
107 PrinterParams const pp = params();
108 string command(lyxrc.print_command + ' ');
110 if (pp.target == PrinterParams::PRINTER
111 && lyxrc.print_adapt_output // dvips wants a printer name
112 && !pp.printer_name.empty()) {// printer name given
113 command += lyxrc.print_to_printer
118 if (!pp.all_pages && pp.from_page) {
119 command += lyxrc.print_pagerange_flag + ' ';
120 command += tostr(pp.from_page);
122 // we have a range "from-to"
129 // If both are, or both are not selected, then skip the odd/even printing
130 if (pp.odd_pages != pp.even_pages) {
132 command += lyxrc.print_oddpage_flag + ' ';
133 } else if (pp.even_pages) {
134 command += lyxrc.print_evenpage_flag + ' ';
138 if (pp.count_copies > 1) {
139 if (pp.sorted_copies) {
140 command += lyxrc.print_collcopies_flag;
142 command += lyxrc.print_copies_flag;
145 + tostr(pp.count_copies)
149 if (pp.reverse_order) {
150 command += lyxrc.print_reverse_flag + ' ';
153 if (!lyxrc.print_extra_options.empty()) {
154 command += lyxrc.print_extra_options + ' ';
157 command += converters.dvips_options(buffer()) + ' ';
159 if (!Exporter::Export(buffer(), "dvi", true)) {
160 showPrintError(buffer()->fileName());
164 // Push directory path.
165 string path = buffer()->filePath();
166 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
167 path = buffer()->tmppath;
171 // there are three cases here:
172 // 1. we print to a file
173 // 2. we print directly to a printer
174 // 3. we print using a spool command (print to file first)
177 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
179 case PrinterParams::PRINTER:
180 if (!lyxrc.print_spool_command.empty()) {
181 // case 3: print using a spool
182 string const psname = ChangeExtension(dviname, ".ps");
183 command += lyxrc.print_to_file
186 + QuoteName(dviname);
188 string command2 = lyxrc.print_spool_command + ' ';
189 if (!pp.printer_name.empty()) {
190 command2 += lyxrc.print_spool_printerprefix
194 command2 += QuoteName(psname);
196 // If successful, then spool command
197 res = one.startscript(Systemcall::Wait, command);
199 res = one.startscript(Systemcall::DontWait,
202 // case 2: print directly to a printer
203 res = one.startscript(Systemcall::DontWait,
204 command + QuoteName(dviname));
208 case PrinterParams::FILE:
209 // case 1: print to a file
210 command += lyxrc.print_to_file
211 + QuoteName(MakeAbsPath(pp.file_name, path))
213 + QuoteName(dviname);
214 res = one.startscript(Systemcall::DontWait, command);
218 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
219 << command << '"' << endl;
222 showPrintError(buffer()->fileName());