]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
major GUII cleanup + Baruchs patch + Angus's patch + removed a couple of generated...
[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 "minibuffer.h"
28
29 extern LyXRC lyxrc;
30 extern bool RunScript(Buffer * buffer, bool wait, string const & command,
31                       string const & orgname = string(), bool need_shell=true);
32
33
34 #ifdef CXX_WORKING_NAMESPACES
35 namespace Liason 
36 {
37 #endif
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         bool result(false);
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         BufferParams params(buffer->params);
91         if (params.orientation
92             == BufferParams::ORIENTATION_LANDSCAPE) {
93                 command += lyxrc.print_landscape_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         char real_papersize = params.papersize;
112         if (real_papersize == BufferParams::PAPER_DEFAULT) {
113                 real_papersize = lyxrc.default_papersize;
114         }
115
116         if (params.use_geometry
117             && params.papersize2 == BufferParams::VM_PAPER_CUSTOM
118             && !lyxrc.print_paper_dimension_flag.empty()
119             && !params.paperwidth.empty()
120             && !params.paperheight.empty()) {
121                 // using a custom papersize
122                 command += lyxrc.print_paper_dimension_flag + ' ';
123                 command += params.paperwidth + ',';
124                 command += params.paperheight + ' ';
125         } else if (!lyxrc.print_paper_flag.empty()
126                    && (real_papersize != BufferParams::PAPER_USLETTER
127                        || params.orientation 
128                        == BufferParams::ORIENTATION_PORTRAIT)) {
129                 // There's a problem with US Letter + landscape
130                 string paper;
131                 switch (real_papersize) {
132                 case BufferParams::PAPER_USLETTER:
133                         paper = "letter";
134                         break;
135                 case BufferParams::PAPER_A3PAPER:
136                         paper = "a3";
137                         break;
138                 case BufferParams::PAPER_A4PAPER:
139                         paper = "a4";
140                         break;
141                 case BufferParams::PAPER_A5PAPER:
142                         paper = "a5";
143                         break;
144                 case BufferParams::PAPER_B5PAPER:
145                         paper = "b5";
146                         break;
147                 case BufferParams::PAPER_EXECUTIVEPAPER:
148                         paper = "foolscap";
149                         break;
150                 case BufferParams::PAPER_LEGALPAPER:
151                         paper = "legal";
152                         break;
153                 default: /* If nothing else fits, keep empty value */
154                         break;
155                 }
156                 if (!paper.empty()) {
157                         command += lyxrc.print_paper_flag + ' ';
158                         command += paper + ' ';
159                 }
160         }
161
162         // Push directory path if necessary.
163         // PS file should go where the source file is unless it's a
164         // read-only directory in which case we write it to tmpdir.
165         // All temporary files go in tmpdir (like spool files).
166         string path = OnlyPath(buffer->fileName());
167         if ((pp.target != PrinterParams::FILE
168              && lyxrc.use_tempdir)
169             || (IsDirWriteable(path) < 1)) {
170                 path = buffer->tmppath;
171         }
172         Path p(path);
173
174         // there are three cases here:
175         // 1. we print to a file
176         // 2. we print direct to a printer
177         // 3. we print using a spool command (print to file first)
178         switch (pp.target) {
179         case PrinterParams::PRINTER:
180                 if (!lyxrc.print_spool_command.empty()) {
181                                 // case 3
182                         command += lyxrc.print_to_file
183                                 + QuoteName(pp.file_name);
184                         string command2 = lyxrc.print_spool_command
185                                 + ' ';
186                         if (!pp.printer_name.empty()) {
187                                 command2 += lyxrc.print_spool_printerprefix
188                                         + pp.printer_name;
189                         }
190                                 // First run dvips.
191                                 // If successful, then spool command
192                         if (result = RunScript(buffer, true, command)) {
193                                 result = RunScript(buffer, false,
194                                                    command2,
195                                                    QuoteName(pp.file_name));
196                         }
197                 } else {
198                                 // case 2
199                         result = RunScript(buffer, false, command);
200                 }
201                 break;
202
203         case PrinterParams::FILE:
204                 // case 1
205                 command += lyxrc.print_to_file
206                         + QuoteName(MakeAbsPath(pp.file_name, path));
207                 result = RunScript(buffer, false, command);
208                 break;
209         }
210         return result;
211 }
212
213 void setMinibuffer(LyXView * lv, char const * msg)
214 {
215         lv->getMiniBuffer()->Set(msg);
216 }
217
218 #ifdef CXX_WORKING_NAMESPACES
219 }
220 #endif
221