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