1 ///////////////////////////////////////////////////////////////////////////// 
   4 // Author:      Robert Roebling 
   6 // Copyright:   (c) 1998 Robert Roebling 
   7 // Licence:     wxWindows licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  11 #pragma implementation "radiobox.h" 
  18 #include "wx/radiobox.h" 
  20 #include "wx/dialog.h" 
  24 #include "wx/gtk/private.h" 
  25 #include <gdk/gdkkeysyms.h> 
  27 #include "wx/gtk/win_gtk.h" 
  29 //----------------------------------------------------------------------------- 
  31 //----------------------------------------------------------------------------- 
  33 extern void wxapp_install_idle_handler(); 
  36 //----------------------------------------------------------------------------- 
  38 //----------------------------------------------------------------------------- 
  40 extern bool          g_blockEventsOnDrag
; 
  41 extern wxWindowGTK  
*g_delayedFocus
; 
  43 //----------------------------------------------------------------------------- 
  45 //----------------------------------------------------------------------------- 
  47 static void gtk_radiobutton_clicked_callback( GtkToggleButton 
*button
, wxRadioBox 
*rb 
) 
  49     if (g_isIdle
) wxapp_install_idle_handler(); 
  51     if (!rb
->m_hasVMT
) return; 
  52     if (g_blockEventsOnDrag
) return; 
  54     if (!button
->active
) return; 
  56     wxCommandEvent 
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() ); 
  57     event
.SetInt( rb
->GetSelection() ); 
  58     event
.SetString( rb
->GetStringSelection() ); 
  59     event
.SetEventObject( rb 
); 
  60     rb
->GetEventHandler()->ProcessEvent(event
); 
  63 //----------------------------------------------------------------------------- 
  65 //----------------------------------------------------------------------------- 
  67 static gint 
gtk_radiobox_keypress_callback( GtkWidget 
*widget
, GdkEventKey 
*gdk_event
, wxRadioBox 
*rb 
) 
  70         wxapp_install_idle_handler(); 
  72     if (!rb
->m_hasVMT
) return FALSE
; 
  73     if (g_blockEventsOnDrag
) return FALSE
; 
  75     if ((gdk_event
->keyval 
!= GDK_Up
) && 
  76         (gdk_event
->keyval 
!= GDK_Down
) && 
  77         (gdk_event
->keyval 
!= GDK_Left
) && 
  78         (gdk_event
->keyval 
!= GDK_Right
)) 
  83     wxList::compatibility_iterator node 
= rb
->m_boxes
.Find( (wxObject
*) widget 
); 
  89     gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" ); 
  91     if ((gdk_event
->keyval 
== GDK_Up
) || 
  92         (gdk_event
->keyval 
== GDK_Left
)) 
  94         if (node 
== rb
->m_boxes
.GetFirst()) 
  95             node 
= rb
->m_boxes
.GetLast(); 
  97             node 
= node
->GetPrevious(); 
 101         if (node 
== rb
->m_boxes
.GetLast()) 
 102             node 
= rb
->m_boxes
.GetFirst(); 
 104             node 
= node
->GetNext(); 
 107     GtkWidget 
*button 
= (GtkWidget
*) node
->GetData(); 
 109     gtk_widget_grab_focus( button 
); 
 114 static gint 
