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 if (cb
->m_blockEvent
) return;
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
);
44 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
46 // ------------------------------------------------------------------------
47 // wxBitmapToggleButton
48 // ------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxBitmapToggleButton
, wxControl
)
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
,
60 if (!PreCreation(parent
, pos
, size
) ||
61 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
63 wxFAIL_MSG(wxT("wxBitmapToggleButton creation failed"));
67 // Create the gtk widget.
68 m_widget
= gtk_toggle_button_new();
70 if (style
& wxNO_BORDER
)
71 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
76 g_signal_connect (m_widget
, "clicked",
77 G_CALLBACK (gtk_togglebutton_clicked_callback
),
80 m_parent
->DoAddChild(this);
87 // void SetValue(bool state)
88 // Set the value of the toggle button.
89 void wxBitmapToggleButton::SetValue(bool state
)
91 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
93 if (state
== GetValue())
98 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
100 m_blockEvent
= false;
103 // bool GetValue() const
104 // Get the value of the toggle button.
105 bool wxBitmapToggleButton::GetValue() const
107 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
109 return gtk_toggle_button_get_active((GtkToggleButton
*)m_widget
);
112 void wxBitmapToggleButton::SetLabel(const wxBitmap
& label
)
114 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
117 InvalidateBestSize();
122 void wxBitmapToggleButton::OnSetBitmap()
124 if (!m_bitmap
.Ok()) return;
126 GtkWidget
* image
= ((GtkBin
*)m_widget
)->child
;
129 image
= gtk_image_new();
130 gtk_widget_show(image
);
131 gtk_container_add((GtkContainer
*)m_widget
, image
);
133 // always use pixbuf, because pixmap mask does not
134 // work with disabled images in some themes
135 gtk_image_set_from_pixbuf((GtkImage
*)image
, m_bitmap
.GetPixbuf());
138 bool wxBitmapToggleButton::Enable(bool enable
/*=true*/)
140 if (!wxControl::Enable(enable
))
143 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
148 void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
150 gtk_widget_modify_style(m_widget
, style
);
151 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
155 wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
157 return GTK_BUTTON(m_widget
)->event_window
;
160 // Get the "best" size for this control.
161 wxSize
wxBitmapToggleButton::DoGetBestSize() const
167 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
168 best
.x
= m_bitmap
.GetWidth()+border
;
169 best
.y
= m_bitmap
.GetHeight()+border
;
178 wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
180 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
184 // ------------------------------------------------------------------------
186 // ------------------------------------------------------------------------
188 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
190 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
191 const wxString
&label
, const wxPoint
&pos
,
192 const wxSize
&size
, long style
,
193 const wxValidator
& validator
,
194 const wxString
&name
)
196 m_blockEvent
= false;
198 if (!PreCreation(parent
, pos
, size
) ||
199 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
201 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
205 // Create the gtk widget.
206 m_widget
= gtk_toggle_button_new_with_mnemonic("");
210 g_signal_connect (m_widget
, "clicked",
211 G_CALLBACK (gtk_togglebutton_clicked_callback
),
214 m_parent
->DoAddChild(this);
221 // void SetValue(bool state)
222 // Set the value of the toggle button.
223 void wxToggleButton::SetValue(bool state
)
225 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
227 if (state
== GetValue())
232 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
234 m_blockEvent
= false;
237 // bool GetValue() const
238 // Get the value of the toggle button.
239 bool wxToggleButton::GetValue() const
241 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
243 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
246 void wxToggleButton::SetLabel(const wxString
& label
)
248 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
250 wxControl::SetLabel(label
);
252 const wxString labelGTK
= GTKConvertMnemonics(label
);
254 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
256 ApplyWidgetStyle( false );
259 bool wxToggleButton::Enable(bool enable
/*=true*/)
261 if (!wxControl::Enable(enable
))
264 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
269 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
271 gtk_widget_modify_style(m_widget
, style
);
272 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
276 wxToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
278 return GTK_BUTTON(m_widget
)->event_window
;
281 // Get the "best" size for this control.
282 wxSize
wxToggleButton::DoGetBestSize() const
284 wxSize
ret(wxControl::DoGetBestSize());
286 if (!HasFlag(wxBU_EXACTFIT
))
288 if (ret
.x
< 80) ret
.x
= 80;
297 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
299 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
302 #endif // wxUSE_TOGGLEBTN