]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
aff9ad98f5ba8699346738be75474dfe8718add6
[lyx.git] / src / frontends / Liason.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *        
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Liason.h"
18 #include "BufferView.h"
19 #include "buffer.h"
20 #include "lyxrc.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"
26
27 extern LyXRC lyxrc;
28 extern bool RunScript(Buffer * buffer, bool wait, string const & command,
29                       string const & orgname = string(), bool need_shell=true);
30
31
32 #ifdef CXX_WORKING_NAMESPACES
33 namespace Liason 
34 {
35 #endif
36
37 PrinterParams getPrinterParams(Buffer * buffer)
38 {
39         return PrinterParams(PrinterParams::PRINTER,
40                              lyxrc.printer,
41                              ChangeExtension(buffer->fileName(),
42                                              lyxrc.print_file_extension));
43 }
44
45
46 bool printBuffer(Buffer * buffer, PrinterParams const & pp) 
47 {
48         bool result(false);
49         string command(lyxrc.print_command + ' ');
50         
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
55                         + pp.printer_name
56                         + ' ';
57         }
58
59         switch (pp.which_pages) {
60         case PrinterParams::EVEN:
61                 command += lyxrc.print_evenpage_flag + ' ';
62                 break;
63
64         case PrinterParams::ODD:
65                 command += lyxrc.print_oddpage_flag + ' ';
66                 break;
67     
68         default:
69                 // only option left is print all of them
70                 break;
71         }
72
73         if (!pp.from_page.empty()) {
74                 command += lyxrc.print_pagerange_flag + ' ';
75                 command += pp.from_page;
76                 if (pp.to_page) {
77                                 // we have a range "from-to"
78                         command += '-';
79                         command += tostr(pp.to_page);
80                 }
81                 command += ' ';
82         }
83
84         if (pp.reverse_order) {
85                 command += lyxrc.print_reverse_flag + ' ';
86         }
87
88         BufferParams params(buffer->params);
89         if (params.orientation
90             == BufferParams::ORIENTATION_LANDSCAPE) {
91                 command += lyxrc.print_landscape_flag + ' ';
92         }
93
94         if (1 < pp.count_copies) {
95                 if (pp.unsorted_copies) {
96                         command += lyxrc.print_copies_flag;
97                 } else {
98                         command += lyxrc.print_collcopies_flag;
99                 }
100                 command += ' ';
101                 command += tostr(pp.count_copies);
102                 command += ' ';
103         }
104
105         if (!lyxrc.print_extra_options.empty()) {
106                 command += lyxrc.print_extra_options + ' ';
107         }
108
109         char real_papersize = params.papersize;
110         if (real_papersize == BufferParams::PAPER_DEFAULT) {
111                 real_papersize = lyxrc.default_papersize;
112         }
113
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
128                 string paper;
129                 switch (real_papersize) {
130                 case BufferParams::PAPER_USLETTER:
131                         paper = "letter";
132                         break;
133                 case BufferParams::PAPER_A3PAPER:
134                         paper = "a3";
135                         break;
136                 case BufferParams::PAPER_A4PAPER:
137                         paper = "a4";
138                         break;
139                 case BufferParams::PAPER_A5PAPER:
140                         paper = "a5";
141                         break;
142                 case BufferParams::PAPER_B5PAPER:
143                         paper = "b5";
144                         break;
145                 case BufferParams::PAPER_EXECUTIVEPAPER:
146                         paper = "foolscap";
147                         break;
148                 case BufferParams::PAPER_LEGALPAPER:
149                         paper = "legal";
150                         break;
151                 default: /* If nothing else fits, keep empty value */
152                         break;
153                 }
154                 if (!paper.empty()) {
155                         command += lyxrc.print_paper_flag + ' ';
156                         command += paper + ' ';
157                 }
158         }
159
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;
169         }
170         Path p(path);
171
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)
176         switch (pp.target) {
177         case PrinterParams::PRINTER:
178                 if (!lyxrc.print_spool_command.empty()) {
179                                 // case 3
180                         command += lyxrc.print_to_file
181                                 + QuoteName(pp.file_name);
182                         string command2 = lyxrc.print_spool_command
183                                 + ' ';
184                         if (!pp.printer_name.empty()) {
185                                 command2 += lyxrc.print_spool_printerprefix
186                                         + pp.printer_name;
187                         }
188                                 // First run dvips.
189                                 // If successful, then spool command
190                         if (result = RunScript(buffer, true, command)) {
191                                 result = RunScript(buffer, false,
192                                                    command2,
193                                                    QuoteName(pp.file_name));
194                         }
195                 } else {
196                                 // case 2
197                         result = RunScript(buffer, false, command);
198                 }
199                 break;
200
201         case PrinterParams::FILE:
202                 // case 1
203                 command += lyxrc.print_to_file
204                         + QuoteName(MakeAbsPath(pp.file_name, path));
205                 result = RunScript(buffer, false, command);
206                 break;
207         }
208         return result;
209 }
210
211 #ifdef CXX_WORKING_NAMESPACES
212 }
213 #endif
214