1 /////////////////////////////////////////////////////////////////////////////
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/radiobut.h"
17 #include "wx/gtk/private.h"
19 //-----------------------------------------------------------------------------
21 //-----------------------------------------------------------------------------
23 extern void wxapp_install_idle_handler();
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern bool g_blockEventsOnDrag
;
31 extern wxCursor g_globalCursor
;
32 extern wxWindowGTK
*g_delayedFocus
;
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
40 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
42 if (g_isIdle
) wxapp_install_idle_handler();
44 if (!rb
->m_hasVMT
) return;
46 if (g_blockEventsOnDrag
) return;
48 if (!button
->active
) return;
50 if (rb
->m_blockEvent
) return;
52 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
53 event
.SetInt( rb
->GetValue() );
54 event
.SetEventObject( rb
);
55 rb
->GetEventHandler()->ProcessEvent( event
);
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
65 bool wxRadioButton::Create( wxWindow
*parent
,
67 const wxString
& label
,
71 const wxValidator
& validator
,
72 const wxString
& name
)
74 m_acceptsFocus
= TRUE
;
79 if (!PreCreation( parent
, pos
, size
) ||
80 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
82 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
86 if (HasFlag(wxRB_GROUP
))
89 m_radioButtonGroup
= (GSList
*) NULL
;
93 // search backward for last group start
94 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
95 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
98 wxWindow
*child
= node
->GetData();
99 if (child
->IsRadioButton())
101 chief
= (wxRadioButton
*) child
;
102 if (child
->HasFlag(wxRB_GROUP
))
105 node
= node
->GetPrevious();
109 // we are part of the group started by chief
110 m_radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
115 m_radioButtonGroup
= (GSList
*) NULL
;
119 m_widget
= gtk_radio_button_new_with_label( m_radioButtonGroup
, wxGTK_CONV( label
) );
123 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
124 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
126 m_parent
->DoAddChild( this );
133 void wxRadioButton::SetLabel( const wxString
& label
)
135 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
137 wxControl::SetLabel( label
);
138 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(m_widget
) );
140 wxString label2
= PrepareLabelMnemonics( label
);
141 gtk_label_set_text_with_mnemonic( g_label
, wxGTK_CONV( label2
) );
143 gtk_label_set( g_label
, wxGTK_CONV( GetLabel() ) );
147 void wxRadioButton::SetValue( bool val
)
149 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
151 if (val
== GetValue())
158 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
162 // should give an assert
163 // RL - No it shouldn't. A wxGenericValidator might try to set it
164 // as FALSE. Failing silently is probably TRTTD here.
167 m_blockEvent
= FALSE
;
170 bool wxRadioButton::GetValue() const
172 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
174 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
177 bool wxRadioButton::Enable( bool enable
)
179 if ( !wxControl::Enable( enable
) )
182 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
187 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
189 gtk_widget_modify_style(m_widget
, style
);
190 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
193 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
195 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
198 void wxRadioButton::OnInternalIdle()
200 wxCursor cursor
= m_cursor
;
201 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
203 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
204 if ( win
&& cursor
.Ok())
206 /* I now set the cursor the anew in every OnInternalIdle call
207 as setting the cursor in a parent window also effects the
208 windows above so that checking for the current cursor is
211 gdk_window_set_cursor( win
, cursor
.GetCursor() );
214 if (g_delayedFocus
== this)
216 if (GTK_WIDGET_REALIZED(m_widget
))
218 gtk_widget_grab_focus( m_widget
);
219 g_delayedFocus
= NULL
;
223 if (wxUpdateUIEvent::CanUpdate(this))
224 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
227 wxSize
wxRadioButton::DoGetBestSize() const
229 return wxControl::DoGetBestSize();
234 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
236 wxVisualAttributes attr
;
237 // NB: we need toplevel window so that GTK+ can find the right style
238 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
239 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
240 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
241 attr
= GetDefaultAttributesFromGTKWidget(widget
);
242 gtk_widget_destroy(wnd
);