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"
24 #include "frontends/Alert.h"
26 #include "support/tostr.h"
27 #include "support/filetools.h"
28 #include "support/LAssert.h"
29 #include "support/path.h"
30 #include "support/systemcall.h"
34 using namespace lyx::support;
38 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
39 : ControlDialogBD(lv, d),
44 PrinterParams & ControlPrint::params() const
51 void ControlPrint::setParams()
53 if (params_) delete params_;
55 /// get global printer parameters
56 string const name = ChangeExtension(buffer()->fileName(),
57 lyxrc.print_file_extension);
58 params_ = new PrinterParams (PrinterParams::PRINTER,
61 bc().valid(); // so that the user can press Ok
65 void ControlPrint::clearParams()
74 string const ControlPrint::Browse(string const & in_name)
76 string const title = _("Print to file");
77 string const pattern = "*.ps";
79 // Show the file browser dialog
80 return browseRelFile(in_name, buffer()->filePath(),
81 title, pattern, true);
87 void showPrintError(string const & name)
89 string str = bformat(_("Could not print the document %1$s.\n"
90 "Check that your printer is set up correctly."),
91 MakeDisplayPath(name, 50));
92 Alert::error(_("Print document failed"), str);
98 /// print the current buffer
99 void ControlPrint::apply()
101 if (!bufferIsAvailable())
106 PrinterParams const pp = params();
107 string command(lyxrc.print_command + ' ');
109 if (pp.target == PrinterParams::PRINTER
110 && lyxrc.print_adapt_output // dvips wants a printer name
111 && !pp.printer_name.empty()) {// printer name given
112 command += lyxrc.print_to_printer
117 if (!pp.all_pages && pp.from_page) {
118 command += lyxrc.print_pagerange_flag + ' ';
119 command += tostr(pp.from_page);
121 // we have a range "from-to"
128 // If both are, or both are not selected, then skip the odd/even printing
129 if (pp.odd_pages != pp.even_pages) {
131 command += lyxrc.print_oddpage_flag + ' ';
132 } else if (pp.even_pages) {
133 command += lyxrc.print_evenpage_flag + ' ';
137 if (pp.count_copies > 1) {
138 if (pp.sorted_copies) {
139 command += lyxrc.print_collcopies_flag;
141 command += lyxrc.print_copies_flag;
144 + tostr(pp.count_copies)
148 if (pp.reverse_order) {
149 command += lyxrc.print_reverse_flag + ' ';
152 if (!lyxrc.print_extra_options.empty()) {
153 command += lyxrc.print_extra_options + ' ';
156 command += buffer()->params().dvips_options() + ' ';
158 if (!Exporter::Export(buffer(), "dvi", true)) {
159 showPrintError(buffer()->fileName());
163 // Push directory path.
164 string path = buffer()->filePath();
165 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
166 path = buffer()->temppath();
170 // there are three cases here:
171 // 1. we print to a file
172 // 2. we print directly to a printer
173 // 3. we print using a spool command (print to file first)
176 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
178 case PrinterParams::PRINTER:
179 if (!lyxrc.print_spool_command.empty()) {
180 // case 3: print using a spool
181 string const psname = ChangeExtension(dviname, ".ps");
182 command += lyxrc.print_to_file
185 + QuoteName(dviname);
187 string command2 = lyxrc.print_spool_command + ' ';
188 if (!pp.printer_name.empty()) {
189 command2 += lyxrc.print_spool_printerprefix
193 command2 += QuoteName(psname);
195 // If successful, then spool command
196 res = one.startscript(Systemcall::Wait, command);
198 res = one.startscript(Systemcall::DontWait,
201 // case 2: print directly to a printer
202 res = one.startscript(Systemcall::DontWait,
203 command + QuoteName(dviname));
207 case PrinterParams::FILE:
208 // case 1: print to a file
209 command += lyxrc.print_to_file
210 + QuoteName(MakeAbsPath(pp.file_name, path))
212 + QuoteName(dviname);
213 res = one.startscript(Systemcall::DontWait, command);
217 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
218 << command << '"' << endl;
221 showPrintError(buffer()->fileName());