]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlPrint.C
f848c79df4a6a7c82b18f29ae30b3eeac9b99da0
[lyx.git] / src / frontends / controllers / ControlPrint.C
1 /**
2  * \file ControlPrint.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlPrint.h"
14
15 #include "ViewBase.h"
16 #include "ButtonController.h"
17
18 #include "buffer.h"
19 #include "gettext.h"
20 #include "helper_funcs.h"
21 #include "PrinterParams.h"
22 #include "exporter.h"
23
24 #include "frontends/Alert.h"
25
26 #include "support/tostr.h"
27 #include "support/filetools.h"
28 #include "support/path.h"
29 #include "support/systemcall.h"
30
31 #include "debug.h"
32
33 using namespace lyx::support;
34
35 using std::endl;
36
37 ControlPrint::ControlPrint(LyXView & lv, Dialogs & d)
38         : ControlDialogBD(lv, d),
39           params_(0)
40 {}
41
42
43 PrinterParams & ControlPrint::params() const
44 {
45         Assert(params_);
46         return *params_;
47 }
48
49
50 void ControlPrint::setParams()
51 {
52         if (params_) delete params_;
53
54         /// get global printer parameters
55         string const name =  ChangeExtension(buffer()->fileName(),
56                                         lyxrc.print_file_extension);
57         params_ = new PrinterParams (PrinterParams::PRINTER,
58                                         lyxrc.printer, name);
59
60         bc().valid(); // so that the user can press Ok
61 }
62
63
64 void ControlPrint::clearParams()
65 {
66         if (params_) {
67                 delete params_;
68                 params_ = 0;
69         }
70 }
71
72
73 string const ControlPrint::Browse(string const & in_name)
74 {
75         string const title = _("Print to file");
76         string const pattern = "*.ps";
77
78         // Show the file browser dialog
79         return browseRelFile(in_name, buffer()->filePath(),
80                              title, pattern, true);
81 }
82
83
84 namespace {
85
86 void showPrintError(string const & name)
87 {
88                 string str = bformat(_("Could not print the document %1$s.\n"
89                         "Check that your printer is set up correctly."),
90                         MakeDisplayPath(name, 50));
91                 Alert::error(_("Print document failed"), str);
92 }
93
94 }
95
96
97 /// print the current buffer
98 void ControlPrint::apply()
99 {
100         if (!bufferIsAvailable())
101                 return;
102
103         view().apply();
104
105         PrinterParams const pp = params();
106         string command(lyxrc.print_command + ' ');
107
108         if (pp.target == PrinterParams::PRINTER
109             && lyxrc.print_adapt_output  // dvips wants a printer name
110             && !pp.printer_name.empty()) {// printer name given
111                 command += lyxrc.print_to_printer
112                         + pp.printer_name
113                         + ' ';
114         }
115
116         if (!pp.all_pages && pp.from_page) {
117                 command += lyxrc.print_pagerange_flag + ' ';
118                 command += tostr(pp.from_page);
119                 if (pp.to_page) {
120                         // we have a range "from-to"
121                         command += '-'
122                                 + tostr(pp.to_page);
123                 }
124                 command += ' ';
125         }
126
127         // If both are, or both are not selected, then skip the odd/even printing
128         if (pp.odd_pages != pp.even_pages) {
129                 if (pp.odd_pages) {
130                         command += lyxrc.print_oddpage_flag + ' ';
131                 } else if (pp.even_pages) {
132                         command += lyxrc.print_evenpage_flag + ' ';
133                 }
134         }
135
136         if (pp.count_copies > 1) {
137                 if (pp.sorted_copies) {
138                         command += lyxrc.print_collcopies_flag;
139                 } else {
140                         command += lyxrc.print_copies_flag;
141                 }
142                 command += ' '
143                         + tostr(pp.count_copies)
144                         + ' ';
145         }
146
147         if (pp.reverse_order) {
148                 command += lyxrc.print_reverse_flag + ' ';
149         }
150
151         if (!lyxrc.print_extra_options.empty()) {
152                 command += lyxrc.print_extra_options + ' ';
153         }
154
155         command += buffer()->params.dvips_options() + ' ';
156
157         if (!Exporter::Export(buffer(), "dvi", true)) {
158                 showPrintError(buffer()->fileName());
159                 return;
160         }
161
162         // Push directory path.
163         string path = buffer()->filePath();
164         if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
165                 path = buffer()->tmppath;
166         }
167         Path p(path);
168
169         // there are three cases here:
170         // 1. we print to a file
171         // 2. we print directly to a printer
172         // 3. we print using a spool command (print to file first)
173         Systemcall one;
174         int res = 0;
175         string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
176         switch (pp.target) {
177         case PrinterParams::PRINTER:
178                 if (!lyxrc.print_spool_command.empty()) {
179                         // case 3: print using a spool
180                         string const psname = ChangeExtension(dviname, ".ps");
181                         command += lyxrc.print_to_file
182                                 + QuoteName(psname)
183                                 + ' '
184                                 + QuoteName(dviname);
185
186                         string command2 = lyxrc.print_spool_command + ' ';
187                         if (!pp.printer_name.empty()) {
188                                 command2 += lyxrc.print_spool_printerprefix
189                                         + pp.printer_name
190                                         + ' ';
191                         }
192                         command2 += QuoteName(psname);
193                         // First run dvips.
194                         // If successful, then spool command
195                         res = one.startscript(Systemcall::Wait, command);
196                         if (res == 0)
197                                 res = one.startscript(Systemcall::DontWait,
198                                                       command2);
199                 } else {
200                         // case 2: print directly to a printer
201                         res = one.startscript(Systemcall::DontWait,
202                                               command + QuoteName(dviname));
203                 }
204                 break;
205
206         case PrinterParams::FILE:
207                 // case 1: print to a file
208                 command += lyxrc.print_to_file
209                         + QuoteName(MakeAbsPath(pp.file_name, path))
210                         + ' '
211                         + QuoteName(dviname);
212                 res = one.startscript(Systemcall::DontWait, command);
213                 break;
214         }
215
216         lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
217                              << command << '"' << endl;
218
219         if (res != 0)
220                 showPrintError(buffer()->fileName());
221 }