1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/radiobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/radiobox.h"
17 #include "wx/dialog.h"
21 #include "wx/gtk/private.h"
22 #include <gdk/gdkkeysyms.h>
24 #include "wx/gtk/win_gtk.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 extern wxWindowGTK
*g_delayedFocus
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!rb
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 if (!button
->active
) return;
54 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
55 event
.SetInt( rb
->GetSelection() );
56 event
.SetString( rb
->GetStringSelection() );
57 event
.SetEventObject( rb
);
58 rb
->GetEventHandler()->ProcessEvent(event
);
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
67 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
70 wxapp_install_idle_handler();
72 if (!rb
->m_hasVMT
) return FALSE
;
73 if (g_blockEventsOnDrag
) return FALSE
;
75 if ((gdk_event
->keyval
!= GDK_Up
) &&
76 (gdk_event
->keyval
!= GDK_Down
) &&
77 (gdk_event
->keyval
!= GDK_Left
) &&
78 (gdk_event
->keyval
!= GDK_Right
))
83 wxList::compatibility_iterator node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
89 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
91 if ((gdk_event
->keyval
== GDK_Up
) ||
92 (gdk_event
->keyval
== GDK_Left
))
94 if (node
== rb
->m_boxes
.GetFirst())
95 node
= rb
->m_boxes
.GetLast();
97 node
= node
->GetPrevious();
101 if (node
== rb
->m_boxes
.GetLast())
102 node
= rb
->m_boxes
.GetFirst();
104 node
= node
->GetNext();
107 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
109 gtk_widget_grab_focus( button
);
116 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
117 GdkEvent
*WXUNUSED(event
),
120 if ( win
->m_lostFocus
)
122 // no, we didn't really lose it
123 win
->m_lostFocus
= FALSE
;
125 else if ( !win
->m_hasFocus
)
127 win
->m_hasFocus
= true;
129 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
130 event
.SetEventObject( win
);
132 // never stop the signal emission, it seems to break the kbd handling
133 // inside the radiobox
134 (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;
160 //-----------------------------------------------------------------------------
162 //-----------------------------------------------------------------------------
164 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
166 void wxRadioBox::Init()
169 m_acceptsFocus
= true;
175 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
176 const wxString
& title
,
177 const wxPoint
&pos
, const wxSize
&size
,
178 const wxArrayString
& choices
, int majorDim
,
179 long style
, const wxValidator
& validator
,
180 const wxString
&name
)
182 wxCArrayString
chs(choices
);
184 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
185 chs
.GetStrings(), majorDim
, style
, validator
, name
);
188 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
189 const wxPoint
&pos
, const wxSize
&size
,
190 int n
, const wxString choices
[], int majorDim
,
191 long style
, const wxValidator
& validator
,
192 const wxString
&name
)
194 if (!PreCreation( parent
, pos
, size
) ||
195 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
197 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
201 m_widget
= gtk_frame_new( wxGTK_CONV( title
) );
203 // majorDim may be 0 if all trailing parameters were omitted, so don't
204 // assert here but just use the correct value for it
205 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
207 int num_per_major
= (n
- 1) / m_majorDim
+1;
211 if (HasFlag(wxRA_SPECIFY_COLS
))
213 num_of_cols
= m_majorDim
;
214 num_of_rows
= num_per_major
;
218 num_of_cols
= num_per_major
;
219 num_of_rows
= m_majorDim
;
222 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
224 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
225 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
226 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
227 gtk_widget_show( table
);
228 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
231 GSList
*radio_button_group
= (GSList
*) NULL
;
232 for (int i
= 0; i
< n
; i
++)
235 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
238 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
240 if ( *pc
!= wxT('&') )
244 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
245 gtk_widget_show( GTK_WIDGET(m_radio
) );
247 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
248 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
250 m_boxes
.Append( (wxObject
*) m_radio
);
252 if (HasFlag(wxRA_SPECIFY_COLS
))
254 int left
= i%num_of_cols
;
255 int right
= (i%num_of_cols
) + 1;
256 int top
= i
/num_of_cols
;
257 int bottom
= (i
/num_of_cols
)+1;
258 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
259 GTK_FILL
, GTK_FILL
, 1, 1 );
263 int left
= i
/num_of_rows
;
264 int right
= (i
/num_of_rows
) + 1;
265 int top
= i%num_of_rows
;
266 int bottom
= (i%num_of_rows
)+1;
267 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
268 GTK_FILL
, GTK_FILL
, 1, 1 );
271 ConnectWidget( GTK_WIDGET(m_radio
) );
273 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
275 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
276 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
278 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
279 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
281 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
282 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
285 m_parent
->DoAddChild( this );
294 wxRadioBox::~wxRadioBox()
296 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
299 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
300 gtk_widget_destroy( button
);
301 node
= node
->GetNext();
305 bool wxRadioBox::Show( bool show
)
307 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
309 if (!wxControl::Show(show
))
315 if ( HasFlag(wxNO_BORDER
) )
316 gtk_widget_hide( m_widget
);
318 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
321 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
323 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
325 node
= node
->GetNext();
331 int wxRadioBox::FindString( const wxString
&find
, bool bCase
) const
333 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
337 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
340 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
342 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
344 wxString
str( label
->label
);
346 if (find
.IsSameAs( str
, bCase
))
351 node
= node
->GetNext();
357 void wxRadioBox::SetFocus()
359 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
361 if (m_boxes
.GetCount() == 0) return;
363 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
366 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
369 gtk_widget_grab_focus( GTK_WIDGET(button
) );
372 node
= node
->GetNext();
376 void wxRadioBox::SetSelection( int n
)
378 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
380 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
382 wxCHECK_RET( node
, wxT("radiobox wrong index") );
384 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
388 gtk_toggle_button_set_active( button
, 1 );
393 int wxRadioBox::GetSelection(void) const
395 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
399 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
402 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
403 if (button
->active
) return count
;
405 node
= node
->GetNext();
408 wxFAIL_MSG( wxT("wxRadioBox none selected") );
413 wxString
wxRadioBox::GetString( int n
) const
415 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
417 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
419 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
421 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
424 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
426 wxString
str( label
->label
);
432 void wxRadioBox::SetLabel( const wxString
& label
)
434 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
436 wxControl::SetLabel( label
);
438 gtk_frame_set_label( GTK_FRAME(m_widget
), wxGTK_CONV( wxControl::GetLabel() ) );
441 void wxRadioBox::SetString( int item
, const wxString
& label
)
443 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
445 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
447 wxCHECK_RET( node
, wxT("radiobox wrong index") );
449 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
451 gtk_label_set( g_label
, wxGTK_CONV( label
) );
454 bool wxRadioBox::Enable( bool enable
)
456 if ( !wxControl::Enable( enable
) )
459 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
462 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
463 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
465 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
466 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
467 node
= node
->GetNext();
473 bool wxRadioBox::Enable( int item
, bool enable
)
475 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
477 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
479 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
481 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
482 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
484 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
485 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
490 bool wxRadioBox::Show( int item
, bool show
)
492 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
494 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
496 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
498 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
501 gtk_widget_show( button
);
503 gtk_widget_hide( button
);
508 wxString
wxRadioBox::GetStringSelection() const
510 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
512 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
515 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
518 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
521 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
523 wxString
str( label
->label
);
527 node
= node
->GetNext();
530 wxFAIL_MSG( wxT("wxRadioBox none selected") );
531 return wxEmptyString
;
534 bool wxRadioBox::SetStringSelection( const wxString
&s
)
536 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
538 int res
= FindString( s
);
539 if (res
== wxNOT_FOUND
) return false;
545 int wxRadioBox::GetCount() const
547 return m_boxes
.GetCount();
550 void wxRadioBox::GtkDisableEvents()
552 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
555 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
556 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
558 node
= node
->GetNext();
562 void wxRadioBox::GtkEnableEvents()
564 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
567 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
568 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
570 node
= node
->GetNext();
574 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
576 gtk_widget_modify_style( m_widget
, style
);
579 gtk_widget_modify_style(GTK_FRAME(m_widget
)->label_widget
, style
);
582 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
585 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
587 gtk_widget_modify_style( widget
, style
);
588 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
590 node
= node
->GetNext();
595 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
597 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
600 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
601 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
602 node
= node
->GetNext();
605 #endif // wxUSE_TOOLTIPS
607 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
609 if (window
== m_widget
->window
) return true;
611 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
614 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
616 if (window
== button
->window
) return true;
618 node
= node
->GetNext();
624 void wxRadioBox::OnInternalIdle()
631 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
632 event
.SetEventObject( this );
634 (void)GetEventHandler()->ProcessEvent( event
);
637 if (g_delayedFocus
== this)
639 if (GTK_WIDGET_REALIZED(m_widget
))
641 g_delayedFocus
= NULL
;
649 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
651 wxVisualAttributes attr
;
652 // NB: we need toplevel window so that GTK+ can find the right style
653 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
654 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
655 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
656 attr
= GetDefaultAttributesFromGTKWidget(widget
);
657 gtk_widget_destroy(wnd
);
661 #endif // wxUSE_RADIOBOX