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"
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
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (!rb
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 if (rb
->m_alreadySent
)
56 rb
->m_alreadySent
= FALSE
;
60 rb
->m_alreadySent
= TRUE
;
62 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
63 event
.SetInt( rb
->GetSelection() );
64 event
.SetString( rb
->GetStringSelection() );
65 event
.SetEventObject( rb
);
66 rb
->GetEventHandler()->ProcessEvent(event
);
69 //-----------------------------------------------------------------------------
71 //-----------------------------------------------------------------------------
73 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
76 wxapp_install_idle_handler();
78 if (!rb
->m_hasVMT
) return FALSE
;
79 if (g_blockEventsOnDrag
) return FALSE
;
81 if ((gdk_event
->keyval
!= GDK_Up
) &&
82 (gdk_event
->keyval
!= GDK_Down
) &&
83 (gdk_event
->keyval
!= GDK_Left
) &&
84 (gdk_event
->keyval
!= GDK_Right
))
89 wxNode
*node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
95 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
97 if ((gdk_event
->keyval
== GDK_Up
) ||
98 (gdk_event
->keyval
== GDK_Left
))
100 if (node
== rb
->m_boxes
.First())
101 node
= rb
->m_boxes
.Last();
103 node
= node
->Previous();
107 if (node
== rb
->m_boxes
.Last())
108 node
= rb
->m_boxes
.First();
113 GtkWidget
*button
= (GtkWidget
*) node
->Data();
115 gtk_widget_grab_focus( button
);
120 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
121 GdkEvent
*WXUNUSED(event
),
124 if ( win
->m_lostFocus
)
126 // no, we didn't really lose it
127 win
->m_lostFocus
= FALSE
;
129 else if ( !win
->m_hasFocus
)
131 win
->m_hasFocus
= TRUE
;
133 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
134 event
.SetEventObject( win
);
136 // never stop the signal emission, it seems to break the kbd handling
137 // inside the radiobox
138 (void)win
->GetEventHandler()->ProcessEvent( event
);
144 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
145 GdkEvent
*WXUNUSED(event
),
148 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
149 // Replace with a warning, else we dump core a lot!
150 // if (!win->m_hasFocus)
151 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
153 // we might have lost the focus, but may be not - it may have just gone to
154 // another button in the same radiobox, so we'll check for it in the next
155 // idle iteration (leave m_hasFocus == TRUE for now)
156 win
->m_lostFocus
= TRUE
;
161 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
165 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
167 void wxRadioBox::Init()
169 m_alreadySent
= FALSE
;
171 m_acceptsFocus
= TRUE
;
177 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
178 const wxPoint
&pos
, const wxSize
&size
,
179 int n
, const wxString choices
[], int majorDim
,
180 long style
, const wxValidator
& validator
,
181 const wxString
&name
)
183 if (!PreCreation( parent
, pos
, size
) ||
184 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
186 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
190 m_widget
= gtk_frame_new( title
.mbc_str() );
192 // majorDim may be 0 if all trailing parameters were omitted, so don't
193 // assert here but just use the correct value for it
194 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
196 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
199 GSList
*radio_button_group
= (GSList
*) NULL
;
200 for (int i
= 0; i
< n
; i
++)
203 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
206 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
208 if ( *pc
!= wxT('&') )
212 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
214 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
215 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
217 m_boxes
.Append( (wxObject
*) m_radio
);
219 ConnectWidget( GTK_WIDGET(m_radio
) );
221 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
223 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
224 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
226 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
227 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
229 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
230 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
232 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
234 m_x
+10, m_y
+10+(i
*24), 10, 10 );
237 m_parent
->DoAddChild( this );
245 SetFont( parent
->GetFont() );
247 wxSize ls
= LayoutItems();
252 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
) (m_widget
, &req
);
253 if (req
.width
> ls
.x
) ls
.x
= req
.width
;
255 wxSize newSize
= size
;
256 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
257 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
258 SetSize( newSize
.x
, newSize
.y
);
260 SetBackgroundColour( parent
->GetBackgroundColour() );
261 SetForegroundColour( parent
->GetForegroundColour() );
268 wxRadioBox::~wxRadioBox()
270 wxNode
*node
= m_boxes
.First();
273 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
274 gtk_widget_destroy( button
);
279 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
281 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
286 wxSize
wxRadioBox::LayoutItems()
291 if ( m_majorDim
== 0 )
293 // avoid dividing by 0 below
294 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
299 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
305 if (HasFlag(wxRA_SPECIFY_COLS
))
307 num_of_cols
= m_majorDim
;
308 num_of_rows
= num_per_major
;
312 num_of_cols
= num_per_major
;
313 num_of_rows
= m_majorDim
;
316 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
317 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
319 for (int j
= 0; j
< num_of_cols
; j
++)
324 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
325 for (int i1
= 0; i1
< num_of_rows
; i1
++)
327 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
332 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
335 if (req
.width
> max_len
) max_len
= req
.width
;
337 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
344 // we don't know the max_len before
346 node
= m_boxes
.Nth( j
*num_of_rows
);
347 for (int i2
= 0; i2
< num_of_rows
; i2
++)
349 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
351 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
357 if (y
> res
.y
) res
.y
= y
;
369 wxNode
*node
= m_boxes
.First();
372 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
377 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
380 if (req
.width
> max
) max
= req
.width
;
385 node
= m_boxes
.First();
388 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
390 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
402 bool wxRadioBox::Show( bool show
)
404 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
406 if (!wxControl::Show(show
))
412 if ((m_windowStyle
& wxNO_BORDER
) != 0)
413 gtk_widget_hide( m_widget
);
415 wxNode
*node
= m_boxes
.First();
418 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
420 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
428 int wxRadioBox::FindString( const wxString
&s
) const
430 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
434 wxNode
*node
= m_boxes
.First();
437 GtkButton
*button
= GTK_BUTTON( node
->Data() );
439 GtkLabel
*label
= GTK_LABEL( button
->child
);
440 if (s
== label
->label
) return count
;
449 void wxRadioBox::SetFocus()
451 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
453 if (m_boxes
.GetCount() == 0) return;
455 wxNode
*node
= m_boxes
.First();
458 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
461 gtk_widget_grab_focus( GTK_WIDGET(button
) );
469 void wxRadioBox::SetSelection( int n
)
471 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
473 wxNode
*node
= m_boxes
.Nth( n
);
475 wxCHECK_RET( node
, wxT("radiobox wrong index") );
477 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
481 gtk_toggle_button_set_state( button
, 1 );
486 int wxRadioBox::GetSelection(void) const
488 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
492 wxNode
*node
= m_boxes
.First();
495 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
496 if (button
->active
) return count
;
501 wxFAIL_MSG( wxT("wxRadioBox none selected") );
506 wxString
wxRadioBox::GetString( int n
) const
508 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
510 wxNode
*node
= m_boxes
.Nth( n
);
512 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
514 GtkButton
*button
= GTK_BUTTON( node
->Data() );
515 GtkLabel
*label
= GTK_LABEL( button
->child
);
517 return wxString( label
->label
);
520 wxString
wxRadioBox::GetLabel( int item
) const
522 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
524 return GetString( item
);
527 void wxRadioBox::SetLabel( const wxString
& label
)
529 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
531 wxControl::SetLabel( label
);
533 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
536 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
538 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
540 wxNode
*node
= m_boxes
.Nth( item
);
542 wxCHECK_RET( node
, wxT("radiobox wrong index") );
544 GtkButton
*button
= GTK_BUTTON( node
->Data() );
545 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
547 gtk_label_set( g_label
, label
.mbc_str() );
550 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
552 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
555 bool wxRadioBox::Enable( bool enable
)
557 if ( !wxControl::Enable( enable
) )
560 wxNode
*node
= m_boxes
.First();
563 GtkButton
*button
= GTK_BUTTON( node
->Data() );
564 GtkWidget
*label
= button
->child
;
565 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
566 gtk_widget_set_sensitive( label
, enable
);
573 void wxRadioBox::Enable( int item
, bool enable
)
575 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
577 wxNode
*node
= m_boxes
.Nth( item
);
579 wxCHECK_RET( node
, wxT("radiobox wrong index") );
581 GtkButton
*button
= GTK_BUTTON( node
->Data() );
582 GtkWidget
*label
= button
->child
;
583 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
584 gtk_widget_set_sensitive( label
, enable
);
587 void wxRadioBox::Show( int item
, bool show
)
589 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
591 wxNode
*node
= m_boxes
.Nth( item
);
593 wxCHECK_RET( node
, wxT("radiobox wrong index") );
595 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
598 gtk_widget_show( button
);
600 gtk_widget_hide( button
);
603 wxString
wxRadioBox::GetStringSelection() const
605 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
607 wxNode
*node
= m_boxes
.First();
610 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
613 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
619 wxFAIL_MSG( wxT("wxRadioBox none selected") );
623 bool wxRadioBox::SetStringSelection( const wxString
&s
)
625 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
627 int res
= FindString( s
);
628 if (res
== -1) return FALSE
;
634 int wxRadioBox::Number() const
636 return m_boxes
.Number();
639 int wxRadioBox::GetNumberOfRowsOrCols() const
644 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
646 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
649 void wxRadioBox::GtkDisableEvents()
651 wxNode
*node
= m_boxes
.First();
654 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
655 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
661 void wxRadioBox::GtkEnableEvents()
663 wxNode
*node
= m_boxes
.First();
666 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
667 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
673 void wxRadioBox::ApplyWidgetStyle()
677 gtk_widget_set_style( m_widget
, m_widgetStyle
);
679 wxNode
*node
= m_boxes
.First();
682 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
683 gtk_widget_set_style( widget
, m_widgetStyle
);
685 GtkButton
*button
= GTK_BUTTON( node
->Data() );
686 gtk_widget_set_style( button
->child
, m_widgetStyle
);
693 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
695 wxNode
*node
= m_boxes
.First();
698 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
699 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
703 #endif // wxUSE_TOOLTIPS
705 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
707 if (window
== m_widget
->window
) return TRUE
;
709 wxNode
*node
= m_boxes
.First();
712 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
714 if (window
== button
->window
) return TRUE
;
722 void wxRadioBox::OnInternalIdle()
729 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
730 event
.SetEventObject( this );
732 (void)GetEventHandler()->ProcessEvent( event
);
736 #endif // wxUSE_RADIOBOX