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
;
66 m_isRadioButton
= TRUE
;
68 if (!PreCreation( parent
, pos
, size
) ||
69 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
71 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
75 if (HasFlag(wxRB_GROUP
))
77 /* start a new group */
78 m_radioButtonGroup
= (GSList
*) NULL
;
82 /* search backward for last group start */
83 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
84 wxWindowList::Node
*node
= parent
->GetChildren().GetLast();
87 wxWindow
*child
= node
->GetData();
88 if (child
->m_isRadioButton
)
90 chief
= (wxRadioButton
*) child
;
91 if (child
->HasFlag(wxRB_GROUP
)) break;
93 node
= node
->GetPrevious();
97 /* we are part of the group started by chief */
98 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
102 /* start a new group */
103 m_radioButtonGroup
= (GSList
*) NULL
;
107 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, label
.mbc_str() );
111 wxSize newSize
= size
;
112 if (newSize
.x
== -1) newSize
.x
= 22+gdk_string_measure( m_widget
->style
->font
, label
.mbc_str() );
113 if (newSize
.y
== -1) newSize
.y
= 26;
114 SetSize( newSize
.x
, newSize
.y
);
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 SetBackgroundColour( parent
->GetBackgroundColour() );
124 SetForegroundColour( parent
->GetForegroundColour() );
125 SetFont( parent
->GetFont() );
132 void wxRadioButton::SetLabel( const wxString
& label
)
134 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
136 wxControl::SetLabel( label
);
137 GtkButton
*bin
= GTK_BUTTON( m_widget
);
138 GtkLabel
*g_label
= GTK_LABEL( bin
->child
);
139 gtk_label_set( g_label
, GetLabel().mbc_str() );
142 void wxRadioButton::SetValue( bool val
)
144 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
146 if (val
== GetValue())
149 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
),
150 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
154 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
158 // should give an assert
161 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
162 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
165 bool wxRadioButton::GetValue() const
167 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
169 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
172 bool wxRadioButton::Enable( bool enable
)
174 if ( !wxControl::Enable( enable
) )
177 gtk_widget_set_sensitive( GTK_BUTTON(m_widget
)->child
, enable
);
182 void wxRadioButton::ApplyWidgetStyle()
185 gtk_widget_set_style( m_widget
, m_widgetStyle
);
186 gtk_widget_set_style( GTK_BUTTON(m_widget
)->child
, m_widgetStyle
);