]>
Commit | Line | Data |
---|---|---|
c801d85f KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: radiobox.cpp | |
3 | // Purpose: | |
4 | // Author: Robert Roebling | |
5 | // Created: 01/02/97 | |
6 | // Id: | |
7 | // Copyright: (c) 1998 Robert Roebling, Julian Smart and Markus Holzem | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "radiobox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/radiobox.h" | |
17 | #include "wx/dialog.h" | |
18 | #include "wx/frame.h" | |
19 | #include "wx/gtk/win_gtk.h" | |
20 | ||
66bd6b93 RR |
21 | //----------------------------------------------------------------------------- |
22 | // data | |
23 | //----------------------------------------------------------------------------- | |
24 | ||
25 | extern bool g_blockEventsOnDrag; | |
26 | ||
c801d85f | 27 | //----------------------------------------------------------------------------- |
b4071e91 | 28 | // "clicked" |
c801d85f KB |
29 | //----------------------------------------------------------------------------- |
30 | ||
66bd6b93 | 31 | static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb ) |
c801d85f | 32 | { |
66bd6b93 RR |
33 | if (!rb->HasVMT()) return; |
34 | if (g_blockEventsOnDrag) return; | |
35 | ||
47908e25 RR |
36 | if (rb->m_alreadySent) |
37 | { | |
38 | rb->m_alreadySent = FALSE; | |
39 | return; | |
40 | } | |
41 | ||
42 | rb->m_alreadySent = TRUE; | |
43 | ||
c801d85f KB |
44 | wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() ); |
45 | event.SetInt( rb->GetSelection() ); | |
46 | wxString tmp( rb->GetStringSelection() ); | |
47 | event.SetString( WXSTRINGCAST(tmp) ); | |
48 | event.SetEventObject( rb ); | |
47908e25 | 49 | rb->GetEventHandler()->ProcessEvent(event); |
6de97a3b | 50 | } |
c801d85f | 51 | |
b4071e91 RR |
52 | //----------------------------------------------------------------------------- |
53 | // wxRadioBox | |
c801d85f KB |
54 | //----------------------------------------------------------------------------- |
55 | ||
56 | IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl) | |
57 | ||
3f659fd6 RR |
58 | BEGIN_EVENT_TABLE(wxRadioBox, wxControl) |
59 | EVT_SIZE(wxRadioBox::OnSize) | |
60 | END_EVENT_TABLE() | |
61 | ||
c801d85f KB |
62 | wxRadioBox::wxRadioBox(void) |
63 | { | |
6de97a3b | 64 | } |
c801d85f | 65 | |
debe6624 JS |
66 | bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title, |
67 | const wxPoint &pos, const wxSize &size, | |
6de97a3b RR |
68 | int n, const wxString choices[], int WXUNUSED(majorDim), |
69 | long style, const wxValidator& validator, const wxString &name ) | |
c801d85f | 70 | { |
47908e25 | 71 | m_alreadySent = FALSE; |
c801d85f KB |
72 | m_needParent = TRUE; |
73 | ||
74 | PreCreation( parent, id, pos, size, style, name ); | |
75 | ||
6de97a3b RR |
76 | SetValidator( validator ); |
77 | ||
c801d85f KB |
78 | m_widget = gtk_frame_new( title ); |
79 | ||
80 | int x = m_x+5; | |
81 | int y = m_y+15; | |
82 | int maxLen = 0; | |
83 | int height = 20; | |
84 | ||
85 | // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0)) | |
86 | if (n > 0) | |
87 | { | |
c67daf87 | 88 | GSList *radio_button_group = (GSList *) NULL; |
c801d85f KB |
89 | for (int i = 0; i < n; i++) |
90 | { | |
91 | if (i) radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) ); | |
47908e25 | 92 | |
c801d85f KB |
93 | m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, choices[i] ) ); |
94 | ||
b4071e91 RR |
95 | ConnectWidget( GTK_WIDGET(m_radio) ); |
96 | ||
c801d85f KB |
97 | if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE ); |
98 | ||
99 | gtk_signal_connect( GTK_OBJECT(m_radio), "clicked", | |
100 | GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this ); | |
101 | ||
102 | gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow), GTK_WIDGET(m_radio), x, y ); | |
103 | ||
104 | int tmp = 22+gdk_string_measure( GTK_WIDGET(m_radio)->style->font, choices[i] ); | |
105 | if (tmp > maxLen) maxLen = tmp; | |
106 | ||
107 | int width = m_width-10; | |
108 | if (size.x == -1) width = tmp; | |
109 | gtk_widget_set_usize( GTK_WIDGET(m_radio), width, 20 ); | |
110 | ||
111 | y += 20; | |
112 | height += 20; | |
113 | ||
6de97a3b RR |
114 | } |
115 | } | |
c801d85f KB |
116 | |
117 | wxSize newSize = size; | |
118 | if (newSize.x == -1) newSize.x = maxLen+10; | |
119 | if (newSize.y == -1) newSize.y = height; | |
120 | SetSize( newSize.x, newSize.y ); | |
121 | ||
122 | PostCreation(); | |
123 | ||
124 | Show( TRUE ); | |
125 | ||
126 | return TRUE; | |
6de97a3b | 127 | } |
c801d85f | 128 | |
b4071e91 | 129 | void wxRadioBox::OnSize( wxSizeEvent &event ) |
3f659fd6 | 130 | { |
b4071e91 RR |
131 | wxControl::OnSize( event ); |
132 | ||
3f659fd6 RR |
133 | int x = m_x+5; |
134 | int y = m_y+15; | |
135 | ||
136 | GSList *item = gtk_radio_button_group( m_radio ); | |
137 | while (item) | |
138 | { | |
139 | GtkWidget *button = GTK_WIDGET( item->data ); | |
140 | ||
141 | gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, x, y ); | |
142 | ||
143 | y += 20; | |
144 | ||
145 | item = item->next; | |
146 | } | |
147 | } | |
148 | ||
debe6624 | 149 | bool wxRadioBox::Show( bool show ) |
c801d85f KB |
150 | { |
151 | wxWindow::Show( show ); | |
152 | ||
153 | GSList *item = gtk_radio_button_group( m_radio ); | |
154 | while (item) | |
155 | { | |
156 | GtkWidget *w = GTK_WIDGET( item->data ); | |
157 | if (show) gtk_widget_show( w ); else gtk_widget_hide( w ); | |
158 | item = item->next; | |
6de97a3b | 159 | } |
c801d85f KB |
160 | |
161 | return TRUE; | |
6de97a3b | 162 | } |
c801d85f | 163 | |
47908e25 | 164 | int wxRadioBox::FindString( const wxString &s ) const |
c801d85f | 165 | { |
47908e25 RR |
166 | GSList *item = gtk_radio_button_group( m_radio ); |
167 | ||
168 | int count = g_slist_length(item)-1; | |
169 | ||
170 | while (item) | |
171 | { | |
172 | GtkButton *b = GTK_BUTTON( item->data ); | |
173 | GtkLabel *l = GTK_LABEL( b->child ); | |
174 | if (s == l->label) return count; | |
175 | count--; | |
176 | item = item->next; | |
6de97a3b | 177 | } |
47908e25 RR |
178 | |
179 | return -1; | |
6de97a3b | 180 | } |
c801d85f | 181 | |
47908e25 | 182 | void wxRadioBox::SetSelection( int n ) |
c801d85f | 183 | { |
47908e25 RR |
184 | GSList *item = gtk_radio_button_group( m_radio ); |
185 | item = g_slist_nth( item, g_slist_length(item)-n-1 ); | |
d3904ceb RR |
186 | |
187 | if (!item) | |
188 | { | |
189 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
190 | return; | |
191 | } | |
47908e25 RR |
192 | |
193 | GtkToggleButton *button = GTK_TOGGLE_BUTTON( item->data ); | |
194 | ||
195 | gtk_toggle_button_set_state( button, 1 ); | |
6de97a3b | 196 | } |
c801d85f KB |
197 | |
198 | int wxRadioBox::GetSelection(void) const | |
199 | { | |
200 | GSList *item = gtk_radio_button_group( m_radio ); | |
201 | int count = 0; | |
2b5bd7fd | 202 | int found = -1; |
c801d85f KB |
203 | while (item) |
204 | { | |
205 | GtkButton *button = GTK_BUTTON( item->data ); | |
2b5bd7fd | 206 | if (GTK_TOGGLE_BUTTON(button)->active) found = count; |
c801d85f KB |
207 | count++; |
208 | item = item->next; | |
6de97a3b | 209 | } |
2b5bd7fd KB |
210 | |
211 | return found != -1 ? count-found-1 : -1; | |
6de97a3b | 212 | } |
c801d85f | 213 | |
47908e25 | 214 | wxString wxRadioBox::GetString( int n ) const |
c801d85f | 215 | { |
47908e25 RR |
216 | GSList *item = gtk_radio_button_group( m_radio ); |
217 | ||
218 | item = g_slist_nth( item, g_slist_length(item)-n-1 ); | |
47908e25 | 219 | |
d3904ceb RR |
220 | if (!item) |
221 | { | |
222 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
223 | return ""; | |
224 | } | |
47908e25 | 225 | |
d3904ceb RR |
226 | GtkButton *button = GTK_BUTTON( item->data ); |
227 | GtkLabel *label = GTK_LABEL( button->child ); | |
47908e25 RR |
228 | |
229 | return wxString( label->label ); | |
6de97a3b | 230 | } |
c801d85f | 231 | |
d3904ceb | 232 | wxString wxRadioBox::GetLabel( int item ) const |
c801d85f | 233 | { |
d3904ceb | 234 | return GetString( item ); |
6de97a3b | 235 | } |
c801d85f | 236 | |
d3904ceb | 237 | void wxRadioBox::SetLabel( const wxString& label ) |
c801d85f | 238 | { |
d3904ceb RR |
239 | wxControl::SetLabel( label ); |
240 | GtkFrame *frame = GTK_FRAME( m_widget ); | |
241 | gtk_frame_set_label( frame, wxControl::GetLabel() ); | |
6de97a3b | 242 | } |
c801d85f | 243 | |
d3904ceb | 244 | void wxRadioBox::SetLabel( int item, const wxString& label ) |
c801d85f | 245 | { |
d3904ceb RR |
246 | GSList *list = gtk_radio_button_group( m_radio ); |
247 | list = g_slist_nth( list, g_slist_length(list)-item-1 ); | |
248 | ||
249 | if (!list) | |
250 | { | |
251 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
252 | return; | |
253 | } | |
254 | ||
255 | GtkButton *button = GTK_BUTTON( list->data ); | |
256 | GtkLabel *g_label = GTK_LABEL( button->child ); | |
257 | ||
258 | gtk_label_set( g_label, label ); | |
6de97a3b | 259 | } |
c801d85f | 260 | |
debe6624 | 261 | void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) ) |
c801d85f | 262 | { |
a3622daa | 263 | wxFAIL_MSG("wxRadioBox::SetLabel not implemented."); |
6de97a3b | 264 | } |
c801d85f | 265 | |
d3904ceb | 266 | void wxRadioBox::Enable( bool enable ) |
c801d85f | 267 | { |
d3904ceb RR |
268 | wxControl::Enable( enable ); |
269 | ||
270 | GSList *item = gtk_radio_button_group( m_radio ); | |
271 | while (item) | |
272 | { | |
273 | GtkButton *button = GTK_BUTTON( item->data ); | |
274 | GtkWidget *label = button->child; | |
275 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
276 | gtk_widget_set_sensitive( label, enable ); | |
277 | item = item->next; | |
278 | } | |
6de97a3b | 279 | } |
c801d85f | 280 | |
d3904ceb | 281 | void wxRadioBox::Enable( int item, bool enable ) |
c801d85f | 282 | { |
d3904ceb RR |
283 | GSList *list = gtk_radio_button_group( m_radio ); |
284 | list = g_slist_nth( list, g_slist_length(list)-item-1 ); | |
285 | ||
286 | if (!list) | |
287 | { | |
288 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
289 | return; | |
290 | } | |
291 | ||
292 | GtkButton *button = GTK_BUTTON( list->data ); | |
293 | ||
294 | GtkWidget *label = button->child; | |
295 | gtk_widget_set_sensitive( GTK_WIDGET(button), enable ); | |
296 | gtk_widget_set_sensitive( label, enable ); | |
6de97a3b | 297 | } |
c801d85f | 298 | |
d3904ceb | 299 | void wxRadioBox::Show( int item, bool show ) |
c801d85f | 300 | { |
d3904ceb RR |
301 | GSList *list = gtk_radio_button_group( m_radio ); |
302 | list = g_slist_nth( list, g_slist_length(list)-item-1 ); | |
303 | ||
304 | if (!list) | |
305 | { | |
306 | wxFAIL_MSG( "wxRadioBox wrong index" ); | |
307 | return; | |
308 | } | |
309 | ||
310 | GtkWidget *button = GTK_WIDGET( list->data ); | |
c801d85f | 311 | |
d3904ceb RR |
312 | if (show) |
313 | gtk_widget_show( button ); | |
314 | else | |
315 | gtk_widget_hide( button ); | |
6de97a3b | 316 | } |
c801d85f KB |
317 | |
318 | wxString wxRadioBox::GetStringSelection(void) const | |
319 | { | |
320 | GSList *item = gtk_radio_button_group( m_radio ); | |
321 | while (item) | |
322 | { | |
323 | GtkButton *button = GTK_BUTTON( item->data ); | |
324 | if (GTK_TOGGLE_BUTTON(button)->active) | |
325 | { | |
326 | GtkLabel *label = GTK_LABEL( button->child ); | |
327 | return label->label; | |
6de97a3b | 328 | } |
c801d85f | 329 | item = item->next; |
6de97a3b | 330 | } |
c801d85f | 331 | return ""; |
6de97a3b | 332 | } |
c801d85f | 333 | |
47908e25 | 334 | bool wxRadioBox::SetStringSelection( const wxString&s ) |
c801d85f | 335 | { |
47908e25 RR |
336 | int res = FindString( s ); |
337 | if (res == -1) return FALSE; | |
338 | SetSelection( res ); | |
c801d85f | 339 | return TRUE; |
6de97a3b | 340 | } |
c801d85f KB |
341 | |
342 | int wxRadioBox::Number(void) const | |
343 | { | |
344 | int count = 0; | |
345 | GSList *item = gtk_radio_button_group( m_radio ); | |
346 | while (item) | |
347 | { | |
348 | item = item->next; | |
349 | count++; | |
6de97a3b | 350 | } |
c801d85f | 351 | return count; |
6de97a3b | 352 | } |
c801d85f KB |
353 | |
354 | int wxRadioBox::GetNumberOfRowsOrCols(void) const | |
355 | { | |
356 | return 1; | |
6de97a3b | 357 | } |
c801d85f | 358 | |
debe6624 | 359 | void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) ) |
c801d85f | 360 | { |
a3622daa | 361 | wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented."); |
6de97a3b | 362 | } |
c801d85f | 363 | |
868a2826 RR |
364 | void wxRadioBox::SetFont( const wxFont &font ) |
365 | { | |
366 | wxWindow::SetFont( font ); | |
367 | ||
368 | GSList *item = gtk_radio_button_group( m_radio ); | |
369 | while (item) | |
370 | { | |
371 | GtkButton *button = GTK_BUTTON( item->data ); | |
372 | ||
373 | gtk_widget_set_style( button->child, | |
374 | gtk_style_ref( | |
375 | gtk_widget_get_style( m_widget ) ) ); | |
376 | ||
377 | item = item->next; | |
378 | } | |
379 | } | |
b4071e91 RR |
380 | |
381 | bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window ) | |
382 | { | |
383 | if (window == m_widget->window) return TRUE; | |
384 | ||
385 | GSList *item = gtk_radio_button_group( m_radio ); | |
386 | while (item) | |
387 | { | |
388 | GtkWidget *button = GTK_WIDGET( item->data ); | |
389 | if (window == button->window) return TRUE; | |
390 | item = item->next; | |
391 | } | |
392 | ||
393 | return FALSE; | |
394 | } |