1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/radiobut.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/radiobut.h"
16 #include "wx/gtk1/private.h"
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 extern void wxapp_install_idle_handler();
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern bool g_blockEventsOnDrag
;
30 extern wxCursor g_globalCursor
;
31 extern wxWindowGTK
*g_delayedFocus
;
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
39 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
41 if (g_isIdle
) wxapp_install_idle_handler();
43 if (!rb
->m_hasVMT
) return;
45 if (g_blockEventsOnDrag
) return;
47 if (!button
->active
) return;
49 if (rb
->m_blockEvent
) return;
51 wxCommandEvent
event( wxEVT_RADIOBUTTON
, rb
->GetId());
52 event
.SetInt( rb
->GetValue() );
53 event
.SetEventObject( rb
);
54 rb
->HandleWindowEvent( event
);
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 bool wxRadioButton::Create( wxWindow
*parent
,
64 const wxString
& label
,
68 const wxValidator
& validator
,
69 const wxString
& name
)
71 m_acceptsFocus
= TRUE
;
76 if (!PreCreation( parent
, pos
, size
) ||
77 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
79 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
83 GSList
* radioButtonGroup
= NULL
;
84 if (!HasFlag(wxRB_GROUP
))
86 // search backward for last group start
87 wxRadioButton
*chief
= NULL
;
88 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
91 wxWindow
*child
= node
->GetData();
92 if (child
->IsRadioButton())
94 chief
= (wxRadioButton
*) child
;
95 if (child
->HasFlag(wxRB_GROUP
))
98 node
= node
->GetPrevious();
102 // we are part of the group started by chief
103 radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
107 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
111 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
112 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
114 m_parent
->DoAddChild( this );
121 void wxRadioButton::SetLabel( const wxString
& label
)
123 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
125 GTKSetLabelForLabel(GTK_LABEL(BUTTON_CHILD(m_widget
)), label
);
128 void wxRadioButton::SetValue( bool val
)
130 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
132 if (val
== GetValue())
139 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
143 // should give an assert
144 // RL - No it shouldn't. A wxGenericValidator might try to set it
145 // as FALSE. Failing silently is probably TRTTD here.
148 m_blockEvent
= FALSE
;
151 bool wxRadioButton::GetValue() const
153 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
155 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
158 bool wxRadioButton::Enable( bool enable
)
160 if ( !wxControl::Enable( enable
) )
163 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
168 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
170 gtk_widget_modify_style(m_widget
, style
);
171 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
174 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
176 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
179 void wxRadioButton::OnInternalIdle()
181 wxCursor cursor
= m_cursor
;
182 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
184 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
185 if ( win
&& cursor
.IsOk())
187 /* I now set the cursor the anew in every OnInternalIdle call
188 as setting the cursor in a parent window also effects the
189 windows above so that checking for the current cursor is
192 gdk_window_set_cursor( win
, cursor
.GetCursor() );
195 if (g_delayedFocus
== this)
197 if (GTK_WIDGET_REALIZED(m_widget
))
199 gtk_widget_grab_focus( m_widget
);
200 g_delayedFocus
= NULL
;
204 if (wxUpdateUIEvent::CanUpdate(this))
205 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
208 wxSize
wxRadioButton::DoGetBestSize() const
210 return wxControl::DoGetBestSize();
215 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
217 wxVisualAttributes attr
;
218 // NB: we need toplevel window so that GTK+ can find the right style
219 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
220 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
221 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
222 attr
= GetDefaultAttributesFromGTKWidget(widget
);
223 gtk_widget_destroy(wnd
);