1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/radiobut.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/radiobut.h"
18 #include "wx/gtk/private.h"
19 #include "wx/gtk/private/gtk2-compat.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
25 extern bool g_blockEventsOnDrag
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
33 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
35 if (g_blockEventsOnDrag
) return;
37 if (!gtk_toggle_button_get_active(button
)) return;
39 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
40 event
.SetInt( rb
->GetValue() );
41 event
.SetEventObject( rb
);
42 rb
->HandleWindowEvent( event
);
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 bool wxRadioButton::Create( wxWindow
*parent
,
52 const wxString
& label
,
56 const wxValidator
& validator
,
57 const wxString
& name
)
59 if (!PreCreation( parent
, pos
, size
) ||
60 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
62 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
66 // Check if this radio button should be put into an existing group. This
67 // shouldn't be done if it's given a style to explicitly start a new group
68 // or if it's not meant to be a part of a group at all.
69 GSList
* radioButtonGroup
= NULL
;
70 if (!HasFlag(wxRB_GROUP
) && !HasFlag(wxRB_SINGLE
))
72 // search backward for last group start
73 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
74 for (; node
; node
= node
->GetPrevious())
76 wxWindow
*child
= node
->GetData();
78 // We stop at the first previous radio button in any case as it
79 // wouldn't make sense to put this button in a group with another
80 // one if there is a radio button that is not part of the same
81 // group between them.
82 if (wxIsKindOf(child
, wxRadioButton
))
84 // Any preceding radio button can be used to get its group, not
85 // necessarily one with wxRB_GROUP style, but exclude
86 // wxRB_SINGLE ones as their group should never be shared.
87 if (!child
->HasFlag(wxRB_SINGLE
))
89 radioButtonGroup
= gtk_radio_button_get_group(
90 GTK_RADIO_BUTTON(child
->m_widget
));
98 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
99 g_object_ref(m_widget
);
103 g_signal_connect_after (m_widget
, "clicked",
104 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
106 m_parent
->DoAddChild( this );
113 void wxRadioButton::SetLabel( const wxString
& label
)
115 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
117 // save the original label
118 wxControlBase::SetLabel(label
);
120 GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget
))), label
);
123 void wxRadioButton::SetValue( bool val
)
125 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
127 if (val
== GetValue())
130 g_signal_handlers_block_by_func(
131 m_widget
, (void*)gtk_radiobutton_clicked_callback
, this);
135 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
139 // should give an assert
140 // RL - No it shouldn't. A wxGenericValidator might try to set it
141 // as FALSE. Failing silently is probably TRTTD here.
144 g_signal_handlers_unblock_by_func(
145 m_widget
, (void*)gtk_radiobutton_clicked_callback
, this);
148 bool wxRadioButton::GetValue() const
150 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobutton") );
152 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget
)) != 0;
155 bool wxRadioButton::Enable( bool enable
)
157 if (!base_type::Enable(enable
))
160 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
168 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
170 GTKApplyStyle(m_widget
, style
);
171 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
175 wxRadioButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
177 return gtk_button_get_event_window(GTK_BUTTON(m_widget
));
182 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
184 wxVisualAttributes attr
;
185 // NB: we need toplevel window so that GTK+ can find the right style
186 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
187 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
188 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
189 attr
= GetDefaultAttributesFromGTKWidget(widget
);
190 gtk_widget_destroy(wnd
);