]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlPrint.C
bcc51336dd79a4ae97312442d5290453650f2c97
[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
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
33 #include "debug.h" // for lyxerr
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         lyx::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 /// print the current buffer
85 void ControlPrint::apply()
86 {
87         if (!bufferIsAvailable())
88                 return;
89
90         view().apply();
91
92         PrinterParams const pp = params();
93         string command(lyxrc.print_command + ' ');
94
95         if (pp.target == PrinterParams::PRINTER
96             && lyxrc.print_adapt_output  // dvips wants a printer name
97             && !pp.printer_name.empty()) {// printer name given
98                 command += lyxrc.print_to_printer
99                         + pp.printer_name
100                         + ' ';
101         }
102
103         if (!pp.all_pages && pp.from_page) {
104                 command += lyxrc.print_pagerange_flag + ' ';
105                 command += tostr(pp.from_page);
106                 if (pp.to_page) {
107                         // we have a range "from-to"
108                         command += '-'
109                                 + tostr(pp.to_page);
110                 }
111                 command += ' ';
112         }
113
114         // If both are, or both are not selected, then skip the odd/even printing
115         if (pp.odd_pages != pp.even_pages) {
116                 if (pp.odd_pages) {
117                         command += lyxrc.print_oddpage_flag + ' ';
118                 } else if (pp.even_pages) {
119                         command += lyxrc.print_evenpage_flag + ' ';
120                 }
121         }
122
123         if (pp.count_copies > 1) {
124                 if (pp.sorted_copies) {
125                         command += lyxrc.print_collcopies_flag;
126                 } else {
127                         command += lyxrc.print_copies_flag;
128                 }
129                 command += ' '
130                         + tostr(pp.count_copies)
131                         + ' ';
132         }
133
134         if (pp.reverse_order) {
135                 command += lyxrc.print_reverse_flag + ' ';
136         }
137
138         if (!lyxrc.print_extra_options.empty()) {
139                 command += lyxrc.print_extra_options + ' ';
140         }
141
142         command += converters.dvips_options(buffer()) + ' ';
143
144         if (!Exporter::Export(buffer(), "dvi", true)) {
145                 Alert::alert(_("Error:"),
146                            _("Unable to print"),
147                            _("Check that your parameters are correct"));
148                 return;
149         }
150
151         // Push directory path.
152         string path = buffer()->filePath();
153         if (lyxrc.use_tempdir || !IsDirWriteable(path)) {
154                 path = buffer()->tmppath;
155         }
156         Path p(path);
157
158         // there are three cases here:
159         // 1. we print to a file
160         // 2. we print directly to a printer
161         // 3. we print using a spool command (print to file first)
162         Systemcall one;
163         int res = 0;
164         string const dviname = ChangeExtension(buffer()->getLatexName(true), "dvi");
165         switch (pp.target) {
166         case PrinterParams::PRINTER:
167                 if (!lyxrc.print_spool_command.empty()) {
168                         // case 3: print using a spool
169                         string const psname = ChangeExtension(dviname, ".ps");
170                         command += lyxrc.print_to_file
171                                 + QuoteName(psname)
172                                 + ' '
173                                 + QuoteName(dviname);
174
175                         string command2 = lyxrc.print_spool_command + ' ';
176                         if (!pp.printer_name.empty()) {
177                                 command2 += lyxrc.print_spool_printerprefix
178                                         + pp.printer_name
179                                         + ' ';
180                         }
181                         command2 += QuoteName(psname);
182                         // First run dvips.
183                         // If successful, then spool command
184                         res = one.startscript(Systemcall::Wait, command);
185                         if (res == 0)
186                                 res = one.startscript(Systemcall::DontWait,
187                                                       command2);
188                 } else {
189                         // case 2: print directly to a printer
190                         res = one.startscript(Systemcall::DontWait,
191                                               command + QuoteName(dviname));
192                 }
193                 break;
194
195         case PrinterParams::FILE:
196                 // case 1: print to a file
197                 command += lyxrc.print_to_file
198                         + QuoteName(MakeAbsPath(pp.file_name, path))
199                         + ' '
200                         + QuoteName(dviname);
201                 res = one.startscript(Systemcall::DontWait, command);
202                 break;
203         }
204
205         lyxerr[Debug::LATEX] << "ControlPrint::apply(): print command = \""
206                              << command << '"' << endl;
207
208         if (res != 0) {
209                 Alert::alert(_("Error:"),
210                            _("Unable to print"),
211                            _("Check that your parameters are correct"));
212         }
213 }