Distrib things
[wxWidgets.git] / src / gtk / radiobut.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobut.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "radiobut.h"
13 #endif
14
15 #include "wx/radiobut.h"
16
17 #if wxUSE_RADIOBOX
18
19 #include "gdk/gdk.h"
20 #include "gtk/gtk.h"
21
22 //-----------------------------------------------------------------------------
23 // idle system
24 //-----------------------------------------------------------------------------
25
26 extern void wxapp_install_idle_handler();
27 extern bool g_isIdle;
28
29 //-----------------------------------------------------------------------------
30 // data
31 //-----------------------------------------------------------------------------
32
33 extern bool g_blockEventsOnDrag;
34
35 //-----------------------------------------------------------------------------
36 // "clicked"
37 //-----------------------------------------------------------------------------
38
39 static
40 void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioButton *rb )
41 {
42 if (g_isIdle) wxapp_install_idle_handler();
43
44 if (!rb->m_hasVMT) return;
45
46 if (g_blockEventsOnDrag) return;
47
48 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
49 event.SetInt( rb->GetValue() );
50 event.SetEventObject( rb );
51 rb->GetEventHandler()->ProcessEvent( event );
52 }
53
54 //-----------------------------------------------------------------------------
55 // wxRadioButton
56 //-----------------------------------------------------------------------------
57
58 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
59
60 bool wxRadioButton::Create( wxWindow *parent, wxWindowID id, const wxString& label,
61 const wxPoint& pos, const wxSize& size, long style,
62 const wxValidator& validator, const wxString& name )
63 {
64 m_acceptsFocus = TRUE;
65 m_needParent = TRUE;
66
67 wxSize newSize = size;
68
69 PreCreation( parent, id, pos, newSize, style, name );
70
71 m_isRadioButton = TRUE;
72
73 #if wxUSE_VALIDATORS
74 SetValidator( validator );
75 #endif
76
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)
89 {
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();
97 }
98 if (chief)
99 {
100 /* we are part of the group started by chief */
101 m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) );
102 }
103 else
104 {
105 /* start a new group */
106 m_radioButtonGroup = (GSList*) NULL;
107 }
108 }
109
110 m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() );
111
112 SetLabel(label);
113
114 if (newSize.x == -1) newSize.x = 22+gdk_string_measure( m_widget->style->font, label.mbc_str() );
115 if (newSize.y == -1) newSize.y = 26;
116 SetSize( newSize.x, newSize.y );
117
118 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
119 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
120
121 m_parent->DoAddChild( this );
122
123 PostCreation();
124
125 SetBackgroundColour( parent->GetBackgroundColour() );
126 SetForegroundColour( parent->GetForegroundColour() );
127 SetFont( parent->GetFont() );
128
129 Show( TRUE );
130
131 return TRUE;
132 }
133
134 void wxRadioButton::SetLabel( const wxString& label )
135 {
136 wxCHECK_RET( m_widget != NULL, _T("invalid radiobutton") );
137
138 wxControl::SetLabel( label );
139 GtkButton *bin = GTK_BUTTON( m_widget );
140 GtkLabel *g_label = GTK_LABEL( bin->child );
141 gtk_label_set( g_label, GetLabel().mbc_str() );
142 }
143
144 void wxRadioButton::SetValue( bool val )
145 {
146 wxCHECK_RET( m_widget != NULL, _T("invalid radiobutton") );
147
148 if (val == GetValue())
149 return;
150
151 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
152 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
153
154 if (val)
155 {
156 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), TRUE );
157 }
158 else
159 {
160 // should give an assert
161 }
162
163 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
164 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
165 }
166
167 bool wxRadioButton::GetValue() const
168 {
169 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobutton") );
170
171 return GTK_TOGGLE_BUTTON(m_widget)->active;
172 }
173
174 bool wxRadioButton::Enable( bool enable )
175 {
176 if ( !wxControl::Enable( enable ) )
177 return FALSE;
178
179 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
180
181 return TRUE;
182 }
183
184 void wxRadioButton::ApplyWidgetStyle()
185 {
186 SetWidgetStyle();
187 gtk_widget_set_style( m_widget, m_widgetStyle );
188 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
189 }
190
191 #endif