]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tglbtn.cpp
Remove several hack from PS code, use high resolution code from GNOME print, invert...
[wxWidgets.git] / src / gtk / tglbtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/tglbtn.cpp
3 // Purpose: Definition of the wxToggleButton class, which implements a
4 // toggle button under wxGTK.
5 // Author: John Norris, minor changes by Axel Schlueter
6 // Modified by:
7 // Created: 08.02.01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2000 Johnny C. Norris II
10 // License: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
15
16 #if wxUSE_TOGGLEBTN
17
18 #include "wx/tglbtn.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/button.h"
22 #endif
23
24 #include "wx/gtk/private.h"
25
26 extern bool g_blockEventsOnDrag;
27
28 extern "C" {
29 static void gtk_togglebutton_clicked_callback(GtkWidget *WXUNUSED(widget), wxToggleButton *cb)
30 {
31 if (!cb->m_hasVMT || g_blockEventsOnDrag)
32 return;
33
34 if (cb->m_blockEvent) return;
35
36 // Generate a wx event.
37 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
38 event.SetInt(cb->GetValue());
39 event.SetEventObject(cb);
40 cb->GetEventHandler()->ProcessEvent(event);
41 }
42 }
43
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
45
46 // ------------------------------------------------------------------------
47 // wxToggleBitmapButton
48 // ------------------------------------------------------------------------
49
50 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton, wxControl)
51
52 bool wxToggleBitmapButton::Create(wxWindow *parent, wxWindowID id,
53 const wxBitmap &label, const wxPoint &pos,
54 const wxSize &size, long style,
55 const wxValidator& validator,
56 const wxString &name)
57 {
58 m_blockEvent = false;
59
60 if (!PreCreation(parent, pos, size) ||
61 !CreateBase(parent, id, pos, size, style, validator, name ))
62 {
63 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
64 return false;
65 }
66
67 // Create the gtk widget.
68 m_widget = gtk_toggle_button_new();
69
70 if (style & wxNO_BORDER)
71 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
72
73 m_bitmap = label;
74 OnSetBitmap();
75
76 g_signal_connect (m_widget, "clicked",
77 G_CALLBACK (gtk_togglebutton_clicked_callback),
78 this);
79
80 m_parent->DoAddChild(this);
81
82 PostCreation(size);
83
84 return true;
85 }
86
87 // void SetValue(bool state)
88 // Set the value of the toggle button.
89 void wxToggleBitmapButton::SetValue(bool state)
90 {
91 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
92
93 if (state == GetValue())
94 return;
95
96 m_blockEvent = true;
97
98 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
99
100 m_blockEvent = false;
101 }
102
103 // bool GetValue() const
104 // Get the value of the toggle button.
105 bool wxToggleBitmapButton::GetValue() const
106 {
107 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
108
109 return gtk_toggle_button_get_active((GtkToggleButton*)m_widget);
110 }
111
112 void wxToggleBitmapButton::SetLabel(const wxBitmap& label)
113 {
114 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
115
116 m_bitmap = label;
117 InvalidateBestSize();
118
119 OnSetBitmap();
120 }
121
122 void wxToggleBitmapButton::OnSetBitmap()
123 {
124 if (!m_bitmap.Ok()) return;
125
126 GtkWidget* image = ((GtkBin*)m_widget)->child;
127 if (image == NULL)
128 {
129 // initial bitmap
130 image = gtk_image_new_from_pixbuf(m_bitmap.GetPixbuf());
131 gtk_widget_show(image);
132 gtk_container_add((GtkContainer*)m_widget, image);
133 }
134 else
135 { // subsequent bitmaps
136 gtk_image_set_from_pixbuf((GtkImage*)image, m_bitmap.GetPixbuf());
137 }
138 }
139
140 bool wxToggleBitmapButton::Enable(bool enable /*=true*/)
141 {
142 if (!wxControl::Enable(enable))
143 return false;
144
145 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
146
147 return true;
148 }
149
150 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle *style)
151 {
152 gtk_widget_modify_style(m_widget, style);
153 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
154 }
155
156 GdkWindow *
157 wxToggleBitmapButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
158 {
159 return GTK_BUTTON(m_widget)->event_window;
160 }
161
162 // Get the "best" size for this control.
163 wxSize wxToggleBitmapButton::DoGetBestSize() const
164 {
165 wxSize best;
166
167 if (m_bitmap.Ok())
168 {
169 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
170 best.x = m_bitmap.GetWidth()+border;
171 best.y = m_bitmap.GetHeight()+border;
172 }
173 CacheBestSize(best);
174 return best;
175 }
176
177
178 // static
179 wxVisualAttributes
180 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
181 {
182 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
183 }
184
185
186 // ------------------------------------------------------------------------
187 // wxToggleButton
188 // ------------------------------------------------------------------------
189
190 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
191
192 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
193 const wxString &label, const wxPoint &pos,
194 const wxSize &size, long style,
195 const wxValidator& validator,
196 const wxString &name)
197 {
198 m_blockEvent = false;
199
200 if (!PreCreation(parent, pos, size) ||
201 !CreateBase(parent, id, pos, size, style, validator, name ))
202 {
203 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
204 return false;
205 }
206
207 // Create the gtk widget.
208 m_widget = gtk_toggle_button_new_with_mnemonic("");
209
210 SetLabel(label);
211
212 g_signal_connect (m_widget, "clicked",
213 G_CALLBACK (gtk_togglebutton_clicked_callback),
214 this);
215
216 m_parent->DoAddChild(this);
217
218 PostCreation(size);
219
220 return true;
221 }
222
223 // void SetValue(bool state)
224 // Set the value of the toggle button.
225 void wxToggleButton::SetValue(bool state)
226 {
227 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
228
229 if (state == GetValue())
230 return;
231
232 m_blockEvent = true;
233
234 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
235
236 m_blockEvent = false;
237 }
238
239 // bool GetValue() const
240 // Get the value of the toggle button.
241 bool wxToggleButton::GetValue() const
242 {
243 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
244
245 return GTK_TOGGLE_BUTTON(m_widget)->active;
246 }
247
248 void wxToggleButton::SetLabel(const wxString& label)
249 {
250 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
251
252 wxControl::SetLabel(label);
253
254 const wxString labelGTK = GTKConvertMnemonics(label);
255
256 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
257
258 ApplyWidgetStyle( false );
259 }
260
261 bool wxToggleButton::Enable(bool enable /*=true*/)
262 {
263 if (!wxControl::Enable(enable))
264 return false;
265
266 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
267
268 return true;
269 }
270
271 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
272 {
273 gtk_widget_modify_style(m_widget, style);
274 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
275 }
276
277 GdkWindow *
278 wxToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
279 {
280 return GTK_BUTTON(m_widget)->event_window;
281 }
282
283 // Get the "best" size for this control.
284 wxSize wxToggleButton::DoGetBestSize() const
285 {
286 wxSize ret(wxControl::DoGetBestSize());
287
288 if (!HasFlag(wxBU_EXACTFIT))
289 {
290 if (ret.x < 80) ret.x = 80;
291 }
292
293 CacheBestSize(ret);
294 return ret;
295 }
296
297 // static
298 wxVisualAttributes
299 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
300 {
301 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
302 }
303
304 #endif // wxUSE_TOGGLEBTN