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