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 "ButtonControllerBase.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);
84 /// print the current buffer
85 void ControlPrint::apply()
87 if (!bufferIsAvailable())
92 PrinterParams const pp = params();
93 string command(lyxrc.print_command + ' ');
95 if (pp.target == PrinterParams::PRINTER
96 && lyxrc.print_adapt_output // dvips wants a printer name
97 && !pp.printer_name.empty()) {// printer name given
98 command += lyxrc.print_to_printer
103 if (!pp.all_pages && pp.from_page) {
104 command += lyxrc.print_pagerange_flag + ' ';
105 command += tostr(pp.from_page);
107 // we have a range "from-to"
114 // If both are, or both are not selected, then skip the odd/even printing
115 if (pp.odd_pages != pp.even_pages) {
117 command += lyxrc.print_oddpage_flag + ' ';
118 } else if (pp.even_pages) {
119 command += lyxrc.print_evenpage_flag + ' ';
123 if (pp.count_copies > 1) {
124 if (pp.sorted_copies) {
125 command += lyxrc.print_collcopies_flag;
127 command += lyxrc.print_copies_flag;
130 + tostr(pp.count_copies)
134 if (pp.reverse_order) {
135 command += lyxrc.print_reverse_flag + ' ';
138 if (!lyxrc.print_extra_options.empty()) {
139 command += lyxrc.print_extra_options + ' ';
142 command += converters.dvips_options(buffer()) + ' ';
144 if (!Exporter::Export(buffer(), "dvi", true)) {
145 Alert::alert(_("Error:"),
146 _("Unable to print"),
147 _("Check that your parameters are correct"));
151 // Push directory path.
152 string path = buffer()->filePath();
153 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
154 path = buffer()->tmppath;
158 // there are three cases here:
159 // 1. we print to a file
160 // 2. we print directly to a printer
161 // 3. we print using a spool command (print to file first)
164 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
166 case PrinterParams::PRINTER:
167 if (!lyxrc.print_spool_command.empty()) {
168 // case 3: print using a spool
169 string const psname = ChangeExtension(dviname, ".ps");
170 command += lyxrc.print_to_file
173 + QuoteName(dviname);
175 string command2 = lyxrc.print_spool_command + ' ';
176 if (!pp.printer_name.empty()) {
177 command2 += lyxrc.print_spool_printerprefix
181 command2 += QuoteName(psname);
183 // If successful, then spool command
184 res = one.startscript(Systemcall::Wait, command);
186 res = one.startscript(Systemcall::DontWait,
189 // case 2: print directly to a printer
190 res = one.startscript(Systemcall::DontWait,
191 command + QuoteName(dviname));
195 case PrinterParams::FILE:
196 // case 1: print to a file
197 command += lyxrc.print_to_file
198 + QuoteName(MakeAbsPath(pp.file_name, path))
200 + QuoteName(dviname);
201 res = one.startscript(Systemcall::DontWait, command);
205 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
206 << command << '"' << endl;
209 Alert::alert(_("Error:"),
210 _("Unable to print"),
211 _("Check that your parameters are correct"));