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 (!rb
->m_hasVMT
) return;
37 if (g_blockEventsOnDrag
) return;
39 if (!gtk_toggle_button_get_active(button
)) return;
41 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
42 event
.SetInt( rb
->GetValue() );
43 event
.SetEventObject( rb
);
44 rb
->HandleWindowEvent( event
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 bool wxRadioButton::Create( wxWindow
*parent
,
54 const wxString
& label
,
58 const wxValidator
& validator
,
59 const wxString
& name
)
61 if (!PreCreation( parent
, pos
, size
) ||
62 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
64 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
68 // Check if this radio button should be put into an existing group. This
69 // shouldn't be done if it's given a style to explicitly start a new group
70 // or if it's not meant to be a part of a group at all.
71 GSList
* radioButtonGroup
= NULL
;
72 if (!HasFlag(wxRB_GROUP
) && !HasFlag(wxRB_SINGLE
))
74 // search backward for last group start
75 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
76 for (; node
; node
= node
->GetPrevious())
78 wxWindow
*child
= node
->GetData();
80 // We stop at the first previous radio button in any case as it
81 // wouldn't make sense to put this button in a group with another
82 // one if there is a radio button that is not part of the same
83 // group between them.
84 if (wxIsKindOf(child
, wxRadioButton
))
86 // Any preceding radio button can be used to get its group, not
87 // necessarily one with wxRB_GROUP style, but exclude
88 // wxRB_SINGLE ones as their group should never be shared.
89 if (!child
->HasFlag(wxRB_SINGLE
))
91 radioButtonGroup
= gtk_radio_button_get_group(
92 GTK_RADIO_BUTTON(child
->m_widget
));
100 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
101 g_object_ref(m_widget
);
105 g_signal_connect_after (m_widget
, "clicked",
106 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
108 m_parent
->DoAddChild( this );
115 void wxRadioButton::SetLabel( const wxString
& label
)
117 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
119 // save the original label
120 wxControlBase::SetLabel(label
);
122 GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget
))), label
);
125 void wxRadioButton::SetValue( bool val
)
127 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
129 if (val
== GetValue())
132 g_signal_handlers_block_by_func(
133 m_widget
, (void*)gtk_radiobutton_clicked_callback
, this);
137 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
141 // should give an assert
142 // RL - No it shouldn't. A wxGenericValidator might try to set it
143 // as FALSE. Failing silently is probably TRTTD here.
146 g_signal_handlers_unblock_by_func(
147 m_widget
, (void*)gtk_radiobutton_clicked_callback
, this);
150 bool wxRadioButton::GetValue() const
152 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobutton") );
154 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget
)) != 0;
157 bool wxRadioButton::Enable( bool enable
)
159 if (!base_type::Enable(enable
))
162 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget
)), enable
);
170 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
172 GTKApplyStyle(m_widget
, style
);
173 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget
)), style
);
177 wxRadioButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
179 return gtk_button_get_event_window(GTK_BUTTON(m_widget
));
184 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
186 wxVisualAttributes attr
;
187 // NB: we need toplevel window so that GTK+ can find the right style
188 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
189 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
190 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
191 attr
= GetDefaultAttributesFromGTKWidget(widget
);
192 gtk_widget_destroy(wnd
);