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