support for GTK3
[wxWidgets.git] / src / gtk / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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_RADIOBTN
14
15 #include "wx/radiobut.h"
16
17 #include <gtk/gtk.h>
18 #include "wx/gtk/private.h"
19 #include "wx/gtk/private/gtk2-compat.h"
20
21 //-----------------------------------------------------------------------------
22 // data
23 //-----------------------------------------------------------------------------
24
25 extern bool g_blockEventsOnDrag;
26
27 //-----------------------------------------------------------------------------
28 // "clicked"
29 //-----------------------------------------------------------------------------
30
31 extern "C" {
32 static
33 void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
34 {
35 if (!rb->m_hasVMT) return;
36
37 if (g_blockEventsOnDrag) return;
38
39 if (!gtk_toggle_button_get_active(button)) return;
40
41 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
42 event.SetInt( rb->GetValue() );
43 event.SetEventObject( rb );
44 rb->HandleWindowEvent( event );
45 }
46 }
47
48 //-----------------------------------------------------------------------------
49 // wxRadioButton
50 //-----------------------------------------------------------------------------
51
52 bool wxRadioButton::Create( wxWindow *parent,
53 wxWindowID id,
54 const wxString& label,
55 const wxPoint& pos,
56 const wxSize& size,
57 long style,
58 const wxValidator& validator,
59 const wxString& name )
60 {
61 if (!PreCreation( parent, pos, size ) ||
62 !CreateBase( parent, id, pos, size, style, validator, name ))
63 {
64 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
65 return false;
66 }
67
68 // Check if this radio button should be put into an existing group. This
69 // shouldn't be done if it's given a style to explicitly start a new group
70 // or if it's not meant to be a part of a group at all.
71 GSList* radioButtonGroup = NULL;
72 if (!HasFlag(wxRB_GROUP) && !HasFlag(wxRB_SINGLE))
73 {
74 // search backward for last group start
75 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
76 for (; node; node = node->GetPrevious())
77 {
78 wxWindow *child = node->GetData();
79
80 // We stop at the first previous radio button in any case as it
81 // wouldn't make sense to put this button in a group with another
82 // one if there is a radio button that is not part of the same
83 // group between them.
84 if (wxIsKindOf(child, wxRadioButton))
85 {
86 // Any preceding radio button can be used to get its group, not
87 // necessarily one with wxRB_GROUP style, but exclude
88 // wxRB_SINGLE ones as their group should never be shared.
89 if (!child->HasFlag(wxRB_SINGLE))
90 {
91 radioButtonGroup = gtk_radio_button_get_group(
92 GTK_RADIO_BUTTON(child->m_widget));
93 }
94
95 break;
96 }
97 }
98 }
99
100 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
101 g_object_ref(m_widget);
102
103 SetLabel(label);
104
105 g_signal_connect_after (m_widget, "clicked",
106 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
107
108 m_parent->DoAddChild( this );
109
110 PostCreation(size);
111
112 return true;
113 }
114
115 void wxRadioButton::SetLabel( const wxString& label )
116 {
117 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
118
119 // save the original label
120 wxControlBase::SetLabel(label);
121
122 GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget))), label);
123 }
124
125 void wxRadioButton::SetValue( bool val )
126 {
127 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
128
129 if (val == GetValue())
130 return;
131
132 g_signal_handlers_block_by_func(
133 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
134
135 if (val)
136 {
137 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
138 }
139 else
140 {
141 // should give an assert
142 // RL - No it shouldn't. A wxGenericValidator might try to set it
143 // as FALSE. Failing silently is probably TRTTD here.
144 }
145
146 g_signal_handlers_unblock_by_func(
147 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
148 }
149
150 bool wxRadioButton::GetValue() const
151 {
152 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobutton") );
153
154 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0;
155 }
156
157 bool wxRadioButton::Enable( bool enable )
158 {
159 if (!base_type::Enable(enable))
160 return false;
161
162 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable);
163
164 if (enable)
165 GTKFixSensitivity();
166
167 return true;
168 }
169
170 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
171 {
172 GTKApplyStyle(m_widget, style);
173 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
174 }
175
176 GdkWindow *
177 wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
178 {
179 return gtk_button_get_event_window(GTK_BUTTON(m_widget));
180 }
181
182 // static
183 wxVisualAttributes
184 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
185 {
186 wxVisualAttributes attr;
187 // NB: we need toplevel window so that GTK+ can find the right style
188 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
189 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
190 gtk_container_add(GTK_CONTAINER(wnd), widget);
191 attr = GetDefaultAttributesFromGTKWidget(widget);
192 gtk_widget_destroy(wnd);
193 return attr;
194 }
195
196
197 #endif