Various changes to focus handling when TLW start.
[wxWidgets.git] / src / gtk1 / 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
11 #ifdef __GNUG__
12 #pragma implementation "radiobut.h"
13 #endif
14
15 #include "wx/defs.h"
16
17 #if wxUSE_RADIOBOX
18
19 #include "wx/radiobut.h"
20
21 #include "wx/gtk/private.h"
22
23 //-----------------------------------------------------------------------------
24 // idle system
25 //-----------------------------------------------------------------------------
26
27 extern void wxapp_install_idle_handler();
28 extern bool g_isIdle;
29
30 //-----------------------------------------------------------------------------
31 // data
32 //-----------------------------------------------------------------------------
33
34 extern bool g_blockEventsOnDrag;
35 extern wxCursor g_globalCursor;
36 extern wxWindowGTK *g_delayedFocus;
37
38 //-----------------------------------------------------------------------------
39 // "clicked"
40 //-----------------------------------------------------------------------------
41
42 static
43 void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
44 {
45 if (g_isIdle) wxapp_install_idle_handler();
46
47 if (!rb->m_hasVMT) return;
48
49 if (g_blockEventsOnDrag) return;
50
51 if (!button->active) return;
52
53 if (rb->m_blockEvent) return;
54
55 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
56 event.SetInt( rb->GetValue() );
57 event.SetEventObject( rb );
58 rb->GetEventHandler()->ProcessEvent( event );
59 }
60
61 //-----------------------------------------------------------------------------
62 // wxRadioButton
63 //-----------------------------------------------------------------------------
64
65 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
66
67 bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
68 const wxPoint& pos, const wxSize& size, long style,
69 const wxValidator& validator, const wxString& name )
70 {
71 m_acceptsFocus = TRUE;
72 m_needParent = TRUE;
73 m_isRadioButton = TRUE;
74
75 m_blockEvent = FALSE;
76
77 if (!PreCreation( parent, pos, size ) ||
78 !CreateBase( parent, id, pos, size, style, validator, name ))
79 {
80 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
81 return FALSE;
82 }
83
84 if (HasFlag(wxRB_GROUP))
85 {
86 // start a new group
87 m_radioButtonGroup = (GSList*) NULL;
88 }
89 else
90 {
91 // search backward for last group start
92 wxRadioButton *chief = (wxRadioButton*) NULL;
93 wxWindowList::Node *node = parent->GetChildren().GetLast();
94 while (node)
95 {
96 wxWindow *child = node->GetData();
97 if (child->m_isRadioButton)
98 {
99 chief = (wxRadioButton*) child;
100 if (child->HasFlag(wxRB_GROUP)) break;
101 }
102 node = node->GetPrevious();
103 }
104 if (chief)
105 {
106 // we are part of the group started by chief
107 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
108 }
109 else
110 {
111 // start a new group
112 m_radioButtonGroup = (GSList*) NULL;
113 }
114 }
115
116 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
117
118 SetLabel(label);
119
120 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
121 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
122
123 m_parent->DoAddChild( this );
124
125 PostCreation();
126
127 SetFont( parent->GetFont() );
128
129 wxSize size_best( DoGetBestSize() );
130 wxSize new_size( size );
131 if (new_size.x == -1)
132 new_size.x = size_best.x;
133 if (new_size.y == -1)
134 new_size.y = size_best.y;
135 if ((new_size.x != size.x) || (new_size.y != size.y))
136 SetSize( new_size.x, new_size.y );
137
138 SetBackgroundColour( parent->GetBackgroundColour() );
139 SetForegroundColour( parent->GetForegroundColour() );
140
141 Show( TRUE );
142
143 return TRUE;
144 }
145
146 void wxRadioButton::SetLabel( const wxString& label )
147 {
148 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
149
150 wxControl::SetLabel( label );
151 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) );
152 gtk_label_set( g_label, GetLabel().mbc_str() );
153 }
154
155 void wxRadioButton::SetValue( bool val )
156 {
157 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
158
159 if (val == GetValue())
160 return;
161
162 m_blockEvent = TRUE;
163
164 if (val)
165 {
166 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
167 }
168 else
169 {
170 // should give an assert
171 // RL - No it shouldn't. A wxGenericValidator might try to set it
172 // as FALSE. Failing silently is probably TRTTD here.
173 }
174
175 m_blockEvent = FALSE;
176 }
177
178 bool wxRadioButton::GetValue() const
179 {
180 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
181
182 return GTK_TOGGLE_BUTTON(m_widget)->active;
183 }
184
185 bool wxRadioButton::Enable( bool enable )
186 {
187 if ( !wxControl::Enable( enable ) )
188 return FALSE;
189
190 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
191
192 return TRUE;
193 }
194
195 void wxRadioButton::ApplyWidgetStyle()
196 {
197 SetWidgetStyle();
198 gtk_widget_set_style( m_widget, m_widgetStyle );
199 gtk_widget_set_style( BUTTON_CHILD(m_widget), m_widgetStyle );
200 }
201
202 bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
203 {
204 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
205 }
206
207 void wxRadioButton::OnInternalIdle()
208 {
209 wxCursor cursor = m_cursor;
210 if (g_globalCursor.Ok()) cursor = g_globalCursor;
211
212 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
213 if ( win && cursor.Ok())
214 {
215 /* I now set the cursor the anew in every OnInternalIdle call
216 as setting the cursor in a parent window also effects the
217 windows above so that checking for the current cursor is
218 not possible. */
219
220 gdk_window_set_cursor( win, cursor.GetCursor() );
221 }
222
223 if (g_delayedFocus == this)
224 {
225 if (GTK_WIDGET_REALIZED(m_widget))
226 {
227 gtk_widget_grab_focus( m_widget );
228 g_delayedFocus = NULL;
229 }
230 }
231
232 UpdateWindowUI();
233 }
234
235 wxSize wxRadioButton::DoGetBestSize() const
236 {
237 return wxControl::DoGetBestSize();
238 }
239
240 #endif