1 /////////////////////////////////////////////////////////////////////////////
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"
17 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern bool g_blockEventsOnDrag
;
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
31 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
33 if (!rb
->m_hasVMT
) return;
35 if (g_blockEventsOnDrag
) return;
37 if (!button
->active
) return;
39 if (rb
->m_blockEvent
) return;
41 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
42 event
.SetInt( rb
->GetValue() );
43 event
.SetEventObject( rb
);
44 rb
->GetEventHandler()->ProcessEvent( event
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
54 bool wxRadioButton::Create( wxWindow
*parent
,
56 const wxString
& label
,
60 const wxValidator
& validator
,
61 const wxString
& name
)
65 if (!PreCreation( parent
, pos
, size
) ||
66 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
68 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
72 GSList
* radioButtonGroup
= NULL
;
73 if (!HasFlag(wxRB_GROUP
))
75 // search backward for last group start
76 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
77 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
80 wxWindow
*child
= node
->GetData();
81 if (child
->IsRadioButton())
83 chief
= (wxRadioButton
*) child
;
84 if (child
->HasFlag(wxRB_GROUP
))
87 node
= node
->GetPrevious();
91 // we are part of the group started by chief
92 radioButtonGroup
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
96 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
100 g_signal_connect (m_widget
, "clicked",
101 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
103 m_parent
->DoAddChild( this );
110 void wxRadioButton::SetLabel( const wxString
& label
)
112 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
114 GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget
)->child
), label
);
117 void wxRadioButton::SetValue( bool val
)
119 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
121 if (val
== GetValue())
128 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
132 // should give an assert
133 // RL - No it shouldn't. A wxGenericValidator might try to set it
134 // as FALSE. Failing silently is probably TRTTD here.
137 m_blockEvent
= FALSE
;
140 bool wxRadioButton::GetValue() const
142 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
144 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
147 bool wxRadioButton::Enable( bool enable
)
149 if ( !wxControl::Enable( enable
) )
152 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
157 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
159 gtk_widget_modify_style(m_widget
, style
);
160 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
164 wxRadioButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
166 return GTK_BUTTON(m_widget
)->event_window
;
169 wxSize
wxRadioButton::DoGetBestSize() const
171 return wxControl::DoGetBestSize();
176 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
178 wxVisualAttributes attr
;
179 // NB: we need toplevel window so that GTK+ can find the right style
180 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
181 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
182 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
183 attr
= GetDefaultAttributesFromGTKWidget(widget
);
184 gtk_widget_destroy(wnd
);