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( GtkWidget 
*WXUNUSED(widget
), wxRadioButton 
*rb 
) 
  45     if (g_isIdle
) wxapp_install_idle_handler(); 
  47     if (!rb
->m_hasVMT
) return; 
  49     if (g_blockEventsOnDrag
) return; 
  51     wxCommandEvent 
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId()); 
  52     event
.SetInt( rb
->GetValue() ); 
  53     event
.SetEventObject( rb 
); 
  54     rb
->GetEventHandler()->ProcessEvent( event 
); 
  57 //----------------------------------------------------------------------------- 
  59 //----------------------------------------------------------------------------- 
  61 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
) 
  63 bool wxRadioButton::Create( wxWindow 
*parent
, wxWindowID id
, const wxString
& label
, 
  64   const wxPoint
& pos
,  const wxSize
& size
, long style
, 
  65   const wxValidator
& validator
, const wxString
& name 
) 
  67     m_acceptsFocus 
= TRUE
; 
  69     m_isRadioButton 
= TRUE
; 
  71     if (!PreCreation( parent
, pos
, size 
) || 
  72         !CreateBase( parent
, id
, pos
, size
, style
, validator
, name 
)) 
  74         wxFAIL_MSG( wxT("wxRadioButton creation failed") ); 
  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; 
  96                 node 
= node
->GetPrevious(); 
 100             /* we are part of the group started by chief */ 
 101                 m_radioButtonGroup 
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) ); 
 105             /* start a new group */ 
 106             m_radioButtonGroup 
= (GSList
*) NULL
; 
 110     m_widget 
= gtk_radio_button_new_with_label( m_radioButtonGroup
, label
.mbc_str() ); 
 114     gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",  
 115       GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this ); 
 117     m_parent
->DoAddChild( this ); 
 121     SetFont( parent
->GetFont() ); 
 123     wxSize 
size_best( DoGetBestSize() ); 
 124     wxSize 
new_size( size 
); 
 125     if (new_size
.x 
== -1) 
 126         new_size
.x 
= size_best
.x
; 
 127     if (new_size
.y 
== -1) 
 128         new_size
.y 
= size_best
.y
; 
 129     if ((new_size
.x 
!= size
.x
) || (new_size
.y 
!= size
.y
)) 
 130         SetSize( new_size
.x
, new_size
.y 
); 
 132     SetBackgroundColour( parent
->GetBackgroundColour() ); 
 133     SetForegroundColour( parent
->GetForegroundColour() ); 
 140 void wxRadioButton::SetLabel( const wxString
& label 
) 
 142     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobutton") ); 
 144     wxControl::SetLabel( label 
); 
 145     GtkButton 
*bin 
= GTK_BUTTON( m_widget 
); 
 146     GtkLabel 
*g_label 
= GTK_LABEL( bin
->child 
); 
 147     gtk_label_set( g_label
, GetLabel().mbc_str() ); 
 150 void wxRadioButton::SetValue( bool val 
) 
 152     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobutton") ); 
 154     if (val 
== GetValue()) 
 157     gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget
), 
 158       GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this ); 
 162         gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget
), TRUE 
); 
 166         // should give an assert 
 167         // RL - No it shouldn't.  A wxGenericValidator might try to set it 
 168         //      as FALSE.  Failing silently is probably TRTTD here. 
 171     gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",  
 172       GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this ); 
 175 bool wxRadioButton::GetValue() const 
 177     wxCHECK_MSG( m_widget 
!= NULL
, FALSE
, wxT("invalid radiobutton") ); 
 179     return GTK_TOGGLE_BUTTON(m_widget
)->active
; 
 182 bool wxRadioButton::Enable( bool enable 
) 
 184     if ( !wxControl::Enable( enable 
) ) 
 187     gtk_widget_set_sensitive( GTK_BUTTON(m_widget
)->child
, enable 
); 
 192 void wxRadioButton::ApplyWidgetStyle() 
 195     gtk_widget_set_style( m_widget
, m_widgetStyle 
); 
 196     gtk_widget_set_style( GTK_BUTTON(m_widget
)->child
, m_widgetStyle 
); 
 199 bool wxRadioButton::IsOwnGtkWindow( GdkWindow 
*window 
) 
 201     return (window 
== GTK_TOGGLE_BUTTON(m_widget
)->event_window
); 
 204 void wxRadioButton::OnInternalIdle() 
 206     wxCursor cursor 
= m_cursor
; 
 207     if (g_globalCursor
.Ok()) cursor 
= g_globalCursor
; 
 209     if (GTK_TOGGLE_BUTTON(m_widget
)->event_window 
&& cursor
.Ok()) 
 211         /* I now set the cursor the anew in every OnInternalIdle call 
 212            as setting the cursor in a parent window also effects the 
 213            windows above so that checking for the current cursor is 
 216            gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget
)->event_window
, cursor
.GetCursor() ); 
 222 wxSize 
wxRadioButton::DoGetBestSize() const 
 224     return wxControl::DoGetBestSize();