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
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
40 void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioButton
*rb
)
42 if (g_isIdle
) wxapp_install_idle_handler();
44 if (!rb
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
49 event
.SetInt( rb
->GetValue() );
50 event
.SetEventObject( rb
);
51 rb
->GetEventHandler()->ProcessEvent( event
);
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
60 bool wxRadioButton::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
61 const wxPoint
& pos
, const wxSize
& size
, long style
,
62 const wxValidator
& validator
, const wxString
& name
)
64 m_acceptsFocus
= TRUE
;
67 wxSize newSize
= size
;
69 PreCreation( parent
, id
, pos
, newSize
, style
, name
);
71 m_isRadioButton
= TRUE
;
74 SetValidator( validator
);
78 if (HasFlag(wxRB_GROUP
))
80 /* start a new group */
81 m_radioButtonGroup
= (GSList
*) NULL
;
85 /* search backward for last group start */
86 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
87 wxWindowList::Node
*node
= parent
->GetChildren().GetLast();
90 wxWindow
*child
= node
->GetData();
91 if (child
->m_isRadioButton
)
93 chief
= (wxRadioButton
*) child
;
94 if (child
->HasFlag(wxRB_GROUP
)) break;
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
;
106 node
= node
->GetPrevious();
110 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, label
.mbc_str() );
114 if (newSize
.x
== -1) newSize
.x
= 22+gdk_string_measure( m_widget
->style
->font
, label
.mbc_str() );
115 if (newSize
.y
== -1) newSize
.y
= 26;
116 SetSize( newSize
.x
, newSize
.y
);
118 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
119 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
121 m_parent
->DoAddChild( this );
125 SetBackgroundColour( parent
->GetBackgroundColour() );
126 SetForegroundColour( parent
->GetForegroundColour() );
127 SetFont( parent
->GetFont() );
134 void wxRadioButton::SetLabel( const wxString
& label
)
136 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobutton") );
138 wxControl::SetLabel( label
);
139 GtkButton
*bin
= GTK_BUTTON( m_widget
);
140 GtkLabel
*g_label
= GTK_LABEL( bin
->child
);
141 gtk_label_set( g_label
, GetLabel().mbc_str() );
144 void wxRadioButton::SetValue( bool val
)
146 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobutton") );
148 if (val
== GetValue())
151 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
152 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
156 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
160 // should give an assert
163 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
164 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
167 bool wxRadioButton::GetValue() const
169 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobutton") );
171 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
174 bool wxRadioButton::Enable( bool enable
)
176 if ( !wxControl::Enable( enable
) )
179 gtk_widget_set_sensitive( GTK_BUTTON(m_widget
)->child
, enable
);
184 void wxRadioButton::ApplyWidgetStyle()
187 gtk_widget_set_style( m_widget
, m_widgetStyle
);
188 gtk_widget_set_style( GTK_BUTTON(m_widget
)->child
, m_widgetStyle
);