]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlPrint.C
5 new lfuns, move all apply code out of ControlDocument and into the core.
[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 "ButtonController.h"
16 #include "helper_funcs.h"
17
18 #include "buffer.h"
19 #include "bufferparams.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "PrinterParams.h"
23
24 #include "support/tostr.h"
25 #include "support/filetools.h"
26 #include "support/globbing.h"
27
28 using lyx::support::ChangeExtension;
29 using lyx::support::FileFilterList;
30
31 using std::string;
32
33 ControlPrint::ControlPrint(Dialog & parent)
34         : Dialog::Controller(parent),
35           params_(0)
36 {}
37
38
39 bool ControlPrint::initialiseParams(std::string const &)
40 {
41         /// get global printer parameters
42         string const name =  ChangeExtension(kernel().buffer().fileName(),
43                                         lyxrc.print_file_extension);
44         params_.reset(new PrinterParams (PrinterParams::PRINTER,
45                                          lyxrc.printer, name));
46
47         dialog().bc().valid(); // so that the user can press Ok
48         return true;
49 }
50
51
52 void ControlPrint::clearParams()
53 {
54         params_.reset();
55 }
56
57
58 PrinterParams & ControlPrint::params() const
59 {
60         BOOST_ASSERT(params_.get());
61         return *params_;
62 }
63
64
65 string const ControlPrint::browse(string const & in_name) const
66 {
67         return browseRelFile(in_name, kernel().buffer().filePath(),
68                              _("Print to file"),
69                              FileFilterList("PostScript files (*.ps)"),
70                              true);
71 }
72
73
74 /// print the current buffer
75 void ControlPrint::dispatchParams()
76 {
77         PrinterParams const pp = params();
78         string command(lyxrc.print_command + ' ');
79
80         if (pp.target == PrinterParams::PRINTER
81             && lyxrc.print_adapt_output  // dvips wants a printer name
82             && !pp.printer_name.empty()) {// printer name given
83                 command += lyxrc.print_to_printer
84                         + pp.printer_name
85                         + ' ';
86         }
87
88         if (!pp.all_pages && pp.from_page) {
89                 command += lyxrc.print_pagerange_flag + ' ';
90                 command += tostr(pp.from_page);
91                 if (pp.to_page) {
92                         // we have a range "from-to"
93                         command += '-'
94                                 + tostr(pp.to_page);
95                 }
96                 command += ' ';
97         }
98
99         // If both are, or both are not selected, then skip the odd/even printing
100         if (pp.odd_pages != pp.even_pages) {
101                 if (pp.odd_pages) {
102                         command += lyxrc.print_oddpage_flag + ' ';
103                 } else if (pp.even_pages) {
104                         command += lyxrc.print_evenpage_flag + ' ';
105                 }
106         }
107
108         if (pp.count_copies > 1) {
109                 if (pp.sorted_copies) {
110                         command += lyxrc.print_collcopies_flag;
111                 } else {
112                         command += lyxrc.print_copies_flag;
113                 }
114                 command += ' '
115                         + tostr(pp.count_copies)
116                         + ' ';
117         }
118
119         if (pp.reverse_order) {
120                 command += lyxrc.print_reverse_flag + ' ';
121         }
122
123         if (!lyxrc.print_extra_options.empty()) {
124                 command += lyxrc.print_extra_options + ' ';
125         }
126
127         command += kernel().buffer().params().dvips_options() + ' ';
128
129         string const target = (pp.target == PrinterParams::PRINTER) ?
130                 "printer" : "file";
131         
132         string const target_name = (pp.target == PrinterParams::PRINTER) ?
133                 (pp.printer_name.empty() ? "default" : pp.printer_name) :
134                 pp.file_name;
135
136         string const data = target + " " + target_name + " " + command;
137         kernel().dispatch(FuncRequest(LFUN_PRINT, data));
138 }