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