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"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
36 extern wxCursor g_globalCursor
;
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
, wxWindowID id
, const wxString
& label
,
68 const wxPoint
& pos
, const wxSize
& size
, long style
,
69 const wxValidator
& validator
, const wxString
& name
)
71 m_acceptsFocus
= TRUE
;
73 m_isRadioButton
= TRUE
;
77 if (!PreCreation( parent
, pos
, size
) ||
78 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
80 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
84 if (HasFlag(wxRB_GROUP
))
87 m_radioButtonGroup
= (GSList
*) NULL
;
91 // search backward for last group start
92 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
93 wxWindowList::Node
*node
= parent
->GetChildren().GetLast();
96 wxWindow
*child
= node
->GetData();
97 if (child
->m_isRadioButton
)
99 chief
= (wxRadioButton
*) child
;
100 if (child
->HasFlag(wxRB_GROUP
)) break;
102 node
= node
->GetPrevious();
106 // we are part of the group started by chief
107 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
112 m_radioButtonGroup
= (GSList
*) NULL
;
116 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, label
.mbc_str() );
120 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
121 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
123 m_parent
->DoAddChild( this );
127 SetFont( parent
->GetFont() );
129 wxSize
size_best( DoGetBestSize() );
130 wxSize
new_size( size
);
131 if (new_size
.x
== -1)
132 new_size
.x
= size_best
.x
;
133 if (new_size
.y
== -1)
134 new_size
.y
= size_best
.y
;
135 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
136 SetSize( new_size
.x
, new_size
.y
);
138 SetBackgroundColour( parent
->GetBackgroundColour() );
139 SetForegroundColour( parent
->GetForegroundColour() );
146 void wxRadioButton::SetLabel( const wxString
& label
)
148 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
150 wxControl::SetLabel( label
);
151 GtkButton
*bin
= GTK_BUTTON( m_widget
);
152 GtkLabel
*g_label
= GTK_LABEL( bin
->child
);
153 gtk_label_set( g_label
, GetLabel().mbc_str() );
156 void wxRadioButton::SetValue( bool val
)
158 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
160 if (val
== GetValue())
167 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
171 // should give an assert
172 // RL - No it shouldn't. A wxGenericValidator might try to set it
173 // as FALSE. Failing silently is probably TRTTD here.
176 m_blockEvent
= FALSE
;
179 bool wxRadioButton::GetValue() const
181 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
183 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
186 bool wxRadioButton::Enable( bool enable
)
188 if ( !wxControl::Enable( enable
) )
191 gtk_widget_set_sensitive( GTK_BUTTON(m_widget
)->child
, enable
);
196 void wxRadioButton::ApplyWidgetStyle()
199 gtk_widget_set_style( m_widget
, m_widgetStyle
);
200 gtk_widget_set_style( GTK_BUTTON(m_widget
)->child
, m_widgetStyle
);
203 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
205 return (window
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
);
208 void wxRadioButton::OnInternalIdle()
210 wxCursor cursor
= m_cursor
;
211 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
213 if (GTK_TOGGLE_BUTTON(m_widget
)->event_window
&& cursor
.Ok())
215 /* I now set the cursor the anew in every OnInternalIdle call
216 as setting the cursor in a parent window also effects the
217 windows above so that checking for the current cursor is
220 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget
)->event_window
, cursor
.GetCursor() );
226 wxSize
wxRadioButton::DoGetBestSize() const
228 return wxControl::DoGetBestSize();