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
)
32 wxapp_install_idle_handler();
34 if (!cb
->m_hasVMT
|| g_blockEventsOnDrag
)
37 if (cb
->m_blockEvent
) return;
39 // Generate a wx event.
40 wxCommandEvent
event(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
, cb
->GetId());
41 event
.SetInt(cb
->GetValue());
42 event
.SetEventObject(cb
);
43 cb
->GetEventHandler()->ProcessEvent(event
);
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
49 // ------------------------------------------------------------------------
50 // wxToggleBitmapButton
51 // ------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxToggleBitmapButton
, wxControl
)
55 bool wxToggleBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
56 const wxBitmap
&label
, const wxPoint
&pos
,
57 const wxSize
&size
, long style
,
58 const wxValidator
& validator
,
62 m_acceptsFocus
= true;
66 if (!PreCreation(parent
, pos
, size
) ||
67 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
69 wxFAIL_MSG(wxT("wxToggleBitmapButton creation failed"));
73 // Create the gtk widget.
74 m_widget
= gtk_toggle_button_new();
76 if (style
& wxNO_BORDER
)
77 gtk_button_set_relief( GTK_BUTTON(m_widget
), GTK_RELIEF_NONE
);
82 g_signal_connect (m_widget
, "clicked",
83 G_CALLBACK (gtk_togglebutton_clicked_callback
),
86 m_parent
->DoAddChild(this);
93 // void SetValue(bool state)
94 // Set the value of the toggle button.
95 void wxToggleBitmapButton::SetValue(bool state
)
97 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
99 if (state
== GetValue())
104 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
106 m_blockEvent
= false;
109 // bool GetValue() const
110 // Get the value of the toggle button.
111 bool wxToggleBitmapButton::GetValue() const
113 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
115 return gtk_toggle_button_get_active((GtkToggleButton
*)m_widget
);
118 void wxToggleBitmapButton::SetLabel(const wxBitmap
& label
)
120 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
123 InvalidateBestSize();
128 void wxToggleBitmapButton::OnSetBitmap()
130 if (!m_bitmap
.Ok()) return;
132 GtkWidget
* image
= ((GtkBin
*)m_widget
)->child
;
136 image
= gtk_image_new_from_pixbuf(m_bitmap
.GetPixbuf());
137 gtk_widget_show(image
);
138 gtk_container_add((GtkContainer
*)m_widget
, image
);
141 { // subsequent bitmaps
142 gtk_image_set_from_pixbuf((GtkImage
*)image
, m_bitmap
.GetPixbuf());
146 bool wxToggleBitmapButton::Enable(bool enable
/*=true*/)
148 if (!wxControl::Enable(enable
))
151 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
156 void wxToggleBitmapButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
158 gtk_widget_modify_style(m_widget
, style
);
159 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
163 wxToggleBitmapButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
165 return GTK_BUTTON(m_widget
)->event_window
;
168 // Get the "best" size for this control.
169 wxSize
wxToggleBitmapButton::DoGetBestSize() const
175 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
176 best
.x
= m_bitmap
.GetWidth()+border
;
177 best
.y
= m_bitmap
.GetHeight()+border
;
186 wxToggleBitmapButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
188 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
192 // ------------------------------------------------------------------------
194 // ------------------------------------------------------------------------
196 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
198 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
199 const wxString
&label
, const wxPoint
&pos
,
200 const wxSize
&size
, long style
,
201 const wxValidator
& validator
,
202 const wxString
&name
)
205 m_acceptsFocus
= true;
207 m_blockEvent
= false;
209 if (!PreCreation(parent
, pos
, size
) ||
210 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
212 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
216 // Create the gtk widget.
217 m_widget
= gtk_toggle_button_new_with_mnemonic("");
221 g_signal_connect (m_widget
, "clicked",
222 G_CALLBACK (gtk_togglebutton_clicked_callback
),
225 m_parent
->DoAddChild(this);
232 // void SetValue(bool state)
233 // Set the value of the toggle button.
234 void wxToggleButton::SetValue(bool state
)
236 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
238 if (state
== GetValue())
243 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
245 m_blockEvent
= false;
248 // bool GetValue() const
249 // Get the value of the toggle button.
250 bool wxToggleButton::GetValue() const
252 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
254 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
257 void wxToggleButton::SetLabel(const wxString
& label
)
259 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
261 wxControl::SetLabel(label
);
263 const wxString labelGTK
= GTKConvertMnemonics(label
);
265 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
267 ApplyWidgetStyle( false );
270 bool wxToggleButton::Enable(bool enable
/*=true*/)
272 if (!wxControl::Enable(enable
))
275 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
280 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
282 gtk_widget_modify_style(m_widget
, style
);
283 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
287 wxToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
289 return GTK_BUTTON(m_widget
)->event_window
;
292 // Get the "best" size for this control.
293 wxSize
wxToggleButton::DoGetBestSize() const
295 wxSize
ret(wxControl::DoGetBestSize());
297 if (!HasFlag(wxBU_EXACTFIT
))
299 if (ret
.x
< 80) ret
.x
= 80;
308 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
310 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
313 #endif // wxUSE_TOGGLEBTN