]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobut.cpp
oops, commited debugging code by mistake
[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
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) );
eaafd2f8
VS
157#ifdef __WXGTK20__
158 wxString label2 = PrepareLabelMnemonics( label );
159 gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) );
160#else
fab591c5 161 gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) );
eaafd2f8 162#endif
6de97a3b
RR
163}
164
165void wxRadioButton::SetValue( bool val )
166{
223d09f6 167 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 168
953704c1 169 if (val == GetValue())
0659e7ee
RR
170 return;
171
9864c56d 172 m_blockEvent = TRUE;
953704c1 173
f5d29b39 174 if (val)
953704c1 175 {
e2762ff0 176 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 177 }
f5d29b39 178 else
953704c1
RR
179 {
180 // should give an assert
f6bcfd97
BP
181 // RL - No it shouldn't. A wxGenericValidator might try to set it
182 // as FALSE. Failing silently is probably TRTTD here.
953704c1 183 }
f6bcfd97 184
9864c56d 185 m_blockEvent = FALSE;
6de97a3b
RR
186}
187
eb082a08 188bool wxRadioButton::GetValue() const
6de97a3b 189{
223d09f6 190 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
f96aa4d9 191
f5d29b39 192 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
193}
194
f03fc89f 195bool wxRadioButton::Enable( bool enable )
d3904ceb 196{
f03fc89f
VZ
197 if ( !wxControl::Enable( enable ) )
198 return FALSE;
f96aa4d9 199
9e691f46 200 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
201
202 return TRUE;
d3904ceb
RR
203}
204
58614078 205void wxRadioButton::ApplyWidgetStyle()
868a2826 206{
f5d29b39
RR
207 SetWidgetStyle();
208 gtk_widget_set_style( m_widget, m_widgetStyle );
9e691f46 209 gtk_widget_set_style( BUTTON_CHILD(m_widget), m_widgetStyle );
f96aa4d9 210}
dcf924a3 211
2f073eb2
RR
212bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
213{
9e691f46 214 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
215}
216
217void wxRadioButton::OnInternalIdle()
218{
219 wxCursor cursor = m_cursor;
220 if (g_globalCursor.Ok()) cursor = g_globalCursor;
221
9e691f46
VZ
222 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
223 if ( win && cursor.Ok())
2f073eb2
RR
224 {
225 /* I now set the cursor the anew in every OnInternalIdle call
e2762ff0
RR
226 as setting the cursor in a parent window also effects the
227 windows above so that checking for the current cursor is
228 not possible. */
229
9e691f46 230 gdk_window_set_cursor( win, cursor.GetCursor() );
2f073eb2
RR
231 }
232
d7fa7eaa
RR
233 if (g_delayedFocus == this)
234 {
235 if (GTK_WIDGET_REALIZED(m_widget))
236 {
237 gtk_widget_grab_focus( m_widget );
238 g_delayedFocus = NULL;
239 }
240 }
241
2f073eb2
RR
242 UpdateWindowUI();
243}
244
db434467
RR
245wxSize wxRadioButton::DoGetBestSize() const
246{
247 return wxControl::DoGetBestSize();
248}
249
dcf924a3 250#endif