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