1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
18 #include "BufferView.h"
21 #include "PrinterParams.h"
22 #include "lyx_gui_misc.h"
23 #include "support/lstrings.h"
24 #include "support/filetools.h"
25 #include "support/path.h"
28 extern bool RunScript(Buffer * buffer, bool wait, string const & command,
29 string const & orgname = string(), bool need_shell=true);
32 #ifdef CXX_WORKING_NAMESPACES
37 PrinterParams getPrinterParams(Buffer * buffer)
39 return PrinterParams(PrinterParams::PRINTER,
41 ChangeExtension(buffer->fileName(),
42 lyxrc.print_file_extension));
46 bool printBuffer(Buffer * buffer, PrinterParams const & pp)
49 string command(lyxrc.print_command + ' ');
51 if (pp.target == PrinterParams::PRINTER
52 && lyxrc.print_adapt_output // dvips wants a printer name
53 && !pp.printer_name.empty()) {// printer name given
54 command += lyxrc.print_to_printer
59 switch (pp.which_pages) {
60 case PrinterParams::EVEN:
61 command += lyxrc.print_evenpage_flag + ' ';
64 case PrinterParams::ODD:
65 command += lyxrc.print_oddpage_flag + ' ';
69 // only option left is print all of them
73 if (!pp.from_page.empty()) {
74 command += lyxrc.print_pagerange_flag + ' ';
75 command += pp.from_page;
77 // we have a range "from-to"
79 command += tostr(pp.to_page);
84 if (pp.reverse_order) {
85 command += lyxrc.print_reverse_flag + ' ';
88 BufferParams params(buffer->params);
89 if (params.orientation
90 == BufferParams::ORIENTATION_LANDSCAPE) {
91 command += lyxrc.print_landscape_flag + ' ';
94 if (1 < pp.count_copies) {
95 if (pp.unsorted_copies) {
96 command += lyxrc.print_copies_flag;
98 command += lyxrc.print_collcopies_flag;
101 command += tostr(pp.count_copies);
105 if (!lyxrc.print_extra_options.empty()) {
106 command += lyxrc.print_extra_options + ' ';
109 char real_papersize = params.papersize;
110 if (real_papersize == BufferParams::PAPER_DEFAULT) {
111 real_papersize = lyxrc.default_papersize;
114 if (params.use_geometry
115 && params.papersize2 == BufferParams::VM_PAPER_CUSTOM
116 && !lyxrc.print_paper_dimension_flag.empty()
117 && !params.paperwidth.empty()
118 && !params.paperheight.empty()) {
119 // using a custom papersize
120 command += lyxrc.print_paper_dimension_flag + ' ';
121 command += params.paperwidth + ',';
122 command += params.paperheight + ' ';
123 } else if (!lyxrc.print_paper_flag.empty()
124 && (real_papersize != BufferParams::PAPER_USLETTER
125 || params.orientation
126 == BufferParams::ORIENTATION_PORTRAIT)) {
127 // There's a problem with US Letter + landscape
129 switch (real_papersize) {
130 case BufferParams::PAPER_USLETTER:
133 case BufferParams::PAPER_A3PAPER:
136 case BufferParams::PAPER_A4PAPER:
139 case BufferParams::PAPER_A5PAPER:
142 case BufferParams::PAPER_B5PAPER:
145 case BufferParams::PAPER_EXECUTIVEPAPER:
148 case BufferParams::PAPER_LEGALPAPER:
151 default: /* If nothing else fits, keep empty value */
154 if (!paper.empty()) {
155 command += lyxrc.print_paper_flag + ' ';
156 command += paper + ' ';
160 // Push directory path if necessary.
161 // PS file should go where the source file is unless it's a
162 // read-only directory in which case we write it to tmpdir.
163 // All temporary files go in tmpdir (like spool files).
164 string path = OnlyPath(buffer->fileName());
165 if ((pp.target != PrinterParams::FILE
166 && lyxrc.use_tempdir)
167 || (IsDirWriteable(path) < 1)) {
168 path = buffer->tmppath;
172 // there are three cases here:
173 // 1. we print to a file
174 // 2. we print direct to a printer
175 // 3. we print using a spool command (print to file first)
177 case PrinterParams::PRINTER:
178 if (!lyxrc.print_spool_command.empty()) {
180 command += lyxrc.print_to_file
181 + QuoteName(pp.file_name);
182 string command2 = lyxrc.print_spool_command
184 if (!pp.printer_name.empty()) {
185 command2 += lyxrc.print_spool_printerprefix
189 // If successful, then spool command
190 if (result = RunScript(buffer, true, command)) {
191 result = RunScript(buffer, false,
193 QuoteName(pp.file_name));
197 result = RunScript(buffer, false, command);
201 case PrinterParams::FILE:
203 command += lyxrc.print_to_file
204 + QuoteName(MakeAbsPath(pp.file_name, path));
205 result = RunScript(buffer, false, command);
211 #ifdef CXX_WORKING_NAMESPACES