]> git.lyx.org Git - lyx.git/blob - src/frontends/gnome/FormPrint.C
Clean-up of the button controller.
[lyx.git] / src / frontends / gnome / FormPrint.C
1 /* FormPrint.C
2  * FormPrint Interface Class Implementation
3  * This file is part of
4  * ====================================================== 
5  *
6  *           LyX, The Document Processor
7  *
8  *           Copyright 2000 The LyX Team.
9  *
10  * ======================================================
11  */
12
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "FormPrint.h"
21 #include "LyXView.h"
22 #include "Dialogs.h"
23 #include "lyxrc.h"
24 #include "PrinterParams.h"
25 #include "Liason.h"
26 #include "debug.h"
27 #include "BufferView.h"
28 #include "lyx_gui_misc.h"
29 #include "gettext.h"
30
31 extern "C" {
32 #include "diaprint_interface.h"
33 #include "support.h"
34 }
35
36 #include <gtk--/base.h>
37 #include <gtk--/button.h>
38
39 using Liason::printBuffer;
40 using Liason::getPrinterParams;
41
42 FormPrint::FormPrint(LyXView * lv, Dialogs * d)
43   : dialog_(0), lv_(lv), d_(d), u_(0), h_(0)
44 {
45   // let the dialog be shown
46   // This is a permanent connection so we won't bother
47   // storing a copy because we won't be disconnecting.
48   d->showPrint.connect(SigC::slot(this,&FormPrint::show));
49 }
50
51
52 FormPrint::~FormPrint()
53 {
54   hide();
55 }
56
57
58 void FormPrint::show()
59 {
60   Gtk::Button * b_ok;
61   Gtk::Button * b_cancel;
62   if (!dialog_)
63     {
64       GtkWidget * pd = create_DiaPrint();
65
66       dialog_ = Gtk::wrap(pd);
67       print_all_  = Gtk::wrap( GTK_RADIO_BUTTON( lookup_widget(pd, "print_all") ) );
68       print_odd_  = Gtk::wrap( GTK_RADIO_BUTTON( lookup_widget(pd, "print_odd") ) );
69       print_even_ = Gtk::wrap( GTK_RADIO_BUTTON( lookup_widget(pd, "print_even") ) );
70
71       print_pages_ = Gtk::wrap( GTK_CHECK_BUTTON( lookup_widget(pd, "print_pages") ) );
72
73       print_from_ = Gtk::wrap( GTK_SPIN_BUTTON( lookup_widget(pd, "print_from") ) );
74       print_to_ = Gtk::wrap( GTK_SPIN_BUTTON( lookup_widget(pd, "print_to") ) );
75
76       order_normal_ = Gtk::wrap( GTK_RADIO_BUTTON( lookup_widget(pd, "order_normal") ) );
77       order_reverse_ = Gtk::wrap( GTK_RADIO_BUTTON( lookup_widget(pd, "order_reverse") ) );
78
79       copies_unsorted_ = Gtk::wrap( GTK_CHECK_BUTTON( lookup_widget(pd, "copies_unsorted") ) );
80       copies_count_ = Gtk::wrap( GTK_SPIN_BUTTON( lookup_widget(pd, "copies_count") ) );
81
82       printto_printer_ = Gtk::wrap( GTK_RADIO_BUTTON( lookup_widget(pd, "printto_printer") ) );
83       printto_file_ = Gtk::wrap( GTK_RADIO_BUTTON( lookup_widget(pd, "printto_file") ) );
84
85       printto_printcommand_ = Gtk::wrap( GNOME_ENTRY( lookup_widget(pd, "printto_printcommand") ) );
86       printto_fileentry_ = Gtk::wrap( GNOME_FILE_ENTRY( lookup_widget(pd, "printto_fileentry") ) );
87
88       b_ok = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_print") ) );
89       b_cancel = Gtk::wrap( GTK_BUTTON( lookup_widget(pd, "button_cancel") ) );
90
91       // setting up connections
92       b_ok->clicked.connect(SigC::slot(this, &FormPrint::apply));
93       b_ok->clicked.connect(dialog_->destroy.slot());
94       b_cancel->clicked.connect(dialog_->destroy.slot());
95       dialog_->destroy.connect(SigC::slot(this, &FormPrint::free));
96
97       u_ = d_->updateBufferDependent.connect(SigC::slot(this,
98                                                   &FormPrint::updateSlot));
99       h_ = d_->hideBufferDependent.connect(dialog_->destroy.slot());
100
101       if (!dialog_->is_visible()) dialog_->show_all();
102
103       updateSlot();  // make sure its up-to-date
104     }
105   else
106     {
107       Gdk_Window dialog_win(dialog_->get_window());
108       dialog_win.raise();
109     }
110 }
111
112 void FormPrint::hide()
113 {
114   if (dialog_!=0) dialog_->destroy();
115 }
116
117 void FormPrint::free()
118 {
119   if (dialog_!=0)
120     {
121       dialog_ = 0;
122       u_.disconnect();
123       h_.disconnect();
124     }
125 }
126
127
128 void FormPrint::apply()
129 {
130   if (!lv_->view()->available()) return;
131
132   PrinterParams::WhichPages wp(PrinterParams::ALL);
133   if (print_odd_->get_active()) wp = PrinterParams::ODD;
134   else if (print_even_->get_active()) wp = PrinterParams::EVEN;
135
136   string from;
137   int to(0);
138   if (print_pages_->get_active())
139     {
140       from = print_from_->get_text();
141       to = print_to_->get_value_as_int();
142     }
143   
144   PrinterParams::Target t(PrinterParams::PRINTER);
145   if (printto_file_->get_active()) t = PrinterParams::FILE;
146   
147   // we really should use the return value here I think.
148   if (!printBuffer(lv_->buffer(),
149                    PrinterParams(t,
150                                  printto_printcommand_->get_entry()->get_text(),
151                                  printto_fileentry_->get_full_path(false),
152                                  wp, from, to,
153                                  order_reverse_->get_active(),
154                                  copies_unsorted_->get_active(),
155                                  copies_count_->get_value_as_int())))
156     {
157       WriteAlert(_("Error:"),
158                  _("Unable to print"),
159                  _("Check that your parameters are correct"));
160     }
161 }
162
163
164 // we can safely ignore the parameter because we can always update
165 void FormPrint::updateSlot(bool)
166 {
167   if (dialog_ != 0 &&
168       lv_->view()->available())
169     {
170       PrinterParams pp(getPrinterParams(lv_->buffer()));
171
172       printto_printcommand_->get_entry()->set_text( pp.printer_name.c_str() );
173       ((Gtk::Entry *)printto_fileentry_->gtk_entry())->set_text(pp.file_name.c_str());
174
175       if (pp.target == PrinterParams::PRINTER) printto_printer_->set_active(true);
176       else printto_file_->set_active(true);
177
178       if (pp.reverse_order) order_reverse_->set_active(true);
179       else order_normal_->set_active(true);
180
181       switch (pp.which_pages)
182         {
183         case PrinterParams::ODD:
184           print_odd_->set_active(true);
185           break;
186           
187         case PrinterParams::EVEN:
188           print_even_->set_active(true);
189           break;
190           
191         case PrinterParams::ALL:
192         default:
193           print_all_->set_active(true);
194           break;
195         }
196       
197       // hmmm... maybe a bit weird but maybe not
198       // we might just be remembering the last
199       // time this was printed.
200       if (!pp.from_page.empty())
201         {
202           print_to_->set_value(pp.to_page);
203           print_from_->set_value(strToInt(pp.from_page));
204         }
205     }
206 }