]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobut.cpp
Fix memory leak when a spacer is added, and crash when a window is added before wxSiz...
[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
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
10
14f355c2 11#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
12#pragma implementation "radiobut.h"
13#endif
14
14f355c2
VS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
dcf924a3
RR
17
18#if wxUSE_RADIOBOX
19
1e6feb95
VZ
20#include "wx/radiobut.h"
21
9e691f46 22#include "wx/gtk/private.h"
c801d85f 23
acfd422a
RR
24//-----------------------------------------------------------------------------
25// idle system
26//-----------------------------------------------------------------------------
27
28extern void wxapp_install_idle_handler();
29extern bool g_isIdle;
30
6de97a3b
RR
31//-----------------------------------------------------------------------------
32// data
33//-----------------------------------------------------------------------------
34
d7fa7eaa
RR
35extern bool g_blockEventsOnDrag;
36extern wxCursor g_globalCursor;
37extern wxWindowGTK *g_delayedFocus;
6de97a3b
RR
38
39//-----------------------------------------------------------------------------
bb4549de 40// "clicked"
6de97a3b
RR
41//-----------------------------------------------------------------------------
42
865bb325 43extern "C" {
bb4549de 44static
e2762ff0 45void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
6de97a3b 46{
acfd422a
RR
47 if (g_isIdle) wxapp_install_idle_handler();
48
a2053b27 49 if (!rb->m_hasVMT) return;
bb4549de 50
f5d29b39 51 if (g_blockEventsOnDrag) return;
6de97a3b 52
e2762ff0 53 if (!button->active) return;
9864c56d
RR
54
55 if (rb->m_blockEvent) return;
e2762ff0 56
f5d29b39
RR
57 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
58 event.SetInt( rb->GetValue() );
59 event.SetEventObject( rb );
60 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b 61}
865bb325 62}
6de97a3b 63
bb4549de
RR
64//-----------------------------------------------------------------------------
65// wxRadioButton
66//-----------------------------------------------------------------------------
67
68IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
69
2b4f3c9f
VZ
70bool wxRadioButton::Create( wxWindow *parent,
71 wxWindowID id,
72 const wxString& label,
73 const wxPoint& pos,
74 const wxSize& size,
75 long style,
76 const wxValidator& validator,
77 const wxString& name )
6de97a3b 78{
b292e2f5 79 m_acceptsFocus = TRUE;
f5d29b39 80 m_needParent = TRUE;
9864c56d
RR
81
82 m_blockEvent = FALSE;
6de97a3b 83
4dcaf11a
RR
84 if (!PreCreation( parent, pos, size ) ||
85 !CreateBase( parent, id, pos, size, style, validator, name ))
86 {
223d09f6 87 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
9864c56d 88 return FALSE;
4dcaf11a 89 }
953704c1
RR
90
91 if (HasFlag(wxRB_GROUP))
92 {
9864c56d 93 // start a new group
953704c1
RR
94 m_radioButtonGroup = (GSList*) NULL;
95 }
96 else
97 {
9864c56d 98 // search backward for last group start
953704c1 99 wxRadioButton *chief = (wxRadioButton*) NULL;
222ed1d6 100 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
953704c1 101 while (node)
e2762ff0
RR
102 {
103 wxWindow *child = node->GetData();
2b4f3c9f 104 if (child->IsRadioButton())
e2762ff0
RR
105 {
106 chief = (wxRadioButton*) child;
2b4f3c9f
VZ
107 if (child->HasFlag(wxRB_GROUP))
108 break;
e2762ff0
RR
109 }
110 node = node->GetPrevious();
953704c1 111 }
e2762ff0
RR
112 if (chief)
113 {
9864c56d 114 // we are part of the group started by chief
e2762ff0
RR
115 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
116 }
117 else
118 {
9864c56d 119 // start a new group
0544bc0a 120 m_radioButtonGroup = (GSList*) NULL;
e2762ff0 121 }
953704c1
RR
122 }
123
fab591c5 124 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, wxGTK_CONV( label ) );
6de97a3b 125
f5d29b39 126 SetLabel(label);
6de97a3b 127
f5d29b39
RR
128 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
129 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b 130
f03fc89f 131 m_parent->DoAddChild( this );
6ca41e57 132
abdeb9e7 133 PostCreation(size);
6de97a3b 134
f5d29b39 135 return TRUE;
6de97a3b
RR
136}
137
138void wxRadioButton::SetLabel( const wxString& label )
139{
223d09f6 140 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f96aa4d9 141
f5d29b39 142 wxControl::SetLabel( label );
9e691f46 143 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(m_widget) );
eaafd2f8
VS
144#ifdef __WXGTK20__
145 wxString label2 = PrepareLabelMnemonics( label );
146 gtk_label_set_text_with_mnemonic( g_label, wxGTK_CONV( label2 ) );
147#else
fab591c5 148 gtk_label_set( g_label, wxGTK_CONV( GetLabel() ) );
eaafd2f8 149#endif
6de97a3b
RR
150}
151
152void wxRadioButton::SetValue( bool val )
153{
223d09f6 154 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 155
953704c1 156 if (val == GetValue())
0659e7ee
RR
157 return;
158
9864c56d 159 m_blockEvent = TRUE;
953704c1 160
f5d29b39 161 if (val)
953704c1 162 {
e2762ff0 163 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 164 }
f5d29b39 165 else
953704c1
RR
166 {
167 // should give an assert
f6bcfd97
BP
168 // RL - No it shouldn't. A wxGenericValidator might try to set it
169 // as FALSE. Failing silently is probably TRTTD here.
953704c1 170 }
f6bcfd97 171
9864c56d 172 m_blockEvent = FALSE;
6de97a3b
RR
173}
174
eb082a08 175bool wxRadioButton::GetValue() const
6de97a3b 176{
223d09f6 177 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
f96aa4d9 178
f5d29b39 179 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
180}
181
f03fc89f 182bool wxRadioButton::Enable( bool enable )
d3904ceb 183{
f03fc89f
VZ
184 if ( !wxControl::Enable( enable ) )
185 return FALSE;
f96aa4d9 186
9e691f46 187 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
188
189 return TRUE;
d3904ceb
RR
190}
191
f40fdaa3 192void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 193{
f40fdaa3
VS
194 gtk_widget_modify_style(m_widget, style);
195 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
f96aa4d9 196}
dcf924a3 197
2f073eb2
RR
198bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
199{
9e691f46 200 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
201}
202
203void wxRadioButton::OnInternalIdle()
204{
205 wxCursor cursor = m_cursor;
206 if (g_globalCursor.Ok()) cursor = g_globalCursor;
207
9e691f46
VZ
208 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
209 if ( win && cursor.Ok())
2f073eb2
RR
210 {
211 /* I now set the cursor the anew in every OnInternalIdle call
e2762ff0
RR
212 as setting the cursor in a parent window also effects the
213 windows above so that checking for the current cursor is
214 not possible. */
215
9e691f46 216 gdk_window_set_cursor( win, cursor.GetCursor() );
2f073eb2
RR
217 }
218
d7fa7eaa
RR
219 if (g_delayedFocus == this)
220 {
221 if (GTK_WIDGET_REALIZED(m_widget))
222 {
223 gtk_widget_grab_focus( m_widget );
224 g_delayedFocus = NULL;
225 }
226 }
227
e39af974
JS
228 if (wxUpdateUIEvent::CanUpdate(this))
229 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
230}
231
db434467
RR
232wxSize wxRadioButton::DoGetBestSize() const
233{
234 return wxControl::DoGetBestSize();
235}
236
9d522606
RD
237// static
238wxVisualAttributes
239wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
240{
241 wxVisualAttributes attr;
bc0eb46c
VS
242 // NB: we need toplevel window so that GTK+ can find the right style
243 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 244 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
bc0eb46c 245 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 246 attr = GetDefaultAttributesFromGTKWidget(widget);
bc0eb46c 247 gtk_widget_destroy(wnd);
9d522606
RD
248 return attr;
249}
250
251
dcf924a3 252#endif