1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "radiobox.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
19 #include "wx/radiobox.h"
21 #include "wx/dialog.h"
25 #include "wx/gtk/private.h"
26 #include <gdk/gdkkeysyms.h>
28 #include "wx/gtk/win_gtk.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern bool g_blockEventsOnDrag
;
42 extern wxWindowGTK
*g_delayedFocus
;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
50 if (g_isIdle
) wxapp_install_idle_handler();
52 if (!rb
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 if (!button
->active
) return;
57 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
58 event
.SetInt( rb
->GetSelection() );
59 event
.SetString( rb
->GetStringSelection() );
60 event
.SetEventObject( rb
);
61 rb
->GetEventHandler()->ProcessEvent(event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
71 wxapp_install_idle_handler();
73 if (!rb
->m_hasVMT
) return FALSE
;
74 if (g_blockEventsOnDrag
) return FALSE
;
76 if ((gdk_event
->keyval
!= GDK_Up
) &&
77 (gdk_event
->keyval
!= GDK_Down
) &&
78 (gdk_event
->keyval
!= GDK_Left
) &&
79 (gdk_event
->keyval
!= GDK_Right
))
84 wxList::compatibility_iterator node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
90 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
92 if ((gdk_event
->keyval
== GDK_Up
) ||
93 (gdk_event
->keyval
== GDK_Left
))
95 if (node
== rb
->m_boxes
.GetFirst())
96 node
= rb
->m_boxes
.GetLast();
98 node
= node
->GetPrevious();
102 if (node
== rb
->m_boxes
.GetLast())
103 node
= rb
->m_boxes
.GetFirst();
105 node
= node
->GetNext();
108 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
110 gtk_widget_grab_focus( button
);
115 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
116 GdkEvent
*WXUNUSED(event
),
119 if ( win
->m_lostFocus
)
121 // no, we didn't really lose it
122 win
->m_lostFocus
= FALSE
;
124 else if ( !win
->m_hasFocus
)
126 win
->m_hasFocus
= TRUE
;
128 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
129 event
.SetEventObject( win
);
131 // never stop the signal emission, it seems to break the kbd handling
132 // inside the radiobox
133 (void)win
->GetEventHandler()->ProcessEvent( event
);
139 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
140 GdkEvent
*WXUNUSED(event
),
143 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
144 // Replace with a warning, else we dump core a lot!
145 // if (!win->m_hasFocus)
146 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
148 // we might have lost the focus, but may be not - it may have just gone to
149 // another button in the same radiobox, so we'll check for it in the next
150 // idle iteration (leave m_hasFocus == TRUE for now)
151 win
->m_lostFocus
= TRUE
;
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
162 void wxRadioBox::Init()
165 m_acceptsFocus
= TRUE
;
171 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
172 const wxPoint
&pos
, const wxSize
&size
,
173 int n
, const wxString choices
[], int majorDim
,
174 long style
, const wxValidator
& validator
,
175 const wxString
&name
)
177 if (!PreCreation( parent
, pos
, size
) ||
178 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
180 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
184 m_widget
= gtk_frame_new( wxGTK_CONV( title
) );
186 // majorDim may be 0 if all trailing parameters were omitted, so don't
187 // assert here but just use the correct value for it
188 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
190 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
193 GSList
*radio_button_group
= (GSList
*) NULL
;
194 for (int i
= 0; i
< n
; i
++)
197 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
200 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
202 if ( *pc
!= wxT('&') )
206 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
208 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
209 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
211 m_boxes
.Append( (wxObject
*) m_radio
);
213 ConnectWidget( GTK_WIDGET(m_radio
) );
215 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
217 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
218 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
220 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
221 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
223 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
224 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
226 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
228 m_x
+10, m_y
+10+(i
*24), 10, 10 );
231 m_parent
->DoAddChild( this );
240 SetFont( parent
->GetFont() );
242 wxSize ls
= LayoutItems();
247 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
) (m_widget
, &req
);
248 if (req
.width
> ls
.x
) ls
.x
= req
.width
;
250 wxSize newSize
= size
;
251 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
252 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
253 SetSize( newSize
.x
, newSize
.y
);
260 wxRadioBox::~wxRadioBox()
262 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
265 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
266 gtk_widget_destroy( button
);
267 node
= node
->GetNext();
271 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
273 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
278 wxSize
wxRadioBox::LayoutItems()
283 if ( m_majorDim
== 0 )
285 // avoid dividing by 0 below
286 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
291 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
297 if (HasFlag(wxRA_SPECIFY_COLS
))
299 num_of_cols
= m_majorDim
;
300 num_of_rows
= num_per_major
;
304 num_of_cols
= num_per_major
;
305 num_of_rows
= m_majorDim
;
308 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
309 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
311 for (int j
= 0; j
< num_of_cols
; j
++)
316 wxList::compatibility_iterator node
= m_boxes
.Item( j
*num_of_rows
);
317 for (int i1
= 0; i1
< num_of_rows
; i1
++)
319 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
324 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
327 if (req
.width
> max_len
) max_len
= req
.width
;
329 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
332 node
= node
->GetNext();
336 // we don't know the max_len before
338 node
= m_boxes
.Item( j
*num_of_rows
);
339 for (int i2
= 0; i2
< num_of_rows
; i2
++)
341 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
343 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
345 node
= node
->GetNext();
349 if (y
> res
.y
) res
.y
= y
;
361 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
364 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
369 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
372 if (req
.width
> max
) max
= req
.width
;
374 node
= node
->GetNext();
377 node
= m_boxes
.GetFirst();
380 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
382 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
385 node
= node
->GetNext();
394 bool wxRadioBox::Show( bool show
)
396 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
398 if (!wxControl::Show(show
))
404 if ((m_windowStyle
& wxNO_BORDER
) != 0)
405 gtk_widget_hide( m_widget
);
407 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
410 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
412 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
414 node
= node
->GetNext();
420 int wxRadioBox::FindString( const wxString
&find
) const
422 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
426 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
429 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
431 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
433 wxString
str( label
->label
);
440 node
= node
->GetNext();
446 void wxRadioBox::SetFocus()
448 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
450 if (m_boxes
.GetCount() == 0) return;
452 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
455 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
458 gtk_widget_grab_focus( GTK_WIDGET(button
) );
461 node
= node
->GetNext();
465 void wxRadioBox::SetSelection( int n
)
467 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
469 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
471 wxCHECK_RET( node
, wxT("radiobox wrong index") );
473 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
477 gtk_toggle_button_set_active( button
, 1 );
482 int wxRadioBox::GetSelection(void) const
484 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
488 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
491 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
492 if (button
->active
) return count
;
494 node
= node
->GetNext();
497 wxFAIL_MSG( wxT("wxRadioBox none selected") );
502 wxString
wxRadioBox::GetString( int n
) const
504 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
506 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
508 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
510 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
513 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
515 wxString
str( label
->label
);
521 void wxRadioBox::SetLabel( const wxString
& label
)
523 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
525 wxControl::SetLabel( label
);
527 gtk_frame_set_label( GTK_FRAME(m_widget
), wxGTK_CONV( wxControl::GetLabel() ) );
530 void wxRadioBox::SetString( int item
, const wxString
& label
)
532 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
534 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
536 wxCHECK_RET( node
, wxT("radiobox wrong index") );
538 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
540 gtk_label_set( g_label
, wxGTK_CONV( label
) );
543 bool wxRadioBox::Enable( bool enable
)
545 if ( !wxControl::Enable( enable
) )
548 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
551 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
552 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
554 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
555 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
556 node
= node
->GetNext();
562 void wxRadioBox::Enable( int item
, bool enable
)
564 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
566 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
568 wxCHECK_RET( node
, wxT("radiobox wrong index") );
570 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
571 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
573 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
574 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
577 void wxRadioBox::Show( int item
, bool show
)
579 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
581 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
583 wxCHECK_RET( node
, wxT("radiobox wrong index") );
585 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
588 gtk_widget_show( button
);
590 gtk_widget_hide( button
);
593 wxString
wxRadioBox::GetStringSelection() const
595 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
597 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
600 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
603 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
606 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
608 wxString
str( label
->label
);
612 node
= node
->GetNext();
615 wxFAIL_MSG( wxT("wxRadioBox none selected") );
619 bool wxRadioBox::SetStringSelection( const wxString
&s
)
621 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
623 int res
= FindString( s
);
624 if (res
== -1) return FALSE
;
630 int wxRadioBox::GetCount() const
632 return m_boxes
.GetCount();
635 int wxRadioBox::GetNumberOfRowsOrCols() const
640 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
642 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
645 void wxRadioBox::GtkDisableEvents()
647 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
650 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
651 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
653 node
= node
->GetNext();
657 void wxRadioBox::GtkEnableEvents()
659 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
662 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
663 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
665 node
= node
->GetNext();
669 void wxRadioBox::ApplyWidgetStyle()
673 gtk_widget_set_style( m_widget
, m_widgetStyle
);
675 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
678 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
679 gtk_widget_set_style( widget
, m_widgetStyle
);
681 gtk_widget_set_style( BUTTON_CHILD(node
->GetData()), m_widgetStyle
);
683 node
= node
->GetNext();
688 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
690 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
693 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
694 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
695 node
= node
->GetNext();
698 #endif // wxUSE_TOOLTIPS
700 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
702 if (window
== m_widget
->window
) return TRUE
;
704 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
707 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
709 if (window
== button
->window
) return TRUE
;
711 node
= node
->GetNext();
717 void wxRadioBox::OnInternalIdle()
724 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
725 event
.SetEventObject( this );
727 (void)GetEventHandler()->ProcessEvent( event
);
730 if (g_delayedFocus
== this)
732 if (GTK_WIDGET_REALIZED(m_widget
))
734 g_delayedFocus
= NULL
;
740 #endif // wxUSE_RADIOBOX