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