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