1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Robert Roebling 
   6 // Copyright:   (c) 1998 Robert Roebling 
   7 // Licence:     wxWindows licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) 
  12 #pragma implementation "radiobut.h" 
  15 // For compilers that support precompilation, includes "wx.h". 
  16 #include "wx/wxprec.h" 
  20 #include "wx/radiobut.h" 
  22 #include "wx/gtk/private.h" 
  24 //----------------------------------------------------------------------------- 
  26 //----------------------------------------------------------------------------- 
  28 extern void wxapp_install_idle_handler(); 
  31 //----------------------------------------------------------------------------- 
  33 //----------------------------------------------------------------------------- 
  35 extern bool           g_blockEventsOnDrag
; 
  36 extern wxCursor       g_globalCursor
; 
  37 extern wxWindowGTK   
*g_delayedFocus
; 
  39 //----------------------------------------------------------------------------- 
  41 //----------------------------------------------------------------------------- 
  45 void gtk_radiobutton_clicked_callback( GtkToggleButton 
*button
, wxRadioButton 
*rb 
) 
  47     if (g_isIdle
) wxapp_install_idle_handler(); 
  49     if (!rb
->m_hasVMT
) return; 
  51     if (g_blockEventsOnDrag
) return; 
  53     if (!button
->active
) return; 
  55     if (rb
->m_blockEvent
) return; 
  57     wxCommandEvent 
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId()); 
  58     event
.SetInt( rb
->GetValue() ); 
  59     event
.SetEventObject( rb 
); 
  60     rb
->GetEventHandler()->ProcessEvent( event 
); 
  64 //----------------------------------------------------------------------------- 
  66 //----------------------------------------------------------------------------- 
  68 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
) 
  70 bool wxRadioButton::Create( wxWindow 
*parent
, 
  72                             const wxString
& label
, 
  76                             const wxValidator
& validator
, 
  77                             const wxString
& name 
) 
  79     m_acceptsFocus 
= TRUE
; 
  84     if (!PreCreation( parent
, pos
, size 
) || 
  85         !CreateBase( parent
, id
, pos
, size
, style
, validator
, name 
)) 
  87         wxFAIL_MSG( wxT("wxRadioButton creation failed") ); 
  91     if (HasFlag(wxRB_GROUP
)) 
  94         m_radioButtonGroup 
= (GSList
*) NULL
; 
  98         // search backward for last group start 
  99         wxRadioButton 
*chief 
= (wxRadioButton
*) NULL
; 
 100         wxWindowList::compatibility_iterator node 
= parent
->GetChildren().GetLast(); 
 103             wxWindow 
*child 
= node
->GetData(); 
 104             if (child
->IsRadioButton()) 
 106                 chief 
= (wxRadioButton
*) child
; 
 107                 if (child
->HasFlag(wxRB_GROUP
)) 
 110             node 
= node
->GetPrevious(); 
 114             // we are part of the group started by chief 
 115             m_radioButtonGroup 
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) ); 
 120             m_radioButtonGroup 
= (GSList
*) NULL
; 
 124     m_widget 
= gtk_radio_button_new_with_label( m_radioButtonGroup
, wxGTK_CONV( label 
) ); 
 128     gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",  
 129       GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this ); 
 131     m_parent
->DoAddChild( this ); 
 138 void wxRadioButton::SetLabel( const wxString
& label 
) 
 140     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobutton") ); 
 142     wxControl::SetLabel( label 
); 
 143     GtkLabel 
*g_label 
= GTK_LABEL( BUTTON_CHILD(m_widget
) ); 
 145     wxString label2 
= PrepareLabelMnemonics( label 
); 
 146     gtk_label_set_text_with_mnemonic( g_label
, wxGTK_CONV( label2 
) ); 
 148     gtk_label_set( g_label
, wxGTK_CONV( GetLabel() ) ); 
 152 void wxRadioButton::SetValue( bool val 
) 
 154     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobutton") ); 
 156     if (val 
== GetValue()) 
 163         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE 
); 
 167         // should give an assert 
 168         // RL - No it shouldn't.  A wxGenericValidator might try to set it 
 169         //      as FALSE.  Failing silently is probably TRTTD here. 
 172     m_blockEvent 
= FALSE
; 
 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( BUTTON_CHILD(m_widget
), enable 
); 
 192 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle 
*style
) 
 194     gtk_widget_modify_style(m_widget
, style
); 
 195     gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
); 
 198 bool wxRadioButton::IsOwnGtkWindow( GdkWindow 
*window 
) 
 200     return window 
== TOGGLE_BUTTON_EVENT_WIN(m_widget
); 
 203 void wxRadioButton::OnInternalIdle() 
 205     wxCursor cursor 
= m_cursor
; 
 206     if (g_globalCursor
.Ok()) cursor 
= g_globalCursor
; 
 208     GdkWindow 
*win 
= TOGGLE_BUTTON_EVENT_WIN(m_widget
); 
 209     if ( win 
&& 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( win
, cursor
.GetCursor() ); 
 219     if (g_delayedFocus 
== this) 
 221         if (GTK_WIDGET_REALIZED(m_widget
)) 
 223             gtk_widget_grab_focus( m_widget 
); 
 224             g_delayedFocus 
= NULL
; 
 228     if (wxUpdateUIEvent::CanUpdate(this)) 
 229         UpdateWindowUI(wxUPDATE_UI_FROMIDLE
); 
 232 wxSize 
wxRadioButton::DoGetBestSize() const 
 234     return wxControl::DoGetBestSize(); 
 239 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant 
WXUNUSED(variant
)) 
 241     wxVisualAttributes attr
; 
 242     // NB: we need toplevel window so that GTK+ can find the right style 
 243     GtkWidget 
*wnd 
= gtk_window_new(GTK_WINDOW_TOPLEVEL
); 
 244     GtkWidget
* widget 
= gtk_radio_button_new_with_label(NULL
, ""); 
 245     gtk_container_add(GTK_CONTAINER(wnd
), widget
); 
 246     attr 
= GetDefaultAttributesFromGTKWidget(widget
); 
 247     gtk_widget_destroy(wnd
);