]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/radiobut.cpp
extracted 4 copies of identical code into a function
[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
1e6feb95 15#include "wx/defs.h"
dcf924a3
RR
16
17#if wxUSE_RADIOBOX
18
1e6feb95
VZ
19#include "wx/radiobut.h"
20
3f26799e
RR
21#include <gdk/gdk.h>
22#include <gtk/gtk.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
2f073eb2
RR
35extern bool g_blockEventsOnDrag;
36extern wxCursor g_globalCursor;
6de97a3b
RR
37
38//-----------------------------------------------------------------------------
bb4549de 39// "clicked"
6de97a3b
RR
40//-----------------------------------------------------------------------------
41
bb4549de
RR
42static
43void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioButton *rb )
6de97a3b 44{
acfd422a
RR
45 if (g_isIdle) wxapp_install_idle_handler();
46
a2053b27 47 if (!rb->m_hasVMT) return;
bb4549de 48
f5d29b39 49 if (g_blockEventsOnDrag) return;
6de97a3b 50
f5d29b39
RR
51 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
52 event.SetInt( rb->GetValue() );
53 event.SetEventObject( rb );
54 rb->GetEventHandler()->ProcessEvent( event );
6de97a3b
RR
55}
56
bb4549de
RR
57//-----------------------------------------------------------------------------
58// wxRadioButton
59//-----------------------------------------------------------------------------
60
61IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
62
6de97a3b
RR
63bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
64 const wxPoint& pos, const wxSize& size, long style,
65 const wxValidator& validator, const wxString& name )
66{
b292e2f5 67 m_acceptsFocus = TRUE;
f5d29b39 68 m_needParent = TRUE;
953704c1 69 m_isRadioButton = TRUE;
6de97a3b 70
4dcaf11a
RR
71 if (!PreCreation( parent, pos, size ) ||
72 !CreateBase( parent, id, pos, size, style, validator, name ))
73 {
223d09f6 74 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
4dcaf11a
RR
75 return FALSE;
76 }
953704c1
RR
77
78 if (HasFlag(wxRB_GROUP))
79 {
80 /* start a new group */
81 m_radioButtonGroup = (GSList*) NULL;
82 }
83 else
84 {
85 /* search backward for last group start */
86 wxRadioButton *chief = (wxRadioButton*) NULL;
87 wxWindowList::Node *node = parent->GetChildren().GetLast();
88 while (node)
953704c1 89 {
db434467
RR
90 wxWindow *child = node->GetData();
91 if (child->m_isRadioButton)
92 {
93 chief = (wxRadioButton*) child;
94 if (child->HasFlag(wxRB_GROUP)) break;
95 }
96 node = node->GetPrevious();
953704c1 97 }
db434467
RR
98 if (chief)
99 {
0544bc0a 100 /* we are part of the group started by chief */
db434467
RR
101 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
102 }
103 else
104 {
0544bc0a
RR
105 /* start a new group */
106 m_radioButtonGroup = (GSList*) NULL;
db434467 107 }
953704c1
RR
108 }
109
110 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
6de97a3b 111
f5d29b39 112 SetLabel(label);
6de97a3b 113
f5d29b39
RR
114 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
115 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
6de97a3b 116
f03fc89f 117 m_parent->DoAddChild( this );
6ca41e57 118
f5d29b39 119 PostCreation();
6de97a3b 120
db434467
RR
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") );
f6bcfd97 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
f6bcfd97
BP
167 // RL - No it shouldn't. A wxGenericValidator might try to set it
168 // as FALSE. Failing silently is probably TRTTD here.
953704c1 169 }
f6bcfd97 170
953704c1
RR
171 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
172 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
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
f5d29b39 187 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
f03fc89f
VZ
188
189 return TRUE;
d3904ceb
RR
190}
191
58614078 192void wxRadioButton::ApplyWidgetStyle()
868a2826 193{
f5d29b39
RR
194 SetWidgetStyle();
195 gtk_widget_set_style( m_widget, m_widgetStyle );
196 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
f96aa4d9 197}
dcf924a3 198
2f073eb2
RR
199bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
200{
201 return (window == GTK_TOGGLE_BUTTON(m_widget)->event_window);
202}
203
204void wxRadioButton::OnInternalIdle()
205{
206 wxCursor cursor = m_cursor;
207 if (g_globalCursor.Ok()) cursor = g_globalCursor;
208
209 if (GTK_TOGGLE_BUTTON(m_widget)->event_window && cursor.Ok())
210 {
211 /* I now set the cursor the anew in every OnInternalIdle call
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
db434467 216 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget)->event_window, cursor.GetCursor() );
2f073eb2
RR
217 }
218
219 UpdateWindowUI();
220}
221
db434467
RR
222wxSize wxRadioButton::DoGetBestSize() const
223{
224 return wxControl::DoGetBestSize();
225}
226
dcf924a3 227#endif