gtk_radiobutton_focus_in( GtkWidget 
*widget
, 
 115                                       GdkEvent 
*WXUNUSED(event
), 
 118     if ( win
->m_lostFocus 
) 
 120         // no, we didn't really lose it 
 121         win
->m_lostFocus 
= FALSE
; 
 123     else if ( !win
->m_hasFocus 
) 
 125         win
->m_hasFocus 
= TRUE
; 
 127         wxFocusEvent 
event( wxEVT_SET_FOCUS
, win
->GetId() ); 
 128         event
.SetEventObject( win 
); 
 130         // never stop the signal emission, it seems to break the kbd handling 
 131         // inside the radiobox 
 132         (void)win
->GetEventHandler()->ProcessEvent( event 
); 
 138 static gint 
gtk_radiobutton_focus_out( GtkWidget 
*widget
, 
 139                                        GdkEvent 
*WXUNUSED(event
), 
 142   //    wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") ); 
 143   // Replace with a warning, else we dump core a lot! 
 144   //  if (!win->m_hasFocus) 
 145   //      wxLogWarning(_T("Radiobox got focus out without any focus in.") ); 
 147     // we might have lost the focus, but may be not - it may have just gone to 
 148     // another button in the same radiobox, so we'll check for it in the next 
 149     // idle iteration (leave m_hasFocus == TRUE for now) 
 150     win
->m_lostFocus 
= TRUE
; 
 155 //----------------------------------------------------------------------------- 
 157 //----------------------------------------------------------------------------- 
 159 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
) 
 161 void wxRadioBox::Init() 
 164     m_acceptsFocus 
= TRUE
; 
 170 bool wxRadioBox::Create( wxWindow 
*parent
, wxWindowID id
, const wxString
& title
, 
 171                          const wxPoint 
&pos
, const wxSize 
&size
, 
 172                          int n
, const wxString choices
[], int majorDim
, 
 173                          long style
, const wxValidator
& validator
, 
 174                          const wxString 
&name 
) 
 176     if (!PreCreation( parent
, pos
, size 
) || 
 177         !CreateBase( parent
, id
, pos
, size
, style
, validator
, name 
)) 
 179         wxFAIL_MSG( wxT("wxRadioBox creation failed") ); 
 183     m_widget 
= gtk_frame_new( wxGTK_CONV( title 
) ); 
 185     // majorDim may be 0 if all trailing parameters were omitted, so don't 
 186     // assert here but just use the correct value for it 
 187     m_majorDim 
= majorDim 
== 0 ? n 
: majorDim
; 
 189     GtkRadioButton 
*m_radio 
= (GtkRadioButton
*) NULL
; 
 192     GSList 
*radio_button_group 
= (GSList 
*) NULL
; 
 193     for (int i 
= 0; i 
< n
; i
++) 
 196             radio_button_group 
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) ); 
 199         for ( const wxChar 
*pc 
= choices
[i
]; *pc
; pc
++ ) 
 201             if ( *pc 
!= wxT('&') ) 
 205         m_radio 
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label 
) ) ); 
 207         gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event", 
 208            GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this ); 
 210         m_boxes
.Append( (wxObject
*) m_radio 
); 
 212         ConnectWidget( GTK_WIDGET(m_radio
) ); 
 214         if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE 
); 
 216         gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked", 
 217             GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this ); 
 219         gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event", 
 220             GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this ); 
 222         gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event", 
 223             GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this ); 
 225         gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
), 
 227                          m_x
+10, m_y
+10+(i
*24), 10, 10 ); 
 230     m_parent
->DoAddChild( this ); 
 238     SetFont( parent
->GetFont() ); 
 240     wxSize ls 
= LayoutItems(); 
 245     (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request 
) (m_widget
, &req 
); 
 246     if (req
.width 
> ls
.x
) ls
.x 
= req
.width
; 
 248     wxSize newSize 
= size
; 
 249     if (newSize
.x 
== -1) newSize
.x 
= ls
.x
; 
 250     if (newSize
.y 
== -1) newSize
.y 
= ls
.y
; 
 251     SetSize( newSize
.x
, newSize
.y 
); 
 253     SetBackgroundColour( parent
->GetBackgroundColour() ); 
 254     SetForegroundColour( parent
->GetForegroundColour() ); 
 261 wxRadioBox::~wxRadioBox() 
 263     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 266         GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 267         gtk_widget_destroy( button 
); 
 268         node 
= node
->GetNext(); 
 272 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags 
) 
 274     wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags 
); 
 279 wxSize 
wxRadioBox::LayoutItems() 
 284     if ( m_majorDim 
== 0 ) 
 286         // avoid dividing by 0 below 
 287         wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") ); 
 292     int num_per_major 
= (m_boxes
.GetCount() - 1) / m_majorDim 
+1; 
 298     if (HasFlag(wxRA_SPECIFY_COLS
)) 
 300         num_of_cols 
= m_majorDim
; 
 301         num_of_rows 
= num_per_major
; 
 305         num_of_cols 
= num_per_major
; 
 306         num_of_rows 
= m_majorDim
; 
 309     if ( HasFlag(wxRA_SPECIFY_COLS
) || 
 310          (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols 
> 1)) ) 
 312         for (int j 
= 0; j 
< num_of_cols
; j
++) 
 317             wxList::compatibility_iterator node 
= m_boxes
.Item( j
*num_of_rows 
); 
 318             for (int i1 
= 0; i1
< num_of_rows
; i1
++) 
 320                 GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 325                 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request 
) 
 328                 if (req
.width 
> max_len
) max_len 
= req
.width
; 
 330                 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y 
); 
 333                 node 
= node
->GetNext(); 
 337             // we don't know the max_len before 
 339             node 
