STL-ification patch for wxMSW and wxGTK.
[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 #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,
68 wxWindowID id,
69 const wxString& label,
70 const wxPoint& pos,
71 const wxSize& size,
72 long style,
73 const wxValidator& validator,
74 const wxString& name )
75 {
76 m_acceptsFocus = TRUE;
77 m_needParent = TRUE;
78
79 m_blockEvent = FALSE;
80
81 if (!PreCreation( parent, pos, size ) ||
82 !CreateBase( parent, id, pos, size, style, validator, name ))
83 {
84 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
85 return FALSE;
86 }
87
88 if (HasFlag(wxRB_GROUP))
89 {
90 // start a new group
91 m_radioButtonGroup = (GSList*) NULL;
92 }
93 else
94 {
95 // search backward for last group start
96 wxRadioButton *chief = (wxRadioButton*) NULL;
97 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
98 while (node)
99 {
100 wxWindow *child = node->GetData();
101 if (child->IsRadioButton())
102 {
103 chief = (wxRadioButton*) child;
104 if (child->HasFlag(wxRB_GROUP))
105 break;
106 }
107 node = node->GetPrevious();
108 }
109 if (chief)
110 {
111 // we are part of the group started by chief
112 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
113 }
114 else
115 {
116 // start a new group
117 m_radioButtonGroup = (GSList*) NULL;
118 }
119 }
120
121 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, wxGTK_CONV( label ) );
122
123 SetLabel(label);
124
125 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
126 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
127
128 m_parent->DoAddChild( this );
129
130 PostCreation();
131
132 SetFont( parent->GetFont() );
133
134 wxSize size_best( DoGetBestSize() );
135 wxSize new_size( size );
136 if (new_size.x == -1)
137 new_size.x = size_best.x;
138 if (new_size.y == -1)
139 new_size.y = size_best.y;
140 if ((new_size.x != size.x) || (new_size.y != size.y))
141 SetSize( new_size.x, new_size.y );
142
143 SetBackgroundColour( parent->GetBackgroundColour() );
144 SetForegroundColour( parent->GetForegroundColour() );
145
146 Show( TRUE );
147
148 return TRUE;
149 }
150
151 void wxRadioButton::SetLabel( const wxString& label )
152 {
153 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
154
155 wxControl::SetLabel( label );
156 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) );
157 #ifdef __WXGTK20__
158 wxString label2 = PrepareLabelMnemonics( label );
159 gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) );
160 #else
161 gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) );
162 #endif
163 }
164
165 void wxRadioButton::SetValue( bool val )
166 {
167 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
168
169 if (val == GetValue())
170 return;
171
172 m_blockEvent = TRUE;
173
174 if (val)
175 {
176 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
177 }
178 else
179 {
180 // should give an assert
181 // RL - No it shouldn't. A wxGenericValidator might try to set it
182 // as FALSE. Failing silently is probably TRTTD here.
183 }
184
185 m_blockEvent = FALSE;
186 }
187
188 bool wxRadioButton::GetValue() const
189 {
190 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
191
192 return GTK_TOGGLE_BUTTON(m_widget)->active;
193 }
194
195 bool wxRadioButton::Enable( bool enable )
196 {
197 if ( !wxControl::Enable( enable ) )
198 return FALSE;
199
200 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
201
202 return TRUE;
203 }
204
205 void wxRadioButton::ApplyWidgetStyle()
206 {
207 SetWidgetStyle();
208 gtk_widget_set_style( m_widget, m_widgetStyle );
209 gtk_widget_set_style( BUTTON_CHILD(m_widget), m_widgetStyle );
210 }
211
212 bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
213 {
214 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
215 }
216
217 void wxRadioButton::OnInternalIdle()
218 {
219 wxCursor cursor = m_cursor;
220 if (g_globalCursor.Ok()) cursor = g_globalCursor;
221
222 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
223 if ( win && cursor.Ok())
224 {
225 /* I now set the cursor the anew in every OnInternalIdle call
226 as setting the cursor in a parent window also effects the
227 windows above so that checking for the current cursor is
228 not possible. */
229
230 gdk_window_set_cursor( win, cursor.GetCursor() );
231 }
232
233 if (g_delayedFocus == this)
234 {
235 if (GTK_WIDGET_REALIZED(m_widget))
236 {
237 gtk_widget_grab_focus( m_widget );
238 g_delayedFocus = NULL;
239 }
240 }
241
242 if (wxUpdateUIEvent::CanUpdate(this))
243 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
244 }
245
246 wxSize wxRadioButton::DoGetBestSize() const
247 {
248 return wxControl::DoGetBestSize();
249 }
250
251 #endif