1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/radiobut.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/radiobut.h"
17 #include "wx/gtk/private.h"
18 #include "wx/gtk/private/gtk2-compat.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
32 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
34 if (g_blockEventsOnDrag
) return;
36 if (!gtk_toggle_button_get_active(button
)) return;
38 wxCommandEvent
event( wxEVT_RADIOBUTTON
, rb
->GetId());
39 event
.SetInt( rb
->GetValue() );
40 event
.SetEventObject( rb
);
41 rb
->HandleWindowEvent( event
);
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 bool wxRadioButton::Create( wxWindow
*parent
,
51 const wxString
& label
,
55 const wxValidator
& validator
,
56 const wxString
& name
)
58 if (!PreCreation( parent
, pos
, size
) ||
59 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
61 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
65 // Check if this radio button should be put into an existing group. This
66 // shouldn't be done if it's given a style to explicitly start a new group
67 // or if it's not meant to be a part of a group at all.
68 GSList
* radioButtonGroup
= NULL
;
69 if (!HasFlag(wxRB_GROUP
) && !HasFlag(wxRB_SINGLE
))
71 // search backward for last group start
72 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
73 for (; node
; node
= node
->GetPrevious())
75 wxWindow
*child
= node
->GetData();
77 // We stop at the first previous radio button in any case as it
78 // wouldn't make sense to put this button in a group with another
79 // one if there is a radio button that is not part of the same
80 // group between them.
81 if (wxIsKindOf(child
, wxRadioButton
))
83 // Any preceding radio button can be used to get its group, not
84 // necessarily one with wxRB_GROUP style, but exclude
85 // wxRB_SINGLE ones as their group should never be shared.
86 if (!child
->HasFlag(wxRB_SINGLE
))
88 radioButtonGroup
= gtk_radio_button_get_group(
89 GTK_RADIO_BUTTON(child
->m_widget
));
97 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
98 g_object_ref(m_widget
);
102 g_signal_connect_after (m_widget
, "clicked",
103 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
105 m_parent
->DoAddChild( this );
112 void wxRadioButton::SetLabel( const wxString
& label
)
114 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
116 // save the original label
117 wxControlBase::SetLabel(label
);
119 GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget
))), label
);
122 void wxRadioButton::SetValue( bool val
)
124 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
126 if (val
== GetValue())
129 g_signal_handlers_block_by_func(
130 m_widget
, (void*)gtk_radiobutton_clicked_callback
, this);
134 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
138 // should give an assert
139 // RL - No it shouldn't. A wxGenericValidator might try to set it
140 // as FALSE. Failing silently is probably TRTTD here.
143 g_signal_handlers_unblock_by_func(
144 m_widget
, (void*)gtk_radiobutton_clicked_callback
, this);
147 bool wxRadioButton::GetValue() const
149 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobutton") );
151 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget
)) != 0;
154 bool wxRadioButton::Enable( bool enable
)
156 if (!base_type::Enable(enable
))
159 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
167 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
169 GTKApplyStyle(m_widget
, style
);
170 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
174 wxRadioButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
176 return gtk_button_get_event_window(GTK_BUTTON(m_widget
));
181 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
183 return GetDefaultAttributesFromGTKWidget(gtk_radio_button_new_with_label(NULL
, ""));