1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
22 #include "wx/dialog.h"
24 #include "wx/gtk1/private.h"
25 #include <gdk/gdkkeysyms.h>
27 #include "wx/gtk1/win_gtk.h"
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern void wxapp_install_idle_handler();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 extern bool g_blockEventsOnDrag
;
41 extern wxWindowGTK
*g_delayedFocus
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
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
);
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
70 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
73 wxapp_install_idle_handler();
75 if (!rb
->m_hasVMT
) return FALSE
;
76 if (g_blockEventsOnDrag
) return FALSE
;
78 if ((gdk_event
->keyval
!= GDK_Up
) &&
79 (gdk_event
->keyval
!= GDK_Down
) &&
80 (gdk_event
->keyval
!= GDK_Left
) &&
81 (gdk_event
->keyval
!= GDK_Right
))
86 wxList::compatibility_iterator node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
92 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
94 if ((gdk_event
->keyval
== GDK_Up
) ||
95 (gdk_event
->keyval
== GDK_Left
))
97 if (node
== rb
->m_boxes
.GetFirst())
98 node
= rb
->m_boxes
.GetLast();
100 node
= node
->GetPrevious();
104 if (node
== rb
->m_boxes
.GetLast())
105 node
= rb
->m_boxes
.GetFirst();
107 node
= node
->GetNext();
110 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
112 gtk_widget_grab_focus( button
);
119 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
120 GdkEvent
*WXUNUSED(event
),
123 if ( win
->m_lostFocus
)
125 // no, we didn't really lose it
126 win
->m_lostFocus
= FALSE
;
128 else if ( !win
->m_hasFocus
)
130 win
->m_hasFocus
= true;
132 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
133 event
.SetEventObject( win
);
135 // never stop the signal emission, it seems to break the kbd handling
136 // inside the radiobox
137 (void)win
->GetEventHandler()->ProcessEvent( event
);
145 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
146 GdkEvent
*WXUNUSED(event
),
149 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
150 // Replace with a warning, else we dump core a lot!
151 // if (!win->m_hasFocus)
152 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
154 // we might have lost the focus, but may be not - it may have just gone to
155 // another button in the same radiobox, so we'll check for it in the next
156 // idle iteration (leave m_hasFocus == true for now)
157 win
->m_lostFocus
= true;
163 //-----------------------------------------------------------------------------
165 //-----------------------------------------------------------------------------
167 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
169 void wxRadioBox::Init()
172 m_acceptsFocus
= true;
178 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
179 const wxString
& title
,
180 const wxPoint
&pos
, const wxSize
&size
,
181 const wxArrayString
& choices
, int majorDim
,
182 long style
, const wxValidator
& validator
,
183 const wxString
&name
)
185 wxCArrayString
chs(choices
);
187 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
188 chs
.GetStrings(), majorDim
, style
, validator
, name
);
191 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
192 const wxPoint
&pos
, const wxSize
&size
,
193 int n
, const wxString choices
[], int majorDim
,
194 long style
, const wxValidator
& validator
,
195 const wxString
&name
)
197 if (!PreCreation( parent
, pos
, size
) ||
198 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
200 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
204 m_widget
= gtk_frame_new(NULL
);
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 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
212 unsigned int num_of_cols
= GetColumnCount();
213 unsigned int num_of_rows
= GetRowCount();
215 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
217 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
218 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
219 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
220 gtk_widget_show( table
);
221 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
224 GSList
*radio_button_group
= (GSList
*) NULL
;
225 for (int i
= 0; i
< n
; i
++)
228 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
231 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
233 if ( *pc
!= wxT('&') )
237 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
238 gtk_widget_show( GTK_WIDGET(m_radio
) );
240 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
241 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
243 m_boxes
.Append( (wxObject
*) m_radio
);
245 if (HasFlag(wxRA_SPECIFY_COLS
))
247 int left
= i%num_of_cols
;
248 int right
= (i%num_of_cols
) + 1;
249 int top
= i
/num_of_cols
;
250 int bottom
= (i
/num_of_cols
)+1;
251 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
252 GTK_FILL
, GTK_FILL
, 1, 1 );
256 int left
= i
/num_of_rows
;
257 int right
= (i
/num_of_rows
) + 1;
258 int top
= i%num_of_rows
;
259 int bottom
= (i%num_of_rows
)+1;
260 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
261 GTK_FILL
, GTK_FILL
, 1, 1 );
264 ConnectWidget( GTK_WIDGET(m_radio
) );
266 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
268 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
269 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
271 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
272 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
274 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
275 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
278 m_parent
->DoAddChild( this );
285 wxRadioBox::~wxRadioBox()
287 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
290 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
291 gtk_widget_destroy( button
);
292 node
= node
->GetNext();
296 bool wxRadioBox::Show(bool show
)
298 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
300 if (!wxControl::Show(show
))
306 if ( HasFlag(wxNO_BORDER
) )
307 gtk_widget_hide( m_widget
);
309 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
312 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
315 gtk_widget_show( button
);
317 gtk_widget_hide( button
);
319 node
= node
->GetNext();
325 void wxRadioBox::SetFocus()
327 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
329 if (m_boxes
.GetCount() == 0) return;
331 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
334 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
337 gtk_widget_grab_focus( GTK_WIDGET(button
) );
340 node
= node
->GetNext();
344 void wxRadioBox::SetSelection( int n
)
346 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
348 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
350 wxCHECK_RET( node
, wxT("radiobox wrong index") );
352 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
356 gtk_toggle_button_set_active( button
, 1 );
361 int wxRadioBox::GetSelection(void) const
363 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
367 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
370 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
371 if (button
->active
) return count
;
373 node
= node
->GetNext();
376 wxFAIL_MSG( wxT("wxRadioBox none selected") );
381 wxString
wxRadioBox::GetString(unsigned int n
) const
383 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
385 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
387 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
389 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
391 wxString
str( label
->label
);
396 void wxRadioBox::SetLabel( const wxString
& label
)
398 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
400 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
403 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
405 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
407 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
409 wxCHECK_RET( node
, wxT("radiobox wrong index") );
411 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
413 gtk_label_set( g_label
, wxGTK_CONV( label
) );
416 bool wxRadioBox::Enable( bool enable
)
418 if ( !wxControl::Enable( enable
) )
421 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
424 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
425 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
427 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
428 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
429 node
= node
->GetNext();
435 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
437 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
439 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
441 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
443 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
444 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
446 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
447 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
452 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
454 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
456 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
458 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
460 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
462 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
463 // the parent radiobox is disabled
464 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
467 bool wxRadioBox::Show(unsigned int item
, bool show
)
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 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
478 gtk_widget_show( button
);
480 gtk_widget_hide( button
);
485 bool wxRadioBox::IsItemShown(unsigned int item
) const
487 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
489 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
491 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
493 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
495 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
498 unsigned int wxRadioBox::GetCount() const
500 return m_boxes
.GetCount();
503 void wxRadioBox::GtkDisableEvents()
505 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
508 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
509 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
511 node
= node
->GetNext();
515 void wxRadioBox::GtkEnableEvents()
517 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
520 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
521 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
523 node
= node
->GetNext();
527 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
529 gtk_widget_modify_style( m_widget
, style
);
531 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
534 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
536 gtk_widget_modify_style( widget
, style
);
537 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
539 node
= node
->GetNext();
544 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
546 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
549 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
550 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
551 node
= node
->GetNext();
554 #endif // wxUSE_TOOLTIPS
556 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
558 if (window
== m_widget
->window
)
561 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
564 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
566 if (window
== button
->window
)
569 node
= node
->GetNext();
575 void wxRadioBox::OnInternalIdle()
582 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
583 event
.SetEventObject( this );
585 (void)GetEventHandler()->ProcessEvent( event
);
588 if (g_delayedFocus
== this)
590 if (GTK_WIDGET_REALIZED(m_widget
))
592 g_delayedFocus
= NULL
;
600 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
602 wxVisualAttributes attr
;
603 // NB: we need toplevel window so that GTK+ can find the right style
604 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
605 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
606 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
607 attr
= GetDefaultAttributesFromGTKWidget(widget
);
608 gtk_widget_destroy(wnd
);
612 #endif // wxUSE_RADIOBOX