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