1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "radiobut.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
20 #include "wx/radiobut.h"
22 #include "wx/gtk/private.h"
24 //-----------------------------------------------------------------------------
26 //-----------------------------------------------------------------------------
28 extern void wxapp_install_idle_handler();
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 extern bool g_blockEventsOnDrag
;
36 extern wxCursor g_globalCursor
;
37 extern wxWindowGTK
*g_delayedFocus
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
44 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!rb
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 if (!button
->active
) return;
54 if (rb
->m_blockEvent
) return;
56 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
57 event
.SetInt( rb
->GetValue() );
58 event
.SetEventObject( rb
);
59 rb
->GetEventHandler()->ProcessEvent( event
);
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
66 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
68 bool wxRadioButton::Create( wxWindow
*parent
,
70 const wxString
& label
,
74 const wxValidator
& validator
,
75 const wxString
& name
)
77 m_acceptsFocus
= TRUE
;
82 if (!PreCreation( parent
, pos
, size
) ||
83 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
85 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
89 if (HasFlag(wxRB_GROUP
))
92 m_radioButtonGroup
= (GSList
*) NULL
;
96 // search backward for last group start
97 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
98 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
101 wxWindow
*child
= node
->GetData();
102 if (child
->IsRadioButton())
104 chief
= (wxRadioButton
*) child
;
105 if (child
->HasFlag(wxRB_GROUP
))
108 node
= node
->GetPrevious();
112 // we are part of the group started by chief
113 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
118 m_radioButtonGroup
= (GSList
*) NULL
;
122 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, wxGTK_CONV( label
) );
126 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
127 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
129 m_parent
->DoAddChild( this );
136 void wxRadioButton::SetLabel( const wxString
& label
)
138 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
140 wxControl::SetLabel( label
);
141 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
143 wxString label2
= PrepareLabelMnemonics( label
);
144 gtk_label_set_text_with_mnemonic( g_label
, wxGTK_CONV( label2
) );
146 gtk_label_set( g_label
, wxGTK_CONV( GetLabel() ) );
150 void wxRadioButton::SetValue( bool val
)
152 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
154 if (val
== GetValue())
161 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
165 // should give an assert
166 // RL - No it shouldn't. A wxGenericValidator might try to set it
167 // as FALSE. Failing silently is probably TRTTD here.
170 m_blockEvent
= FALSE
;
173 bool wxRadioButton::GetValue() const
175 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
177 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
180 bool wxRadioButton::Enable( bool enable
)
182 if ( !wxControl::Enable( enable
) )
185 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
190 void wxRadioButton::ApplyWidgetStyle()
193 gtk_widget_set_style( m_widget
, m_widgetStyle
);
194 gtk_widget_set_style( BUTTON_CHILD(m_widget
), m_widgetStyle
);
197 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
199 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
202 void wxRadioButton::OnInternalIdle()
204 wxCursor cursor
= m_cursor
;
205 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
207 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
208 if ( win
&& cursor
.Ok())
210 /* I now set the cursor the anew in every OnInternalIdle call
211 as setting the cursor in a parent window also effects the
212 windows above so that checking for the current cursor is
215 gdk_window_set_cursor( win
, cursor
.GetCursor() );
218 if (g_delayedFocus
== this)
220 if (GTK_WIDGET_REALIZED(m_widget
))
222 gtk_widget_grab_focus( m_widget
);
223 g_delayedFocus
= NULL
;
227 if (wxUpdateUIEvent::CanUpdate(this))
228 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
231 wxSize
wxRadioButton::DoGetBestSize() const
233 return wxControl::DoGetBestSize();
238 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
240 wxVisualAttributes attr
;
241 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
242 attr
= GetDefaultAttributesFromGTKWidget(widget
);
243 gtk_widget_destroy(widget
);