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