Implemented wxRadioBox using a GtkTable.
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "radiobox.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #if wxUSE_RADIOBOX
18
19 #include "wx/radiobox.h"
20
21 #include "wx/dialog.h"
22 #include "wx/frame.h"
23 #include "wx/log.h"
24
25 #include "wx/gtk/private.h"
26 #include <gdk/gdkkeysyms.h>
27
28 #include "wx/gtk/win_gtk.h"
29
30 //-----------------------------------------------------------------------------
31 // idle system
32 //-----------------------------------------------------------------------------
33
34 extern void wxapp_install_idle_handler();
35 extern bool g_isIdle;
36
37 //-----------------------------------------------------------------------------
38 // data
39 //-----------------------------------------------------------------------------
40
41 extern bool g_blockEventsOnDrag;
42 extern wxWindowGTK *g_delayedFocus;
43
44 //-----------------------------------------------------------------------------
45 // "clicked"
46 //-----------------------------------------------------------------------------
47
48 static void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioBox *rb )
49 {
50 if (g_isIdle) wxapp_install_idle_handler();
51
52 if (!rb->m_hasVMT) return;
53 if (g_blockEventsOnDrag) return;
54
55 if (!button->active) return;
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 // "key_press_event"
66 //-----------------------------------------------------------------------------
67
68 static gint gtk_radiobox_keypress_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxRadioBox *rb )
69 {
70 if (g_isIdle)
71 wxapp_install_idle_handler();
72
73 if (!rb->m_hasVMT) return FALSE;
74 if (g_blockEventsOnDrag) return FALSE;
75
76 if ((gdk_event->keyval != GDK_Up) &&
77 (gdk_event->keyval != GDK_Down) &&
78 (gdk_event->keyval != GDK_Left) &&
79 (gdk_event->keyval != GDK_Right))
80 {
81 return FALSE;
82 }
83
84 wxList::compatibility_iterator node = rb->m_boxes.Find( (wxObject*) widget );
85 if (!node)
86 {
87 return FALSE;
88 }
89
90 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
91
92 if ((gdk_event->keyval == GDK_Up) ||
93 (gdk_event->keyval == GDK_Left))
94 {
95 if (node == rb->m_boxes.GetFirst())
96 node = rb->m_boxes.GetLast();
97 else
98 node = node->GetPrevious();
99 }
100 else
101 {
102 if (node == rb->m_boxes.GetLast())
103 node = rb->m_boxes.GetFirst();
104 else
105 node = node->GetNext();
106 }
107
108 GtkWidget *button = (GtkWidget*) node->GetData();
109
110 gtk_widget_grab_focus( button );
111
112 return TRUE;
113 }
114
115 static gint gtk_radiobutton_focus_in( GtkWidget *widget,
116 GdkEvent *WXUNUSED(event),
117 wxRadioBox *win )
118 {
119 if ( win->m_lostFocus )
120 {
121 // no, we didn't really lose it
122 win->m_lostFocus = FALSE;
123 }
124 else if ( !win->m_hasFocus )
125 {
126 win->m_hasFocus = true;
127
128 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
129 event.SetEventObject( win );
130
131 // never stop the signal emission, it seems to break the kbd handling
132 // inside the radiobox
133 (void)win->GetEventHandler()->ProcessEvent( event );
134 }
135
136 return FALSE;
137 }
138
139 static gint gtk_radiobutton_focus_out( GtkWidget *widget,
140 GdkEvent *WXUNUSED(event),
141 wxRadioBox *win )
142 {
143 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
144 // Replace with a warning, else we dump core a lot!
145 // if (!win->m_hasFocus)
146 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
147
148 // we might have lost the focus, but may be not - it may have just gone to
149 // another button in the same radiobox, so we'll check for it in the next
150 // idle iteration (leave m_hasFocus == true for now)
151 win->m_lostFocus = true;
152
153 return FALSE;
154 }
155
156 //-----------------------------------------------------------------------------
157 // wxRadioBox
158 //-----------------------------------------------------------------------------
159
160 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
161
162 void wxRadioBox::Init()
163 {
164 m_needParent = true;
165 m_acceptsFocus = true;
166
167 m_hasFocus =
168 m_lostFocus = false;
169 }
170
171 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id,
172 const wxString& title,
173 const wxPoint &pos, const wxSize &size,
174 const wxArrayString& choices, int majorDim,
175 long style, const wxValidator& validator,
176 const wxString &name )
177 {
178 wxCArrayString chs(choices);
179
180 return Create( parent, id, title, pos, size, chs.GetCount(),
181 chs.GetStrings(), majorDim, style, validator, name );
182 }
183
184 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
185 const wxPoint &pos, const wxSize &size,
186 int n, const wxString choices[], int majorDim,
187 long style, const wxValidator& validator,
188 const wxString &name )
189 {
190 if (!PreCreation( parent, pos, size ) ||
191 !CreateBase( parent, id, pos, size, style, validator, name ))
192 {
193 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
194 return false;
195 }
196
197 m_widget = gtk_frame_new( wxGTK_CONV( title ) );
198
199 // majorDim may be 0 if all trailing parameters were omitted, so don't
200 // assert here but just use the correct value for it
201 m_majorDim = majorDim == 0 ? n : majorDim;
202
203 int num_per_major = (n - 1) / m_majorDim +1;
204
205 int num_of_cols = 0;
206 int num_of_rows = 0;
207 if (HasFlag(wxRA_SPECIFY_COLS))
208 {
209 num_of_cols = m_majorDim;
210 num_of_rows = num_per_major;
211 }
212 else
213 {
214 num_of_cols = num_per_major;
215 num_of_rows = m_majorDim;
216 }
217
218 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
219
220 GtkWidget *table = gtk_table_new( num_of_rows, num_of_cols, FALSE );
221 gtk_table_set_col_spacings( GTK_TABLE(table), 2 );
222 gtk_table_set_row_spacings( GTK_TABLE(table), 2 );
223 gtk_widget_show( table );
224 gtk_container_add( GTK_CONTAINER(m_widget), table );
225
226 wxString label;
227 GSList *radio_button_group = (GSList *) NULL;
228 for (int i = 0; i < n; i++)
229 {
230 if ( i != 0 )
231 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
232
233 label.Empty();
234 for ( const wxChar *pc = choices[i]; *pc; pc++ )
235 {
236 if ( *pc != wxT('&') )
237 label += *pc;
238 }
239
240 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, wxGTK_CONV( label ) ) );
241 gtk_widget_show( GTK_WIDGET(m_radio) );
242
243 gtk_signal_connect( GTK_OBJECT(m_radio), "key_press_event",
244 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback), (gpointer)this );
245
246 m_boxes.Append( (wxObject*) m_radio );
247
248 if (HasFlag(wxRA_SPECIFY_COLS))
249 {
250 int left = i%num_of_cols;
251 int right = (i%num_of_cols) + 1;
252 int top = i/num_of_cols;
253 int bottom = (i/num_of_cols)+1;
254 gtk_table_attach_defaults( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom );
255 }
256 else
257 {
258 int left = i/num_of_rows;
259 int right = (i/num_of_rows) + 1;
260 int top = i%num_of_rows;
261 int bottom = (i%num_of_rows)+1;
262 gtk_table_attach_defaults( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom );
263 }
264
265 ConnectWidget( GTK_WIDGET(m_radio) );
266
267 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
268
269 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
270 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
271
272 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_in_event",
273 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in), (gpointer)this );
274
275 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_out_event",
276 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out), (gpointer)this );
277 }
278
279 m_parent->DoAddChild( this );
280
281 SetLabel( title );
282
283 PostCreation(size);
284
285 return true;
286 }
287
288 wxRadioBox::~wxRadioBox()
289 {
290 wxList::compatibility_iterator node = m_boxes.GetFirst();
291 while (node)
292 {
293 GtkWidget *button = GTK_WIDGET( node->GetData() );
294 gtk_widget_destroy( button );
295 node = node->GetNext();
296 }
297 }
298
299 bool wxRadioBox::Show( bool show )
300 {
301 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
302
303 if (!wxControl::Show(show))
304 {
305 // nothing to do
306 return false;
307 }
308
309 if ( HasFlag(wxNO_BORDER) )
310 gtk_widget_hide( m_widget );
311
312 wxList::compatibility_iterator node = m_boxes.GetFirst();
313 while (node)
314 {
315 GtkWidget *button = GTK_WIDGET( node->GetData() );
316
317 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
318
319 node = node->GetNext();
320 }
321
322 return true;
323 }
324
325 int wxRadioBox::FindString( const wxString &find ) const
326 {
327 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
328
329 int count = 0;
330
331 wxList::compatibility_iterator node = m_boxes.GetFirst();
332 while (node)
333 {
334 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
335 #ifdef __WXGTK20__
336 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
337 #else
338 wxString str( label->label );
339 #endif
340 if (find == str)
341 return count;
342
343 count++;
344
345 node = node->GetNext();
346 }
347
348 return wxNOT_FOUND;
349 }
350
351 void wxRadioBox::SetFocus()
352 {
353 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
354
355 if (m_boxes.GetCount() == 0) return;
356
357 wxList::compatibility_iterator node = m_boxes.GetFirst();
358 while (node)
359 {
360 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
361 if (button->active)
362 {
363 gtk_widget_grab_focus( GTK_WIDGET(button) );
364 return;
365 }
366 node = node->GetNext();
367 }
368 }
369
370 void wxRadioBox::SetSelection( int n )
371 {
372 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
373
374 wxList::compatibility_iterator node = m_boxes.Item( n );
375
376 wxCHECK_RET( node, wxT("radiobox wrong index") );
377
378 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
379
380 GtkDisableEvents();
381
382 gtk_toggle_button_set_active( button, 1 );
383
384 GtkEnableEvents();
385 }
386
387 int wxRadioBox::GetSelection(void) const
388 {
389 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
390
391 int count = 0;
392
393 wxList::compatibility_iterator node = m_boxes.GetFirst();
394 while (node)
395 {
396 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
397 if (button->active) return count;
398 count++;
399 node = node->GetNext();
400 }
401
402 wxFAIL_MSG( wxT("wxRadioBox none selected") );
403
404 return wxNOT_FOUND;
405 }
406
407 wxString wxRadioBox::GetString( int n ) const
408 {
409 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
410
411 wxList::compatibility_iterator node = m_boxes.Item( n );
412
413 wxCHECK_MSG( node, wxEmptyString, wxT("radiobox wrong index") );
414
415 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
416
417 #ifdef __WXGTK20__
418 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
419 #else
420 wxString str( label->label );
421 #endif
422
423 return str;
424 }
425
426 void wxRadioBox::SetLabel( const wxString& label )
427 {
428 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
429
430 wxControl::SetLabel( label );
431
432 gtk_frame_set_label( GTK_FRAME(m_widget), wxGTK_CONV( wxControl::GetLabel() ) );
433 }
434
435 void wxRadioBox::SetString( int item, const wxString& label )
436 {
437 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
438
439 wxList::compatibility_iterator node = m_boxes.Item( item );
440
441 wxCHECK_RET( node, wxT("radiobox wrong index") );
442
443 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
444
445 gtk_label_set( g_label, wxGTK_CONV( label ) );
446 }
447
448 bool wxRadioBox::Enable( bool enable )
449 {
450 if ( !wxControl::Enable( enable ) )
451 return false;
452
453 wxList::compatibility_iterator node = m_boxes.GetFirst();
454 while (node)
455 {
456 GtkButton *button = GTK_BUTTON( node->GetData() );
457 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
458
459 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
460 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
461 node = node->GetNext();
462 }
463
464 return true;
465 }
466
467 bool wxRadioBox::Enable( int item, bool enable )
468 {
469 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
470
471 wxList::compatibility_iterator node = m_boxes.Item( item );
472
473 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
474
475 GtkButton *button = GTK_BUTTON( node->GetData() );
476 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
477
478 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
479 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
480
481 return true;
482 }
483
484 bool wxRadioBox::Show( int item, bool show )
485 {
486 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
487
488 wxList::compatibility_iterator node = m_boxes.Item( item );
489
490 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
491
492 GtkWidget *button = GTK_WIDGET( node->GetData() );
493
494 if (show)
495 gtk_widget_show( button );
496 else
497 gtk_widget_hide( button );
498
499 return true;
500 }
501
502 wxString wxRadioBox::GetStringSelection() const
503 {
504 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
505
506 wxList::compatibility_iterator node = m_boxes.GetFirst();
507 while (node)
508 {
509 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
510 if (button->active)
511 {
512 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
513
514 #ifdef __WXGTK20__
515 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
516 #else
517 wxString str( label->label );
518 #endif
519 return str;
520 }
521 node = node->GetNext();
522 }
523
524 wxFAIL_MSG( wxT("wxRadioBox none selected") );
525 return wxEmptyString;
526 }
527
528 bool wxRadioBox::SetStringSelection( const wxString &s )
529 {
530 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
531
532 int res = FindString( s );
533 if (res == wxNOT_FOUND) return false;
534 SetSelection( res );
535
536 return true;
537 }
538
539 int wxRadioBox::GetCount() const
540 {
541 return m_boxes.GetCount();
542 }
543
544 void wxRadioBox::GtkDisableEvents()
545 {
546 wxList::compatibility_iterator node = m_boxes.GetFirst();
547 while (node)
548 {
549 gtk_signal_disconnect_by_func( GTK_OBJECT(node->GetData()),
550 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
551
552 node = node->GetNext();
553 }
554 }
555
556 void wxRadioBox::GtkEnableEvents()
557 {
558 wxList::compatibility_iterator node = m_boxes.GetFirst();
559 while (node)
560 {
561 gtk_signal_connect( GTK_OBJECT(node->GetData()), "clicked",
562 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
563
564 node = node->GetNext();
565 }
566 }
567
568 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle *style)
569 {
570 gtk_widget_modify_style( m_widget, style );
571
572 #ifdef __WXGTK20__
573 gtk_widget_modify_style(GTK_FRAME(m_widget)->label_widget, style);
574 #endif
575
576 wxList::compatibility_iterator node = m_boxes.GetFirst();
577 while (node)
578 {
579 GtkWidget *widget = GTK_WIDGET( node->GetData() );
580
581 gtk_widget_modify_style( widget, style );
582 gtk_widget_modify_style( BUTTON_CHILD(node->GetData()), style );
583
584 node = node->GetNext();
585 }
586 }
587
588 #if wxUSE_TOOLTIPS
589 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
590 {
591 wxList::compatibility_iterator node = m_boxes.GetFirst();
592 while (node)
593 {
594 GtkWidget *widget = GTK_WIDGET( node->GetData() );
595 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
596 node = node->GetNext();
597 }
598 }
599 #endif // wxUSE_TOOLTIPS
600
601 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
602 {
603 if (window == m_widget->window) return true;
604
605 wxList::compatibility_iterator node = m_boxes.GetFirst();
606 while (node)
607 {
608 GtkWidget *button = GTK_WIDGET( node->GetData() );
609
610 if (window == button->window) return true;
611
612 node = node->GetNext();
613 }
614
615 return false;
616 }
617
618 void wxRadioBox::OnInternalIdle()
619 {
620 if ( m_lostFocus )
621 {
622 m_hasFocus = false;
623 m_lostFocus = false;
624
625 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
626 event.SetEventObject( this );
627
628 (void)GetEventHandler()->ProcessEvent( event );
629 }
630
631 if (g_delayedFocus == this)
632 {
633 if (GTK_WIDGET_REALIZED(m_widget))
634 {
635 g_delayedFocus = NULL;
636 SetFocus();
637 }
638 }
639 }
640
641 // static
642 wxVisualAttributes
643 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
644 {
645 wxVisualAttributes attr;
646 // NB: we need toplevel window so that GTK+ can find the right style
647 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
648 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
649 gtk_container_add(GTK_CONTAINER(wnd), widget);
650 attr = GetDefaultAttributesFromGTKWidget(widget);
651 gtk_widget_destroy(wnd);
652 return attr;
653 }
654
655 #if WXWIN_COMPATIBILITY_2_2
656
657 int wxRadioBox::Number() const
658 {
659 return GetCount();
660 }
661
662 wxString wxRadioBox::GetLabel(int n) const
663 {
664 return GetString(n);
665 }
666
667 void wxRadioBox::SetLabel( int item, const wxString& label )
668 {
669 SetString(item, label);
670 }
671
672 #endif // WXWIN_COMPATIBILITY_2_2
673
674 #endif // wxUSE_RADIOBOX
675