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