]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
35c151315839c1ec207fcfdec82074f96c528811
[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 "LyXView.h"
19 #include "BufferView.h"
20 #include "buffer.h"
21 #include "lyxrc.h"
22 #include "PrinterParams.h"
23 #include "lyx_gui_misc.h"
24 #include "support/lstrings.h"
25 #include "support/filetools.h"
26 #include "support/path.h"
27 #include "exporter.h"
28 #include "minibuffer.h"
29
30 extern LyXRC lyxrc;
31 extern bool RunScript(Buffer * buffer, bool wait, string const & command,
32                       string const & orgname = string(), bool need_shell=true);
33
34
35 #ifdef CXX_WORKING_NAMESPACES
36 namespace Liason 
37 {
38 #endif
39
40 PrinterParams getPrinterParams(Buffer * buffer)
41 {
42         return PrinterParams(PrinterParams::PRINTER,
43                              lyxrc.printer,
44                              ChangeExtension(buffer->fileName(),
45                                              lyxrc.print_file_extension));
46 }
47
48
49 bool printBuffer(Buffer * buffer, PrinterParams const & pp) 
50 {
51         bool result(false);
52         string command(lyxrc.print_command + ' ');
53         
54         if (pp.target == PrinterParams::PRINTER
55             && lyxrc.print_adapt_output  // dvips wants a printer name
56             && !pp.printer_name.empty()) {// printer name given
57                 command += lyxrc.print_to_printer
58                         + pp.printer_name
59                         + ' ';
60         }
61
62         switch (pp.which_pages) {
63         case PrinterParams::EVEN:
64                 command += lyxrc.print_evenpage_flag + ' ';
65                 break;
66
67         case PrinterParams::ODD:
68                 command += lyxrc.print_oddpage_flag + ' ';
69                 break;
70     
71         default:
72                 // only option left is print all of them
73                 break;
74         }
75
76         if (!pp.from_page.empty()) {
77                 command += lyxrc.print_pagerange_flag + ' ';
78                 command += pp.from_page;
79                 if (pp.to_page) {
80                                 // we have a range "from-to"
81                         command += '-';
82                         command += tostr(pp.to_page);
83                 }
84                 command += ' ';
85         }
86
87         if (pp.reverse_order) {
88                 command += lyxrc.print_reverse_flag + ' ';
89         }
90
91         BufferParams params(buffer->params);
92         if (params.orientation
93             == BufferParams::ORIENTATION_LANDSCAPE) {
94                 command += lyxrc.print_landscape_flag + ' ';
95         }
96
97         if (1 < pp.count_copies) {
98                 if (pp.unsorted_copies) {
99                         command += lyxrc.print_copies_flag;
100                 } else {
101                         command += lyxrc.print_collcopies_flag;
102                 }
103                 command += ' ';
104                 command += tostr(pp.count_copies);
105                 command += ' ';
106         }
107
108         if (!lyxrc.print_extra_options.empty()) {
109                 command += lyxrc.print_extra_options + ' ';
110         }
111
112         char real_papersize = params.papersize;
113         if (real_papersize == BufferParams::PAPER_DEFAULT) {
114                 real_papersize = lyxrc.default_papersize;
115         }
116
117         if (params.use_geometry
118             && params.papersize2 == BufferParams::VM_PAPER_CUSTOM
119             && !lyxrc.print_paper_dimension_flag.empty()
120             && !params.paperwidth.empty()
121             && !params.paperheight.empty()) {
122                 // using a custom papersize
123                 command += lyxrc.print_paper_dimension_flag + ' ';
124                 command += params.paperwidth + ',';
125                 command += params.paperheight + ' ';
126         } else if (!lyxrc.print_paper_flag.empty()
127                    && (real_papersize != BufferParams::PAPER_USLETTER
128                        || params.orientation 
129                        == BufferParams::ORIENTATION_PORTRAIT)) {
130                 // There's a problem with US Letter + landscape
131                 string paper;
132                 switch (real_papersize) {
133                 case BufferParams::PAPER_USLETTER:
134                         paper = "letter";
135                         break;
136                 case BufferParams::PAPER_A3PAPER:
137                         paper = "a3";
138                         break;
139                 case BufferParams::PAPER_A4PAPER:
140                         paper = "a4";
141                         break;
142                 case BufferParams::PAPER_A5PAPER:
143                         paper = "a5";
144                         break;
145                 case BufferParams::PAPER_B5PAPER:
146                         paper = "b5";
147                         break;
148                 case BufferParams::PAPER_EXECUTIVEPAPER:
149                         paper = "foolscap";
150                         break;
151                 case BufferParams::PAPER_LEGALPAPER:
152                         paper = "legal";
153                         break;
154                 default: /* If nothing else fits, keep empty value */
155                         break;
156                 }
157                 if (!paper.empty()) {
158                         command += lyxrc.print_paper_flag + ' ';
159                         command += paper + ' ';
160                 }
161         }
162
163         // Push directory path if necessary.
164         // PS file should go where the source file is unless it's a
165         // read-only directory in which case we write it to tmpdir.
166         // All temporary files go in tmpdir (like spool files).
167         string path = OnlyPath(buffer->fileName());
168         if ((pp.target != PrinterParams::FILE
169              && lyxrc.use_tempdir)
170             || (IsDirWriteable(path) < 1)) {
171                 path = buffer->tmppath;
172         }
173         Path p(path);
174
175 #ifndef NEW_EXPORT
176         // there are three cases here:
177         // 1. we print to a file
178         // 2. we print direct to a printer
179         // 3. we print using a spool command (print to file first)
180         switch (pp.target) {
181         case PrinterParams::PRINTER:
182                 if (!lyxrc.print_spool_command.empty()) {
183                                 // case 3
184                         command += lyxrc.print_to_file
185                                 + QuoteName(pp.file_name);
186                         string command2 = lyxrc.print_spool_command
187                                 + ' ';
188                         if (!pp.printer_name.empty()) {
189                                 command2 += lyxrc.print_spool_printerprefix
190                                         + pp.printer_name;
191                         }
192                                 // First run dvips.
193                                 // If successful, then spool command
194                         if (result = RunScript(buffer, true, command)) {
195                                 result = RunScript(buffer, false,
196                                                    command2,
197                                                    QuoteName(pp.file_name));
198                         }
199                 } else {
200                                 // case 2
201                         result = RunScript(buffer, false, command);
202                 }
203                 break;
204
205         case PrinterParams::FILE:
206                 // case 1
207                 command += lyxrc.print_to_file
208                         + QuoteName(MakeAbsPath(pp.file_name, path));
209                 result = RunScript(buffer, false, command);
210                 break;
211         }
212 #endif
213         return result;
214 }
215
216 void setMinibuffer(LyXView * lv, char const * msg)
217 {
218         lv->getMiniBuffer()->Set(msg);
219 }
220
221 #ifdef CXX_WORKING_NAMESPACES
222 }
223 #endif
224