]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobut.cpp
Typo fix
[wxWidgets.git] / src / gtk / radiobut.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: radiobut.cpp
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
e2762ff0 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "radiobut.h"
13#endif
14
1e6feb95 15#include "wx/defs.h"
dcf924a3
RR
16
17#if wxUSE_RADIOBOX
18
1e6feb95
VZ
19#include "wx/radiobut.h"
20
9e691f46 21#include "wx/gtk/private.h"
c801d85f 22
acfd422a
RR
23//-----------------------------------------------------------------------------
24// idle system
25//-----------------------------------------------------------------------------
26
27extern void wxapp_install_idle_handler();
28extern bool g_isIdle;
29
6de97a3b
RR
30//-----------------------------------------------------------------------------
31// data
32//-----------------------------------------------------------------------------
33
d7fa7eaa
RR
34extern bool g_blockEventsOnDrag;
35extern wxCursor g_globalCursor;
36extern wxWindowGTK *g_delayedFocus;
6de97a3b
RR
37
38//-----------------------------------------------------------------------------
bb4549de 39// "clicked"
6de97a3b
RR
40//-----------------------------------------------------------------------------
41
bb4549de 42static
e2762ff0 43void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
6de97a3b 44{
acfd422a
RR
45 if (g_isIdle) wxapp_install_idle_handler();
46
a2053b27 47 if (!rb->m_hasVMT) return;
bb4549de 48
f5d29b39 49 if (g_blockEventsOnDrag) return;
6de97a3b 50
e2762ff0 51 if (!button->active) return;
9864c56d
RR
52
53 if (rb->m_blockEvent) return;
e2762ff0 54
f5d29b39
RR
55 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
56 event.SetInt( rb->GetValue() );
57 event.SetEventObject( rb );
58 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b
RR
59}
60
bb4549de
RR
61//-----------------------------------------------------------------------------
62// wxRadioButton
63//-----------------------------------------------------------------------------
64
65IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
66
6de97a3b
RR
67bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
68 const wxPoint& pos, const wxSize& size, long style,
69 const wxValidator& validator, const wxString& name )
70{
b292e2f5 71 m_acceptsFocus = TRUE;
f5d29b39 72 m_needParent = TRUE;
953704c1 73 m_isRadioButton = TRUE;
9864c56d
RR
74
75 m_blockEvent = FALSE;
6de97a3b 76
4dcaf11a
RR
77 if (!PreCreation( parent, pos, size ) ||
78 !CreateBase( parent, id, pos, size, style, validator, name ))
79 {
223d09f6 80 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
9864c56d 81 return FALSE;
4dcaf11a 82 }
953704c1
RR
83
84 if (HasFlag(wxRB_GROUP))
85 {
9864c56d 86 // start a new group
953704c1
RR
87 m_radioButtonGroup = (GSList*) NULL;
88 }
89 else
90 {
9864c56d 91 // search backward for last group start
953704c1
RR
92 wxRadioButton *chief = (wxRadioButton*) NULL;
93 wxWindowList::Node *node = parent->GetChildren().GetLast();
94 while (node)
e2762ff0
RR
95 {
96 wxWindow *child = node->GetData();
97 if (child->m_isRadioButton)
98 {
99 chief = (wxRadioButton*) child;
100 if (child->HasFlag(wxRB_GROUP)) break;
101 }
102 node = node->GetPrevious();
953704c1 103 }
e2762ff0
RR
104 if (chief)
105 {
9864c56d 106 // we are part of the group started by chief
e2762ff0
RR
107 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
108 }
109 else
110 {
9864c56d 111 // start a new group
0544bc0a 112 m_radioButtonGroup = (GSList*) NULL;
e2762ff0 113 }
953704c1
RR
114 }
115
116 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
6de97a3b 117
f5d29b39 118 SetLabel(label);
6de97a3b 119
f5d29b39
RR
120 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
121 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b 122
f03fc89f 123 m_parent->DoAddChild( this );
6ca41e57 124
f5d29b39 125 PostCreation();
6de97a3b 126
db434467
RR
127 SetFont( parent->GetFont() );
128
129 wxSize size_best( DoGetBestSize() );
130 wxSize new_size( size );
131 if (new_size.x == -1)
132 new_size.x = size_best.x;
133 if (new_size.y == -1)
134 new_size.y = size_best.y;
135 if ((new_size.x != size.x) || (new_size.y != size.y))
136 SetSize( new_size.x, new_size.y );
137
f5d29b39
RR
138 SetBackgroundColour( parent->GetBackgroundColour() );
139 SetForegroundColour( parent->GetForegroundColour() );
58614078 140
f5d29b39 141 Show( TRUE );
6de97a3b 142
f5d29b39 143 return TRUE;
6de97a3b
RR
144}
145
146void wxRadioButton::SetLabel( const wxString& label )
147{
223d09f6 148 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f96aa4d9 149
f5d29b39 150 wxControl::SetLabel( label );
9e691f46 151 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) );
b019151f 152 gtk_label_set( g_label, GetLabel().mbc_str() );
6de97a3b
RR
153}
154
155void wxRadioButton::SetValue( bool val )
156{
223d09f6 157 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 158
953704c1 159 if (val == GetValue())
0659e7ee
RR
160 return;
161
9864c56d 162 m_blockEvent = TRUE;
953704c1 163
f5d29b39 164 if (val)
953704c1 165 {
e2762ff0 166 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 167 }
f5d29b39 168 else
953704c1
RR
169 {
170 // should give an assert
f6bcfd97
BP
171 // RL - No it shouldn't. A wxGenericValidator might try to set it
172 // as FALSE. Failing silently is probably TRTTD here.
953704c1 173 }
f6bcfd97 174
9864c56d 175 m_blockEvent = FALSE;
6de97a3b
RR
176}
177
eb082a08 178bool wxRadioButton::GetValue() const
6de97a3b 179{
223d09f6 180 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
f96aa4d9 181
f5d29b39 182 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
183}
184
f03fc89f 185bool wxRadioButton::Enable( bool enable )
d3904ceb 186{
f03fc89f
VZ
187 if ( !wxControl::Enable( enable ) )
188 return FALSE;
f96aa4d9 189
9e691f46 190 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
191
192 return TRUE;
d3904ceb
RR
193}
194
58614078 195void wxRadioButton::ApplyWidgetStyle()
868a2826 196{
f5d29b39
RR
197 SetWidgetStyle();
198 gtk_widget_set_style( m_widget, m_widgetStyle );
9e691f46 199 gtk_widget_set_style( BUTTON_CHILD(m_widget), m_widgetStyle );
f96aa4d9 200}
dcf924a3 201
2f073eb2
RR
202bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
203{
9e691f46 204 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
205}
206
207void wxRadioButton::OnInternalIdle()
208{
209 wxCursor cursor = m_cursor;
210 if (g_globalCursor.Ok()) cursor = g_globalCursor;
211
9e691f46
VZ
212 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
213 if ( win && cursor.Ok())
2f073eb2
RR
214 {
215 /* I now set the cursor the anew in every OnInternalIdle call
e2762ff0
RR
216 as setting the cursor in a parent window also effects the
217 windows above so that checking for the current cursor is
218 not possible. */
219
9e691f46 220 gdk_window_set_cursor( win, cursor.GetCursor() );
2f073eb2
RR
221 }
222
d7fa7eaa
RR
223 if (g_delayedFocus == this)
224 {
225 if (GTK_WIDGET_REALIZED(m_widget))
226 {
227 gtk_widget_grab_focus( m_widget );
228 g_delayedFocus = NULL;
229 }
230 }
231
2f073eb2
RR
232 UpdateWindowUI();
233}
234
db434467
RR
235wxSize wxRadioButton::DoGetBestSize() const
236{
237 return wxControl::DoGetBestSize();
238}
239
dcf924a3 240#endif