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
9 // Copyright: (c) 2000 Johnny C. Norris II
10 // License: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
18 #include "wx/tglbtn.h"
21 #include "wx/button.h"
24 #include "wx/gtk/private.h"
26 extern bool g_blockEventsOnDrag
;
29 static void gtk_togglebutton_clicked_callback(GtkWidget
*WXUNUSED(widget
), wxToggleButton
*cb
)
31 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
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
);
42 wxDEFINE_EVENT( wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, wxCommandEvent
)
44 // ------------------------------------------------------------------------
45 // wxBitmapToggleButton
46 // ------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton
, wxControl
)
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
,
56 if (!PreCreation(parent
, pos
, size
) ||
57 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
59 wxFAIL_MSG(wxT("wxBitmapToggleButton creation failed"));
63 // Create the gtk widget.
64 m_widget
= gtk_toggle_button_new();
65 g_object_ref(m_widget
);
67 if (style
& wxNO_BORDER
)
68 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
73 g_signal_connect (m_widget
, "clicked",
74 G_CALLBACK (gtk_togglebutton_clicked_callback
),
77 m_parent
->DoAddChild(this);
84 void wxBitmapToggleButton::GTKDisableEvents()
86 g_signal_handlers_block_by_func(m_widget
,
87 (gpointer
) gtk_togglebutton_clicked_callback
, this);
90 void wxBitmapToggleButton::GTKEnableEvents()
92 g_signal_handlers_unblock_by_func(m_widget
,
93 (gpointer
) gtk_togglebutton_clicked_callback
, this);
96 // void SetValue(bool state)
97 // Set the value of the toggle button.
98 void wxBitmapToggleButton::SetValue(bool state
)
100 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
102 if (state
== GetValue())
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
112 // bool GetValue() const
113 // Get the value of the toggle button.
114 bool wxBitmapToggleButton::GetValue() const
116 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
118 return gtk_toggle_button_get_active((GtkToggleButton
*)m_widget
);
121 void wxBitmapToggleButton::SetLabel(const wxBitmap
& label
)
123 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
126 InvalidateBestSize();
131 void wxBitmapToggleButton::OnSetBitmap()
133 if (!m_bitmap
.Ok()) return;
135 GtkWidget
* image
= ((GtkBin
*)m_widget
)->child
;
138 image
= gtk_image_new();
139 gtk_widget_show(image
);
140 gtk_container_add((GtkContainer
*)m_widget
, image
);
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());
147 bool wxBitmapToggleButton::Enable(bool enable
/*=true*/)
149 bool isEnabled
= IsEnabled();
151 if (!wxControl::Enable(enable
))
154 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
156 if (!isEnabled
&& enable
)
164 void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
166 gtk_widget_modify_style(m_widget
, style
);
167 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
171 wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
173 return GTK_BUTTON(m_widget
)->event_window
;
176 // Get the "best" size for this control.
177 wxSize
wxBitmapToggleButton::DoGetBestSize() const
183 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
184 best
.x
= m_bitmap
.GetWidth()+border
;
185 best
.y
= m_bitmap
.GetHeight()+border
;
194 wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
196 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
200 // ------------------------------------------------------------------------
202 // ------------------------------------------------------------------------
204 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
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
)
212 if (!PreCreation(parent
, pos
, size
) ||
213 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
215 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
219 // Create the gtk widget.
220 m_widget
= gtk_toggle_button_new_with_mnemonic("");
221 g_object_ref(m_widget
);
225 g_signal_connect (m_widget
, "clicked",
226 G_CALLBACK (gtk_togglebutton_clicked_callback
),
229 m_parent
->DoAddChild(this);
236 void wxToggleButton::GTKDisableEvents()
238 g_signal_handlers_block_by_func(m_widget
,
239 (gpointer
) gtk_togglebutton_clicked_callback
, this);
242 void wxToggleButton::GTKEnableEvents()
244 g_signal_handlers_unblock_by_func(m_widget
,
245 (gpointer
) gtk_togglebutton_clicked_callback
, this);
248 // void SetValue(bool state)
249 // Set the value of the toggle button.
250 void wxToggleButton::SetValue(bool state
)
252 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
254 if (state
== GetValue())
259 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
264 // bool GetValue() const
265 // Get the value of the toggle button.
266 bool wxToggleButton::GetValue() const
268 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
270 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
273 void wxToggleButton::SetLabel(const wxString
& label
)
275 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
277 wxControl::SetLabel(label
);
279 const wxString labelGTK
= GTKConvertMnemonics(label
);
281 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
283 ApplyWidgetStyle( false );
286 bool wxToggleButton::Enable(bool enable
/*=true*/)
288 bool isEnabled
= IsEnabled();
290 if (!wxControl::Enable(enable
))
293 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
295 if (!isEnabled
&& enable
)
303 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
305 gtk_widget_modify_style(m_widget
, style
);
306 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
310 wxToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
312 return GTK_BUTTON(m_widget
)->event_window
;
315 // Get the "best" size for this control.
316 wxSize
wxToggleButton::DoGetBestSize() const
318 wxSize
ret(wxControl::DoGetBestSize());
320 if (!HasFlag(wxBU_EXACTFIT
))
322 if (ret
.x
< 80) ret
.x
= 80;
331 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
333 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
336 #endif // wxUSE_TOGGLEBTN