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