]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GPrint.C
54ec6849a0fbf9509b7859cbd20ee6818ee7e07a
[lyx.git] / src / frontends / gtk / GPrint.C
1 /**
2  * \file GPrint.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Huang Ying
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 // Too hard to make concept checks work with this file
14 #ifdef _GLIBCPP_CONCEPT_CHECKS
15 #undef _GLIBCPP_CONCEPT_CHECKS
16 #endif
17
18 #include "GPrint.h"
19 #include "ControlPrint.h"
20 #include "ghelpers.h"
21
22 #include "PrinterParams.h"
23
24 #include "support/lstrings.h"
25 #include "support/convert.h"
26
27 #include <libglademm.h>
28
29 using std::string;
30 using namespace lyx::support;
31
32 namespace lyx {
33 namespace frontend {
34
35 GPrint::GPrint(Dialog & parent, string const & title)
36         : GViewCB<ControlPrint, GViewGladeB>(parent, title, false)
37 {
38 }
39
40
41 void GPrint::apply()
42 {
43         PrinterParams pp;
44         pp.target = printer_->get_active() ? PrinterParams::PRINTER : PrinterParams::FILE;
45         pp.printer_name = printerEntry_->get_text();
46         pp.file_name = fileEntry_->get_text();
47         pp.all_pages = all_->get_active();
48         pp.from_page = pp.to_page = 0;
49         if (!fromEntry_->get_text().empty()) {
50                 pp.from_page = strToInt(fromEntry_->get_text());
51                 if (!toEntry_->get_text().empty())
52                         pp.to_page = strToInt(toEntry_->get_text());
53         }
54         pp.odd_pages = odd_->get_active();
55         pp.even_pages = even_->get_active();
56         pp.count_copies = number_->get_value_as_int();
57         pp.sorted_copies = sorted_->get_active();
58         pp.reverse_order = reverse_->get_active();
59         controller().params() = pp;
60 }
61
62
63 void GPrint::update()
64 {
65         PrinterParams & pp = controller().params();
66         printer_->set_active(pp.target == PrinterParams::PRINTER);
67         printerEntry_->set_text(pp.printer_name);
68         fileEntry_->set_text(pp.file_name);
69         all_->set_active(pp.all_pages);
70
71         string const from = ( pp.from_page ? convert<string>(pp.from_page) : string() );
72         string const to   = ( pp.to_page   ? convert<string>(pp.to_page)   : string() );
73         fromEntry_->set_text(from);
74         toEntry_->set_text(to);
75         odd_->set_active(pp.odd_pages);
76         even_->set_active(pp.even_pages);
77         reverse_->set_active(pp.reverse_order);
78         sorted_->set_active(pp.sorted_copies);
79         number_->set_value(pp.count_copies);
80         bool const enable_counter = pp.target == PrinterParams::PRINTER;
81         number_->set_sensitive(enable_counter);
82         sorted_->set_sensitive(enable_counter && pp.count_copies > 1);
83 }
84
85
86 void GPrint::updateUI()
87 {
88         ButtonPolicy::SMInput activate = ButtonPolicy::SMI_VALID;
89         // disable OK/Apply buttons when file output is selected, but no file name entered
90         if (file_->get_active() && fileEntry_->get_text().empty())
91                 activate = ButtonPolicy::SMI_INVALID;
92         // check 'from' and 'to' fields only when 'from/to' radio button is selected
93         if (fromTo_->get_active()) {
94                 string from = fromEntry_->get_text();
95                 string to = toEntry_->get_text();
96                 if (from.empty() || (!to.empty() && strToInt(from) > strToInt(to)))
97                         activate = ButtonPolicy::SMI_INVALID;
98         }
99         bool const enableCounter = printer_->get_active();
100         number_->set_sensitive(enableCounter);
101         bool const enableSorted = enableCounter && number_->get_value_as_int() > 1;
102         sorted_->set_sensitive(enableSorted);
103         bc().input(activate);
104 }
105
106
107 void GPrint::onBrowse()
108 {
109         string const inName = fileEntry_->get_text();
110         string const outName = Glib::locale_to_utf8(controller().browse(Glib::locale_from_utf8(inName)));
111         if (outName != inName && !outName.empty())
112                 fileEntry_->set_text(outName);
113         if (!outName.empty())
114                 file_->set_active(true);
115         updateUI();
116 }
117
118
119 void GPrint::onTargetEdit(Gtk::Entry const * who)
120 {
121         if (who == fileEntry_)
122                 file_->set_active(true);
123         else if (who == printerEntry_)
124                 printer_->set_active(true);
125         updateUI();
126 }
127
128
129 void GPrint::onFromToEdit()
130 {
131         fromTo_->set_active(true);
132         updateUI();
133 }
134
135
136 void GPrint::doBuild()
137 {
138         string const gladeName = findGladeFile("print");
139         xml_ = Gnome::Glade::Xml::create(gladeName);
140         xml_->get_widget("Printer", printer_);
141         xml_->get_widget("File", file_);
142         xml_->get_widget("All", all_);
143         xml_->get_widget("FromTo", fromTo_);
144         xml_->get_widget("Odd", odd_);
145         xml_->get_widget("Even", even_);
146         xml_->get_widget("Reverse", reverse_);
147         xml_->get_widget("Number", number_);
148         xml_->get_widget("Collate", sorted_);
149         xml_->get_widget("FromEntry", fromEntry_);
150         xml_->get_widget("ToEntry", toEntry_);
151         xml_->get_widget("PrinterEntry", printerEntry_);
152         xml_->get_widget("FileEntry", fileEntry_);
153
154         Gtk::Button * ok;
155         Gtk::Button * cancel;
156         xml_->get_widget("PrintButton", ok);
157         xml_->get_widget("CancelButton", cancel);
158
159         setOK(ok);
160         setCancel(cancel);
161
162         Gtk::Button * browse;
163         xml_->get_widget("Browse", browse);
164         browse->signal_clicked().connect(sigc::mem_fun(*this, &GPrint::onBrowse));
165
166         fileEntry_->signal_changed().connect(sigc::bind(sigc::mem_fun(*this, &GPrint::onTargetEdit), fileEntry_));
167         printerEntry_->signal_changed().connect(sigc::bind(sigc::mem_fun(*this, &GPrint::onTargetEdit), printerEntry_));
168         fromEntry_->signal_changed().connect(sigc::mem_fun(*this, &GPrint::onFromToEdit));
169         toEntry_->signal_changed().connect(sigc::mem_fun(*this, &GPrint::onFromToEdit));
170         printer_->signal_toggled().connect(sigc::mem_fun(*this, &GPrint::updateUI));
171         file_->signal_toggled().connect(sigc::mem_fun(*this, &GPrint::updateUI));
172         all_->signal_toggled().connect(sigc::mem_fun(*this, &GPrint::updateUI));
173         fromTo_->signal_toggled().connect(sigc::mem_fun(*this, &GPrint::updateUI));
174         number_->signal_changed().connect(sigc::mem_fun(*this, &GPrint::updateUI));
175
176         controller().initialiseParams("");
177         update();
178         updateUI();
179 }
180
181 } // namespace frontend
182 } // namespace lyx