1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/radiobut.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/radiobut.h"
17 #include "wx/gtk1/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
->HandleWindowEvent( event
);
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 bool wxRadioButton::Create( wxWindow
*parent
,
65 const wxString
& label
,
69 const wxValidator
& validator
,
70 const wxString
& name
)
72 m_acceptsFocus
= TRUE
;
77 if (!PreCreation( parent
, pos
, size
) ||
78 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
80 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
84 GSList
* radioButtonGroup
= NULL
;
85 if (!HasFlag(wxRB_GROUP
))
87 // search backward for last group start
88 wxRadioButton
*chief
= NULL
;
89 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
92 wxWindow
*child
= node
->GetData();
93 if (child
->IsRadioButton())
95 chief
= (wxRadioButton
*) child
;
96 if (child
->HasFlag(wxRB_GROUP
))
99 node
= node
->GetPrevious();
103 // we are part of the group started by chief
104 radioButtonGroup
= gtk_radio_button_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
108 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
112 gtk_signal_connect( GTK_OBJECT(m_widget
), "clicked",
113 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
115 m_parent
->DoAddChild( this );
122 void wxRadioButton::SetLabel( const wxString
& label
)
124 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
126 GTKSetLabelForLabel(GTK_LABEL(BUTTON_CHILD(m_widget
)), label
);
129 void wxRadioButton::SetValue( bool val
)
131 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
133 if (val
== GetValue())
140 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
144 // should give an assert
145 // RL - No it shouldn't. A wxGenericValidator might try to set it
146 // as FALSE. Failing silently is probably TRTTD here.
149 m_blockEvent
= FALSE
;
152 bool wxRadioButton::GetValue() const
154 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
156 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
159 bool wxRadioButton::Enable( bool enable
)
161 if ( !wxControl::Enable( enable
) )
164 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget
), enable
);
169 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
171 gtk_widget_modify_style(m_widget
, style
);
172 gtk_widget_modify_style(BUTTON_CHILD(m_widget
), style
);
175 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
177 return window
== TOGGLE_BUTTON_EVENT_WIN(m_widget
);
180 void wxRadioButton::OnInternalIdle()
182 wxCursor cursor
= m_cursor
;
183 if (g_globalCursor
.IsOk()) cursor
= g_globalCursor
;
185 GdkWindow
*win
= TOGGLE_BUTTON_EVENT_WIN(m_widget
);
186 if ( win
&& cursor
.IsOk())
188 /* I now set the cursor the anew in every OnInternalIdle call
189 as setting the cursor in a parent window also effects the
190 windows above so that checking for the current cursor is
193 gdk_window_set_cursor( win
, cursor
.GetCursor() );
196 if (g_delayedFocus
== this)
198 if (GTK_WIDGET_REALIZED(m_widget
))
200 gtk_widget_grab_focus( m_widget
);
201 g_delayedFocus
= NULL
;
205 if (wxUpdateUIEvent::CanUpdate(this))
206 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
209 wxSize
wxRadioButton::DoGetBestSize() const
211 return wxControl::DoGetBestSize();
216 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
218 wxVisualAttributes attr
;
219 // NB: we need toplevel window so that GTK+ can find the right style
220 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
221 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
222 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
223 attr
= GetDefaultAttributesFromGTKWidget(widget
);
224 gtk_widget_destroy(wnd
);