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