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 bool g_blockEventsOnDrag
;
24 extern wxCursor g_globalCursor
;
25 extern wxWindowGTK
*g_delayedFocus
;
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
33 void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioButton
*rb
)
35 if (g_isIdle
) wxapp_install_idle_handler();
37 if (!rb
->m_hasVMT
) return;
39 if (g_blockEventsOnDrag
) return;
41 if (!button
->active
) return;
43 if (rb
->m_blockEvent
) return;
45 wxCommandEvent
event( wxEVT_COMMAND_RADIOBUTTON_SELECTED
, rb
->GetId());
46 event
.SetInt( rb
->GetValue() );
47 event
.SetEventObject( rb
);
48 rb
->GetEventHandler()->ProcessEvent( event
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton
,wxControl
)
58 bool wxRadioButton::Create( wxWindow
*parent
,
60 const wxString
& label
,
64 const wxValidator
& validator
,
65 const wxString
& name
)
67 m_acceptsFocus
= TRUE
;
72 if (!PreCreation( parent
, pos
, size
) ||
73 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
75 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
79 GSList
* radioButtonGroup
= NULL
;
80 if (!HasFlag(wxRB_GROUP
))
82 // search backward for last group start
83 wxRadioButton
*chief
= (wxRadioButton
*) NULL
;
84 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetLast();
87 wxWindow
*child
= node
->GetData();
88 if (child
->IsRadioButton())
90 chief
= (wxRadioButton
*) child
;
91 if (child
->HasFlag(wxRB_GROUP
))
94 node
= node
->GetPrevious();
98 // we are part of the group started by chief
99 radioButtonGroup
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(chief
->m_widget
) );
103 m_widget
= gtk_radio_button_new_with_label( radioButtonGroup
, wxGTK_CONV( label
) );
107 g_signal_connect (m_widget
, "clicked",
108 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
110 m_parent
->DoAddChild( this );
117 void wxRadioButton::SetLabel( const wxString
& label
)
119 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
121 GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget
)->child
), label
);
124 void wxRadioButton::SetValue( bool val
)
126 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobutton") );
128 if (val
== GetValue())
135 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget
), TRUE
);
139 // should give an assert
140 // RL - No it shouldn't. A wxGenericValidator might try to set it
141 // as FALSE. Failing silently is probably TRTTD here.
144 m_blockEvent
= FALSE
;
147 bool wxRadioButton::GetValue() const
149 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobutton") );
151 return GTK_TOGGLE_BUTTON(m_widget
)->active
;
154 bool wxRadioButton::Enable( bool enable
)
156 if ( !wxControl::Enable( enable
) )
159 gtk_widget_set_sensitive(GTK_BIN(m_widget
)->child
, enable
);
164 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle
*style
)
166 gtk_widget_modify_style(m_widget
, style
);
167 gtk_widget_modify_style(GTK_BIN(m_widget
)->child
, style
);
170 bool wxRadioButton::IsOwnGtkWindow( GdkWindow
*window
)
172 return window
== GTK_BUTTON(m_widget
)->event_window
;
175 void wxRadioButton::OnInternalIdle()
177 wxCursor cursor
= m_cursor
;
178 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
180 GdkWindow
*win
= GTK_BUTTON(m_widget
)->event_window
;
181 if ( win
&& cursor
.Ok())
183 /* I now set the cursor the anew in every OnInternalIdle call
184 as setting the cursor in a parent window also effects the
185 windows above so that checking for the current cursor is
188 gdk_window_set_cursor( win
, cursor
.GetCursor() );
191 if (g_delayedFocus
== this)
193 if (GTK_WIDGET_REALIZED(m_widget
))
195 gtk_widget_grab_focus( m_widget
);
196 g_delayedFocus
= NULL
;
200 if (wxUpdateUIEvent::CanUpdate(this))
201 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
204 wxSize
wxRadioButton::DoGetBestSize() const
206 return wxControl::DoGetBestSize();
211 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
213 wxVisualAttributes attr
;
214 // NB: we need toplevel window so that GTK+ can find the right style
215 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
216 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
217 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
218 attr
= GetDefaultAttributesFromGTKWidget(widget
);
219 gtk_widget_destroy(wnd
);