]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: radiobut.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
f96aa4d9 RR |
5 | // Id: $Id$ |
6 | // Copyright: (c) 1998 Robert Roebling | |
e2762ff0 | 7 | // Licence: wxWindows licence |
c801d85f KB |
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 | ||
28 | extern void wxapp_install_idle_handler(); | |
29 | extern bool g_isIdle; | |
30 | ||
6de97a3b RR |
31 | //----------------------------------------------------------------------------- |
32 | // data | |
33 | //----------------------------------------------------------------------------- | |
34 | ||
2f073eb2 RR |
35 | extern bool g_blockEventsOnDrag; |
36 | extern wxCursor g_globalCursor; | |
6de97a3b RR |
37 | |
38 | //----------------------------------------------------------------------------- | |
bb4549de | 39 | // "clicked" |
6de97a3b RR |
40 | //----------------------------------------------------------------------------- |
41 | ||
bb4549de | 42 | static |
e2762ff0 | 43 | void gtk_radiobutton_clicked_callback( GtkToggleButton *button, 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 | |
e2762ff0 RR |
51 | if (!button->active) return; |
52 | ||
f5d29b39 RR |
53 | wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId()); |
54 | event.SetInt( rb->GetValue() ); | |
55 | event.SetEventObject( rb ); | |
56 | rb->GetEventHandler()->ProcessEvent( event ); | |
6de97a3b RR |
57 | } |
58 | ||
bb4549de RR |
59 | //----------------------------------------------------------------------------- |
60 | // wxRadioButton | |
61 | //----------------------------------------------------------------------------- | |
62 | ||
63 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton,wxControl) | |
64 | ||
6de97a3b RR |
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 | { | |
b292e2f5 | 69 | m_acceptsFocus = TRUE; |
f5d29b39 | 70 | m_needParent = TRUE; |
953704c1 | 71 | m_isRadioButton = TRUE; |
6de97a3b | 72 | |
4dcaf11a RR |
73 | if (!PreCreation( parent, pos, size ) || |
74 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
75 | { | |
223d09f6 | 76 | wxFAIL_MSG( wxT("wxRadioButton creation failed") ); |
e2762ff0 | 77 | return FALSE; |
4dcaf11a | 78 | } |
953704c1 RR |
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) | |
e2762ff0 RR |
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(); | |
953704c1 | 99 | } |
e2762ff0 RR |
100 | if (chief) |
101 | { | |
0544bc0a | 102 | /* we are part of the group started by chief */ |
e2762ff0 RR |
103 | m_radioButtonGroup = gtk_radio_button_group( GTK_RADIO_BUTTON(chief->m_widget) ); |
104 | } | |
105 | else | |
106 | { | |
0544bc0a RR |
107 | /* start a new group */ |
108 | m_radioButtonGroup = (GSList*) NULL; | |
e2762ff0 | 109 | } |
953704c1 RR |
110 | } |
111 | ||
112 | m_widget = gtk_radio_button_new_with_label( m_radioButtonGroup, label.mbc_str() ); | |
6de97a3b | 113 | |
f5d29b39 | 114 | SetLabel(label); |
6de97a3b | 115 | |
f5d29b39 RR |
116 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", |
117 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
6de97a3b | 118 | |
f03fc89f | 119 | m_parent->DoAddChild( this ); |
6ca41e57 | 120 | |
f5d29b39 | 121 | PostCreation(); |
6de97a3b | 122 | |
db434467 RR |
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 | ||
f5d29b39 RR |
134 | SetBackgroundColour( parent->GetBackgroundColour() ); |
135 | SetForegroundColour( parent->GetForegroundColour() ); | |
58614078 | 136 | |
f5d29b39 | 137 | Show( TRUE ); |
6de97a3b | 138 | |
f5d29b39 | 139 | return TRUE; |
6de97a3b RR |
140 | } |
141 | ||
142 | void wxRadioButton::SetLabel( const wxString& label ) | |
143 | { | |
223d09f6 | 144 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
f96aa4d9 | 145 | |
f5d29b39 RR |
146 | wxControl::SetLabel( label ); |
147 | GtkButton *bin = GTK_BUTTON( m_widget ); | |
148 | GtkLabel *g_label = GTK_LABEL( bin->child ); | |
b019151f | 149 | gtk_label_set( g_label, GetLabel().mbc_str() ); |
6de97a3b RR |
150 | } |
151 | ||
152 | void wxRadioButton::SetValue( bool val ) | |
153 | { | |
223d09f6 | 154 | wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") ); |
f6bcfd97 | 155 | |
953704c1 | 156 | if (val == GetValue()) |
0659e7ee RR |
157 | return; |
158 | ||
953704c1 RR |
159 | gtk_signal_disconnect_by_func( GTK_OBJECT(m_widget), |
160 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
161 | ||
f5d29b39 | 162 | if (val) |
953704c1 | 163 | { |
e2762ff0 | 164 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE ); |
953704c1 | 165 | } |
f5d29b39 | 166 | else |
953704c1 RR |
167 | { |
168 | // should give an assert | |
f6bcfd97 BP |
169 | // RL - No it shouldn't. A wxGenericValidator might try to set it |
170 | // as FALSE. Failing silently is probably TRTTD here. | |
953704c1 | 171 | } |
f6bcfd97 | 172 | |
953704c1 RR |
173 | gtk_signal_connect( GTK_OBJECT(m_widget), "clicked", |
174 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
6de97a3b RR |
175 | } |
176 | ||
eb082a08 | 177 | bool wxRadioButton::GetValue() const |
6de97a3b | 178 | { |
223d09f6 | 179 | wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobutton") ); |
f96aa4d9 | 180 | |
f5d29b39 | 181 | return GTK_TOGGLE_BUTTON(m_widget)->active; |
6de97a3b RR |
182 | } |
183 | ||
f03fc89f | 184 | bool wxRadioButton::Enable( bool enable ) |
d3904ceb | 185 | { |
f03fc89f VZ |
186 | if ( !wxControl::Enable( enable ) ) |
187 | return FALSE; | |
f96aa4d9 | 188 | |
f5d29b39 | 189 | gtk_widget_set_sensitive( GTK_BUTTON(m_widget)->child, enable ); |
f03fc89f VZ |
190 | |
191 | return TRUE; | |
d3904ceb RR |
192 | } |
193 | ||
58614078 | 194 | void wxRadioButton::ApplyWidgetStyle() |
868a2826 | 195 | { |
f5d29b39 RR |
196 | SetWidgetStyle(); |
197 | gtk_widget_set_style( m_widget, m_widgetStyle ); | |
198 | gtk_widget_set_style( GTK_BUTTON(m_widget)->child, m_widgetStyle ); | |
f96aa4d9 | 199 | } |
dcf924a3 | 200 | |
2f073eb2 RR |
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 | |
e2762ff0 RR |
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() ); | |
2f073eb2 RR |
219 | } |
220 | ||
221 | UpdateWindowUI(); | |
222 | } | |
223 | ||
db434467 RR |
224 | wxSize wxRadioButton::DoGetBestSize() const |
225 | { | |
226 | return wxControl::DoGetBestSize(); | |
227 | } | |
228 | ||
dcf924a3 | 229 | #endif |