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
), 2 );
222 gtk_table_set_row_spacings( GTK_TABLE(table
), 2 );
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_defaults( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
);
258 int left
= i
/num_of_rows
;
259 int right
= (i
/num_of_rows
) + 1;
260 int top
= i
%num_of_rows
;
261 int bottom
= (i
%num_of_rows
)+1;
262 gtk_table_attach_defaults( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
);
265 ConnectWidget( GTK_WIDGET(m_radio
) );
267 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
269 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
270 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
272 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
273 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
275 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
276 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
279 m_parent
->DoAddChild( this );
288 wxRadioBox
::~wxRadioBox()
290 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
293 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
294 gtk_widget_destroy( button
);
295 node
= node
->GetNext();
299 bool wxRadioBox
::Show( bool show
)
301 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
303 if (!wxControl
::Show(show
))
309 if ( HasFlag(wxNO_BORDER
) )
310 gtk_widget_hide( m_widget
);
312 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
315 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
317 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
319 node
= node
->GetNext();
325 int wxRadioBox
::FindString( const wxString
&find
) const
327 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
331 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
334 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
336 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
338 wxString
str( label
->label
);
345 node
= node
->GetNext();
351 void wxRadioBox
::SetFocus()
353 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
355 if (m_boxes
.GetCount() == 0) return;
357 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
360 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
363 gtk_widget_grab_focus( GTK_WIDGET(button
) );
366 node
= node
->GetNext();
370 void wxRadioBox
::SetSelection( int n
)
372 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
374 wxList
::compatibility_iterator node
= m_boxes
.Item( n
);
376 wxCHECK_RET( node
, wxT("radiobox wrong index") );
378 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
382 gtk_toggle_button_set_active( button
, 1 );
387 int wxRadioBox
::GetSelection(void) const
389 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
393 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
396 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
397 if (button
->active
) return count
;
399 node
= node
->GetNext();
402 wxFAIL_MSG( wxT("wxRadioBox none selected") );
407 wxString wxRadioBox
::GetString( int n
) const
409 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
411 wxList
::compatibility_iterator node
= m_boxes
.Item( n
);
413 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
415 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
418 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
420 wxString
str( label
->label
);
426 void wxRadioBox
::SetLabel( const wxString
& label
)
428 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
430 wxControl
::SetLabel( label
);
432 gtk_frame_set_label( GTK_FRAME(m_widget
), wxGTK_CONV( wxControl
::GetLabel() ) );
435 void wxRadioBox
::SetString( int item
, const wxString
& label
)
437 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
439 wxList
::compatibility_iterator node
= m_boxes
.Item( item
);
441 wxCHECK_RET( node
, wxT("radiobox wrong index") );
443 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
445 gtk_label_set( g_label
, wxGTK_CONV( label
) );
448 bool wxRadioBox
::Enable( bool enable
)
450 if ( !wxControl
::Enable( enable
) )
453 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
456 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
457 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
459 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
460 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
461 node
= node
->GetNext();
467 bool wxRadioBox
::Enable( int item
, bool enable
)
469 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
471 wxList
::compatibility_iterator node
= m_boxes
.Item( item
);
473 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
475 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
476 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
478 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
479 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
484 bool wxRadioBox
::Show( int item
, bool show
)
486 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
488 wxList
::compatibility_iterator node
= m_boxes
.Item( item
);
490 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
492 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
495 gtk_widget_show( button
);
497 gtk_widget_hide( button
);
502 wxString wxRadioBox
::GetStringSelection() const
504 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
506 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
509 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
512 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
515 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
517 wxString
str( label
->label
);
521 node
= node
->GetNext();
524 wxFAIL_MSG( wxT("wxRadioBox none selected") );
525 return wxEmptyString
;
528 bool wxRadioBox
::SetStringSelection( const wxString
&s
)
530 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
532 int res
= FindString( s
);
533 if (res
== wxNOT_FOUND
) return false;
539 int wxRadioBox
::GetCount() const
541 return m_boxes
.GetCount();
544 void wxRadioBox
::GtkDisableEvents()
546 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
549 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
550 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
552 node
= node
->GetNext();
556 void wxRadioBox
::GtkEnableEvents()
558 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
561 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
562 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
564 node
= node
->GetNext();
568 void wxRadioBox
::DoApplyWidgetStyle(GtkRcStyle
*style
)
570 gtk_widget_modify_style( m_widget
, style
);
573 gtk_widget_modify_style(GTK_FRAME(m_widget
)->label_widget
, style
);
576 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
579 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
581 gtk_widget_modify_style( widget
, style
);
582 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
584 node
= node
->GetNext();
589 void wxRadioBox
::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
591 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
594 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
595 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
596 node
= node
->GetNext();
599 #endif // wxUSE_TOOLTIPS
601 bool wxRadioBox
::IsOwnGtkWindow( GdkWindow
*window
)
603 if (window
== m_widget
->window
) return true;
605 wxList
::compatibility_iterator node
= m_boxes
.GetFirst();
608 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
610 if (window
== button
->window
) return true;
612 node
= node
->GetNext();
618 void wxRadioBox
::OnInternalIdle()
625 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
626 event
.SetEventObject( this );
628 (void)GetEventHandler()->ProcessEvent( event
);
631 if (g_delayedFocus
== this)
633 if (GTK_WIDGET_REALIZED(m_widget
))
635 g_delayedFocus
= NULL
;
643 wxRadioBox
::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
645 wxVisualAttributes attr
;
646 // NB: we need toplevel window so that GTK+ can find the right style
647 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
648 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
649 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
650 attr
= GetDefaultAttributesFromGTKWidget(widget
);
651 gtk_widget_destroy(wnd
);
655 #if WXWIN_COMPATIBILITY_2_2
657 int wxRadioBox
::Number() const
662 wxString wxRadioBox
::GetLabel(int n
) const
667 void wxRadioBox
::SetLabel( int item
, const wxString
& label
)
669 SetString(item
, label
);
672 #endif // WXWIN_COMPATIBILITY_2_2
674 #endif // wxUSE_RADIOBOX