]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/radiobut.cpp
readability cleanup
[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/gtk/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->GetEventHandler()->ProcessEvent( 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 if (HasFlag(wxRB_GROUP))
87 {
88 // start a new group
89 m_radioButtonGroup = (GSList*) NULL;
90 }
91 else
92 {
93 // search backward for last group start
94 wxRadioButton *chief = (wxRadioButton*) NULL;
95 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
96 while (node)
97 {
98 wxWindow *child = node->GetData();
99 if (child->IsRadioButton())
100 {
101 chief = (wxRadioButton*) child;
102 if (child->HasFlag(wxRB_GROUP))
103 break;
104 }
105 node = node->GetPrevious();
106 }
107 if (chief)
108 {
109 // we are part of the group started by chief
110 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
111 }
112 else
113 {
114 // start a new group
115 m_radioButtonGroup = (GSList*) NULL;
116 }
117 }
118
119 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, wxGTK_CONV( label ) );
120
121 SetLabel(label);
122
123 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
124 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
125
126 m_parent->DoAddChild( this );
127
128 PostCreation(size);
129
130 return TRUE;
131 }
132
133 void wxRadioButton::SetLabel( const wxString& label )
134 {
135 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
136
137 wxControl::SetLabel( label );
138 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) );
139 #ifdef __WXGTK20__
140 wxString label2 = PrepareLabelMnemonics( label );
141 gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) );
142 #else
143 gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) );
144 #endif
145 }
146
147 void wxRadioButton::SetValue( bool val )
148 {
149 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
150
151 if (val == GetValue())
152 return;
153
154 m_blockEvent = TRUE;
155
156 if (val)
157 {
158 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
159 }
160 else
161 {
162 // should give an assert
163 // RL - No it shouldn't. A wxGenericValidator might try to set it
164 // as FALSE. Failing silently is probably TRTTD here.
165 }
166
167 m_blockEvent = FALSE;
168 }
169
170 bool wxRadioButton::GetValue() const
171 {
172 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
173
174 return GTK_TOGGLE_BUTTON(m_widget)->active;
175 }
176
177 bool wxRadioButton::Enable( bool enable )
178 {
179 if ( !wxControl::Enable( enable ) )
180 return FALSE;
181
182 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
183
184 return TRUE;
185 }
186
187 void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
188 {
189 gtk_widget_modify_style(m_widget, style);
190 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
191 }
192
193 bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
194 {
195 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
196 }
197
198 void wxRadioButton::OnInternalIdle()
199 {
200 wxCursor cursor = m_cursor;
201 if (g_globalCursor.Ok()) cursor = g_globalCursor;
202
203 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
204 if ( win && cursor.Ok())
205 {
206 /* I now set the cursor the anew in every OnInternalIdle call
207 as setting the cursor in a parent window also effects the
208 windows above so that checking for the current cursor is
209 not possible. */
210
211 gdk_window_set_cursor( win, cursor.GetCursor() );
212 }
213
214 if (g_delayedFocus == this)
215 {
216 if (GTK_WIDGET_REALIZED(m_widget))
217 {
218 gtk_widget_grab_focus( m_widget );
219 g_delayedFocus = NULL;
220 }
221 }
222
223 if (wxUpdateUIEvent::CanUpdate(this))
224 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
225 }
226
227 wxSize wxRadioButton::DoGetBestSize() const
228 {
229 return wxControl::DoGetBestSize();
230 }
231
232 // static
233 wxVisualAttributes
234 wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
235 {
236 wxVisualAttributes attr;
237 // NB: we need toplevel window so that GTK+ can find the right style
238 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
239 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
240 gtk_container_add(GTK_CONTAINER(wnd), widget);
241 attr = GetDefaultAttributesFromGTKWidget(widget);
242 gtk_widget_destroy(wnd);
243 return attr;
244 }
245
246
247 #endif