]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
Martin's Angstrom patch.
[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-2001 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 "lyxrc.h"
19 #include "PrinterParams.h"
20
21 #include "LyXView.h"
22 #include "BufferView.h"
23 #include "buffer.h"
24 #include "exporter.h"
25 #include "converter.h"
26 #include "lyx_gui_misc.h"
27
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
30 #include "support/filetools.h"
31 #include "support/path.h"
32 #include "support/systemcall.h"
33
34 using std::endl;
35
36 extern LyXRC lyxrc;
37
38 namespace Liason {
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         string command(lyxrc.print_command + ' ');
52         
53         if (pp.target == PrinterParams::PRINTER
54             && lyxrc.print_adapt_output  // dvips wants a printer name
55             && !pp.printer_name.empty()) {// printer name given
56                 command += lyxrc.print_to_printer
57                         + pp.printer_name
58                         + ' ';
59         }
60
61         switch (pp.which_pages) {
62         case PrinterParams::EVEN:
63                 command += lyxrc.print_evenpage_flag + ' ';
64                 break;
65
66         case PrinterParams::ODD:
67                 command += lyxrc.print_oddpage_flag + ' ';
68                 break;
69     
70         default:
71                 // only option left is print all of them
72                 break;
73         }
74
75         if (!pp.from_page.empty()) {
76                 command += lyxrc.print_pagerange_flag + ' ';
77                 command += pp.from_page;
78                 if (pp.to_page) {
79                                 // we have a range "from-to"
80                         command += '-';
81                         command += tostr(pp.to_page);
82                 }
83                 command += ' ';
84         }
85
86         if (pp.reverse_order) {
87                 command += lyxrc.print_reverse_flag + ' ';
88         }
89
90         if (1 < pp.count_copies) {
91                 if (pp.unsorted_copies) {
92                         command += lyxrc.print_copies_flag;
93                 } else {
94                         command += lyxrc.print_collcopies_flag;
95                 }
96                 command += ' ';
97                 command += tostr(pp.count_copies);
98                 command += ' ';
99         }
100
101         if (!lyxrc.print_extra_options.empty()) {
102                 command += lyxrc.print_extra_options + ' ';
103         }
104
105         command += converters.dvips_options(buffer) + ' ';
106
107         if (!Exporter::Export(buffer, "dvi", true))
108                 return false;
109
110         // Push directory path.
111         string path = buffer->filePath();
112         if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
113                 path = buffer->tmppath;
114         }
115         Path p(path);
116
117         // there are three cases here:
118         // 1. we print to a file
119         // 2. we print direct to a printer
120         // 3. we print using a spool command (print to file first)
121         Systemcall one;
122         int res = 0;
123         string dviname = ChangeExtension(buffer->getLatexName(true), "dvi");
124         switch (pp.target) {
125         case PrinterParams::PRINTER:
126                 if (!lyxrc.print_spool_command.empty()) {
127                         // case 3
128                         string psname = ChangeExtension(dviname, ".ps");
129                         command += lyxrc.print_to_file
130                                 + QuoteName(psname) + ' ';
131                         command += QuoteName(dviname);
132                         string command2 = lyxrc.print_spool_command + ' ';
133                         if (!pp.printer_name.empty())
134                                 command2 += lyxrc.print_spool_printerprefix
135                                         + pp.printer_name + ' ';
136                         command2 += QuoteName(psname);
137                         // First run dvips.
138                         // If successful, then spool command
139                         res = one.startscript(Systemcall::Wait, command);
140                         if (res == 0)
141                                 res = one.startscript(Systemcall::DontWait,
142                                                       command2);
143                 } else
144                         // case 2
145                         res = one.startscript(Systemcall::DontWait,
146                                               command + QuoteName(dviname));
147                 break;
148
149         case PrinterParams::FILE:
150                 // case 1
151                 command += lyxrc.print_to_file
152                         + QuoteName(MakeAbsPath(pp.file_name, path));
153                 command += ' ' + QuoteName(dviname);
154                 res = one.startscript(Systemcall::DontWait, command);
155                 break;
156         }
157         return res == 0;
158 }
159
160
161 void setMinibuffer(LyXView * lv, string const & msg)
162 {
163         lyx::Assert(lv);
164         lv->message(msg);
165 }
166
167 } // namespace Liason