]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
patch from Dekel + some simplifications
[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 using std::endl;
33
34 extern LyXRC lyxrc;
35
36 #ifdef CXX_WORKING_NAMESPACES
37 namespace Liason 
38 {
39 #endif
40
41 PrinterParams getPrinterParams(Buffer * buffer)
42 {
43         return PrinterParams(PrinterParams::PRINTER,
44                              lyxrc.printer,
45                              ChangeExtension(buffer->fileName(),
46                                              lyxrc.print_file_extension));
47 }
48
49
50 bool printBuffer(Buffer * buffer, PrinterParams const & pp) 
51 {
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         if (1 < pp.count_copies) {
92                 if (pp.unsorted_copies) {
93                         command += lyxrc.print_copies_flag;
94                 } else {
95                         command += lyxrc.print_collcopies_flag;
96                 }
97                 command += ' ';
98                 command += tostr(pp.count_copies);
99                 command += ' ';
100         }
101
102         if (!lyxrc.print_extra_options.empty()) {
103                 command += lyxrc.print_extra_options + ' ';
104         }
105
106         command += Converter::dvips_options(buffer) + ' ';
107
108         if (!Exporter::Export(buffer, "dvi", true))
109                 return false;
110
111         // Push directory path.
112         string path = OnlyPath(buffer->fileName());
113         if (lyxrc.use_tempdir || (IsDirWriteable(path) < 1)) {
114                 path = buffer->tmppath;
115         }
116         Path p(path);
117
118         // there are three cases here:
119         // 1. we print to a file
120         // 2. we print direct to a printer
121         // 3. we print using a spool command (print to file first)
122         Systemcalls one;
123         int res = 0;
124         string dviname = ChangeExtension(buffer->getLatexName(true), "dvi");
125         switch (pp.target) {
126         case PrinterParams::PRINTER:
127                 if (!lyxrc.print_spool_command.empty()) {
128                         // case 3
129                         string psname = ChangeExtension(dviname, ".ps");
130                         command += lyxrc.print_to_file
131                                 + QuoteName(psname) + ' ';
132                         command += QuoteName(dviname);
133                         string command2 = lyxrc.print_spool_command + ' ';
134                         if (!pp.printer_name.empty())
135                                 command2 += lyxrc.print_spool_printerprefix
136                                         + pp.printer_name + ' ';
137                         command2 += QuoteName(psname);
138                         // First run dvips.
139                         // If successful, then spool command
140                         res = one.startscript(Systemcalls::System, command);
141                         if (res == 0)
142                                 res = one.startscript(Systemcalls::SystemDontWait,
143                                                       command2);
144                 } else
145                         // case 2
146                         res = one.startscript(Systemcalls::SystemDontWait, command);
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(Systemcalls::SystemDontWait, command);
155                 break;
156         }
157         return res == 0;
158 }
159
160 void setMinibuffer(LyXView * lv, char const * msg)
161 {
162         lv->getMiniBuffer()->Set(msg);
163 }
164
165 #ifdef CXX_WORKING_NAMESPACES
166 }
167 #endif
168