]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/radiobut.cpp
Oops.
[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,
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::Node *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, label.mbc_str() );
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 gtk_label_set( g_label, GetLabel().mbc_str() );
158 }
159
160 void wxRadioButton::SetValue( bool val )
161 {
162 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
163
164 if (val == GetValue())
165 return;
166
167 m_blockEvent = TRUE;
168
169 if (val)
170 {
171 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
172 }
173 else
174 {
175 // should give an assert
176 // RL - No it shouldn't. A wxGenericValidator might try to set it
177 // as FALSE. Failing silently is probably TRTTD here.
178 }
179
180 m_blockEvent = FALSE;
181 }
182
183 bool wxRadioButton::GetValue() const
184 {
185 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
186
187 return GTK_TOGGLE_BUTTON(m_widget)->active;
188 }
189
190 bool wxRadioButton::Enable( bool enable )
191 {
192 if ( !wxControl::Enable( enable ) )
193 return FALSE;
194
195 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
196
197 return TRUE;
198 }
199
200 void wxRadioButton::ApplyWidgetStyle()
201 {
202 SetWidgetStyle();
203 gtk_widget_set_style( m_widget, m_widgetStyle );
204 gtk_widget_set_style( BUTTON_CHILD(m_widget), m_widgetStyle );
205 }
206
207 bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
208 {
209 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
210 }
211
212 void wxRadioButton::OnInternalIdle()
213 {
214 wxCursor cursor = m_cursor;
215 if (g_globalCursor.Ok()) cursor = g_globalCursor;
216
217 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
218 if ( win && cursor.Ok())
219 {
220 /* I now set the cursor the anew in every OnInternalIdle call
221 as setting the cursor in a parent window also effects the
222 windows above so that checking for the current cursor is
223 not possible. */
224
225 gdk_window_set_cursor( win, cursor.GetCursor() );
226 }
227
228 if (g_delayedFocus == this)
229 {
230 if (GTK_WIDGET_REALIZED(m_widget))
231 {
232 gtk_widget_grab_focus( m_widget );
233 g_delayedFocus = NULL;
234 }
235 }
236
237 UpdateWindowUI();
238 }
239
240 wxSize wxRadioButton::DoGetBestSize() const
241 {
242 return wxControl::DoGetBestSize();
243 }
244
245 #endif