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
;
36 extern wxWindowGTK
*g_delayedFocus
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
43 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!rb
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 if (!button
->active
) return;
53 if (rb
->m_blockEvent
) return;
55 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
56 event
.SetInt( rb
->GetValue() );
57 event
.SetEventObject( rb
);
58 rb
->GetEventHandler()->ProcessEvent( event
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
67 bool wxRadioButton::Create( wxWindow
*parent
,
69 const wxString
& label
,
73 const wxValidator
& validator
,
74 const wxString
& name
)
76 m_acceptsFocus
= TRUE
;
81 if (!PreCreation( parent
, pos
, size
) ||
82 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
84 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
88 if (HasFlag(wxRB_GROUP
))
91 m_radioButtonGroup
= (GSList
*) NULL
;
95 // search backward for last group start
96 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
97 wxWindowList::Node
*node
= parent
->GetChildren().GetLast();
100 wxWindow
*child
= node
->GetData();
101 if (child
->IsRadioButton())
103 chief
= (wxRadioButton
*) child
;
104 if (child
->HasFlag(wxRB_GROUP
))
107 node
= node
->GetPrevious();
111 // we are part of the group started by chief
112 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
117 m_radioButtonGroup
= (GSList
*) NULL
;
121 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, wxGTK_CONV( label
) );
125 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
126 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
128 m_parent
->DoAddChild( this );
132 SetFont( parent
->GetFont() );
134 wxSize
size_best( DoGetBestSize() );
135 wxSize
new_size( size
);
136 if (new_size
.x
== -1)
137 new_size
.x
= size_best
.x
;
138 if (new_size
.y
== -1)
139 new_size
.y
= size_best
.y
;
140 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
141 SetSize( new_size
.x
, new_size
.y
);
143 SetBackgroundColour( parent
->GetBackgroundColour() );
144 SetForegroundColour( parent
->GetForegroundColour() );
151 void wxRadioButton::SetLabel( const wxString
& label
)
153 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
155 wxControl::SetLabel( label
);
156 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
157 gtk_label_set( g_label
, wxGTK_CONV( GetLabel() ) );
160 void wxRadioButton::SetValue( bool val
)
162 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
164 if (val
== GetValue())
171 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
175 // should give an assert
176 // RL - No it shouldn't. A wxGenericValidator might try to set it
177 // as FALSE. Failing silently is probably TRTTD here.
180 m_blockEvent
= FALSE
;
183 bool wxRadioButton::GetValue() const
185 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
187 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
190 bool wxRadioButton::Enable( bool enable
)
192 if ( !wxControl::Enable( enable
) )
195 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
200 void wxRadioButton::ApplyWidgetStyle()
203 gtk_widget_set_style( m_widget
, m_widgetStyle
);
204 gtk_widget_set_style( BUTTON_CHILD(m_widget
), m_widgetStyle
);
207 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
209 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
212 void wxRadioButton::OnInternalIdle()
214 wxCursor cursor
= m_cursor
;
215 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
217 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
218 if ( win
&& cursor
.Ok())
220 /* I now set the cursor the anew in every OnInternalIdle call
221 as setting the cursor in a parent window also effects the
222 windows above so that checking for the current cursor is
225 gdk_window_set_cursor( win
, cursor
.GetCursor() );
228 if (g_delayedFocus
== this)
230 if (GTK_WIDGET_REALIZED(m_widget
))
232 gtk_widget_grab_focus( m_widget
);
233 g_delayedFocus
= NULL
;
240 wxSize
wxRadioButton::DoGetBestSize() const
242 return wxControl::DoGetBestSize();