]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
Rob's printer patch
[lyx.git] / src / frontends / Liason.C
1 /**
2  * \file Liason.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Allan Rae
7  *
8  * Full author contact details are available in file CREDITS
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 "frontends/LyXView.h"
22 #include "BufferView.h"
23 #include "buffer.h"
24 #include "exporter.h"
25 #include "converter.h"
26
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
29 #include "support/filetools.h"
30 #include "support/path.h"
31 #include "support/systemcall.h"
32
33 #include "debug.h" // for lyxerr
34
35 using std::endl;
36
37 namespace Liason {
38
39 PrinterParams getPrinterParams(Buffer * buffer)
40 {
41         return PrinterParams(PrinterParams::PRINTER,
42                              lyxrc.printer,
43                              ChangeExtension(buffer->fileName(),
44                                              lyxrc.print_file_extension));
45 }
46
47
48 bool printBuffer(Buffer * buffer, PrinterParams const & pp)
49 {
50         string command(lyxrc.print_command + ' ');
51
52         if (pp.target == PrinterParams::PRINTER
53             && lyxrc.print_adapt_output  // dvips wants a printer name
54             && !pp.printer_name.empty()) {// printer name given
55                 command += lyxrc.print_to_printer
56                         + pp.printer_name
57                         + ' ';
58         }
59
60         if (!pp.all_pages && pp.from_page) {
61                 command += lyxrc.print_pagerange_flag + ' ';
62                 command += tostr(pp.from_page);
63                 if (pp.to_page) {
64                         // we have a range "from-to"
65                         command += '-';
66                         command += tostr(pp.to_page);
67                 }
68                 command += ' ';
69         }
70
71         // If both are, or both are not selected, then skip the odd/even printing
72         if (pp.odd_pages != pp.even_pages) {
73                 if (pp.odd_pages) {
74                         command += lyxrc.print_oddpage_flag + ' ';
75                 } else if (pp.even_pages) {
76                         command += lyxrc.print_evenpage_flag + ' ';
77                 }
78         }
79
80         if (pp.count_copies > 1) {
81                 if (pp.sorted_copies) {
82                         command += lyxrc.print_collcopies_flag;
83                 } else {
84                         command += lyxrc.print_copies_flag;
85                 }
86                 command += ' ';
87                 command += tostr(pp.count_copies);
88                 command += ' ';
89         }
90         
91         if (pp.reverse_order) {
92                 command += lyxrc.print_reverse_flag + ' ';
93         }
94
95         if (!lyxrc.print_extra_options.empty()) {
96                 command += lyxrc.print_extra_options + ' ';
97         }
98
99         command += converters.dvips_options(buffer) + ' ';
100
101         if (!Exporter::Export(buffer, "dvi", true))
102                 return false;
103
104         // Push directory path.
105         string path = buffer->filePath();
106         if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
107                 path = buffer->tmppath;
108         }
109         Path p(path);
110
111         // there are three cases here:
112         // 1. we print to a file
113         // 2. we print direct to a printer
114         // 3. we print using a spool command (print to file first)
115         Systemcall one;
116         int res = 0;
117         string dviname = ChangeExtension(buffer->getLatexName(true), "dvi");
118         switch (pp.target) {
119         case PrinterParams::PRINTER:
120                 if (!lyxrc.print_spool_command.empty()) {
121                         // case 3
122                         string psname = ChangeExtension(dviname, ".ps");
123                         command += lyxrc.print_to_file
124                                 + QuoteName(psname) + ' ';
125                         command += QuoteName(dviname);
126                         string command2 = lyxrc.print_spool_command + ' ';
127                         if (!pp.printer_name.empty())
128                                 command2 += lyxrc.print_spool_printerprefix
129                                         + pp.printer_name + ' ';
130                         command2 += QuoteName(psname);
131                         // First run dvips.
132                         // If successful, then spool command
133                         res = one.startscript(Systemcall::Wait, command);
134                         if (res == 0)
135                                 res = one.startscript(Systemcall::DontWait,
136                                                       command2);
137                 } else
138                         // case 2
139                         res = one.startscript(Systemcall::DontWait,
140                                               command + QuoteName(dviname));
141                 break;
142
143         case PrinterParams::FILE:
144                 // case 1
145                 command += lyxrc.print_to_file
146                         + QuoteName(MakeAbsPath(pp.file_name, path));
147                 command += ' ' + QuoteName(dviname);
148                 res = one.startscript(Systemcall::DontWait, command);
149                 break;
150         }
151
152         lyxerr[Debug::LATEX] << "printBuffer: \"" << command << "\"\n";
153
154         return res == 0;
155 }
156
157
158 void setMinibuffer(LyXView * lv, string const & msg)
159 {
160         lyx::Assert(lv);
161         lv->message(msg);
162 }
163
164 } // namespace Liason