1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "radiobut.h"
15 #include "wx/radiobut.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
34 extern wxCursor g_globalCursor
;
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
41 void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioButton
*rb
)
43 if (g_isIdle
) wxapp_install_idle_handler();
45 if (!rb
->m_hasVMT
) return;
47 if (g_blockEventsOnDrag
) return;
49 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
50 event
.SetInt( rb
->GetValue() );
51 event
.SetEventObject( rb
);
52 rb
->GetEventHandler()->ProcessEvent( event
);
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
61 bool wxRadioButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
62 const wxPoint
& pos
, const wxSize
& size
, long style
,
63 const wxValidator
& validator
, const wxString
& name
)
65 m_acceptsFocus
= TRUE
;
67 m_isRadioButton
= TRUE
;
69 if (!PreCreation( parent
, pos
, size
) ||
70 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
72 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
76 if (HasFlag(wxRB_GROUP
))
78 /* start a new group */
79 m_radioButtonGroup
= (GSList
*) NULL
;
83 /* search backward for last group start */
84 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
85 wxWindowList::Node
*node
= parent
->GetChildren().GetLast();
88 wxWindow
*child
= node
->GetData();
89 if (child
->m_isRadioButton
)
91 chief
= (wxRadioButton
*) child
;
92 if (child
->HasFlag(wxRB_GROUP
)) break;
94 node
= node
->GetPrevious();
98 /* we are part of the group started by chief */
99 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
103 /* start a new group */
104 m_radioButtonGroup
= (GSList
*) NULL
;
108 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, label
.mbc_str() );
112 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
113 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
115 m_parent
->DoAddChild( this );
119 SetFont( parent
->GetFont() );
121 wxSize
size_best( DoGetBestSize() );
122 wxSize
new_size( size
);
123 if (new_size
.x
== -1)
124 new_size
.x
= size_best
.x
;
125 if (new_size
.y
== -1)
126 new_size
.y
= size_best
.y
;
127 if ((new_size
.x
!= size
.x
) || (new_size
.y
!= size
.y
))
128 SetSize( new_size
.x
, new_size
.y
);
130 SetBackgroundColour( parent
->GetBackgroundColour() );
131 SetForegroundColour( parent
->GetForegroundColour() );
138 void wxRadioButton::SetLabel( const wxString
& label
)
140 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
142 wxControl::SetLabel( label
);
143 GtkButton
*bin
= GTK_BUTTON( m_widget
);
144 GtkLabel
*g_label
= GTK_LABEL( bin
->child
);
145 gtk_label_set( g_label
, GetLabel().mbc_str() );
148 void wxRadioButton::SetValue( bool val
)
150 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
152 if (val
== GetValue())
155 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
156 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
160 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
164 // should give an assert
167 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
168 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
171 bool wxRadioButton::GetValue() const
173 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
175 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
178 bool wxRadioButton::Enable( bool enable
)
180 if ( !wxControl::Enable( enable
) )
183 gtk_widget_set_sensitive( GTK_BUTTON(m_widget
)->child
, enable
);
188 void wxRadioButton::ApplyWidgetStyle()
191 gtk_widget_set_style( m_widget
, m_widgetStyle
);
192 gtk_widget_set_style( GTK_BUTTON(m_widget
)->child
, m_widgetStyle
);
195 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
197 return (window
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
);
200 void wxRadioButton::OnInternalIdle()
202 wxCursor cursor
= m_cursor
;
203 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
205 if (GTK_TOGGLE_BUTTON(m_widget
)->event_window
&& cursor
.Ok())
207 /* I now set the cursor the anew in every OnInternalIdle call
208 as setting the cursor in a parent window also effects the
209 windows above so that checking for the current cursor is
212 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget
)->event_window
, cursor
.GetCursor() );
218 wxSize
wxRadioButton::DoGetBestSize() const
220 return wxControl::DoGetBestSize();