= m_boxes
.Item( j
*num_of_rows 
); 
 340             for (int i2 
= 0; i2
< num_of_rows
; i2
++) 
 342                 GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 344                 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 ); 
 346                 node 
= node
->GetNext(); 
 350             if (y 
> res
.y
) res
.y 
= y
; 
 362         wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 365             GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 370             (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request 
) 
 373             if (req
.width 
> max
) max 
= req
.width
; 
 375             node 
= node
->GetNext(); 
 378         node 
= m_boxes
.GetFirst(); 
 381             GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 383             gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 ); 
 386             node 
= node
->GetNext(); 
 395 bool wxRadioBox::Show( bool show 
) 
 397     wxCHECK_MSG( m_widget 
!= NULL
, FALSE
, wxT("invalid radiobox") ); 
 399     if (!wxControl::Show(show
)) 
 405     if ((m_windowStyle 
& wxNO_BORDER
) != 0) 
 406         gtk_widget_hide( m_widget 
); 
 408     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 411         GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 413         if (show
) gtk_widget_show( button 
); else gtk_widget_hide( button 
); 
 415         node 
= node
->GetNext(); 
 421 int wxRadioBox::FindString( const wxString 
&find 
) const 
 423     wxCHECK_MSG( m_widget 
!= NULL
, -1, wxT("invalid radiobox") ); 
 427     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 430         GtkLabel 
*label 
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) ); 
 432         wxString 
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) ); 
 434         wxString 
str( label
->label 
); 
 441         node 
= node
->GetNext(); 
 447 void wxRadioBox::SetFocus() 
 449     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobox") ); 
 451     if (m_boxes
.GetCount() == 0) return; 
 453     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 456         GtkToggleButton 
*button 
= GTK_TOGGLE_BUTTON( node
->GetData() ); 
 459             gtk_widget_grab_focus( GTK_WIDGET(button
) ); 
 462         node 
= node
->GetNext(); 
 466 void wxRadioBox::SetSelection( int n 
) 
 468     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobox") ); 
 470     wxList::compatibility_iterator node 
= m_boxes
.Item( n 
); 
 472     wxCHECK_RET( node
, wxT("radiobox wrong index") ); 
 474     GtkToggleButton 
*button 
= GTK_TOGGLE_BUTTON( node
->GetData() ); 
 478     gtk_toggle_button_set_active( button
, 1 ); 
 483 int wxRadioBox::GetSelection(void) const 
 485     wxCHECK_MSG( m_widget 
!= NULL
, -1, wxT("invalid radiobox") ); 
 489     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 492         GtkToggleButton 
*button 
= GTK_TOGGLE_BUTTON( node
->GetData() ); 
 493         if (button
->active
) return count
; 
 495         node 
= node
->GetNext(); 
 498     wxFAIL_MSG( wxT("wxRadioBox none selected") ); 
 503 wxString 
wxRadioBox::GetString( int n 
) const 
 505     wxCHECK_MSG( m_widget 
!= NULL
, wxT(""), wxT("invalid radiobox") ); 
 507     wxList::compatibility_iterator node 
= m_boxes
.Item( n 
); 
 509     wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") ); 
 511     GtkLabel 
*label 
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) ); 
 514     wxString 
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) ); 
 516     wxString 
str( label
->label 
); 
 522 void wxRadioBox::SetLabel( const wxString
& label 
) 
 524     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobox") ); 
 526     wxControl::SetLabel( label 
); 
 528     gtk_frame_set_label( GTK_FRAME(m_widget
), wxGTK_CONV( wxControl::GetLabel() ) ); 
 531 void wxRadioBox::SetString( int item
, const wxString
& label 
) 
 533     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobox") ); 
 535     wxList::compatibility_iterator node 
= m_boxes
.Item( item 
); 
 537     wxCHECK_RET( node
, wxT("radiobox wrong index") ); 
 539     GtkLabel 
*g_label 
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) ); 
 541     gtk_label_set( g_label
, wxGTK_CONV( label 
) ); 
 544 bool wxRadioBox::Enable( bool enable 
) 
 546     if ( !wxControl::Enable( enable 
) ) 
 549     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 552         GtkButton 
*button 
= GTK_BUTTON( node
->GetData() ); 
 553         GtkLabel 
*label 
= GTK_LABEL( BUTTON_CHILD(button
) ); 
 555         gtk_widget_set_sensitive( GTK_WIDGET(button
), enable 
); 
 556         gtk_widget_set_sensitive( GTK_WIDGET(label
), enable 
); 
 557         node 
