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 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
54 event
.SetInt( rb
->GetValue() );
55 event
.SetEventObject( rb
);
56 rb
->GetEventHandler()->ProcessEvent( event
);
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
65 bool wxRadioButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
66 const wxPoint
& pos
, const wxSize
& size
, long style
,
67 const wxValidator
& validator
, const wxString
& name
)
69 m_acceptsFocus
= TRUE
;
71 m_isRadioButton
= TRUE
;
73 if (!PreCreation( parent
, pos
, size
) ||
74 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
76 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
80 if (HasFlag(wxRB_GROUP
))
82 /* start a new group */
83 m_radioButtonGroup
= (GSList
*) NULL
;
87 /* search backward for last group start */
88 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
89 wxWindowList::Node
*node
= parent
->GetChildren().GetLast();
92 wxWindow
*child
= node
->GetData();
93 if (child
->m_isRadioButton
)
95 chief
= (wxRadioButton
*) child
;
96 if (child
->HasFlag(wxRB_GROUP
)) break;
98 node
= node
->GetPrevious();
102 /* we are part of the group started by chief */
103 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
107 /* start a new group */
108 m_radioButtonGroup
= (GSList
*) NULL
;
112 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, label
.mbc_str() );
116 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
117 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
119 m_parent
->DoAddChild( this );
123 SetFont( parent
->GetFont() );
125 wxSize
size_best( DoGetBestSize() );
126 wxSize
new_size( size
);
127 if (new_size
.x
== -1)
128 new_size
.x
= size_best
.x
;
129 if (new_size
.y
== -1)
130 new_size
.y
= size_best
.y
;
131 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
132 SetSize( new_size
.x
, new_size
.y
);
134 SetBackgroundColour( parent
->GetBackgroundColour() );
135 SetForegroundColour( parent
->GetForegroundColour() );
142 void wxRadioButton::SetLabel( const wxString
& label
)
144 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
146 wxControl::SetLabel( label
);
147 GtkButton
*bin
= GTK_BUTTON( m_widget
);
148 GtkLabel
*g_label
= GTK_LABEL( bin
->child
);
149 gtk_label_set( g_label
, GetLabel().mbc_str() );
152 void wxRadioButton::SetValue( bool val
)
154 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
156 if (val
== GetValue())
159 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
160 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
164 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
168 // should give an assert
169 // RL - No it shouldn't. A wxGenericValidator might try to set it
170 // as FALSE. Failing silently is probably TRTTD here.
173 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
174 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
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( GTK_BUTTON(m_widget
)->child
, enable
);
194 void wxRadioButton::ApplyWidgetStyle()
197 gtk_widget_set_style( m_widget
, m_widgetStyle
);
198 gtk_widget_set_style( GTK_BUTTON(m_widget
)->child
, m_widgetStyle
);
201 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
203 return (window
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
);
206 void wxRadioButton::OnInternalIdle()
208 wxCursor cursor
= m_cursor
;
209 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
211 if (GTK_TOGGLE_BUTTON(m_widget
)->event_window
&& cursor
.Ok())
213 /* I now set the cursor the anew in every OnInternalIdle call
214 as setting the cursor in a parent window also effects the
215 windows above so that checking for the current cursor is
218 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget
)->event_window
, cursor
.GetCursor() );
224 wxSize
wxRadioButton::DoGetBestSize() const
226 return wxControl::DoGetBestSize();