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