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 #pragma implementation
17 #include "ControlPrint.h"
20 #include "ButtonControllerBase.h"
24 #include "helper_funcs.h"
25 #include "PrinterParams.h"
27 #include "converter.h"
29 #include "frontends/Alert.h"
31 #include "support/LAssert.h"
32 #include "support/filetools.h"
33 #include "support/path.h"
34 #include "support/systemcall.h"
36 #include "debug.h" // for lyxerr
40 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
41 : ControlDialogBD(lv, d),
46 PrinterParams & ControlPrint::params() const
53 void ControlPrint::setParams()
55 if (params_) delete params_;
57 /// get global printer parameters
58 string const name = ChangeExtension(buffer()->fileName(),
59 lyxrc.print_file_extension);
60 params_ = new PrinterParams (PrinterParams::PRINTER,
63 bc().valid(); // so that the user can press Ok
67 void ControlPrint::clearParams()
76 string const ControlPrint::Browse(string const & in_name)
78 string const title = _("Print to file");
79 string const pattern = "*.ps";
81 // Show the file browser dialog
82 return browseRelFile(&lv_, in_name, buffer()->filePath(),
83 title, pattern, true);
87 /// print the current buffer
88 void ControlPrint::apply()
90 if (!bufferIsAvailable())
95 PrinterParams const pp = params();
96 string command(lyxrc.print_command + ' ');
98 if (pp.target == PrinterParams::PRINTER
99 && lyxrc.print_adapt_output // dvips wants a printer name
100 && !pp.printer_name.empty()) {// printer name given
101 command += lyxrc.print_to_printer
106 if (!pp.all_pages && pp.from_page) {
107 command += lyxrc.print_pagerange_flag + ' ';
108 command += tostr(pp.from_page);
110 // we have a range "from-to"
117 // If both are, or both are not selected, then skip the odd/even printing
118 if (pp.odd_pages != pp.even_pages) {
120 command += lyxrc.print_oddpage_flag + ' ';
121 } else if (pp.even_pages) {
122 command += lyxrc.print_evenpage_flag + ' ';
126 if (pp.count_copies > 1) {
127 if (pp.sorted_copies) {
128 command += lyxrc.print_collcopies_flag;
130 command += lyxrc.print_copies_flag;
133 + tostr(pp.count_copies)
137 if (pp.reverse_order) {
138 command += lyxrc.print_reverse_flag + ' ';
141 if (!lyxrc.print_extra_options.empty()) {
142 command += lyxrc.print_extra_options + ' ';
145 command += converters.dvips_options(buffer()) + ' ';
147 if (!Exporter::Export(buffer(), "dvi", true)) {
148 Alert::alert(_("Error:"),
149 _("Unable to print"),
150 _("Check that your parameters are correct"));
154 // Push directory path.
155 string path = buffer()->filePath();
156 if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
157 path = buffer()->tmppath;
161 // there are three cases here:
162 // 1. we print to a file
163 // 2. we print directly to a printer
164 // 3. we print using a spool command (print to file first)
167 string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
169 case PrinterParams::PRINTER:
170 if (!lyxrc.print_spool_command.empty()) {
171 // case 3: print using a spool
172 string const psname = ChangeExtension(dviname, ".ps");
173 command += lyxrc.print_to_file
176 + QuoteName(dviname);
178 string command2 = lyxrc.print_spool_command + ' ';
179 if (!pp.printer_name.empty()) {
180 command2 += lyxrc.print_spool_printerprefix
184 command2 += QuoteName(psname);
186 // If successful, then spool command
187 res = one.startscript(Systemcall::Wait, command);
189 res = one.startscript(Systemcall::DontWait,
192 // case 2: print directly to a printer
193 res = one.startscript(Systemcall::DontWait,
194 command + QuoteName(dviname));
198 case PrinterParams::FILE:
199 // case 1: print to a file
200 command += lyxrc.print_to_file
201 + QuoteName(MakeAbsPath(pp.file_name, path))
203 + QuoteName(dviname);
204 res = one.startscript(Systemcall::DontWait, command);
208 lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \"" << command << "\"" << endl;
211 Alert::alert(_("Error:"),
212 _("Unable to print"),
213 _("Check that your parameters are correct"));