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