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
->GetEventHandler()->ProcessEvent(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
,
62 if (!PreCreation(parent
, pos
, size
) ||
63 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
65 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
69 // Create the gtk widget.
70 m_widget
= gtk_toggle_button_new();
72 if (style
& wxNO_BORDER
)
73 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
78 g_signal_connect (m_widget
, "clicked",
79 G_CALLBACK (gtk_togglebutton_clicked_callback
),
82 m_parent
->DoAddChild(this);
89 // void SetValue(bool state)
90 // Set the value of the toggle button.
91 void wxToggleBitmapButton::SetValue(bool state
)
93 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
95 if (state
== GetValue())
100 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
102 m_blockEvent
= false;
105 // bool GetValue() const
106 // Get the value of the toggle button.
107 bool wxToggleBitmapButton::GetValue() const
109 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
111 return gtk_toggle_button_get_active((GtkToggleButton
*)m_widget
);
114 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
116 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
119 InvalidateBestSize();
124 void wxToggleBitmapButton::OnSetBitmap()
126 if (!m_bitmap
.Ok()) return;
128 GtkWidget
* image
= ((GtkBin
*)m_widget
)->child
;
132 image
= gtk_image_new_from_pixbuf(m_bitmap
.GetPixbuf());
133 gtk_widget_show(image
);
134 gtk_container_add((GtkContainer
*)m_widget
, image
);
137 { // subsequent bitmaps
138 gtk_image_set_from_pixbuf((GtkImage
*)image
, m_bitmap
.GetPixbuf());
142 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
144 if (!wxControl::Enable(enable
))
147 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
152 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
154 gtk_widget_modify_style(m_widget
, style
);
155 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
159 wxToggleBitmapButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
161 return GTK_BUTTON(m_widget
)->event_window
;
164 // Get the "best" size for this control.
165 wxSize
wxToggleBitmapButton::DoGetBestSize() const
171 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
172 best
.x
= m_bitmap
.GetWidth()+border
;
173 best
.y
= m_bitmap
.GetHeight()+border
;
182 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
184 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
188 // ------------------------------------------------------------------------
190 // ------------------------------------------------------------------------
192 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
194 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
195 const wxString
&label
, const wxPoint
&pos
,
196 const wxSize
&size
, long style
,
197 const wxValidator
& validator
,
198 const wxString
&name
)
202 m_blockEvent
= false;
204 if (!PreCreation(parent
, pos
, size
) ||
205 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
207 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
211 // Create the gtk widget.
212 m_widget
= gtk_toggle_button_new_with_mnemonic("");
216 g_signal_connect (m_widget
, "clicked",
217 G_CALLBACK (gtk_togglebutton_clicked_callback
),
220 m_parent
->DoAddChild(this);
227 // void SetValue(bool state)
228 // Set the value of the toggle button.
229 void wxToggleButton::SetValue(bool state
)
231 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
233 if (state
== GetValue())
238 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
240 m_blockEvent
= false;
243 // bool GetValue() const
244 // Get the value of the toggle button.
245 bool wxToggleButton::GetValue() const
247 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
249 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
252 void wxToggleButton::SetLabel(const wxString
& label
)
254 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
256 wxControl::SetLabel(label
);
258 const wxString labelGTK
= GTKConvertMnemonics(label
);
260 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
262 ApplyWidgetStyle( false );
265 bool wxToggleButton::Enable(bool enable
/*=true*/)
267 if (!wxControl::Enable(enable
))
270 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
275 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
277 gtk_widget_modify_style(m_widget
, style
);
278 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
282 wxToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
284 return GTK_BUTTON(m_widget
)->event_window
;
287 // Get the "best" size for this control.
288 wxSize
wxToggleButton::DoGetBestSize() const
290 wxSize
ret(wxControl::DoGetBestSize());
292 if (!HasFlag(wxBU_EXACTFIT
))
294 if (ret
.x
< 80) ret
.x
= 80;
303 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
305 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
308 #endif // wxUSE_TOGGLEBTN