Minimal layout change.
[wxWidgets.git] / src / gtk1 / 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), 1 );
222 gtk_table_set_row_spacings( GTK_TABLE(table), 1 );
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( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom,
255 GTK_FILL, GTK_FILL, 1, 1 );
256 }
257 else
258 {
259 int left = i/num_of_rows;
260 int right = (i/num_of_rows) + 1;
261 int top = i%num_of_rows;
262 int bottom = (i%num_of_rows)+1;
263 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(m_radio), left, right, top, bottom,
264 GTK_FILL, GTK_FILL, 1, 1 );
265 }
266
267 ConnectWidget( GTK_WIDGET(m_radio) );
268
269 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
270
271 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
272 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
273
274 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_in_event",
275 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in), (gpointer)this );
276
277 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_out_event",
278 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out), (gpointer)this );
279 }
280
281 m_parent->DoAddChild( this );
282
283 SetLabel( title );
284
285 PostCreation(size);
286
287 return true;
288 }
289
290 wxRadioBox::~wxRadioBox()
291 {
292 wxList::compatibility_iterator node = m_boxes.GetFirst();
293 while (node)
294 {
295 GtkWidget *button = GTK_WIDGET( node->GetData() );
296 gtk_widget_destroy( button );
297 node = node->GetNext();
298 }
299 }
300
301 bool wxRadioBox::Show( bool show )
302 {
303 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
304
305 if (!wxControl::Show(show))
306 {
307 // nothing to do
308 return false;
309 }
310
311 if ( HasFlag(wxNO_BORDER) )
312 gtk_widget_hide( m_widget );
313
314 wxList::compatibility_iterator node = m_boxes.GetFirst();
315 while (node)
316 {
317 GtkWidget *button = GTK_WIDGET( node->GetData() );
318
319 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
320
321 node = node->GetNext();
322 }
323
324 return true;
325 }
326
327 int wxRadioBox::FindString( const wxString &find ) const
328 {
329 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
330
331 int count = 0;
332
333 wxList::compatibility_iterator node = m_boxes.GetFirst();
334 while (node)
335 {
336 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
337 #ifdef __WXGTK20__
338 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
339 #else
340 wxString str( label->label );
341 #endif
342 if (find == str)
343 return count;
344
345 count++;
346
347 node = node->GetNext();
348 }
349
350 return wxNOT_FOUND;
351 }
352
353 void wxRadioBox::SetFocus()
354 {
355 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
356
357 if (m_boxes.GetCount() == 0) return;
358
359 wxList::compatibility_iterator node = m_boxes.GetFirst();
360 while (node)
361 {
362 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
363 if (button->active)
364 {
365 gtk_widget_grab_focus( GTK_WIDGET(button) );
366 return;
367 }
368 node = node->GetNext();
369 }
370 }
371
372 void wxRadioBox::SetSelection( int n )
373 {
374 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
375
376 wxList::compatibility_iterator node = m_boxes.Item( n );
377
378 wxCHECK_RET( node, wxT("radiobox wrong index") );
379
380 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
381
382 GtkDisableEvents();
383
384 gtk_toggle_button_set_active( button, 1 );
385
386 GtkEnableEvents();
387 }
388
389 int wxRadioBox::GetSelection(void) const
390 {
391 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
392
393 int count = 0;
394
395 wxList::compatibility_iterator node = m_boxes.GetFirst();
396 while (node)
397 {
398 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
399 if (button->active) return count;
400 count++;
401 node = node->GetNext();
402 }
403
404 wxFAIL_MSG( wxT("wxRadioBox none selected") );
405
406 return wxNOT_FOUND;
407 }
408
409 wxString wxRadioBox::GetString( int n ) const
410 {
411 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
412
413 wxList::compatibility_iterator node = m_boxes.Item( n );
414
415 wxCHECK_MSG( node, wxEmptyString, wxT("radiobox wrong index") );
416
417 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
418
419 #ifdef __WXGTK20__
420 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
421 #else
422 wxString str( label->label );
423 #endif
424
425 return str;
426 }
427
428 void wxRadioBox::SetLabel( const wxString& label )
429 {
430 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
431
432 wxControl::SetLabel( label );
433
434 gtk_frame_set_label( GTK_FRAME(m_widget), wxGTK_CONV( wxControl::GetLabel() ) );
435 }
436
437 void wxRadioBox::SetString( int item, const wxString& label )
438 {
439 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
440
441 wxList::compatibility_iterator node = m_boxes.Item( item );
442
443 wxCHECK_RET( node, wxT("radiobox wrong index") );
444
445 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
446
447 gtk_label_set( g_label, wxGTK_CONV( label ) );
448 }
449
450 bool wxRadioBox::Enable( bool enable )
451 {
452 if ( !wxControl::Enable( enable ) )
453 return false;
454
455 wxList::compatibility_iterator node = m_boxes.GetFirst();
456 while (node)
457 {
458 GtkButton *button = GTK_BUTTON( node->GetData() );
459 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
460
461 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
462 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
463 node = node->GetNext();
464 }
465
466 return true;
467 }
468
469 bool wxRadioBox::Enable( int item, bool enable )
470 {
471 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
472
473 wxList::compatibility_iterator node = m_boxes.Item( item );
474
475 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
476
477 GtkButton *button = GTK_BUTTON( node->GetData() );
478 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
479
480 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
481 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
482
483 return true;
484 }
485
486 bool wxRadioBox::Show( int item, bool show )
487 {
488 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
489
490 wxList::compatibility_iterator node = m_boxes.Item( item );
491
492 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
493
494 GtkWidget *button = GTK_WIDGET( node->GetData() );
495
496 if (show)
497 gtk_widget_show( button );
498 else
499 gtk_widget_hide( button );
500
501 return true;
502 }
503
504 wxString wxRadioBox::GetStringSelection() const
505 {
506 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
507
508 wxList::compatibility_iterator node = m_boxes.GetFirst();
509 while (node)
510 {
511 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
512 if (button->active)
513 {
514 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
515
516 #ifdef __WXGTK20__
517 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
518 #else
519 wxString str( label->label );
520 #endif
521 return str;
522 }
523 node = node->GetNext();
524 }
525
526 wxFAIL_MSG( wxT("wxRadioBox none selected") );
527 return wxEmptyString;
528 }
529
530 bool wxRadioBox::SetStringSelection( const wxString &s )
531 {
532 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
533
534 int res = FindString( s );
535 if (res == wxNOT_FOUND) return false;
536 SetSelection( res );
537
538 return true;
539 }
540
541 int wxRadioBox::GetCount() const
542 {
543 return m_boxes.GetCount();
544 }
545
546 void wxRadioBox::GtkDisableEvents()
547 {
548 wxList::compatibility_iterator node = m_boxes.GetFirst();
549 while (node)
550 {
551 gtk_signal_disconnect_by_func( GTK_OBJECT(node->GetData()),
552 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
553
554 node = node->GetNext();
555 }
556 }
557
558 void wxRadioBox::GtkEnableEvents()
559 {
560 wxList::compatibility_iterator node = m_boxes.GetFirst();
561 while (node)
562 {
563 gtk_signal_connect( GTK_OBJECT(node->GetData()), "clicked",
564 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
565
566 node = node->GetNext();
567 }
568 }
569
570 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle *style)
571 {
572 gtk_widget_modify_style( m_widget, style );
573
574 #ifdef __WXGTK20__
575 gtk_widget_modify_style(GTK_FRAME(m_widget)->label_widget, style);
576 #endif
577
578 wxList::compatibility_iterator node = m_boxes.GetFirst();
579 while (node)
580 {
581 GtkWidget *widget = GTK_WIDGET( node->GetData() );
582
583 gtk_widget_modify_style( widget, style );
584 gtk_widget_modify_style( BUTTON_CHILD(node->GetData()), style );
585
586 node = node->GetNext();
587 }
588 }
589
590 #if wxUSE_TOOLTIPS
591 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
592 {
593 wxList::compatibility_iterator node = m_boxes.GetFirst();
594 while (node)
595 {
596 GtkWidget *widget = GTK_WIDGET( node->GetData() );
597 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
598 node = node->GetNext();
599 }
600 }
601 #endif // wxUSE_TOOLTIPS
602
603 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
604 {
605 if (window == m_widget->window) return true;
606
607 wxList::compatibility_iterator node = m_boxes.GetFirst();
608 while (node)
609 {
610 GtkWidget *button = GTK_WIDGET( node->GetData() );
611
612 if (window == button->window) return true;
613
614 node = node->GetNext();
615 }
616
617 return false;
618 }
619
620 void wxRadioBox::OnInternalIdle()
621 {
622 if ( m_lostFocus )
623 {
624 m_hasFocus = false;
625 m_lostFocus = false;
626
627 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
628 event.SetEventObject( this );
629
630 (void)GetEventHandler()->ProcessEvent( event );
631 }
632
633 if (g_delayedFocus == this)
634 {
635 if (GTK_WIDGET_REALIZED(m_widget))
636 {
637 g_delayedFocus = NULL;
638 SetFocus();
639 }
640 }
641 }
642
643 // static
644 wxVisualAttributes
645 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
646 {
647 wxVisualAttributes attr;
648 // NB: we need toplevel window so that GTK+ can find the right style
649 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
650 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
651 gtk_container_add(GTK_CONTAINER(wnd), widget);
652 attr = GetDefaultAttributesFromGTKWidget(widget);
653 gtk_widget_destroy(wnd);
654 return attr;
655 }
656
657 #if WXWIN_COMPATIBILITY_2_2
658
659 int wxRadioBox::Number() const
660 {
661 return GetCount();
662 }
663
664 wxString wxRadioBox::GetLabel(int n) const
665 {
666 return GetString(n);
667 }
668
669 void wxRadioBox::SetLabel( int item, const wxString& label )
670 {
671 SetString(item, label);
672 }
673
674 #endif // WXWIN_COMPATIBILITY_2_2
675
676 #endif // wxUSE_RADIOBOX
677