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
14 #include "ControlPrint.h"
17 #include "ButtonController.h"
21 #include "helper_funcs.h"
22 #include "PrinterParams.h"
24 #include "converter.h"
26 #include "frontends/Alert.h"
28 #include "support/LAssert.h"
29 #include "support/filetools.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
33 #include "debug.h" // for lyxerr
37 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
38 : ControlDialogBD(lv, d),
43 PrinterParams & ControlPrint::params() const
50 void ControlPrint::setParams()
52 if (params_) delete params_;
54 /// get global printer parameters
55 string const name = ChangeExtension(buffer()->fileName(),
56 lyxrc.print_file_extension);
57 params_ = new PrinterParams (PrinterParams::PRINTER,
60 bc().valid(); // so that the user can press Ok
64 void ControlPrint::clearParams()
73 string const ControlPrint::Browse(string const & in_name)
75 string const title = _("Print to file");
76 string const pattern = "*.ps";
78 // Show the file browser dialog
79 return browseRelFile(in_name, buffer()->filePath(),
80 title, pattern, true);
86 void showPrintError(string const & name)
88 string str = bformat(_("Could not print the document %1$s.\n"
89 "Check that your printer is set up correctly."),
90 MakeDisplayPath(name, 50));
91 Alert::error(_("Print document failed"), str);
97 /// print the current buffer
98 void ControlPrint::apply()
100 if (!bufferIsAvailable())
105 PrinterParams const pp = params();
106 string command(lyxrc.print_command + ' ');
108 if (pp.target == PrinterParams::PRINTER
109 && lyxrc.print_adapt_output // dvips wants a printer name
110 && !pp.printer_name.empty()) {// printer name given
111 command += lyxrc.print_to_printer
116 if (!pp.all_pages && pp.from_page) {
117 command += lyxrc.print_pagerange_flag + ' ';
118 command += tostr(pp.from_page);
120 // we have a range "from-to"
127 // If both are, or both are not selected, then skip the odd/even printing
128 if (pp.odd_pages != pp.even_pages) {
130 command += lyxrc.print_oddpage_flag + ' ';
131 } else if (pp.even_pages) {
132 command += lyxrc.print_evenpage_flag + ' ';
136 if (pp.count_copies > 1) {
137 if (pp.sorted_copies) {
138 command += lyxrc.print_collcopies_flag;
140 command += lyxrc.print_copies_flag;
143 + tostr(pp.count_copies)
147 if (pp.reverse_order) {
148 command += lyxrc.print_reverse_flag + ' ';
151 if (!lyxrc.print_extra_options.empty()) {
152 command += lyxrc.print_extra_options + ' ';
155 command += converters.dvips_options(buffer()) + ' ';
157 if (!Exporter::Export(buffer(), "dvi", true)) {
158 showPrintError(buffer()->fileName());
162 // Push directory path.
163 string path = buffer()->filePath();
164 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
165 path = buffer()->tmppath;
169 // there are three cases here:
170 // 1. we print to a file
171 // 2. we print directly to a printer
172 // 3. we print using a spool command (print to file first)
175 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
177 case PrinterParams::PRINTER:
178 if (!lyxrc.print_spool_command.empty()) {
179 // case 3: print using a spool
180 string const psname = ChangeExtension(dviname, ".ps");
181 command += lyxrc.print_to_file
184 + QuoteName(dviname);
186 string command2 = lyxrc.print_spool_command + ' ';
187 if (!pp.printer_name.empty()) {
188 command2 += lyxrc.print_spool_printerprefix
192 command2 += QuoteName(psname);
194 // If successful, then spool command
195 res = one.startscript(Systemcall::Wait, command);
197 res = one.startscript(Systemcall::DontWait,
200 // case 2: print directly to a printer
201 res = one.startscript(Systemcall::DontWait,
202 command + QuoteName(dviname));
206 case PrinterParams::FILE:
207 // case 1: print to a file
208 command += lyxrc.print_to_file
209 + QuoteName(MakeAbsPath(pp.file_name, path))
211 + QuoteName(dviname);
212 res = one.startscript(Systemcall::DontWait, command);
216 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
217 << command << '"' << endl;
220 showPrintError(buffer()->fileName());