1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "radiobox.h"
14 #include "wx/radiobox.h"
18 #include "wx/dialog.h"
24 #include <gdk/gdkkeysyms.h>
26 #include "wx/gtk/win_gtk.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern bool g_blockEventsOnDrag
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!rb
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 if (rb
->m_alreadySent
)
54 rb
->m_alreadySent
= FALSE
;
58 rb
->m_alreadySent
= TRUE
;
60 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
61 event
.SetInt( rb
->GetSelection() );
62 event
.SetString( rb
->GetStringSelection() );
63 event
.SetEventObject( rb
);
64 rb
->GetEventHandler()->ProcessEvent(event
);
67 //-----------------------------------------------------------------------------
69 //-----------------------------------------------------------------------------
71 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
74 wxapp_install_idle_handler();
76 if (!rb
->m_hasVMT
) return FALSE
;
77 if (g_blockEventsOnDrag
) return FALSE
;
79 if ((gdk_event
->keyval
!= GDK_Up
) &&
80 (gdk_event
->keyval
!= GDK_Down
) &&
81 (gdk_event
->keyval
!= GDK_Left
) &&
82 (gdk_event
->keyval
!= GDK_Right
))
87 wxNode
*node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
93 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
95 if ((gdk_event
->keyval
== GDK_Up
) ||
96 (gdk_event
->keyval
== GDK_Left
))
98 if (node
== rb
->m_boxes
.First())
99 node
= rb
->m_boxes
.Last();
101 node
= node
->Previous();
105 if (node
== rb
->m_boxes
.Last())
106 node
= rb
->m_boxes
.First();
111 GtkWidget
*button
= (GtkWidget
*) node
->Data();
113 gtk_widget_grab_focus( button
);
118 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
119 GdkEvent
*WXUNUSED(event
),
122 if ( win
->m_lostFocus
)
124 // no, we didn't really lose it
125 win
->m_lostFocus
= FALSE
;
127 else if ( !win
->m_hasFocus
)
129 win
->m_hasFocus
= TRUE
;
131 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
132 event
.SetEventObject( win
);
134 // never stop the signal emission, it seems to break the kbd handling
135 // inside the radiobox
136 (void)win
->GetEventHandler()->ProcessEvent( event
);
142 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
143 GdkEvent
*WXUNUSED(event
),
146 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
147 // Replace with a warning, else we dump core a lot!
148 // if (!win->m_hasFocus)
149 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
151 // we might have lost the focus, but may be not - it may have just gone to
152 // another button in the same radiobox, so we'll check for it in the next
153 // idle iteration (leave m_hasFocus == TRUE for now)
154 win
->m_lostFocus
= TRUE
;
159 //-----------------------------------------------------------------------------
161 //-----------------------------------------------------------------------------
163 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
165 void wxRadioBox::Init()
167 m_alreadySent
= FALSE
;
169 m_acceptsFocus
= TRUE
;
175 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
176 const wxPoint
&pos
, const wxSize
&size
,
177 int n
, const wxString choices
[], int majorDim
,
178 long style
, const wxValidator
& validator
,
179 const wxString
&name
)
181 if (!PreCreation( parent
, pos
, size
) ||
182 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
184 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
188 m_widget
= gtk_frame_new( title
.mbc_str() );
190 // majorDim may be 0 if all trailing parameters were omitted, so don't
191 // assert here but just use the correct value for it
192 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
194 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
197 GSList
*radio_button_group
= (GSList
*) NULL
;
198 for (int i
= 0; i
< n
; i
++)
201 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
204 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
206 if ( *pc
!= wxT('&') )
210 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
212 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
213 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
215 m_boxes
.Append( (wxObject
*) m_radio
);
217 ConnectWidget( GTK_WIDGET(m_radio
) );
219 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
221 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
222 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
224 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
225 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
227 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
228 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
230 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
232 m_x
+10, m_y
+10+(i
*24), 10, 10 );
235 m_parent
->DoAddChild( this );
243 SetFont( parent
->GetFont() );
245 wxSize ls
= LayoutItems();
250 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
) (m_widget
, &req
);
251 if (req
.width
> ls
.x
) ls
.x
= req
.width
;
253 wxSize newSize
= size
;
254 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
255 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
256 SetSize( newSize
.x
, newSize
.y
);
258 SetBackgroundColour( parent
->GetBackgroundColour() );
259 SetForegroundColour( parent
->GetForegroundColour() );
266 wxRadioBox::~wxRadioBox()
268 wxNode
*node
= m_boxes
.First();
271 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
272 gtk_widget_destroy( button
);
277 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
279 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
284 wxSize
wxRadioBox::LayoutItems()
289 if ( m_majorDim
== 0 )
291 // avoid dividing by 0 below
292 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
297 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
303 if (HasFlag(wxRA_SPECIFY_COLS
))
305 num_of_cols
= m_majorDim
;
306 num_of_rows
= num_per_major
;
310 num_of_cols
= num_per_major
;
311 num_of_rows
= m_majorDim
;
314 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
315 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
317 for (int j
= 0; j
< num_of_cols
; j
++)
322 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
323 for (int i1
= 0; i1
< num_of_rows
; i1
++)
325 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
330 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
333 if (req
.width
> max_len
) max_len
= req
.width
;
335 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
342 // we don't know the max_len before
344 node
= m_boxes
.Nth( j
*num_of_rows
);
345 for (int i2
= 0; i2
< num_of_rows
; i2
++)
347 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
349 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
355 if (y
> res
.y
) res
.y
= y
;
367 wxNode
*node
= m_boxes
.First();
370 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
375 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
378 if (req
.width
> max
) max
= req
.width
;
383 node
= m_boxes
.First();
386 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
388 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
400 bool wxRadioBox::Show( bool show
)
402 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
404 if (!wxControl::Show(show
))
410 if ((m_windowStyle
& wxNO_BORDER
) != 0)
411 gtk_widget_hide( m_widget
);
413 wxNode
*node
= m_boxes
.First();
416 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
418 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
426 int wxRadioBox::FindString( const wxString
&s
) const
428 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
432 wxNode
*node
= m_boxes
.First();
435 GtkButton
*button
= GTK_BUTTON( node
->Data() );
437 GtkLabel
*label
= GTK_LABEL( button
->child
);
438 if (s
== label
->label
) return count
;
447 void wxRadioBox::SetFocus()
449 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
451 if (m_boxes
.GetCount() == 0) return;
453 wxNode
*node
= m_boxes
.First();
456 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
459 gtk_widget_grab_focus( GTK_WIDGET(button
) );
467 void wxRadioBox::SetSelection( int n
)
469 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
471 wxNode
*node
= m_boxes
.Nth( n
);
473 wxCHECK_RET( node
, wxT("radiobox wrong index") );
475 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
479 gtk_toggle_button_set_state( button
, 1 );
484 int wxRadioBox::GetSelection(void) const
486 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
490 wxNode
*node
= m_boxes
.First();
493 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
494 if (button
->active
) return count
;
499 wxFAIL_MSG( wxT("wxRadioBox none selected") );
504 wxString
wxRadioBox::GetString( int n
) const
506 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
508 wxNode
*node
= m_boxes
.Nth( n
);
510 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
512 GtkButton
*button
= GTK_BUTTON( node
->Data() );
513 GtkLabel
*label
= GTK_LABEL( button
->child
);
515 return wxString( label
->label
);
518 wxString
wxRadioBox::GetLabel( int item
) const
520 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
522 return GetString( item
);
525 void wxRadioBox::SetLabel( const wxString
& label
)
527 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
529 wxControl::SetLabel( label
);
531 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
534 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
536 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
538 wxNode
*node
= m_boxes
.Nth( item
);
540 wxCHECK_RET( node
, wxT("radiobox wrong index") );
542 GtkButton
*button
= GTK_BUTTON( node
->Data() );
543 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
545 gtk_label_set( g_label
, label
.mbc_str() );
548 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
550 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
553 bool wxRadioBox::Enable( bool enable
)
555 if ( !wxControl::Enable( enable
) )
558 wxNode
*node
= m_boxes
.First();
561 GtkButton
*button
= GTK_BUTTON( node
->Data() );
562 GtkWidget
*label
= button
->child
;
563 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
564 gtk_widget_set_sensitive( label
, enable
);
571 void wxRadioBox::Enable( int item
, bool enable
)
573 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
575 wxNode
*node
= m_boxes
.Nth( item
);
577 wxCHECK_RET( node
, wxT("radiobox wrong index") );
579 GtkButton
*button
= GTK_BUTTON( node
->Data() );
580 GtkWidget
*label
= button
->child
;
581 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
582 gtk_widget_set_sensitive( label
, enable
);
585 void wxRadioBox::Show( int item
, bool show
)
587 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
589 wxNode
*node
= m_boxes
.Nth( item
);
591 wxCHECK_RET( node
, wxT("radiobox wrong index") );
593 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
596 gtk_widget_show( button
);
598 gtk_widget_hide( button
);
601 wxString
wxRadioBox::GetStringSelection() const
603 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
605 wxNode
*node
= m_boxes
.First();
608 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
611 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
617 wxFAIL_MSG( wxT("wxRadioBox none selected") );
621 bool wxRadioBox::SetStringSelection( const wxString
&s
)
623 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
625 int res
= FindString( s
);
626 if (res
== -1) return FALSE
;
632 int wxRadioBox::Number() const
634 return m_boxes
.Number();
637 int wxRadioBox::GetNumberOfRowsOrCols() const
642 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
644 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
647 void wxRadioBox::GtkDisableEvents()
649 wxNode
*node
= m_boxes
.First();
652 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
653 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
659 void wxRadioBox::GtkEnableEvents()
661 wxNode
*node
= m_boxes
.First();
664 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
665 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
671 void wxRadioBox::ApplyWidgetStyle()
675 gtk_widget_set_style( m_widget
, m_widgetStyle
);
677 wxNode
*node
= m_boxes
.First();
680 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
681 gtk_widget_set_style( widget
, m_widgetStyle
);
683 GtkButton
*button
= GTK_BUTTON( node
->Data() );
684 gtk_widget_set_style( button
->child
, m_widgetStyle
);
691 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
693 wxNode
*node
= m_boxes
.First();
696 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
697 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
701 #endif // wxUSE_TOOLTIPS
703 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
705 if (window
== m_widget
->window
) return TRUE
;
707 wxNode
*node
= m_boxes
.First();
710 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
712 if (window
== button
->window
) return TRUE
;
720 void wxRadioBox::OnInternalIdle()
727 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
728 event
.SetEventObject( this );
730 (void)GetEventHandler()->ProcessEvent( event
);
734 #endif // wxUSE_RADIOBOX