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