]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/tglbtn.cpp
fixing iterator comparison
[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 // Licence: 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 // Generate a wx event.
35 wxCommandEvent event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, cb->GetId());
36 event.SetInt(cb->GetValue());
37 event.SetEventObject(cb);
38 cb->HandleWindowEvent(event);
39 }
40 }
41
42 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, wxCommandEvent );
43
44 // ------------------------------------------------------------------------
45 // wxBitmapToggleButton
46 // ------------------------------------------------------------------------
47
48 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton, wxControl)
49
50 bool wxBitmapToggleButton::Create(wxWindow *parent, wxWindowID id,
51 const wxBitmap &label, const wxPoint &pos,
52 const wxSize &size, long style,
53 const wxValidator& validator,
54 const wxString &name)
55 {
56 if (!PreCreation(parent, pos, size) ||
57 !CreateBase(parent, id, pos, size, style, validator, name ))
58 {
59 wxFAIL_MSG(wxT("wxBitmapToggleButton creation failed"));
60 return false;
61 }
62
63 // Create the gtk widget.
64 m_widget = gtk_toggle_button_new();
65 g_object_ref(m_widget);
66
67 if (style & wxNO_BORDER)
68 gtk_button_set_relief( GTK_BUTTON(m_widget), GTK_RELIEF_NONE );
69
70 m_bitmap = label;
71 OnSetBitmap();
72
73 g_signal_connect (m_widget, "clicked",
74 G_CALLBACK (gtk_togglebutton_clicked_callback),
75 this);
76
77 m_parent->DoAddChild(this);
78
79 PostCreation(size);
80
81 return true;
82 }
83
84 void wxBitmapToggleButton::GTKDisableEvents()
85 {
86 g_signal_handlers_block_by_func(m_widget,
87 (gpointer) gtk_togglebutton_clicked_callback, this);
88 }
89
90 void wxBitmapToggleButton::GTKEnableEvents()
91 {
92 g_signal_handlers_unblock_by_func(m_widget,
93 (gpointer) gtk_togglebutton_clicked_callback, this);
94 }
95
96 // void SetValue(bool state)
97 // Set the value of the toggle button.
98 void wxBitmapToggleButton::SetValue(bool state)
99 {
100 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
101
102 if (state == GetValue())
103 return;
104
105 GTKDisableEvents();
106
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
108
109 GTKEnableEvents();
110 }
111
112 // bool GetValue() const
113 // Get the value of the toggle button.
114 bool wxBitmapToggleButton::GetValue() const
115 {
116 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
117
118 return gtk_toggle_button_get_active((GtkToggleButton*)m_widget);
119 }
120
121 void wxBitmapToggleButton::SetLabel(const wxBitmap& label)
122 {
123 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
124
125 m_bitmap = label;
126 InvalidateBestSize();
127
128 OnSetBitmap();
129 }
130
131 void wxBitmapToggleButton::OnSetBitmap()
132 {
133 if (!m_bitmap.Ok()) return;
134
135 GtkWidget* image = ((GtkBin*)m_widget)->child;
136 if (image == NULL)
137 {
138 image = gtk_image_new();
139 gtk_widget_show(image);
140 gtk_container_add((GtkContainer*)m_widget, image);
141 }
142 // always use pixbuf, because pixmap mask does not
143 // work with disabled images in some themes
144 gtk_image_set_from_pixbuf((GtkImage*)image, m_bitmap.GetPixbuf());
145 }
146
147 bool wxBitmapToggleButton::Enable(bool enable /*=true*/)
148 {
149 bool isEnabled = IsEnabled();
150
151 if (!wxControl::Enable(enable))
152 return false;
153
154 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
155
156 if (!isEnabled && enable)
157 {
158 GTKFixSensitivity();
159 }
160
161 return true;
162 }
163
164 void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
165 {
166 gtk_widget_modify_style(m_widget, style);
167 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
168 }
169
170 GdkWindow *
171 wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
172 {
173 return GTK_BUTTON(m_widget)->event_window;
174 }
175
176 // Get the "best" size for this control.
177 wxSize wxBitmapToggleButton::DoGetBestSize() const
178 {
179 wxSize best;
180
181 if (m_bitmap.Ok())
182 {
183 int border = HasFlag(wxNO_BORDER) ? 4 : 10;
184 best.x = m_bitmap.GetWidth()+border;
185 best.y = m_bitmap.GetHeight()+border;
186 }
187 CacheBestSize(best);
188 return best;
189 }
190
191
192 // static
193 wxVisualAttributes
194 wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
195 {
196 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
197 }
198
199
200 // ------------------------------------------------------------------------
201 // wxToggleButton
202 // ------------------------------------------------------------------------
203
204 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton, wxControl)
205
206 bool wxToggleButton::Create(wxWindow *parent, wxWindowID id,
207 const wxString &label, const wxPoint &pos,
208 const wxSize &size, long style,
209 const wxValidator& validator,
210 const wxString &name)
211 {
212 if (!PreCreation(parent, pos, size) ||
213 !CreateBase(parent, id, pos, size, style, validator, name ))
214 {
215 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
216 return false;
217 }
218
219 // Create the gtk widget.
220 m_widget = gtk_toggle_button_new_with_mnemonic("");
221 g_object_ref(m_widget);
222
223 SetLabel(label);
224
225 g_signal_connect (m_widget, "clicked",
226 G_CALLBACK (gtk_togglebutton_clicked_callback),
227 this);
228
229 m_parent->DoAddChild(this);
230
231 PostCreation(size);
232
233 return true;
234 }
235
236 void wxToggleButton::GTKDisableEvents()
237 {
238 g_signal_handlers_block_by_func(m_widget,
239 (gpointer) gtk_togglebutton_clicked_callback, this);
240 }
241
242 void wxToggleButton::GTKEnableEvents()
243 {
244 g_signal_handlers_unblock_by_func(m_widget,
245 (gpointer) gtk_togglebutton_clicked_callback, this);
246 }
247
248 // void SetValue(bool state)
249 // Set the value of the toggle button.
250 void wxToggleButton::SetValue(bool state)
251 {
252 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
253
254 if (state == GetValue())
255 return;
256
257 GTKDisableEvents();
258
259 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget), state);
260
261 GTKEnableEvents();
262 }
263
264 // bool GetValue() const
265 // Get the value of the toggle button.
266 bool wxToggleButton::GetValue() const
267 {
268 wxCHECK_MSG(m_widget != NULL, false, wxT("invalid toggle button"));
269
270 return GTK_TOGGLE_BUTTON(m_widget)->active;
271 }
272
273 void wxToggleButton::SetLabel(const wxString& label)
274 {
275 wxCHECK_RET(m_widget != NULL, wxT("invalid toggle button"));
276
277 wxControl::SetLabel(label);
278
279 const wxString labelGTK = GTKConvertMnemonics(label);
280
281 gtk_button_set_label(GTK_BUTTON(m_widget), wxGTK_CONV(labelGTK));
282
283 GTKApplyWidgetStyle( false );
284 }
285
286 bool wxToggleButton::Enable(bool enable /*=true*/)
287 {
288 if (!base_type::Enable(enable))
289 return false;
290
291 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
292
293 if (enable)
294 GTKFixSensitivity();
295
296 return true;
297 }
298
299 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle *style)
300 {
301 gtk_widget_modify_style(m_widget, style);
302 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
303 }
304
305 GdkWindow *
306 wxToggleButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
307 {
308 return GTK_BUTTON(m_widget)->event_window;
309 }
310
311 // Get the "best" size for this control.
312 wxSize wxToggleButton::DoGetBestSize() const
313 {
314 wxSize ret(wxControl::DoGetBestSize());
315
316 if (!HasFlag(wxBU_EXACTFIT))
317 {
318 if (ret.x < 80) ret.x = 80;
319 }
320
321 CacheBestSize(ret);
322 return ret;
323 }
324
325 // static
326 wxVisualAttributes
327 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
328 {
329 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new);
330 }
331
332 #endif // wxUSE_TOGGLEBTN