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