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