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