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