]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
b85671aaa8c3e57585e758178a9b241b07fb3d4d
[wxWidgets.git] / src / gtk / radiobox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "radiobox.h"
12 #endif
13
14 #include "wx/radiobox.h"
15
16 #if wxUSE_RADIOBOX
17
18 #include "wx/dialog.h"
19 #include "wx/frame.h"
20
21 #include "gdk/gdk.h"
22 #include "gtk/gtk.h"
23 #include "wx/gtk/win_gtk.h"
24
25 //-----------------------------------------------------------------------------
26 // idle system
27 //-----------------------------------------------------------------------------
28
29 extern void wxapp_install_idle_handler();
30 extern bool g_isIdle;
31
32 //-----------------------------------------------------------------------------
33 // data
34 //-----------------------------------------------------------------------------
35
36 extern bool g_blockEventsOnDrag;
37
38 //-----------------------------------------------------------------------------
39 // "clicked"
40 //-----------------------------------------------------------------------------
41
42 static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), wxRadioBox *rb )
43 {
44 if (g_isIdle) wxapp_install_idle_handler();
45
46 if (!rb->m_hasVMT) return;
47 if (g_blockEventsOnDrag) return;
48
49 if (rb->m_alreadySent)
50 {
51 rb->m_alreadySent = FALSE;
52 return;
53 }
54
55 rb->m_alreadySent = TRUE;
56
57 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
58 event.SetInt( rb->GetSelection() );
59 event.SetString( rb->GetStringSelection() );
60 event.SetEventObject( rb );
61 rb->GetEventHandler()->ProcessEvent(event);
62 }
63
64 //-----------------------------------------------------------------------------
65 // wxRadioBox
66 //-----------------------------------------------------------------------------
67
68 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
69
70 BEGIN_EVENT_TABLE(wxRadioBox, wxControl)
71 EVT_SIZE(wxRadioBox::OnSize)
72 END_EVENT_TABLE()
73
74 wxRadioBox::wxRadioBox()
75 {
76 }
77
78 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
79 const wxPoint &pos, const wxSize &size,
80 int n, const wxString choices[], int majorDim,
81 long style, const wxValidator& validator,
82 const wxString &name )
83 {
84 m_alreadySent = FALSE;
85 m_needParent = TRUE;
86 m_acceptsFocus = TRUE;
87
88 PreCreation( parent, id, pos, size, style, name );
89
90 #if wxUSE_VALIDATORS
91 SetValidator( validator );
92 #endif
93
94 m_widget = gtk_frame_new( title.mbc_str() );
95
96 m_majorDim = majorDim;
97
98 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
99
100 wxString label;
101 GSList *radio_button_group = (GSList *) NULL;
102 for (int i = 0; i < n; i++)
103 {
104 if ( i != 0 )
105 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
106
107 label.Empty();
108 for ( const wxChar *pc = choices[i]; *pc; pc++ )
109 {
110 if ( *pc != _T('&') )
111 label += *pc;
112 }
113
114 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, label.mbc_str() ) );
115
116 m_boxes.Append( (wxObject*) m_radio );
117
118 ConnectWidget( GTK_WIDGET(m_radio) );
119
120 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
121
122 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
123 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
124
125 gtk_myfixed_put( GTK_MYFIXED(m_parent->m_wxwindow),
126 GTK_WIDGET(m_radio),
127 m_x+10, m_y+10+(i*24), 10, 10 );
128 }
129
130 wxSize ls = LayoutItems();
131
132 wxSize newSize = size;
133 if (newSize.x == -1) newSize.x = ls.x;
134 if (newSize.y == -1) newSize.y = ls.y;
135 SetSize( newSize.x, newSize.y );
136
137 m_parent->DoAddChild( this );
138
139 PostCreation();
140
141 SetLabel( title );
142
143 SetBackgroundColour( parent->GetBackgroundColour() );
144 SetForegroundColour( parent->GetForegroundColour() );
145 SetFont( parent->GetFont() );
146
147 Show( TRUE );
148
149 return TRUE;
150 }
151
152 wxRadioBox::~wxRadioBox()
153 {
154 wxNode *node = m_boxes.First();
155 while (node)
156 {
157 GtkWidget *button = GTK_WIDGET( node->Data() );
158 gtk_widget_destroy( button );
159 node = node->Next();
160 }
161 }
162
163 void wxRadioBox::OnSize( wxSizeEvent &event )
164 {
165 LayoutItems();
166
167 event.Skip();
168 }
169
170 wxSize wxRadioBox::LayoutItems()
171 {
172 int x = 7;
173 int y = 15;
174
175 if ( m_majorDim == 0 )
176 {
177 // avoid dividing by 0 below
178 wxFAIL_MSG( _T("dimension of radiobox should not be 0!") );
179
180 m_majorDim = 1;
181 }
182
183 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
184
185 wxSize res( 0, 0 );
186
187 if (m_windowStyle & wxRA_HORIZONTAL)
188 {
189
190 for (int j = 0; j < m_majorDim; j++)
191 {
192 y = 15;
193
194 int max_len = 0;
195 wxNode *node = m_boxes.Nth( j*num_per_major );
196 for (int i1 = 0; i1< num_per_major; i1++)
197 {
198 GtkWidget *button = GTK_WIDGET( node->Data() );
199 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
200 GdkFont *font = m_widget->style->font;
201 int len = 22+gdk_string_measure( font, label->label );
202 if (len > max_len) max_len = len;
203
204 gtk_myfixed_move( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y );
205 y += 22;
206
207 node = node->Next();
208 if (!node) break;
209 }
210
211 // we don't know the max_len before
212
213 node = m_boxes.Nth( j*num_per_major );
214 for (int i2 = 0; i2< num_per_major; i2++)
215 {
216 GtkWidget *button = GTK_WIDGET( node->Data() );
217
218 gtk_myfixed_resize( GTK_MYFIXED(m_parent->m_wxwindow), button, max_len, 20 );
219
220 node = node->Next();
221 if (!node) break;
222 }
223
224 if (y > res.y) res.y = y;
225
226 x += max_len + 2;
227 }
228
229 res.x = x+4;
230 res.y += 9;
231 }
232 else
233 {
234 int max = 0;
235
236 wxNode *node = m_boxes.First();
237 while (node)
238 {
239 GtkButton *button = GTK_BUTTON( node->Data() );
240 GtkLabel *label = GTK_LABEL( button->child );
241
242 GdkFont *font = m_widget->style->font;
243 int len = 22+gdk_string_measure( font, label->label );
244 if (len > max) max = len;
245
246 node = node->Next();
247 }
248
249 node = m_boxes.First();
250 while (node)
251 {
252 GtkWidget *button = GTK_WIDGET( node->Data() );
253
254 gtk_myfixed_set_size( GTK_MYFIXED(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
255 x += max;
256
257 node = node->Next();
258 }
259 res.x = x+4;
260 res.y = 42;
261 }
262
263 return res;
264 }
265
266 bool wxRadioBox::Show( bool show )
267 {
268 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
269
270 wxWindow::Show( show );
271
272 if ((m_windowStyle & wxNO_BORDER) != 0)
273 gtk_widget_hide( m_widget );
274
275 wxNode *node = m_boxes.First();
276 while (node)
277 {
278 GtkWidget *button = GTK_WIDGET( node->Data() );
279
280 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
281
282 node = node->Next();
283 }
284
285 return TRUE;
286 }
287
288 int wxRadioBox::FindString( const wxString &s ) const
289 {
290 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
291
292 int count = 0;
293
294 wxNode *node = m_boxes.First();
295 while (node)
296 {
297 GtkButton *button = GTK_BUTTON( node->Data() );
298
299 GtkLabel *label = GTK_LABEL( button->child );
300 if (s == label->label) return count;
301 count++;
302
303 node = node->Next();
304 }
305
306 return -1;
307 }
308
309 void wxRadioBox::SetFocus()
310 {
311 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
312
313 if (m_boxes.GetCount() == 0) return;
314
315 wxNode *node = m_boxes.First();
316 while (node)
317 {
318 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
319 if (button->active)
320 {
321 gtk_widget_grab_focus( GTK_WIDGET(button) );
322
323 return;
324 }
325 node = node->Next();
326 }
327
328 }
329
330 void wxRadioBox::SetSelection( int n )
331 {
332 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
333
334 wxNode *node = m_boxes.Nth( n );
335
336 wxCHECK_RET( node, _T("radiobox wrong index") );
337
338 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
339
340 DisableEvents();
341
342 gtk_toggle_button_set_state( button, 1 );
343
344 EnableEvents();
345 }
346
347 int wxRadioBox::GetSelection(void) const
348 {
349 wxCHECK_MSG( m_widget != NULL, -1, _T("invalid radiobox") );
350
351 int count = 0;
352
353 wxNode *node = m_boxes.First();
354 while (node)
355 {
356 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
357 if (button->active) return count;
358 count++;
359 node = node->Next();
360 }
361
362 wxFAIL_MSG( _T("wxRadioBox none selected") );
363
364 return -1;
365 }
366
367 wxString wxRadioBox::GetString( int n ) const
368 {
369 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
370
371 wxNode *node = m_boxes.Nth( n );
372
373 wxCHECK_MSG( node, _T(""), _T("radiobox wrong index") );
374
375 GtkButton *button = GTK_BUTTON( node->Data() );
376 GtkLabel *label = GTK_LABEL( button->child );
377
378 return wxString( label->label );
379 }
380
381 wxString wxRadioBox::GetLabel( int item ) const
382 {
383 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
384
385 return GetString( item );
386 }
387
388 void wxRadioBox::SetLabel( const wxString& label )
389 {
390 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
391
392 wxControl::SetLabel( label );
393
394 gtk_frame_set_label( GTK_FRAME(m_widget), wxControl::GetLabel().mbc_str() );
395 }
396
397 void wxRadioBox::SetLabel( int item, const wxString& label )
398 {
399 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
400
401 wxNode *node = m_boxes.Nth( item );
402
403 wxCHECK_RET( node, _T("radiobox wrong index") );
404
405 GtkButton *button = GTK_BUTTON( node->Data() );
406 GtkLabel *g_label = GTK_LABEL( button->child );
407
408 gtk_label_set( g_label, label.mbc_str() );
409 }
410
411 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
412 {
413 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
414 }
415
416 bool wxRadioBox::Enable( bool enable )
417 {
418 if ( !wxControl::Enable( enable ) )
419 return FALSE;
420
421 wxNode *node = m_boxes.First();
422 while (node)
423 {
424 GtkButton *button = GTK_BUTTON( node->Data() );
425 GtkWidget *label = button->child;
426 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
427 gtk_widget_set_sensitive( label, enable );
428 node = node->Next();
429 }
430
431 return TRUE;
432 }
433
434 void wxRadioBox::Enable( int item, bool enable )
435 {
436 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
437
438 wxNode *node = m_boxes.Nth( item );
439
440 wxCHECK_RET( node, _T("radiobox wrong index") );
441
442 GtkButton *button = GTK_BUTTON( node->Data() );
443 GtkWidget *label = button->child;
444 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
445 gtk_widget_set_sensitive( label, enable );
446 }
447
448 void wxRadioBox::Show( int item, bool show )
449 {
450 wxCHECK_RET( m_widget != NULL, _T("invalid radiobox") );
451
452 wxNode *node = m_boxes.Nth( item );
453
454 wxCHECK_RET( node, _T("radiobox wrong index") );
455
456 GtkWidget *button = GTK_WIDGET( node->Data() );
457
458 if (show)
459 gtk_widget_show( button );
460 else
461 gtk_widget_hide( button );
462 }
463
464 wxString wxRadioBox::GetStringSelection(void) const
465 {
466 wxCHECK_MSG( m_widget != NULL, _T(""), _T("invalid radiobox") );
467
468 wxNode *node = m_boxes.First();
469 while (node)
470 {
471 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
472 if (button->active)
473 {
474 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
475 return label->label;
476 }
477 node = node->Next();
478 }
479
480 wxFAIL_MSG( _T("wxRadioBox none selected") );
481 return _T("");
482 }
483
484 bool wxRadioBox::SetStringSelection( const wxString &s )
485 {
486 wxCHECK_MSG( m_widget != NULL, FALSE, _T("invalid radiobox") );
487
488 int res = FindString( s );
489 if (res == -1) return FALSE;
490 SetSelection( res );
491
492 return TRUE;
493 }
494
495 int wxRadioBox::Number(void) const
496 {
497 return m_boxes.Number();
498 }
499
500 int wxRadioBox::GetNumberOfRowsOrCols(void) const
501 {
502 return 1;
503 }
504
505 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
506 {
507 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
508 }
509
510 void wxRadioBox::DisableEvents()
511 {
512 wxNode *node = m_boxes.First();
513 while (node)
514 {
515 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
516 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
517
518 node = node->Next();
519 }
520 }
521
522 void wxRadioBox::EnableEvents()
523 {
524 wxNode *node = m_boxes.First();
525 while (node)
526 {
527 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
528 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
529
530 node = node->Next();
531 }
532 }
533
534 void wxRadioBox::ApplyWidgetStyle()
535 {
536 SetWidgetStyle();
537
538 gtk_widget_set_style( m_widget, m_widgetStyle );
539
540 wxNode *node = m_boxes.First();
541 while (node)
542 {
543 GtkWidget *widget = GTK_WIDGET( node->Data() );
544 gtk_widget_set_style( widget, m_widgetStyle );
545
546 GtkButton *button = GTK_BUTTON( node->Data() );
547 gtk_widget_set_style( button->child, m_widgetStyle );
548
549 node = node->Next();
550 }
551 }
552
553 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
554 {
555 if (window == m_widget->window) return TRUE;
556
557 wxNode *node = m_boxes.First();
558 while (node)
559 {
560 GtkWidget *button = GTK_WIDGET( node->Data() );
561
562 if (window == button->window) return TRUE;
563
564 node = node->Next();
565 }
566
567 return FALSE;
568 }
569
570 #endif