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 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOGGLEBUTTON_CLICKED
)
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 if (!wxControl::Enable(enable
))
152 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
157 void wxBitmapToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
159 gtk_widget_modify_style(m_widget
, style
);
160 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
164 wxBitmapToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
166 return GTK_BUTTON(m_widget
)->event_window
;
169 // Get the "best" size for this control.
170 wxSize
wxBitmapToggleButton::DoGetBestSize() const
176 int border
= HasFlag(wxNO_BORDER
) ? 4 : 10;
177 best
.x
= m_bitmap
.GetWidth()+border
;
178 best
.y
= m_bitmap
.GetHeight()+border
;
187 wxBitmapToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
189 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
193 // ------------------------------------------------------------------------
195 // ------------------------------------------------------------------------
197 IMPLEMENT_DYNAMIC_CLASS(wxToggleButton
, wxControl
)
199 bool wxToggleButton::Create(wxWindow
*parent
, wxWindowID id
,
200 const wxString
&label
, const wxPoint
&pos
,
201 const wxSize
&size
, long style
,
202 const wxValidator
& validator
,
203 const wxString
&name
)
205 if (!PreCreation(parent
, pos
, size
) ||
206 !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
))
208 wxFAIL_MSG(wxT("wxToggleButton creation failed"));
212 // Create the gtk widget.
213 m_widget
= gtk_toggle_button_new_with_mnemonic("");
214 g_object_ref(m_widget
);
218 g_signal_connect (m_widget
, "clicked",
219 G_CALLBACK (gtk_togglebutton_clicked_callback
),
222 m_parent
->DoAddChild(this);
229 void wxToggleButton::GTKDisableEvents()
231 g_signal_handlers_block_by_func(m_widget
,
232 (gpointer
) gtk_togglebutton_clicked_callback
, this);
235 void wxToggleButton::GTKEnableEvents()
237 g_signal_handlers_unblock_by_func(m_widget
,
238 (gpointer
) gtk_togglebutton_clicked_callback
, this);
241 // void SetValue(bool state)
242 // Set the value of the toggle button.
243 void wxToggleButton::SetValue(bool state
)
245 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
247 if (state
== GetValue())
252 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_widget
), state
);
257 // bool GetValue() const
258 // Get the value of the toggle button.
259 bool wxToggleButton::GetValue() const
261 wxCHECK_MSG(m_widget
!= NULL
, false, wxT("invalid toggle button"));
263 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
266 void wxToggleButton::SetLabel(const wxString
& label
)
268 wxCHECK_RET(m_widget
!= NULL
, wxT("invalid toggle button"));
270 wxControl::SetLabel(label
);
272 const wxString labelGTK
= GTKConvertMnemonics(label
);
274 gtk_button_set_label(GTK_BUTTON(m_widget
), wxGTK_CONV(labelGTK
));
276 ApplyWidgetStyle( false );
279 bool wxToggleButton::Enable(bool enable
/*=true*/)
281 if (!wxControl::Enable(enable
))
284 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
289 void wxToggleButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
291 gtk_widget_modify_style(m_widget
, style
);
292 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
296 wxToggleButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
298 return GTK_BUTTON(m_widget
)->event_window
;
301 // Get the "best" size for this control.
302 wxSize
wxToggleButton::DoGetBestSize() const
304 wxSize
ret(wxControl::DoGetBestSize());
306 if (!HasFlag(wxBU_EXACTFIT
))
308 if (ret
.x
< 80) ret
.x
= 80;
317 wxToggleButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
319 return GetDefaultAttributesFromGTKWidget(gtk_toggle_button_new
);
322 #endif // wxUSE_TOGGLEBTN