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