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 m_majorDim
= majorDim
;
192 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
195 GSList
*radio_button_group
= (GSList
*) NULL
;
196 for (int i
= 0; i
< n
; i
++)
199 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
202 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
204 if ( *pc
!= wxT('&') )
208 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
210 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
211 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
213 m_boxes
.Append( (wxObject
*) m_radio
);
215 ConnectWidget( GTK_WIDGET(m_radio
) );
217 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
219 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
220 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
222 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
223 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
225 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
226 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
228 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
230 m_x
+10, m_y
+10+(i
*24), 10, 10 );
233 m_parent
->DoAddChild( this );
241 SetFont( parent
->GetFont() );
243 wxSize ls
= LayoutItems();
248 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
) (m_widget
, &req
);
249 if (req
.width
> ls
.x
) ls
.x
= req
.width
;
251 wxSize newSize
= size
;
252 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
253 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
254 SetSize( newSize
.x
, newSize
.y
);
256 SetBackgroundColour( parent
->GetBackgroundColour() );
257 SetForegroundColour( parent
->GetForegroundColour() );
264 wxRadioBox::~wxRadioBox()
266 wxNode
*node
= m_boxes
.First();
269 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
270 gtk_widget_destroy( button
);
275 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
277 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
282 wxSize
wxRadioBox::LayoutItems()
287 if ( m_majorDim
== 0 )
289 // avoid dividing by 0 below
290 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
295 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
301 if (HasFlag(wxRA_SPECIFY_COLS
))
303 num_of_cols
= m_majorDim
;
304 num_of_rows
= num_per_major
;
308 num_of_cols
= num_per_major
;
309 num_of_rows
= m_majorDim
;
312 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
313 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
315 for (int j
= 0; j
< num_of_cols
; j
++)
320 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
321 for (int i1
= 0; i1
< num_of_rows
; i1
++)
323 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
328 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
331 if (req
.width
> max_len
) max_len
= req
.width
;
333 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
340 // we don't know the max_len before
342 node
= m_boxes
.Nth( j
*num_of_rows
);
343 for (int i2
= 0; i2
< num_of_rows
; i2
++)
345 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
347 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
353 if (y
> res
.y
) res
.y
= y
;
365 wxNode
*node
= m_boxes
.First();
368 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
373 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
376 if (req
.width
> max
) max
= req
.width
;
381 node
= m_boxes
.First();
384 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
386 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
398 bool wxRadioBox::Show( bool show
)
400 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
402 if (!wxControl::Show(show
))
408 if ((m_windowStyle
& wxNO_BORDER
) != 0)
409 gtk_widget_hide( m_widget
);
411 wxNode
*node
= m_boxes
.First();
414 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
416 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
424 int wxRadioBox::FindString( const wxString
&s
) const
426 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
430 wxNode
*node
= m_boxes
.First();
433 GtkButton
*button
= GTK_BUTTON( node
->Data() );
435 GtkLabel
*label
= GTK_LABEL( button
->child
);
436 if (s
== label
->label
) return count
;
445 void wxRadioBox::SetFocus()
447 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
449 if (m_boxes
.GetCount() == 0) return;
451 wxNode
*node
= m_boxes
.First();
454 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
457 gtk_widget_grab_focus( GTK_WIDGET(button
) );
465 void wxRadioBox::SetSelection( int n
)
467 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
469 wxNode
*node
= m_boxes
.Nth( n
);
471 wxCHECK_RET( node
, wxT("radiobox wrong index") );
473 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
477 gtk_toggle_button_set_state( button
, 1 );
482 int wxRadioBox::GetSelection(void) const
484 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
488 wxNode
*node
= m_boxes
.First();
491 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
492 if (button
->active
) return count
;
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 wxNode
*node
= m_boxes
.Nth( n
);
508 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
510 GtkButton
*button
= GTK_BUTTON( node
->Data() );
511 GtkLabel
*label
= GTK_LABEL( button
->child
);
513 return wxString( label
->label
);
516 wxString
wxRadioBox::GetLabel( int item
) const
518 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
520 return GetString( item
);
523 void wxRadioBox::SetLabel( const wxString
& label
)
525 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
527 wxControl::SetLabel( label
);
529 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
532 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
534 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
536 wxNode
*node
= m_boxes
.Nth( item
);
538 wxCHECK_RET( node
, wxT("radiobox wrong index") );
540 GtkButton
*button
= GTK_BUTTON( node
->Data() );
541 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
543 gtk_label_set( g_label
, label
.mbc_str() );
546 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
548 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
551 bool wxRadioBox::Enable( bool enable
)
553 if ( !wxControl::Enable( enable
) )
556 wxNode
*node
= m_boxes
.First();
559 GtkButton
*button
= GTK_BUTTON( node
->Data() );
560 GtkWidget
*label
= button
->child
;
561 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
562 gtk_widget_set_sensitive( label
, enable
);
569 void wxRadioBox::Enable( int item
, bool enable
)
571 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
573 wxNode
*node
= m_boxes
.Nth( item
);
575 wxCHECK_RET( node
, wxT("radiobox wrong index") );
577 GtkButton
*button
= GTK_BUTTON( node
->Data() );
578 GtkWidget
*label
= button
->child
;
579 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
580 gtk_widget_set_sensitive( label
, enable
);
583 void wxRadioBox::Show( int item
, bool show
)
585 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
587 wxNode
*node
= m_boxes
.Nth( item
);
589 wxCHECK_RET( node
, wxT("radiobox wrong index") );
591 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
594 gtk_widget_show( button
);
596 gtk_widget_hide( button
);
599 wxString
wxRadioBox::GetStringSelection() const
601 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
603 wxNode
*node
= m_boxes
.First();
606 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
609 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
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::Number() const
632 return m_boxes
.Number();
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 wxNode
*node
= m_boxes
.First();
650 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
651 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
657 void wxRadioBox::GtkEnableEvents()
659 wxNode
*node
= m_boxes
.First();
662 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
663 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
669 void wxRadioBox::ApplyWidgetStyle()
673 gtk_widget_set_style( m_widget
, m_widgetStyle
);
675 wxNode
*node
= m_boxes
.First();
678 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
679 gtk_widget_set_style( widget
, m_widgetStyle
);
681 GtkButton
*button
= GTK_BUTTON( node
->Data() );
682 gtk_widget_set_style( button
->child
, m_widgetStyle
);
689 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
691 wxNode
*node
= m_boxes
.First();
694 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
695 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
699 #endif // wxUSE_TOOLTIPS
701 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
703 if (window
== m_widget
->window
) return TRUE
;
705 wxNode
*node
= m_boxes
.First();
708 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
710 if (window
== button
->window
) return TRUE
;
718 void wxRadioBox::OnInternalIdle()
725 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
726 event
.SetEventObject( this );
728 (void)GetEventHandler()->ProcessEvent( event
);
732 #endif // wxUSE_RADIOBOX