use native TAB traversal for GTK+ 2
[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
25 //-----------------------------------------------------------------------------
26 // "clicked"
27 //-----------------------------------------------------------------------------
28
29 extern "C" {
30 static
31 void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
32 {
33 if (g_isIdle) wxapp_install_idle_handler();
34
35 if (!rb->m_hasVMT) return;
36
37 if (g_blockEventsOnDrag) return;
38
39 if (!button->active) return;
40
41 if (rb->m_blockEvent) return;
42
43 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
44 event.SetInt( rb->GetValue() );
45 event.SetEventObject( rb );
46 rb->GetEventHandler()->ProcessEvent( event );
47 }
48 }
49
50 //-----------------------------------------------------------------------------
51 // wxRadioButton
52 //-----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
55
56 bool wxRadioButton::Create( wxWindow *parent,
57 wxWindowID id,
58 const wxString& label,
59 const wxPoint& pos,
60 const wxSize& size,
61 long style,
62 const wxValidator& validator,
63 const wxString& name )
64 {
65 m_needParent = TRUE;
66
67 m_blockEvent = FALSE;
68
69 if (!PreCreation( parent, pos, size ) ||
70 !CreateBase( parent, id, pos, size, style, validator, name ))
71 {
72 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
73 return FALSE;
74 }
75
76 GSList* radioButtonGroup = NULL;
77 if (!HasFlag(wxRB_GROUP))
78 {
79 // search backward for last group start
80 wxRadioButton *chief = (wxRadioButton*) NULL;
81 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
82 while (node)
83 {
84 wxWindow *child = node->GetData();
85 if (child->IsRadioButton())
86 {
87 chief = (wxRadioButton*) child;
88 if (child->HasFlag(wxRB_GROUP))
89 break;
90 }
91 node = node->GetPrevious();
92 }
93 if (chief)
94 {
95 // we are part of the group started by chief
96 radioButtonGroup = gtk_radio_button_get_group( GTK_RADIO_BUTTON(chief->m_widget) );
97 }
98 }
99
100 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
101
102 SetLabel(label);
103
104 g_signal_connect (m_widget, "clicked",
105 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
106
107 m_parent->DoAddChild( this );
108
109 PostCreation(size);
110
111 return TRUE;
112 }
113
114 void wxRadioButton::SetLabel( const wxString& label )
115 {
116 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
117
118 GTKSetLabelForLabel(GTK_LABEL(GTK_BIN(m_widget)->child), label);
119 }
120
121 void wxRadioButton::SetValue( bool val )
122 {
123 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
124
125 if (val == GetValue())
126 return;
127
128 m_blockEvent = TRUE;
129
130 if (val)
131 {
132 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
133 }
134 else
135 {
136 // should give an assert
137 // RL - No it shouldn't. A wxGenericValidator might try to set it
138 // as FALSE. Failing silently is probably TRTTD here.
139 }
140
141 m_blockEvent = FALSE;
142 }
143
144 bool wxRadioButton::GetValue() const
145 {
146 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
147
148 return GTK_TOGGLE_BUTTON(m_widget)->active;
149 }
150
151 bool wxRadioButton::Enable( bool enable )
152 {
153 if ( !wxControl::Enable( enable ) )
154 return FALSE;
155
156 gtk_widget_set_sensitive(GTK_BIN(m_widget)->child, enable);
157
158 return TRUE;
159 }
160
161 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
162 {
163 gtk_widget_modify_style(m_widget, style);
164 gtk_widget_modify_style(GTK_BIN(m_widget)->child, style);
165 }
166
167 GdkWindow *
168 wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
169 {
170 return GTK_BUTTON(m_widget)->event_window;
171 }
172
173 wxSize wxRadioButton::DoGetBestSize() const
174 {
175 return wxControl::DoGetBestSize();
176 }
177
178 // static
179 wxVisualAttributes
180 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
181 {
182 wxVisualAttributes attr;
183 // NB: we need toplevel window so that GTK+ can find the right style
184 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
185 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
186 gtk_container_add(GTK_CONTAINER(wnd), widget);
187 attr = GetDefaultAttributesFromGTKWidget(widget);
188 gtk_widget_destroy(wnd);
189 return attr;
190 }
191
192
193 #endif