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 (g_isIdle
) wxapp_install_idle_handler();
35 if (!rb
->m_hasVMT
) return;
37 if (g_blockEventsOnDrag
) return;
39 if (!button
->active
) return;
41 if (rb
->m_blockEvent
) return;
43 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
44 event
.SetInt( rb
->GetValue() );
45 event
.SetEventObject( rb
);
46 rb
->GetEventHandler()->ProcessEvent( event
);
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
56 bool wxRadioButton::Create( wxWindow
*parent
,
58 const wxString
& label
,
62 const wxValidator
& validator
,
63 const wxString
& name
)
65 m_acceptsFocus
= TRUE
;
70 if (!PreCreation( parent
, pos
, size
) ||
71 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
73 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
77 GSList
* radioButtonGroup
= NULL
;
78 if (!HasFlag(wxRB_GROUP
))
80 // search backward for last group start
81 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
82 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
85 wxWindow
*child
= node
->GetData();
86 if (child
->IsRadioButton())
88 chief
= (wxRadioButton
*) child
;
89 if (child
->HasFlag(wxRB_GROUP
))
92 node
= node
->GetPrevious();
96 // we are part of the group started by chief
97 radioButtonGroup
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
101 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
105 g_signal_connect (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 GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget
)->child
), label
);
122 void wxRadioButton::SetValue( bool val
)
124 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
126 if (val
== GetValue())
133 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
137 // should give an assert
138 // RL - No it shouldn't. A wxGenericValidator might try to set it
139 // as FALSE. Failing silently is probably TRTTD here.
142 m_blockEvent
= FALSE
;
145 bool wxRadioButton::GetValue() const
147 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
149 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
152 bool wxRadioButton::Enable( bool enable
)
154 if ( !wxControl::Enable( enable
) )
157 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
162 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
164 gtk_widget_modify_style(m_widget
, style
);
165 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
169 wxRadioButton::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
171 return GTK_BUTTON(m_widget
)->event_window
;
174 wxSize
wxRadioButton::DoGetBestSize() const
176 return wxControl::DoGetBestSize();
181 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
183 wxVisualAttributes attr
;
184 // NB: we need toplevel window so that GTK+ can find the right style
185 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
186 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
187 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
188 attr
= GetDefaultAttributesFromGTKWidget(widget
);
189 gtk_widget_destroy(wnd
);