]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/radiobut.cpp
fixed the last of the off-by-one errors (some are refixed, again...)
[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( GtkWidget *WXUNUSED(widget), 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 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
52 event.SetInt( rb->GetValue() );
53 event.SetEventObject( rb );
54 rb->GetEventHandler()->ProcessEvent( event );
55 }
56
57 //-----------------------------------------------------------------------------
58 // wxRadioButton
59 //-----------------------------------------------------------------------------
60
61 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl)
62
63 bool 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 {
67 m_acceptsFocus = TRUE;
68 m_needParent = TRUE;
69 m_isRadioButton = TRUE;
70
71 if (!PreCreation( parent, pos, size ) ||
72 !CreateBase( parent, id, pos, size, style, validator, name ))
73 {
74 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
75 return FALSE;
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 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
115 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
116
117 m_parent->DoAddChild( this );
118
119 PostCreation();
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
132 SetBackgroundColour( parent->GetBackgroundColour() );
133 SetForegroundColour( parent->GetForegroundColour() );
134
135 Show( TRUE );
136
137 return TRUE;
138 }
139
140 void wxRadioButton::SetLabel( const wxString& label )
141 {
142 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
143
144 wxControl::SetLabel( label );
145 GtkButton *bin = GTK_BUTTON( m_widget );
146 GtkLabel *g_label = GTK_LABEL( bin->child );
147 gtk_label_set( g_label, GetLabel().mbc_str() );
148 }
149
150 void wxRadioButton::SetValue( bool val )
151 {
152 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
153
154 if (val == GetValue())
155 return;
156
157 gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget),
158 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
159
160 if (val)
161 {
162 gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_widget), TRUE );
163 }
164 else
165 {
166 // should give an assert
167 // RL - No it shouldn't. A wxGenericValidator might try to set it
168 // as FALSE. Failing silently is probably TRTTD here.
169 }
170
171 gtk_signal_connect( GTK_OBJECT(m_widget), "clicked",
172 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
173 }
174
175 bool wxRadioButton::GetValue() const
176 {
177 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") );
178
179 return GTK_TOGGLE_BUTTON(m_widget)->active;
180 }
181
182 bool wxRadioButton::Enable( bool enable )
183 {
184 if ( !wxControl::Enable( enable ) )
185 return FALSE;
186
187 gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable );
188
189 return TRUE;
190 }
191
192 void wxRadioButton::ApplyWidgetStyle()
193 {
194 SetWidgetStyle();
195 gtk_widget_set_style( m_widget, m_widgetStyle );
196 gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle );
197 }
198
199 bool wxRadioButton::IsOwnGtkWindow( GdkWindow *window )
200 {
201 return (window == GTK_TOGGLE_BUTTON(m_widget)->event_window);
202 }
203
204 void 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
216 gdk_window_set_cursor( GTK_TOGGLE_BUTTON(m_widget)->event_window, cursor.GetCursor() );
217 }
218
219 UpdateWindowUI();
220 }
221
222 wxSize wxRadioButton::DoGetBestSize() const
223 {
224 return wxControl::DoGetBestSize();
225 }
226
227 #endif