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