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