1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/radiobox.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/radiobox.h"
19 #include "wx/dialog.h"
22 #include "wx/gtk1/private.h"
23 #include <gdk/gdkkeysyms.h>
25 #include "wx/gtk1/win_gtk.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
39 extern wxWindowGTK
*g_delayedFocus
;
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
46 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
48 if (g_isIdle
) wxapp_install_idle_handler();
50 if (!rb
->m_hasVMT
) return;
51 if (g_blockEventsOnDrag
) return;
53 if (!button
->active
) return;
55 wxCommandEvent
event( wxEVT_RADIOBOX
, rb
->GetId() );
56 event
.SetInt( rb
->GetSelection() );
57 event
.SetString( rb
->GetStringSelection() );
58 event
.SetEventObject( rb
);
59 rb
->HandleWindowEvent(event
);
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
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
);
117 static gint
gtk_radiobutton_focus_in( GtkWidget
*WXUNUSED(widget
),
118 GdkEvent
*WXUNUSED(event
),
121 if ( win
->m_lostFocus
)
123 // no, we didn't really lose it
124 win
->m_lostFocus
= FALSE
;
126 else if ( !win
->m_hasFocus
)
128 win
->m_hasFocus
= true;
130 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
131 event
.SetEventObject( win
);
133 // never stop the signal emission, it seems to break the kbd handling
134 // inside the radiobox
135 (void)win
->HandleWindowEvent( event
);
143 static gint
gtk_radiobutton_focus_out( GtkWidget
*WXUNUSED(widget
),
144 GdkEvent
*WXUNUSED(event
),
147 // wxASSERT_MSG( win->m_hasFocus, wxT("got focus out without any focus in?") );
148 // Replace with a warning, else we dump core a lot!
149 // if (!win->m_hasFocus)
150 // wxLogWarning(wxT("Radiobox got focus out without any focus in.") );
152 // we might have lost the focus, but may be not - it may have just gone to
153 // another button in the same radiobox, so we'll check for it in the next
154 // idle iteration (leave m_hasFocus == true for now)
155 win
->m_lostFocus
= true;
161 //-----------------------------------------------------------------------------
163 //-----------------------------------------------------------------------------
165 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
167 void wxRadioBox::Init()
170 m_acceptsFocus
= true;
176 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
177 const wxString
& title
,
178 const wxPoint
&pos
, const wxSize
&size
,
179 const wxArrayString
& choices
, int majorDim
,
180 long style
, const wxValidator
& validator
,
181 const wxString
&name
)
183 wxCArrayString
chs(choices
);
185 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
186 chs
.GetStrings(), majorDim
, style
, validator
, name
);
189 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
190 const wxPoint
&pos
, const wxSize
&size
,
191 int n
, const wxString choices
[], int majorDim
,
192 long style
, const wxValidator
& validator
,
193 const wxString
&name
)
195 if (!PreCreation( parent
, pos
, size
) ||
196 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
198 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
202 m_widget
= gtk_frame_new(NULL
);
204 if ( HasFlag(wxNO_BORDER
) )
206 // If we don't do this here, the wxNO_BORDER style is ignored in Show()
207 gtk_frame_set_shadow_type(GTK_FRAME(m_widget
), GTK_SHADOW_NONE
);
210 // majorDim may be 0 if all trailing parameters were omitted, so don't
211 // assert here but just use the correct value for it
212 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
215 unsigned int num_of_cols
= GetColumnCount();
216 unsigned int num_of_rows
= GetRowCount();
218 GtkRadioButton
*m_radio
= 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
= 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 );
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() );
318 gtk_widget_show( button
);
320 gtk_widget_hide( button
);
322 node
= node
->GetNext();
328 void wxRadioBox::SetFocus()
330 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
332 if (m_boxes
.GetCount() == 0) return;
334 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
337 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
340 gtk_widget_grab_focus( GTK_WIDGET(button
) );
343 node
= node
->GetNext();
347 void wxRadioBox::SetSelection( int n
)
349 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
351 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
353 wxCHECK_RET( node
, wxT("radiobox wrong index") );
355 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
359 gtk_toggle_button_set_active( button
, 1 );
364 int wxRadioBox::GetSelection(void) const
366 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
370 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
373 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
374 if (button
->active
) return count
;
376 node
= node
->GetNext();
379 wxFAIL_MSG( wxT("wxRadioBox none selected") );
384 wxString
wxRadioBox::GetString(unsigned int n
) const
386 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
388 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
390 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
392 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
394 wxString
str( label
->label
);
399 void wxRadioBox::SetLabel( const wxString
& label
)
401 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
403 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
406 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
408 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
410 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
412 wxCHECK_RET( node
, wxT("radiobox wrong index") );
414 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
416 gtk_label_set( g_label
, wxGTK_CONV( label
) );
419 bool wxRadioBox::Enable( bool enable
)
421 if ( !wxControl::Enable( enable
) )
424 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
427 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
428 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
430 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
431 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
432 node
= node
->GetNext();
438 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
440 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
442 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
444 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
446 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
447 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
449 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
450 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
455 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
457 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
459 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
461 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
463 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
465 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
466 // the parent radiobox is disabled
467 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
470 bool wxRadioBox::Show(unsigned int item
, bool show
)
472 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
474 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
476 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
478 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
481 gtk_widget_show( button
);
483 gtk_widget_hide( button
);
488 bool wxRadioBox::IsItemShown(unsigned int item
) const
490 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
492 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
494 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
496 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
498 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
501 unsigned int wxRadioBox::GetCount() const
503 return m_boxes
.GetCount();
506 void wxRadioBox::GtkDisableEvents()
508 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
511 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
512 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
514 node
= node
->GetNext();
518 void wxRadioBox::GtkEnableEvents()
520 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
523 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
524 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
526 node
= node
->GetNext();
530 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
532 gtk_widget_modify_style( m_widget
, style
);
534 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
537 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
539 gtk_widget_modify_style( widget
, style
);
540 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
542 node
= node
->GetNext();
547 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
549 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
552 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
553 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), NULL
);
554 node
= node
->GetNext();
557 #endif // wxUSE_TOOLTIPS
559 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
561 if (window
== m_widget
->window
)
564 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
567 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
569 if (window
== button
->window
)
572 node
= node
->GetNext();
578 void wxRadioBox::OnInternalIdle()
585 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
586 event
.SetEventObject( this );
588 (void)HandleWindowEvent( event
);
591 if (g_delayedFocus
== this)
593 if (GTK_WIDGET_REALIZED(m_widget
))
595 g_delayedFocus
= NULL
;
603 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
605 wxVisualAttributes attr
;
606 // NB: we need toplevel window so that GTK+ can find the right style
607 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
608 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
609 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
610 attr
= GetDefaultAttributesFromGTKWidget(widget
);
611 gtk_widget_destroy(wnd
);
615 #endif // wxUSE_RADIOBOX