]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/radiobox.cpp
A little clarification
[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, const wxString& title,
172 const wxPoint &pos, const wxSize &size,
173 int n, const wxString choices[], int majorDim,
174 long style, const wxValidator& validator,
175 const wxString &name )
176 {
177 if (!PreCreation( parent, pos, size ) ||
178 !CreateBase( parent, id, pos, size, style, validator, name ))
179 {
180 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
181 return FALSE;
182 }
183
184 m_widget = gtk_frame_new( wxGTK_CONV( title ) );
185
186 // majorDim may be 0 if all trailing parameters were omitted, so don't
187 // assert here but just use the correct value for it
188 m_majorDim = majorDim == 0 ? n : majorDim;
189
190 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
191
192 wxString label;
193 GSList *radio_button_group = (GSList *) NULL;
194 for (int i = 0; i < n; i++)
195 {
196 if ( i != 0 )
197 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
198
199 label.Empty();
200 for ( const wxChar *pc = choices[i]; *pc; pc++ )
201 {
202 if ( *pc != wxT('&') )
203 label += *pc;
204 }
205
206 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, wxGTK_CONV( label ) ) );
207
208 gtk_signal_connect( GTK_OBJECT(m_radio), "key_press_event",
209 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback), (gpointer)this );
210
211 m_boxes.Append( (wxObject*) m_radio );
212
213 ConnectWidget( GTK_WIDGET(m_radio) );
214
215 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
216
217 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
218 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
219
220 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_in_event",
221 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in), (gpointer)this );
222
223 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_out_event",
224 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out), (gpointer)this );
225
226 gtk_pizza_put( GTK_PIZZA(m_parent->m_wxwindow),
227 GTK_WIDGET(m_radio),
228 m_x+10, m_y+10+(i*24), 10, 10 );
229 }
230
231 m_parent->DoAddChild( this );
232
233 PostCreation();
234 InheritAttributes();
235
236 ApplyWidgetStyle();
237
238 SetLabel( title );
239
240 SetFont( parent->GetFont() );
241
242 wxSize ls = LayoutItems();
243
244 GtkRequisition req;
245 req.width = 2;
246 req.height = 2;
247 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) (m_widget, &req );
248 if (req.width > ls.x) ls.x = req.width;
249
250 wxSize newSize = size;
251 if (newSize.x == -1) newSize.x = ls.x;
252 if (newSize.y == -1) newSize.y = ls.y;
253 SetSize( newSize.x, newSize.y );
254
255 Show( TRUE );
256
257 return TRUE;
258 }
259
260 wxRadioBox::~wxRadioBox()
261 {
262 wxList::compatibility_iterator node = m_boxes.GetFirst();
263 while (node)
264 {
265 GtkWidget *button = GTK_WIDGET( node->GetData() );
266 gtk_widget_destroy( button );
267 node = node->GetNext();
268 }
269 }
270
271 void wxRadioBox::DoSetSize( int x, int y, int width, int height, int sizeFlags )
272 {
273 wxWindow::DoSetSize( x, y, width, height, sizeFlags );
274
275 LayoutItems();
276 }
277
278 wxSize wxRadioBox::LayoutItems()
279 {
280 int x = 7;
281 int y = 15;
282
283 if ( m_majorDim == 0 )
284 {
285 // avoid dividing by 0 below
286 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
287
288 m_majorDim = 1;
289 }
290
291 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
292
293 wxSize res( 0, 0 );
294
295 int num_of_cols = 0;
296 int num_of_rows = 0;
297 if (HasFlag(wxRA_SPECIFY_COLS))
298 {
299 num_of_cols = m_majorDim;
300 num_of_rows = num_per_major;
301 }
302 else
303 {
304 num_of_cols = num_per_major;
305 num_of_rows = m_majorDim;
306 }
307
308 if ( HasFlag(wxRA_SPECIFY_COLS) ||
309 (HasFlag(wxRA_SPECIFY_ROWS) && (num_of_cols > 1)) )
310 {
311 for (int j = 0; j < num_of_cols; j++)
312 {
313 y = 15;
314
315 int max_len = 0;
316 wxList::compatibility_iterator node = m_boxes.Item( j*num_of_rows );
317 for (int i1 = 0; i1< num_of_rows; i1++)
318 {
319 GtkWidget *button = GTK_WIDGET( node->GetData() );
320
321 GtkRequisition req;
322 req.width = 2;
323 req.height = 2;
324 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button) )->size_request )
325 (button, &req );
326
327 if (req.width > max_len) max_len = req.width;
328
329 gtk_pizza_move( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y );
330 y += req.height;
331
332 node = node->GetNext();
333 if (!node) break;
334 }
335
336 // we don't know the max_len before
337
338 node = m_boxes.Item( j*num_of_rows );
339 for (int i2 = 0; i2< num_of_rows; i2++)
340 {
341 GtkWidget *button = GTK_WIDGET( node->GetData() );
342
343 gtk_pizza_resize( GTK_PIZZA(m_parent->m_wxwindow), button, max_len, 20 );
344
345 node = node->GetNext();
346 if (!node) break;
347 }
348
349 if (y > res.y) res.y = y;
350
351 x += max_len + 2;
352 }
353
354 res.x = x+4;
355 res.y += 4;
356 }
357 else
358 {
359 int max = 0;
360
361 wxList::compatibility_iterator node = m_boxes.GetFirst();
362 while (node)
363 {
364 GtkWidget *button = GTK_WIDGET( node->GetData() );
365
366 GtkRequisition req;
367 req.width = 2;
368 req.height = 2;
369 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button) )->size_request )
370 (button, &req );
371
372 if (req.width > max) max = req.width;
373
374 node = node->GetNext();
375 }
376
377 node = m_boxes.GetFirst();
378 while (node)
379 {
380 GtkWidget *button = GTK_WIDGET( node->GetData() );
381
382 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
383 x += max;
384
385 node = node->GetNext();
386 }
387 res.x = x+4;
388 res.y = 40;
389 }
390
391 return res;
392 }
393
394 bool wxRadioBox::Show( bool show )
395 {
396 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
397
398 if (!wxControl::Show(show))
399 {
400 // nothing to do
401 return FALSE;
402 }
403
404 if ((m_windowStyle & wxNO_BORDER) != 0)
405 gtk_widget_hide( m_widget );
406
407 wxList::compatibility_iterator node = m_boxes.GetFirst();
408 while (node)
409 {
410 GtkWidget *button = GTK_WIDGET( node->GetData() );
411
412 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
413
414 node = node->GetNext();
415 }
416
417 return TRUE;
418 }
419
420 int wxRadioBox::FindString( const wxString &find ) const
421 {
422 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
423
424 int count = 0;
425
426 wxList::compatibility_iterator node = m_boxes.GetFirst();
427 while (node)
428 {
429 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
430 #ifdef __WXGTK20__
431 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
432 #else
433 wxString str( label->label );
434 #endif
435 if (find == str)
436 return count;
437
438 count++;
439
440 node = node->GetNext();
441 }
442
443 return -1;
444 }
445
446 void wxRadioBox::SetFocus()
447 {
448 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
449
450 if (m_boxes.GetCount() == 0) return;
451
452 wxList::compatibility_iterator node = m_boxes.GetFirst();
453 while (node)
454 {
455 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
456 if (button->active)
457 {
458 gtk_widget_grab_focus( GTK_WIDGET(button) );
459 return;
460 }
461 node = node->GetNext();
462 }
463 }
464
465 void wxRadioBox::SetSelection( int n )
466 {
467 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
468
469 wxList::compatibility_iterator node = m_boxes.Item( n );
470
471 wxCHECK_RET( node, wxT("radiobox wrong index") );
472
473 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
474
475 GtkDisableEvents();
476
477 gtk_toggle_button_set_active( button, 1 );
478
479 GtkEnableEvents();
480 }
481
482 int wxRadioBox::GetSelection(void) const
483 {
484 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
485
486 int count = 0;
487
488 wxList::compatibility_iterator node = m_boxes.GetFirst();
489 while (node)
490 {
491 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
492 if (button->active) return count;
493 count++;
494 node = node->GetNext();
495 }
496
497 wxFAIL_MSG( wxT("wxRadioBox none selected") );
498
499 return -1;
500 }
501
502 wxString wxRadioBox::GetString( int n ) const
503 {
504 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
505
506 wxList::compatibility_iterator node = m_boxes.Item( n );
507
508 wxCHECK_MSG( node, wxT(""), wxT("radiobox wrong index") );
509
510 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
511
512 #ifdef __WXGTK20__
513 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
514 #else
515 wxString str( label->label );
516 #endif
517
518 return str;
519 }
520
521 void wxRadioBox::SetLabel( const wxString& label )
522 {
523 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
524
525 wxControl::SetLabel( label );
526
527 gtk_frame_set_label( GTK_FRAME(m_widget), wxGTK_CONV( wxControl::GetLabel() ) );
528 }
529
530 void wxRadioBox::SetString( int item, const wxString& label )
531 {
532 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
533
534 wxList::compatibility_iterator node = m_boxes.Item( item );
535
536 wxCHECK_RET( node, wxT("radiobox wrong index") );
537
538 GtkLabel *g_label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
539
540 gtk_label_set( g_label, wxGTK_CONV( label ) );
541 }
542
543 bool wxRadioBox::Enable( bool enable )
544 {
545 if ( !wxControl::Enable( enable ) )
546 return FALSE;
547
548 wxList::compatibility_iterator node = m_boxes.GetFirst();
549 while (node)
550 {
551 GtkButton *button = GTK_BUTTON( node->GetData() );
552 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
553
554 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
555 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
556 node = node->GetNext();
557 }
558
559 return TRUE;
560 }
561
562 void wxRadioBox::Enable( int item, bool enable )
563 {
564 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
565
566 wxList::compatibility_iterator node = m_boxes.Item( item );
567
568 wxCHECK_RET( node, wxT("radiobox wrong index") );
569
570 GtkButton *button = GTK_BUTTON( node->GetData() );
571 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(button) );
572
573 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
574 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
575 }
576
577 void wxRadioBox::Show( int item, bool show )
578 {
579 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
580
581 wxList::compatibility_iterator node = m_boxes.Item( item );
582
583 wxCHECK_RET( node, wxT("radiobox wrong index") );
584
585 GtkWidget *button = GTK_WIDGET( node->GetData() );
586
587 if (show)
588 gtk_widget_show( button );
589 else
590 gtk_widget_hide( button );
591 }
592
593 wxString wxRadioBox::GetStringSelection() const
594 {
595 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
596
597 wxList::compatibility_iterator node = m_boxes.GetFirst();
598 while (node)
599 {
600 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData() );
601 if (button->active)
602 {
603 GtkLabel *label = GTK_LABEL( BUTTON_CHILD(node->GetData()) );
604
605 #ifdef __WXGTK20__
606 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
607 #else
608 wxString str( label->label );
609 #endif
610 return str;
611 }
612 node = node->GetNext();
613 }
614
615 wxFAIL_MSG( wxT("wxRadioBox none selected") );
616 return wxT("");
617 }
618
619 bool wxRadioBox::SetStringSelection( const wxString &s )
620 {
621 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
622
623 int res = FindString( s );
624 if (res == -1) return FALSE;
625 SetSelection( res );
626
627 return TRUE;
628 }
629
630 int wxRadioBox::GetCount() const
631 {
632 return m_boxes.GetCount();
633 }
634
635 int wxRadioBox::GetNumberOfRowsOrCols() const
636 {
637 return 1;
638 }
639
640 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n) )
641 {
642 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
643 }
644
645 void wxRadioBox::GtkDisableEvents()
646 {
647 wxList::compatibility_iterator node = m_boxes.GetFirst();
648 while (node)
649 {
650 gtk_signal_disconnect_by_func( GTK_OBJECT(node->GetData()),
651 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
652
653 node = node->GetNext();
654 }
655 }
656
657 void wxRadioBox::GtkEnableEvents()
658 {
659 wxList::compatibility_iterator node = m_boxes.GetFirst();
660 while (node)
661 {
662 gtk_signal_connect( GTK_OBJECT(node->GetData()), "clicked",
663 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
664
665 node = node->GetNext();
666 }
667 }
668
669 void wxRadioBox::ApplyWidgetStyle()
670 {
671 SetWidgetStyle();
672
673 gtk_widget_set_style( m_widget, m_widgetStyle );
674
675 wxList::compatibility_iterator node = m_boxes.GetFirst();
676 while (node)
677 {
678 GtkWidget *widget = GTK_WIDGET( node->GetData() );
679 gtk_widget_set_style( widget, m_widgetStyle );
680
681 gtk_widget_set_style( BUTTON_CHILD(node->GetData()), m_widgetStyle );
682
683 node = node->GetNext();
684 }
685 }
686
687 #if wxUSE_TOOLTIPS
688 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
689 {
690 wxList::compatibility_iterator node = m_boxes.GetFirst();
691 while (node)
692 {
693 GtkWidget *widget = GTK_WIDGET( node->GetData() );
694 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
695 node = node->GetNext();
696 }
697 }
698 #endif // wxUSE_TOOLTIPS
699
700 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
701 {
702 if (window == m_widget->window) return TRUE;
703
704 wxList::compatibility_iterator node = m_boxes.GetFirst();
705 while (node)
706 {
707 GtkWidget *button = GTK_WIDGET( node->GetData() );
708
709 if (window == button->window) return TRUE;
710
711 node = node->GetNext();
712 }
713
714 return FALSE;
715 }
716
717 void wxRadioBox::OnInternalIdle()
718 {
719 if ( m_lostFocus )
720 {
721 m_hasFocus = FALSE;
722 m_lostFocus = FALSE;
723
724 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
725 event.SetEventObject( this );
726
727 (void)GetEventHandler()->ProcessEvent( event );
728 }
729
730 if (g_delayedFocus == this)
731 {
732 if (GTK_WIDGET_REALIZED(m_widget))
733 {
734 g_delayedFocus = NULL;
735 SetFocus();
736 }
737 }
738 }
739
740 #endif // wxUSE_RADIOBOX
741