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