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 //-----------------------------------------------------------------------------
49 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
51 if (g_isIdle
) wxapp_install_idle_handler();
53 if (!rb
->m_hasVMT
) return;
54 if (g_blockEventsOnDrag
) return;
56 if (!button
->active
) return;
58 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
59 event
.SetInt( rb
->GetSelection() );
60 event
.SetString( rb
->GetStringSelection() );
61 event
.SetEventObject( rb
);
62 rb
->GetEventHandler()->ProcessEvent(event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
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 wxList::compatibility_iterator 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
.GetFirst())
99 node
= rb
->m_boxes
.GetLast();
101 node
= node
->GetPrevious();
105 if (node
== rb
->m_boxes
.GetLast())
106 node
= rb
->m_boxes
.GetFirst();
108 node
= node
->GetNext();
111 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
113 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
);
146 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
147 GdkEvent
*WXUNUSED(event
),
150 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
151 // Replace with a warning, else we dump core a lot!
152 // if (!win->m_hasFocus)
153 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
155 // we might have lost the focus, but may be not - it may have just gone to
156 // another button in the same radiobox, so we'll check for it in the next
157 // idle iteration (leave m_hasFocus == true for now)
158 win
->m_lostFocus
= true;
164 //-----------------------------------------------------------------------------
166 //-----------------------------------------------------------------------------
168 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
170 void wxRadioBox::Init()
173 m_acceptsFocus
= true;
179 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
180 const wxString
& title
,
181 const wxPoint
&pos
, const wxSize
&size
,
182 const wxArrayString
& choices
, int majorDim
,
183 long style
, const wxValidator
& validator
,
184 const wxString
&name
)
186 wxCArrayString
chs(choices
);
188 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
189 chs
.GetStrings(), majorDim
, style
, validator
, name
);
192 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
193 const wxPoint
&pos
, const wxSize
&size
,
194 int n
, const wxString choices
[], int majorDim
,
195 long style
, const wxValidator
& validator
,
196 const wxString
&name
)
198 if (!PreCreation( parent
, pos
, size
) ||
199 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
201 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
205 m_widget
= gtk_frame_new( wxGTK_CONV( title
) );
207 // majorDim may be 0 if all trailing parameters were omitted, so don't
208 // assert here but just use the correct value for it
209 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
211 int num_per_major
= (n
- 1) / m_majorDim
+1;
215 if (HasFlag(wxRA_SPECIFY_COLS
))
217 num_of_cols
= m_majorDim
;
218 num_of_rows
= num_per_major
;
222 num_of_cols
= num_per_major
;
223 num_of_rows
= m_majorDim
;
226 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
228 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
229 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
230 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
231 gtk_widget_show( table
);
232 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
235 GSList
*radio_button_group
= (GSList
*) NULL
;
236 for (int i
= 0; i
< n
; i
++)
239 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
242 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
244 if ( *pc
!= wxT('&') )
248 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
249 gtk_widget_show( GTK_WIDGET(m_radio
) );
251 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
252 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
254 m_boxes
.Append( (wxObject
*) m_radio
);
256 if (HasFlag(wxRA_SPECIFY_COLS
))
258 int left
= i%num_of_cols
;
259 int right
= (i%num_of_cols
) + 1;
260 int top
= i
/num_of_cols
;
261 int bottom
= (i
/num_of_cols
)+1;
262 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
263 GTK_FILL
, GTK_FILL
, 1, 1 );
267 int left
= i
/num_of_rows
;
268 int right
= (i
/num_of_rows
) + 1;
269 int top
= i%num_of_rows
;
270 int bottom
= (i%num_of_rows
)+1;
271 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
272 GTK_FILL
, GTK_FILL
, 1, 1 );
275 ConnectWidget( GTK_WIDGET(m_radio
) );
277 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
279 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
280 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
282 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
283 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
285 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
286 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
289 m_parent
->DoAddChild( this );
298 wxRadioBox::~wxRadioBox()
300 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
303 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
304 gtk_widget_destroy( button
);
305 node
= node
->GetNext();
309 bool wxRadioBox::Show( bool show
)
311 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
313 if (!wxControl::Show(show
))
319 if ( HasFlag(wxNO_BORDER
) )
320 gtk_widget_hide( m_widget
);
322 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
325 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
327 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
329 node
= node
->GetNext();
335 int wxRadioBox::FindString( const wxString
&find
) const
337 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
341 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
344 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
346 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
348 wxString
str( label
->label
);
355 node
= node
->GetNext();
361 void wxRadioBox::SetFocus()
363 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
365 if (m_boxes
.GetCount() == 0) return;
367 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
370 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
373 gtk_widget_grab_focus( GTK_WIDGET(button
) );
376 node
= node
->GetNext();
380 void wxRadioBox::SetSelection( int n
)
382 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
384 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
386 wxCHECK_RET( node
, wxT("radiobox wrong index") );
388 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
392 gtk_toggle_button_set_active( button
, 1 );
397 int wxRadioBox::GetSelection(void) const
399 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
403 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
406 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
407 if (button
->active
) return count
;
409 node
= node
->GetNext();
412 wxFAIL_MSG( wxT("wxRadioBox none selected") );
417 wxString
wxRadioBox::GetString( int n
) const
419 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
421 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
423 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
425 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
428 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
430 wxString
str( label
->label
);
436 void wxRadioBox::SetLabel( const wxString
& label
)
438 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
440 wxControl::SetLabel( label
);
442 gtk_frame_set_label( GTK_FRAME(m_widget
), wxGTK_CONV( wxControl::GetLabel() ) );
445 void wxRadioBox::SetString( int item
, const wxString
& label
)
447 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
449 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
451 wxCHECK_RET( node
, wxT("radiobox wrong index") );
453 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
455 gtk_label_set( g_label
, wxGTK_CONV( label
) );
458 bool wxRadioBox::Enable( bool enable
)
460 if ( !wxControl::Enable( enable
) )
463 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
466 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
467 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
469 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
470 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
471 node
= node
->GetNext();
477 bool wxRadioBox::Enable( int item
, bool enable
)
479 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
481 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
483 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
485 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
486 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
488 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
489 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
494 bool wxRadioBox::Show( int item
, bool show
)
496 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
498 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
500 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
502 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
505 gtk_widget_show( button
);
507 gtk_widget_hide( button
);
512 wxString
wxRadioBox::GetStringSelection() const
514 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
516 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
519 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
522 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
525 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
527 wxString
str( label
->label
);
531 node
= node
->GetNext();
534 wxFAIL_MSG( wxT("wxRadioBox none selected") );
535 return wxEmptyString
;
538 bool wxRadioBox::SetStringSelection( const wxString
&s
)
540 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
542 int res
= FindString( s
);
543 if (res
== wxNOT_FOUND
) return false;
549 int wxRadioBox::GetCount() const
551 return m_boxes
.GetCount();
554 void wxRadioBox::GtkDisableEvents()
556 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
559 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
560 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
562 node
= node
->GetNext();
566 void wxRadioBox::GtkEnableEvents()
568 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
571 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
572 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
574 node
= node
->GetNext();
578 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
580 gtk_widget_modify_style( m_widget
, style
);
583 gtk_widget_modify_style(GTK_FRAME(m_widget
)->label_widget
, style
);
586 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
589 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
591 gtk_widget_modify_style( widget
, style
);
592 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
594 node
= node
->GetNext();
599 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
601 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
604 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
605 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
606 node
= node
->GetNext();
609 #endif // wxUSE_TOOLTIPS
611 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
613 if (window
== m_widget
->window
) return true;
615 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
618 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
620 if (window
== button
->window
) return true;
622 node
= node
->GetNext();
628 void wxRadioBox::OnInternalIdle()
635 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
636 event
.SetEventObject( this );
638 (void)GetEventHandler()->ProcessEvent( event
);
641 if (g_delayedFocus
== this)
643 if (GTK_WIDGET_REALIZED(m_widget
))
645 g_delayedFocus
= NULL
;
653 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
655 wxVisualAttributes attr
;
656 // NB: we need toplevel window so that GTK+ can find the right style
657 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
658 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
659 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
660 attr
= GetDefaultAttributesFromGTKWidget(widget
);
661 gtk_widget_destroy(wnd
);
665 #if WXWIN_COMPATIBILITY_2_2
667 int wxRadioBox::Number() const
672 wxString
wxRadioBox::GetLabel(int n
) const
677 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
679 SetString(item
, label
);
682 #endif // WXWIN_COMPATIBILITY_2_2
684 #endif // wxUSE_RADIOBOX