applied patch 1376506 - remove unneeded member variable from GTK wxRadioButton
[wxWidgets.git] / src / gtk / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobut.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_RADIOBOX
14
15 #include "wx/radiobut.h"
16
17 #include "wx/gtk/private.h"
18
19 //-----------------------------------------------------------------------------
20 // idle system
21 //-----------------------------------------------------------------------------
22
23 extern void wxapp_install_idle_handler();
24 extern bool g_isIdle;
25
26 //-----------------------------------------------------------------------------
27 // data
28 //-----------------------------------------------------------------------------
29
30 extern bool g_blockEventsOnDrag;
31 extern wxCursor g_globalCursor;
32 extern wxWindowGTK *g_delayedFocus;
33
34 //-----------------------------------------------------------------------------
35 // "clicked"
36 //-----------------------------------------------------------------------------
37
38 extern "C" {
39 static
40 void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
41 {
42 if (g_isIdle) wxapp_install_idle_handler();
43
44 if (!rb->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 if (!button->active) return;
49
50 if (rb->m_blockEvent) return;
51
52 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
53 event.SetInt( rb->GetValue() );
54 event.SetEventObject( rb );
55 rb->GetEventHandler()->ProcessEvent( event );
56 }
57 }
58
59 //-----------------------------------------------------------------------------
60 // wxRadioButton
61 //-----------------------------------------------------------------------------
62
63 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
64
65 bool wxRadioButton::Create( wxWindow *parent,
66 wxWindowID id,
67 const wxString& label,
68 const wxPoint& pos,
69 const wxSize& size,
70 long style,
71 const wxValidator& validator,
72 const wxString& name )
73 {
74 m_acceptsFocus = TRUE;
75 m_needParent = TRUE;
76
77 m_blockEvent = FALSE;
78
79 if (!PreCreation( parent, pos, size ) ||
80 !CreateBase( parent, id, pos, size, style, validator, name ))
81 {
82 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
83 return FALSE;
84 }
85
86 GSList* radioButtonGroup = NULL;
87 if (!HasFlag(wxRB_GROUP))
88 {
89 // search backward for last group start
90 wxRadioButton *chief = (wxRadioButton*) NULL;
91 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
92 while (node)
93 {
94 wxWindow *child = node->GetData();
95 if (child->IsRadioButton())
96 {
97 chief = (wxRadioButton*) child;
98 if (child->HasFlag(wxRB_GROUP))
99 break;
100 }
101 node = node->GetPrevious();
102 }
103 if (chief)
104 {
105 // we are part of the group started by chief
106 radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
107 }
108 }
109
110 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
111
112 SetLabel(label);
113
114 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
115 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
116
117 m_parent->DoAddChild( this );
118
119 PostCreation(size);
120
121 return TRUE;
122 }
123
124 void wxRadioButton::SetLabel( const wxString& label )
125 {
126 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
127
128 wxControl::SetLabel( label );
129 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) );
130 #ifdef __WXGTK20__
131 wxString label2 = PrepareLabelMnemonics( label );
132 gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) );
133 #else
134 gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) );
135 #endif
136 }
137
138 void wxRadioButton::SetValue( bool val )
139 {
140 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
141
142 if (val == GetValue())
143 return;
144
145 m_blockEvent = TRUE;
146
147 if (val)
148 {
149 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
150 }
151 else
152 {
153 // should give an assert
154 // RL - No it shouldn't. A wxGenericValidator might try to set it
155 // as FALSE. Failing silently is probably TRTTD here.
156 }
157
158 m_blockEvent = FALSE;
159 }
160
161 bool wxRadioButton::GetValue() const
162 {
163 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
164
165 return GTK_TOGGLE_BUTTON(m_widget)->active;
166 }
167
168 bool wxRadioButton::Enable( bool enable )
169 {
170 if ( !wxControl::Enable( enable ) )
171 return FALSE;
172
173 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
174
175 return TRUE;
176 }
177
178 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
179 {
180 gtk_widget_modify_style(m_widget, style);
181 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
182 }
183
184 bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
185 {
186 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
187 }
188
189 void wxRadioButton::OnInternalIdle()
190 {
191 wxCursor cursor = m_cursor;
192 if (g_globalCursor.Ok()) cursor = g_globalCursor;
193
194 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
195 if ( win && cursor.Ok())
196 {
197 /* I now set the cursor the anew in every OnInternalIdle call
198 as setting the cursor in a parent window also effects the
199 windows above so that checking for the current cursor is
200 not possible. */
201
202 gdk_window_set_cursor( win, cursor.GetCursor() );
203 }
204
205 if (g_delayedFocus == this)
206 {
207 if (GTK_WIDGET_REALIZED(m_widget))
208 {
209 gtk_widget_grab_focus( m_widget );
210 g_delayedFocus = NULL;
211 }
212 }
213
214 if (wxUpdateUIEvent::CanUpdate(this))
215 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
216 }
217
218 wxSize wxRadioButton::DoGetBestSize() const
219 {
220 return wxControl::DoGetBestSize();
221 }
222
223 // static
224 wxVisualAttributes
225 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
226 {
227 wxVisualAttributes attr;
228 // NB: we need toplevel window so that GTK+ can find the right style
229 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
230 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
231 gtk_container_add(GTK_CONTAINER(wnd), widget);
232 attr = GetDefaultAttributesFromGTKWidget(widget);
233 gtk_widget_destroy(wnd);
234 return attr;
235 }
236
237
238 #endif