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
,
172 const wxString
& title
,
173 const wxPoint
&pos
, const wxSize
&size
,
174 const wxArrayString
& choices
, int majorDim
,
175 long style
, const wxValidator
& validator
,
176 const wxString
&name
)
178 wxCArrayString
chs(choices
);
180 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
181 chs
.GetStrings(), majorDim
, style
, validator
, name
);
184 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
185 const wxPoint
&pos
, const wxSize
&size
,
186 int n
, const wxString choices
[], int majorDim
,
187 long style
, const wxValidator
& validator
,
188 const wxString
&name
)
190 if (!PreCreation( parent
, pos
, size
) ||
191 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
193 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
197 m_widget
= gtk_frame_new( wxGTK_CONV( title
) );
199 // majorDim may be 0 if all trailing parameters were omitted, so don't
200 // assert here but just use the correct value for it
201 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
203 int num_per_major
= (n
- 1) / m_majorDim
+1;
207 if (HasFlag(wxRA_SPECIFY_COLS
))
209 num_of_cols
= m_majorDim
;
210 num_of_rows
= num_per_major
;
214 num_of_cols
= num_per_major
;
215 num_of_rows
= m_majorDim
;
218 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
220 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
221 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
222 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
223 gtk_widget_show( table
);
224 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
227 GSList
*radio_button_group
= (GSList
*) NULL
;
228 for (int i
= 0; i
< n
; i
++)
231 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
234 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
236 if ( *pc
!= wxT('&') )
240 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
241 gtk_widget_show( GTK_WIDGET(m_radio
) );
243 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
244 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
246 m_boxes
.Append( (wxObject
*) m_radio
);
248 if (HasFlag(wxRA_SPECIFY_COLS
))
250 int left
= i%num_of_cols
;
251 int right
= (i%num_of_cols
) + 1;
252 int top
= i
/num_of_cols
;
253 int bottom
= (i
/num_of_cols
)+1;
254 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
255 GTK_FILL
, GTK_FILL
, 1, 1 );
259 int left
= i
/num_of_rows
;
260 int right
= (i
/num_of_rows
) + 1;
261 int top
= i%num_of_rows
;
262 int bottom
= (i%num_of_rows
)+1;
263 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
264 GTK_FILL
, GTK_FILL
, 1, 1 );
267 ConnectWidget( GTK_WIDGET(m_radio
) );
269 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
271 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
272 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
274 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
275 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
277 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
278 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
281 m_parent
->DoAddChild( this );
290 wxRadioBox::~wxRadioBox()
292 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
295 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
296 gtk_widget_destroy( button
);
297 node
= node
->GetNext();
301 bool wxRadioBox::Show( bool show
)
303 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
305 if (!wxControl::Show(show
))
311 if ( HasFlag(wxNO_BORDER
) )
312 gtk_widget_hide( m_widget
);
314 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
317 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
319 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
321 node
= node
->GetNext();
327 int wxRadioBox::FindString( const wxString
&find
) const
329 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
333 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
336 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
338 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
340 wxString
str( label
->label
);
347 node
= node
->GetNext();
353 void wxRadioBox::SetFocus()
355 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
357 if (m_boxes
.GetCount() == 0) return;
359 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
362 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
365 gtk_widget_grab_focus( GTK_WIDGET(button
) );
368 node
= node
->GetNext();
372 void wxRadioBox::SetSelection( int n
)
374 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
376 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
378 wxCHECK_RET( node
, wxT("radiobox wrong index") );
380 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
384 gtk_toggle_button_set_active( button
, 1 );
389 int wxRadioBox::GetSelection(void) const
391 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
395 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
398 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
399 if (button
->active
) return count
;
401 node
= node
->GetNext();
404 wxFAIL_MSG( wxT("wxRadioBox none selected") );
409 wxString
wxRadioBox::GetString( int n
) const
411 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
413 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
415 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
417 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
420 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
422 wxString
str( label
->label
);
428 void wxRadioBox::SetLabel( const wxString
& label
)
430 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
432 wxControl::SetLabel( label
);
434 gtk_frame_set_label( GTK_FRAME(m_widget
), wxGTK_CONV( wxControl::GetLabel() ) );
437 void wxRadioBox::SetString( int item
, const wxString
& label
)
439 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
441 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
443 wxCHECK_RET( node
, wxT("radiobox wrong index") );
445 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
447 gtk_label_set( g_label
, wxGTK_CONV( label
) );
450 bool wxRadioBox::Enable( bool enable
)
452 if ( !wxControl::Enable( enable
) )
455 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
458 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
459 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
461 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
462 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
463 node
= node
->GetNext();
469 bool wxRadioBox::Enable( int item
, bool enable
)
471 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
473 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
475 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
477 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
478 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
480 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
481 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
486 bool wxRadioBox::Show( int item
, bool show
)
488 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
490 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
492 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
494 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
497 gtk_widget_show( button
);
499 gtk_widget_hide( button
);
504 wxString
wxRadioBox::GetStringSelection() const
506 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
508 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
511 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
514 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
517 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
519 wxString
str( label
->label
);
523 node
= node
->GetNext();
526 wxFAIL_MSG( wxT("wxRadioBox none selected") );
527 return wxEmptyString
;
530 bool wxRadioBox::SetStringSelection( const wxString
&s
)
532 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
534 int res
= FindString( s
);
535 if (res
== wxNOT_FOUND
) return false;
541 int wxRadioBox::GetCount() const
543 return m_boxes
.GetCount();
546 void wxRadioBox::GtkDisableEvents()
548 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
551 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
552 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
554 node
= node
->GetNext();
558 void wxRadioBox::GtkEnableEvents()
560 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
563 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
564 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
566 node
= node
->GetNext();
570 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
572 gtk_widget_modify_style( m_widget
, style
);
575 gtk_widget_modify_style(GTK_FRAME(m_widget
)->label_widget
, style
);
578 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
581 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
583 gtk_widget_modify_style( widget
, style
);
584 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
586 node
= node
->GetNext();
591 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
593 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
596 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
597 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
598 node
= node
->GetNext();
601 #endif // wxUSE_TOOLTIPS
603 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
605 if (window
== m_widget
->window
) return true;
607 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
610 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
612 if (window
== button
->window
) return true;
614 node
= node
->GetNext();
620 void wxRadioBox::OnInternalIdle()
627 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
628 event
.SetEventObject( this );
630 (void)GetEventHandler()->ProcessEvent( event
);
633 if (g_delayedFocus
== this)
635 if (GTK_WIDGET_REALIZED(m_widget
))
637 g_delayedFocus
= NULL
;
645 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
647 wxVisualAttributes attr
;
648 // NB: we need toplevel window so that GTK+ can find the right style
649 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
650 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
651 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
652 attr
= GetDefaultAttributesFromGTKWidget(widget
);
653 gtk_widget_destroy(wnd
);
657 #if WXWIN_COMPATIBILITY_2_2
659 int wxRadioBox::Number() const
664 wxString
wxRadioBox::GetLabel(int n
) const
669 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
671 SetString(item
, label
);
674 #endif // WXWIN_COMPATIBILITY_2_2
676 #endif // wxUSE_RADIOBOX