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"
32 #include "support/BoostFormat.h"
34 #include "debug.h" // for lyxerr
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)
90 boost::format fmt(_("Could not print the document %1$s.\n"
91 "Check that your printer is set up correctly."));
92 fmt % MakeDisplayPath(name, 50);
93 string str = fmt.str();
95 string str = _("Could not print the document ");
96 str += MakeDisplayPath(name, 50);
97 str += _(".\nCheck that your printer is set up correctly.");
99 Alert::error(_("Print document failed"), str);
105 /// print the current buffer
106 void ControlPrint::apply()
108 if (!bufferIsAvailable())
113 PrinterParams const pp = params();
114 string command(lyxrc.print_command + ' ');
116 if (pp.target == PrinterParams::PRINTER
117 && lyxrc.print_adapt_output // dvips wants a printer name
118 && !pp.printer_name.empty()) {// printer name given
119 command += lyxrc.print_to_printer
124 if (!pp.all_pages && pp.from_page) {
125 command += lyxrc.print_pagerange_flag + ' ';
126 command += tostr(pp.from_page);
128 // we have a range "from-to"
135 // If both are, or both are not selected, then skip the odd/even printing
136 if (pp.odd_pages != pp.even_pages) {
138 command += lyxrc.print_oddpage_flag + ' ';
139 } else if (pp.even_pages) {
140 command += lyxrc.print_evenpage_flag + ' ';
144 if (pp.count_copies > 1) {
145 if (pp.sorted_copies) {
146 command += lyxrc.print_collcopies_flag;
148 command += lyxrc.print_copies_flag;
151 + tostr(pp.count_copies)
155 if (pp.reverse_order) {
156 command += lyxrc.print_reverse_flag + ' ';
159 if (!lyxrc.print_extra_options.empty()) {
160 command += lyxrc.print_extra_options + ' ';
163 command += converters.dvips_options(buffer()) + ' ';
165 if (!Exporter::Export(buffer(), "dvi", true)) {
166 showPrintError(buffer()->fileName());
170 // Push directory path.
171 string path = buffer()->filePath();
172 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
173 path = buffer()->tmppath;
177 // there are three cases here:
178 // 1. we print to a file
179 // 2. we print directly to a printer
180 // 3. we print using a spool command (print to file first)
183 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
185 case PrinterParams::PRINTER:
186 if (!lyxrc.print_spool_command.empty()) {
187 // case 3: print using a spool
188 string const psname = ChangeExtension(dviname, ".ps");
189 command += lyxrc.print_to_file
192 + QuoteName(dviname);
194 string command2 = lyxrc.print_spool_command + ' ';
195 if (!pp.printer_name.empty()) {
196 command2 += lyxrc.print_spool_printerprefix
200 command2 += QuoteName(psname);
202 // If successful, then spool command
203 res = one.startscript(Systemcall::Wait, command);
205 res = one.startscript(Systemcall::DontWait,
208 // case 2: print directly to a printer
209 res = one.startscript(Systemcall::DontWait,
210 command + QuoteName(dviname));
214 case PrinterParams::FILE:
215 // case 1: print to a file
216 command += lyxrc.print_to_file
217 + QuoteName(MakeAbsPath(pp.file_name, path))
219 + QuoteName(dviname);
220 res = one.startscript(Systemcall::DontWait, command);
224 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
225 << command << '"' << endl;
228 showPrintError(buffer()->fileName());