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 // wxToggleBitmapButton
48 // ------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
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
,
60 if (!PreCreation(parent
, pos
, size
) ||
61 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
63 wxFAIL_MSG(wxT("wxToggleBitmapButton 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 wxToggleBitmapButton::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 wxToggleBitmapButton::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 wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
114 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
117 InvalidateBestSize();
122 void wxToggleBitmapButton::OnSetBitmap()
124 if (!m_bitmap
.Ok()) return;
126 GtkWidget
* image
= ((GtkBin
*)m_widget
)->child
;
130 image
= gtk_image_new_from_pixbuf(m_bitmap
.GetPixbuf());
131 gtk_widget_show(image
);
132 gtk_container_add((GtkContainer
*)m_widget
, image
);
135 { // subsequent bitmaps
136 gtk_image_set_from_pixbuf((GtkImage
*)image
, m_bitmap
.GetPixbuf());
140 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
142 if (!wxControl::Enable(enable
))
145 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
150 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
152 gtk_widget_modify_style(m_widget
, style
);
153 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
157 wxToggleBitmapButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
159 return GTK_BUTTON(m_widget
)->event_window
;
162 // Get the "best" size for this control.
163 wxSize
wxToggleBitmapButton::DoGetBestSize() const
169 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
170 best
.x
= m_bitmap
.GetWidth()+border
;
171 best
.y
= m_bitmap
.GetHeight()+border
;
180 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
182 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
186 // ------------------------------------------------------------------------
188 // ------------------------------------------------------------------------
190 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
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
)
198 m_blockEvent
= false;
200 if (!PreCreation(parent
, pos
, size
) ||
201 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
203 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
207 // Create the gtk widget.
208 m_widget
= gtk_toggle_button_new_with_mnemonic("");
212 g_signal_connect (m_widget
, "clicked",
213 G_CALLBACK (gtk_togglebutton_clicked_callback
),
216 m_parent
->DoAddChild(this);
223 // void SetValue(bool state)
224 // Set the value of the toggle button.
225 void wxToggleButton::SetValue(bool state
)
227 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
229 if (state
== GetValue())
234 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
236 m_blockEvent
= false;
239 // bool GetValue() const
240 // Get the value of the toggle button.
241 bool wxToggleButton::GetValue() const
243 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
245 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
248 void wxToggleButton::SetLabel(const wxString
& label
)
250 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
252 wxControl::SetLabel(label
);
254 const wxString labelGTK
= GTKConvertMnemonics(label
);
256 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
258 ApplyWidgetStyle( false );
261 bool wxToggleButton::Enable(bool enable
/*=true*/)
263 if (!wxControl::Enable(enable
))
266 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
271 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
273 gtk_widget_modify_style(m_widget
, style
);
274 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
278 wxToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
280 return GTK_BUTTON(m_widget
)->event_window
;
283 // Get the "best" size for this control.
284 wxSize
wxToggleButton::DoGetBestSize() const
286 wxSize
ret(wxControl::DoGetBestSize());
288 if (!HasFlag(wxBU_EXACTFIT
))
290 if (ret
.x
< 80) ret
.x
= 80;
299 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
301 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
304 #endif // wxUSE_TOGGLEBTN