]> git.lyx.org Git - lyx.git/blob - src/frontends/Liason.C
7da6ed7da46c01cae2c1d0f6ae12096c87da1544
[lyx.git] / src / frontends / Liason.C
1 #include <config.h>
2
3 //#include "config.h"
4 #include "Liason.h"
5 #include "BufferView.h"
6 #include "buffer.h"
7 #include "lyxrc.h"
8 #include "PrinterParams.h"
9 #include "lyx_gui_misc.h"
10 #include "support/lstrings.h"
11 #include "support/filetools.h"
12 #include "support/path.h"
13
14 extern LyXRC lyxrc;
15 extern bool RunScript(Buffer * buffer, bool wait, string const & command,
16                       string const & orgname = string(), bool need_shell=true);
17
18
19 #ifdef CXX_WORKING_NAMESPACES
20 namespace Liason 
21 {
22 #endif
23
24 PrinterParams getPrinterParams(Buffer * buffer)
25 {
26         return PrinterParams(PrinterParams::PRINTER,
27                              lyxrc.printer,
28                              ChangeExtension(buffer->fileName(),
29                                              lyxrc.print_file_extension));
30 }
31
32
33 bool printBuffer(Buffer * buffer, PrinterParams const & pp) 
34 {
35         bool result(false);
36         string command(lyxrc.print_command + ' ');
37         
38         if (pp.target == PrinterParams::PRINTER
39             && lyxrc.print_adapt_output  // dvips wants a printer name
40             && !pp.printer_name.empty()) {// printer name given
41                 command += lyxrc.print_to_printer
42                         + pp.printer_name
43                         + ' ';
44         }
45
46         switch (pp.which_pages) {
47         case PrinterParams::EVEN:
48                 command += lyxrc.print_evenpage_flag + ' ';
49                 break;
50
51         case PrinterParams::ODD:
52                 command += lyxrc.print_oddpage_flag + ' ';
53                 break;
54     
55         default:
56                 // only option left is print all of them
57                 break;
58         }
59
60         if (!pp.from_page.empty()) {
61                 command += lyxrc.print_pagerange_flag + ' ';
62                 command += 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 (pp.reverse_order) {
72                 command += lyxrc.print_reverse_flag + ' ';
73         }
74
75         BufferParams params(buffer->params);
76         if (params.orientation
77             == BufferParams::ORIENTATION_LANDSCAPE) {
78                 command += lyxrc.print_landscape_flag + ' ';
79         }
80
81         if (1 < pp.count_copies) {
82                 if (pp.unsorted_copies) {
83                         command += lyxrc.print_copies_flag;
84                 } else {
85                         command += lyxrc.print_collcopies_flag;
86                 }
87                 command += ' ';
88                 command += tostr(pp.count_copies);
89                 command += ' ';
90         }
91
92         if (!lyxrc.print_extra_options.empty()) {
93                 command += lyxrc.print_extra_options + ' ';
94         }
95
96         char real_papersize = params.papersize;
97         if (real_papersize == BufferParams::PAPER_DEFAULT) {
98                 real_papersize = lyxrc.default_papersize;
99         }
100
101         if (params.use_geometry
102             && params.papersize2 == BufferParams::VM_PAPER_CUSTOM
103             && !lyxrc.print_paper_dimension_flag.empty()
104             && !params.paperwidth.empty()
105             && !params.paperheight.empty()) {
106                 // using a custom papersize
107                 command += lyxrc.print_paper_dimension_flag + ' ';
108                 command += params.paperwidth + ',';
109                 command += params.paperheight + ' ';
110         } else if (!lyxrc.print_paper_flag.empty()
111                    && (real_papersize != BufferParams::PAPER_USLETTER
112                        || params.orientation 
113                        == BufferParams::ORIENTATION_PORTRAIT)) {
114                 // There's a problem with US Letter + landscape
115                 string paper;
116                 switch (real_papersize) {
117                 case BufferParams::PAPER_USLETTER:
118                         paper = "letter";
119                         break;
120                 case BufferParams::PAPER_A3PAPER:
121                         paper = "a3";
122                         break;
123                 case BufferParams::PAPER_A4PAPER:
124                         paper = "a4";
125                         break;
126                 case BufferParams::PAPER_A5PAPER:
127                         paper = "a5";
128                         break;
129                 case BufferParams::PAPER_B5PAPER:
130                         paper = "b5";
131                         break;
132                 case BufferParams::PAPER_EXECUTIVEPAPER:
133                         paper = "foolscap";
134                         break;
135                 case BufferParams::PAPER_LEGALPAPER:
136                         paper = "legal";
137                         break;
138                 default: /* If nothing else fits, keep empty value */
139                         break;
140                 }
141                 if (!paper.empty()) {
142                         command += lyxrc.print_paper_flag + ' ';
143                         command += paper + ' ';
144                 }
145         }
146
147         // Push directory path if necessary.
148         // PS file should go where the source file is unless it's a
149         // read-only directory in which case we write it to tmpdir.
150         // All temporary files go in tmpdir (like spool files).
151         string path = OnlyPath(buffer->fileName());
152         if ((pp.target != PrinterParams::FILE
153              && lyxrc.use_tempdir)
154             || (IsDirWriteable(path) < 1)) {
155                 path = buffer->tmppath;
156         }
157         Path p(path);
158
159         // there are three cases here:
160         // 1. we print to a file
161         // 2. we print direct to a printer
162         // 3. we print using a spool command (print to file first)
163         switch (pp.target) {
164         case PrinterParams::PRINTER:
165                 if (!lyxrc.print_spool_command.empty()) {
166                                 // case 3
167                         command += lyxrc.print_to_file
168                                 + QuoteName(pp.file_name);
169                         string command2 = lyxrc.print_spool_command
170                                 + ' ';
171                         if (!pp.printer_name.empty()) {
172                                 command2 += lyxrc.print_spool_printerprefix
173                                         + pp.printer_name;
174                         }
175                                 // First run dvips.
176                                 // If successful, then spool command
177                         if (result = RunScript(buffer, true, command)) {
178                                 result = RunScript(buffer, false,
179                                                    command2,
180                                                    QuoteName(pp.file_name));
181                         }
182                 } else {
183                                 // case 2
184                         result = RunScript(buffer, false, command);
185                 }
186                 break;
187
188         case PrinterParams::FILE:
189                 // case 1
190                 command += lyxrc.print_to_file
191                         + QuoteName(MakeAbsPath(pp.file_name, path));
192                 result = RunScript(buffer, false, command);
193                 break;
194         }
195         return result;
196 }
197
198 #ifdef CXX_WORKING_NAMESPACES
199 }
200 #endif
201