]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/radiobut.cpp
'Set to Unspecified' -> 'Set Value to Unspecified'
[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
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
3cbab641 17#include "wx/gtk1/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" {
b2ff89d6 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;
b2ff89d6 45
f5d29b39 46 if (g_blockEventsOnDrag) return;
b2ff89d6 47
e2762ff0 48 if (!button->active) return;
b2ff89d6 49
9864c56d 50 if (rb->m_blockEvent) return;
b2ff89d6 51
f5d29b39
RR
52 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
53 event.SetInt( rb->GetValue() );
54 event.SetEventObject( rb );
937013e0 55 rb->HandleWindowEvent( event );
6de97a3b 56}
865bb325 57}
6de97a3b 58
bb4549de
RR
59//-----------------------------------------------------------------------------
60// wxRadioButton
61//-----------------------------------------------------------------------------
62
63IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
b2ff89d6 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;
b2ff89d6 76
9864c56d 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 85
739b7529
DS
86 GSList* radioButtonGroup = NULL;
87 if (!HasFlag(wxRB_GROUP))
953704c1 88 {
9864c56d 89 // search backward for last group start
d3b9f782 90 wxRadioButton *chief = NULL;
222ed1d6 91 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
953704c1 92 while (node)
e2762ff0
RR
93 {
94 wxWindow *child = node->GetData();
2b4f3c9f 95 if (child->IsRadioButton())
e2762ff0
RR
96 {
97 chief = (wxRadioButton*) child;
2b4f3c9f
VZ
98 if (child->HasFlag(wxRB_GROUP))
99 break;
e2762ff0
RR
100 }
101 node = node->GetPrevious();
953704c1 102 }
e2762ff0
RR
103 if (chief)
104 {
9864c56d 105 // we are part of the group started by chief
739b7529 106 radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
e2762ff0 107 }
953704c1
RR
108 }
109
739b7529 110 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
b2ff89d6 111
f5d29b39 112 SetLabel(label);
6de97a3b 113
b2ff89d6 114 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
f5d29b39 115 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
b2ff89d6 116
f03fc89f 117 m_parent->DoAddChild( this );
b2ff89d6 118
abdeb9e7 119 PostCreation(size);
6de97a3b 120
f5d29b39 121 return TRUE;
6de97a3b
RR
122}
123
124void wxRadioButton::SetLabel( const wxString& label )
125{
223d09f6 126 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
b2ff89d6
VZ
127
128 GTKSetLabelForLabel(GTK_LABEL(BUTTON_CHILD(m_widget)), label);
6de97a3b
RR
129}
130
131void wxRadioButton::SetValue( bool val )
132{
223d09f6 133 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 134
953704c1 135 if (val == GetValue())
0659e7ee
RR
136 return;
137
9864c56d 138 m_blockEvent = TRUE;
953704c1 139
f5d29b39 140 if (val)
953704c1 141 {
e2762ff0 142 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 143 }
f5d29b39 144 else
953704c1
RR
145 {
146 // should give an assert
f6bcfd97
BP
147 // RL - No it shouldn't. A wxGenericValidator might try to set it
148 // as FALSE. Failing silently is probably TRTTD here.
953704c1 149 }
f6bcfd97 150
9864c56d 151 m_blockEvent = FALSE;
6de97a3b
RR
152}
153
eb082a08 154bool wxRadioButton::GetValue() const
6de97a3b 155{
223d09f6 156 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
b2ff89d6 157
f5d29b39 158 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
159}
160
f03fc89f 161bool wxRadioButton::Enable( bool enable )
d3904ceb 162{
f03fc89f
VZ
163 if ( !wxControl::Enable( enable ) )
164 return FALSE;
b2ff89d6 165
9e691f46 166 gtk_widget_set_sensitive( BUTTON_CHILD(m_widget), enable );
f03fc89f
VZ
167
168 return TRUE;
d3904ceb
RR
169}
170
f40fdaa3 171void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 172{
f40fdaa3
VS
173 gtk_widget_modify_style(m_widget, style);
174 gtk_widget_modify_style(BUTTON_CHILD(m_widget), style);
f96aa4d9 175}
dcf924a3 176
2f073eb2
RR
177bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
178{
9e691f46 179 return window == TOGGLE_BUTTON_EVENT_WIN(m_widget);
2f073eb2
RR
180}
181
182void wxRadioButton::OnInternalIdle()
183{
184 wxCursor cursor = m_cursor;
185 if (g_globalCursor.Ok()) cursor = g_globalCursor;
186
9e691f46
VZ
187 GdkWindow *win = TOGGLE_BUTTON_EVENT_WIN(m_widget);
188 if ( win && cursor.Ok())
2f073eb2
RR
189 {
190 /* I now set the cursor the anew in every OnInternalIdle call
e2762ff0
RR
191 as setting the cursor in a parent window also effects the
192 windows above so that checking for the current cursor is
193 not possible. */
b2ff89d6 194
9e691f46 195 gdk_window_set_cursor( win, cursor.GetCursor() );
2f073eb2
RR
196 }
197
d7fa7eaa
RR
198 if (g_delayedFocus == this)
199 {
200 if (GTK_WIDGET_REALIZED(m_widget))
201 {
202 gtk_widget_grab_focus( m_widget );
203 g_delayedFocus = NULL;
204 }
205 }
206
e39af974
JS
207 if (wxUpdateUIEvent::CanUpdate(this))
208 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
2f073eb2
RR
209}
210
db434467
RR
211wxSize wxRadioButton::DoGetBestSize() const
212{
213 return wxControl::DoGetBestSize();
214}
215
9d522606
RD
216// static
217wxVisualAttributes
218wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
219{
220 wxVisualAttributes attr;
bc0eb46c
VS
221 // NB: we need toplevel window so that GTK+ can find the right style
222 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 223 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
bc0eb46c 224 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 225 attr = GetDefaultAttributesFromGTKWidget(widget);
bc0eb46c 226 gtk_widget_destroy(wnd);
9d522606
RD
227 return attr;
228}
229
230
dcf924a3 231#endif