1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "radiobut.h"
19 #include "wx/radiobut.h"
21 #include "wx/gtk/private.h"
23 //-----------------------------------------------------------------------------
25 //-----------------------------------------------------------------------------
27 extern void wxapp_install_idle_handler();
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern bool g_blockEventsOnDrag
;
35 extern wxCursor g_globalCursor
;
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
42 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!rb
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 if (!button
->active
) return;
52 if (rb
->m_blockEvent
) return;
54 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
55 event
.SetInt( rb
->GetValue() );
56 event
.SetEventObject( rb
);
57 rb
->GetEventHandler()->ProcessEvent( event
);
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
66 bool wxRadioButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
67 const wxPoint
& pos
, const wxSize
& size
, long style
,
68 const wxValidator
& validator
, const wxString
& name
)
70 m_acceptsFocus
= TRUE
;
72 m_isRadioButton
= TRUE
;
76 if (!PreCreation( parent
, pos
, size
) ||
77 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
79 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
83 if (HasFlag(wxRB_GROUP
))
86 m_radioButtonGroup
= (GSList
*) NULL
;
90 // search backward for last group start
91 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
92 wxWindowList::Node
*node
= parent
->GetChildren().GetLast();
95 wxWindow
*child
= node
->GetData();
96 if (child
->m_isRadioButton
)
98 chief
= (wxRadioButton
*) child
;
99 if (child
->HasFlag(wxRB_GROUP
)) break;
101 node
= node
->GetPrevious();
105 // we are part of the group started by chief
106 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
111 m_radioButtonGroup
= (GSList
*) NULL
;
115 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, label
.mbc_str() );
119 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
120 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
122 m_parent
->DoAddChild( this );
126 SetFont( parent
->GetFont() );
128 wxSize
size_best( DoGetBestSize() );
129 wxSize
new_size( size
);
130 if (new_size
.x
== -1)
131 new_size
.x
= size_best
.x
;
132 if (new_size
.y
== -1)
133 new_size
.y
= size_best
.y
;
134 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
135 SetSize( new_size
.x
, new_size
.y
);
137 SetBackgroundColour( parent
->GetBackgroundColour() );
138 SetForegroundColour( parent
->GetForegroundColour() );
145 void wxRadioButton::SetLabel( const wxString
& label
)
147 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
149 wxControl::SetLabel( label
);
150 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
151 gtk_label_set( g_label
, GetLabel().mbc_str() );
154 void wxRadioButton::SetValue( bool val
)
156 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
158 if (val
== GetValue())
165 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
169 // should give an assert
170 // RL - No it shouldn't. A wxGenericValidator might try to set it
171 // as FALSE. Failing silently is probably TRTTD here.
174 m_blockEvent
= FALSE
;
177 bool wxRadioButton::GetValue() const
179 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
181 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
184 bool wxRadioButton::Enable( bool enable
)
186 if ( !wxControl::Enable( enable
) )
189 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
194 void wxRadioButton::ApplyWidgetStyle()
197 gtk_widget_set_style( m_widget
, m_widgetStyle
);
198 gtk_widget_set_style( BUTTON_CHILD(m_widget
), m_widgetStyle
);
201 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
203 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
206 void wxRadioButton::OnInternalIdle()
208 wxCursor cursor
= m_cursor
;
209 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
211 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
212 if ( win
&& cursor
.Ok())
214 /* I now set the cursor the anew in every OnInternalIdle call
215 as setting the cursor in a parent window also effects the
216 windows above so that checking for the current cursor is
219 gdk_window_set_cursor( win
, cursor
.GetCursor() );
225 wxSize
wxRadioButton::DoGetBestSize() const
227 return wxControl::DoGetBestSize();