]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobut.cpp
Compilation fix,
[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
c801d85f
KB
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11#ifdef __GNUG__
12#pragma implementation "radiobut.h"
13#endif
14
15#include "wx/radiobut.h"
dcf924a3
RR
16
17#if wxUSE_RADIOBOX
18
3f26799e
RR
19#include <gdk/gdk.h>
20#include <gtk/gtk.h>
c801d85f 21
acfd422a
RR
22//-----------------------------------------------------------------------------
23// idle system
24//-----------------------------------------------------------------------------
25
26extern void wxapp_install_idle_handler();
27extern bool g_isIdle;
28
6de97a3b
RR
29//-----------------------------------------------------------------------------
30// data
31//-----------------------------------------------------------------------------
32
2f073eb2
RR
33extern bool g_blockEventsOnDrag;
34extern wxCursor g_globalCursor;
6de97a3b
RR
35
36//-----------------------------------------------------------------------------
bb4549de 37// "clicked"
6de97a3b
RR
38//-----------------------------------------------------------------------------
39
bb4549de
RR
40static
41void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioButton *rb )
6de97a3b 42{
acfd422a
RR
43 if (g_isIdle) wxapp_install_idle_handler();
44
a2053b27 45 if (!rb->m_hasVMT) return;
bb4549de 46
f5d29b39 47 if (g_blockEventsOnDrag) return;
6de97a3b 48
f5d29b39
RR
49 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
50 event.SetInt( rb->GetValue() );
51 event.SetEventObject( rb );
52 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b
RR
53}
54
bb4549de
RR
55//-----------------------------------------------------------------------------
56// wxRadioButton
57//-----------------------------------------------------------------------------
58
59IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
60
6de97a3b
RR
61bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
62 const wxPoint& pos, const wxSize& size, long style,
63 const wxValidator& validator, const wxString& name )
64{
b292e2f5 65 m_acceptsFocus = TRUE;
f5d29b39 66 m_needParent = TRUE;
953704c1 67 m_isRadioButton = TRUE;
6de97a3b 68
4dcaf11a
RR
69 if (!PreCreation( parent, pos, size ) ||
70 !CreateBase( parent, id, pos, size, style, validator, name ))
71 {
223d09f6 72 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
4dcaf11a
RR
73 return FALSE;
74 }
953704c1
RR
75
76 if (HasFlag(wxRB_GROUP))
77 {
78 /* start a new group */
79 m_radioButtonGroup = (GSList*) NULL;
80 }
81 else
82 {
83 /* search backward for last group start */
84 wxRadioButton *chief = (wxRadioButton*) NULL;
85 wxWindowList::Node *node = parent->GetChildren().GetLast();
86 while (node)
953704c1 87 {
db434467
RR
88 wxWindow *child = node->GetData();
89 if (child->m_isRadioButton)
90 {
91 chief = (wxRadioButton*) child;
92 if (child->HasFlag(wxRB_GROUP)) break;
93 }
94 node = node->GetPrevious();
953704c1 95 }
db434467
RR
96 if (chief)
97 {
0544bc0a 98 /* we are part of the group started by chief */
db434467
RR
99 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
100 }
101 else
102 {
0544bc0a
RR
103 /* start a new group */
104 m_radioButtonGroup = (GSList*) NULL;
db434467 105 }
953704c1
RR
106 }
107
108 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
6de97a3b 109
f5d29b39 110 SetLabel(label);
6de97a3b 111
f5d29b39
RR
112 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
113 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b 114
f03fc89f 115 m_parent->DoAddChild( this );
6ca41e57 116
f5d29b39 117 PostCreation();
6de97a3b 118
db434467
RR
119 ApplyWidgetStyle();
120
121 SetFont( parent->GetFont() );
122
123 wxSize size_best( DoGetBestSize() );
124 wxSize new_size( size );
125 if (new_size.x == -1)
126 new_size.x = size_best.x;
127 if (new_size.y == -1)
128 new_size.y = size_best.y;
129 if ((new_size.x != size.x) || (new_size.y != size.y))
130 SetSize( new_size.x, new_size.y );
131
f5d29b39
RR
132 SetBackgroundColour( parent->GetBackgroundColour() );
133 SetForegroundColour( parent->GetForegroundColour() );
58614078 134
f5d29b39 135 Show( TRUE );
6de97a3b 136
f5d29b39 137 return TRUE;
6de97a3b
RR
138}
139
140void wxRadioButton::SetLabel( const wxString& label )
141{
223d09f6 142 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f96aa4d9 143
f5d29b39
RR
144 wxControl::SetLabel( label );
145 GtkButton *bin = GTK_BUTTON( m_widget );
146 GtkLabel *g_label = GTK_LABEL( bin->child );
b019151f 147 gtk_label_set( g_label, GetLabel().mbc_str() );
6de97a3b
RR
148}
149
150void wxRadioButton::SetValue( bool val )
151{
223d09f6 152 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f96aa4d9 153
953704c1 154 if (val == GetValue())
0659e7ee
RR
155 return;
156
953704c1
RR
157 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
158 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
159
f5d29b39 160 if (val)
953704c1 161 {
f5d29b39 162 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 163 }
f5d29b39 164 else
953704c1
RR
165 {
166 // should give an assert
167 }
168
169 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
170 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b
RR
171}
172
eb082a08 173bool wxRadioButton::GetValue() const
6de97a3b 174{
223d09f6 175 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
f96aa4d9 176
f5d29b39 177 return GTK_TOGGLE_BUTTON(m_widget)->active;
6de97a3b
RR
178}
179
f03fc89f 180bool wxRadioButton::Enable( bool enable )
d3904ceb 181{
f03fc89f
VZ
182 if ( !wxControl::Enable( enable ) )
183 return FALSE;
f96aa4d9 184
f5d29b39 185 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
f03fc89f
VZ
186
187 return TRUE;
d3904ceb
RR
188}
189
58614078 190void wxRadioButton::ApplyWidgetStyle()
868a2826 191{
f5d29b39
RR
192 SetWidgetStyle();
193 gtk_widget_set_style( m_widget, m_widgetStyle );
194 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
f96aa4d9 195}
dcf924a3 196
2f073eb2
RR
197bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
198{
199 return (window == GTK_TOGGLE_BUTTON(m_widget)->event_window);
200}
201
202void wxRadioButton::OnInternalIdle()
203{
204 wxCursor cursor = m_cursor;
205 if (g_globalCursor.Ok()) cursor = g_globalCursor;
206
207 if (GTK_TOGGLE_BUTTON(m_widget)->event_window && cursor.Ok())
208 {
209 /* I now set the cursor the anew in every OnInternalIdle call
210 as setting the cursor in a parent window also effects the
211 windows above so that checking for the current cursor is
212 not possible. */
213
db434467 214 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget)->event_window, cursor.GetCursor() );
2f073eb2
RR
215 }
216
217 UpdateWindowUI();
218}
219
db434467
RR
220wxSize wxRadioButton::DoGetBestSize() const
221{
222 return wxControl::DoGetBestSize();
223}
224
dcf924a3 225#endif