= node
->GetNext(); 
 563 void wxRadioBox::Enable( int item
, bool enable 
) 
 565     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobox") ); 
 567     wxList::compatibility_iterator node 
= m_boxes
.Item( item 
); 
 569     wxCHECK_RET( node
, wxT("radiobox wrong index") ); 
 571     GtkButton 
*button 
= GTK_BUTTON( node
->GetData() ); 
 572     GtkLabel 
*label 
= GTK_LABEL( BUTTON_CHILD(button
) ); 
 574     gtk_widget_set_sensitive( GTK_WIDGET(button
), enable 
); 
 575     gtk_widget_set_sensitive( GTK_WIDGET(label
), enable 
); 
 578 void wxRadioBox::Show( int item
, bool show 
) 
 580     wxCHECK_RET( m_widget 
!= NULL
, wxT("invalid radiobox") ); 
 582     wxList::compatibility_iterator node 
= m_boxes
.Item( item 
); 
 584     wxCHECK_RET( node
, wxT("radiobox wrong index") ); 
 586     GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 589         gtk_widget_show( button 
); 
 591         gtk_widget_hide( button 
); 
 594 wxString 
wxRadioBox::GetStringSelection() const 
 596     wxCHECK_MSG( m_widget 
!= NULL
, wxT(""), wxT("invalid radiobox") ); 
 598     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 601         GtkToggleButton 
*button 
= GTK_TOGGLE_BUTTON( node
->GetData() ); 
 604             GtkLabel 
*label 
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) ); 
 607             wxString 
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) ); 
 609             wxString 
str( label
->label 
); 
 613         node 
= node
->GetNext(); 
 616     wxFAIL_MSG( wxT("wxRadioBox none selected") ); 
 620 bool wxRadioBox::SetStringSelection( const wxString 
&s 
) 
 622     wxCHECK_MSG( m_widget 
!= NULL
, FALSE
, wxT("invalid radiobox") ); 
 624     int res 
= FindString( s 
); 
 625     if (res 
== -1) return FALSE
; 
 631 int wxRadioBox::GetCount() const 
 633     return m_boxes
.GetCount(); 
 636 int wxRadioBox::GetNumberOfRowsOrCols() const 
 641 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) ) 
 643     wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented.")); 
 646 void wxRadioBox::GtkDisableEvents() 
 648     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 651         gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()), 
 652            GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this ); 
 654         node 
= node
->GetNext(); 
 658 void wxRadioBox::GtkEnableEvents() 
 660     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 663         gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked", 
 664            GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this ); 
 666         node 
= node
->GetNext(); 
 670 void wxRadioBox::ApplyWidgetStyle() 
 674     gtk_widget_set_style( m_widget
, m_widgetStyle 
); 
 676     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 679         GtkWidget 
*widget 
= GTK_WIDGET( node
->GetData() ); 
 680         gtk_widget_set_style( widget
, m_widgetStyle 
); 
 682         gtk_widget_set_style( BUTTON_CHILD(node
->GetData()), m_widgetStyle 
); 
 684         node 
= node
->GetNext(); 
 689 void wxRadioBox::ApplyToolTip( GtkTooltips 
*tips
, const wxChar 
*tip 
) 
 691     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 694         GtkWidget 
*widget 
= GTK_WIDGET( node
->GetData() ); 
 695         gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL 
); 
 696         node 
= node
->GetNext(); 
 699 #endif // wxUSE_TOOLTIPS 
 701 bool wxRadioBox::IsOwnGtkWindow( GdkWindow 
*window 
) 
 703     if (window 
== m_widget
->window
) return TRUE
; 
 705     wxList::compatibility_iterator node 
= m_boxes
.GetFirst(); 
 708         GtkWidget 
*button 
= GTK_WIDGET( node
->GetData() ); 
 710         if (window 
== button
->window
) return TRUE
; 
 712         node 
= node
->GetNext(); 
 718 void wxRadioBox::OnInternalIdle() 
 725         wxFocusEvent 
event( wxEVT_KILL_FOCUS
, GetId() ); 
 726         event
.SetEventObject( this ); 
 728         (void)GetEventHandler()->ProcessEvent( event 
); 
 731     if (g_delayedFocus 
== this) 
 733         if (GTK_WIDGET_REALIZED(m_widget
)) 
 735             g_delayedFocus 
= NULL
; 
 741 #endif // wxUSE_RADIOBOX