Hold a reference on m_widget for the life of the associated wxWindow object.
[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->HandleWindowEvent(event);
41 }
42 }
43
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED)
45
46 // ------------------------------------------------------------------------
47 // wxBitmapToggleButton
48 // ------------------------------------------------------------------------
49
50 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxControl)
51
52 bool wxBitmapToggleButton::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("wxBitmapToggleButton creation failed"));
64 return false;
65 }
66
67 // Create the gtk widget.
68 m_widget = gtk_toggle_button_new();
69 g_object_ref(m_widget);
70
71 if (style & wxNO_BORDER)
72 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
73
74 m_bitmap = label;
75 OnSetBitmap();
76
77 g_signal_connect (m_widget, "clicked",
78 G_CALLBACK (gtk_togglebutton_clicked_callback),
79 this);
80
81 m_parent->DoAddChild(this);
82
83 PostCreation(size);
84
85 return true;
86 }
87
88 // void SetValue(bool state)
89 // Set the value of the toggle button.
90 void wxBitmapToggleButton::SetValue(bool state)
91 {
92 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
93
94 if (state == GetValue())
95 return;
96
97 m_blockEvent = true;
98
99 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
100
101 m_blockEvent = false;
102 }
103
104 // bool GetValue() const
105 // Get the value of the toggle button.
106 bool wxBitmapToggleButton::GetValue() const
107 {
108 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
109
110 return gtk_toggle_button_get_active((GtkToggleButton*)m_widget);
111 }
112
113 void wxBitmapToggleButton::SetLabel(const wxBitmap& label)
114 {
115 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
116
117 m_bitmap = label;
118 InvalidateBestSize();
119
120 OnSetBitmap();
121 }
122
123 void wxBitmapToggleButton::OnSetBitmap()
124 {
125 if (!m_bitmap.Ok()) return;
126
127 GtkWidget* image = ((GtkBin*)m_widget)->child;
128 if (image == NULL)
129 {
130 image = gtk_image_new();
131 gtk_widget_show(image);
132 gtk_container_add((GtkContainer*)m_widget, image);
133 }
134 // always use pixbuf, because pixmap mask does not
135 // work with disabled images in some themes
136 gtk_image_set_from_pixbuf((GtkImage*)image, m_bitmap.GetPixbuf());
137 }
138
139 bool wxBitmapToggleButton::Enable(bool enable /*=true*/)
140 {
141 if (!wxControl::Enable(enable))
142 return false;
143
144 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
145
146 return true;
147 }
148
149 void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
150 {
151 gtk_widget_modify_style(m_widget, style);
152 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
153 }
154
155 GdkWindow *
156 wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
157 {
158 return GTK_BUTTON(m_widget)->event_window;
159 }
160
161 // Get the "best" size for this control.
162 wxSize wxBitmapToggleButton::DoGetBestSize() const
163 {
164 wxSize best;
165
166 if (m_bitmap.Ok())
167 {
168 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
169 best.x = m_bitmap.GetWidth()+border;
170 best.y = m_bitmap.GetHeight()+border;
171 }
172 CacheBestSize(best);
173 return best;
174 }
175
176
177 // static
178 wxVisualAttributes
179 wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
180 {
181 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
182 }
183
184
185 // ------------------------------------------------------------------------
186 // wxToggleButton
187 // ------------------------------------------------------------------------
188
189 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
190
191 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
192 const wxString &label, const wxPoint &pos,
193 const wxSize &size, long style,
194 const wxValidator& validator,
195 const wxString &name)
196 {
197 m_blockEvent = false;
198
199 if (!PreCreation(parent, pos, size) ||
200 !CreateBase(parent, id, pos, size, style, validator, name ))
201 {
202 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
203 return false;
204 }
205
206 // Create the gtk widget.
207 m_widget = gtk_toggle_button_new_with_mnemonic("");
208 g_object_ref(m_widget);
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