]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/radiobox.cpp
Made dnd reentrent safe as per stable tree.
[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 #ifdef __GNUG__
11 #pragma implementation "radiobox.h"
12 #endif
13
14 #include "wx/radiobox.h"
15
16 #if wxUSE_RADIOBOX
17
18 #include "wx/dialog.h"
19 #include "wx/frame.h"
20 #include "wx/log.h"
21
22 #include <gdk/gdk.h>
23 #include <gtk/gtk.h>
24 #include <gdk/gdkkeysyms.h>
25
26 #include "wx/gtk/win_gtk.h"
27
28 //-----------------------------------------------------------------------------
29 // idle system
30 //-----------------------------------------------------------------------------
31
32 extern void wxapp_install_idle_handler();
33 extern bool g_isIdle;
34
35 //-----------------------------------------------------------------------------
36 // data
37 //-----------------------------------------------------------------------------
38
39 extern bool g_blockEventsOnDrag;
40
41 //-----------------------------------------------------------------------------
42 // "clicked"
43 //-----------------------------------------------------------------------------
44
45 static void gtk_radiobutton_clicked_callback( GtkWidget *WXUNUSED(widget), 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 (rb->m_alreadySent)
53 {
54 rb->m_alreadySent = FALSE;
55 return;
56 }
57
58 rb->m_alreadySent = TRUE;
59
60 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
61 event.SetInt( rb->GetSelection() );
62 event.SetString( rb->GetStringSelection() );
63 event.SetEventObject( rb );
64 rb->GetEventHandler()->ProcessEvent(event);
65 }
66
67 //-----------------------------------------------------------------------------
68 // "key_press_event"
69 //-----------------------------------------------------------------------------
70
71 static gint gtk_radiobox_keypress_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxRadioBox *rb )
72 {
73 if (g_isIdle)
74 wxapp_install_idle_handler();
75
76 if (!rb->m_hasVMT) return FALSE;
77 if (g_blockEventsOnDrag) return FALSE;
78
79 if ((gdk_event->keyval != GDK_Up) &&
80 (gdk_event->keyval != GDK_Down) &&
81 (gdk_event->keyval != GDK_Left) &&
82 (gdk_event->keyval != GDK_Right))
83 {
84 return FALSE;
85 }
86
87 wxNode *node = rb->m_boxes.Find( (wxObject*) widget );
88 if (!node)
89 {
90 return FALSE;
91 }
92
93 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget), "key_press_event" );
94
95 if ((gdk_event->keyval == GDK_Up) ||
96 (gdk_event->keyval == GDK_Left))
97 {
98 if (node == rb->m_boxes.First())
99 node = rb->m_boxes.Last();
100 else
101 node = node->Previous();
102 }
103 else
104 {
105 if (node == rb->m_boxes.Last())
106 node = rb->m_boxes.First();
107 else
108 node = node->Next();
109 }
110
111 GtkWidget *button = (GtkWidget*) node->Data();
112
113 gtk_widget_grab_focus( button );
114
115 return TRUE;
116 }
117
118 static gint gtk_radiobutton_focus_in( GtkWidget *widget,
119 GdkEvent *WXUNUSED(event),
120 wxRadioBox *win )
121 {
122 if ( win->m_lostFocus )
123 {
124 // no, we didn't really lose it
125 win->m_lostFocus = FALSE;
126 }
127 else if ( !win->m_hasFocus )
128 {
129 win->m_hasFocus = TRUE;
130
131 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
132 event.SetEventObject( win );
133
134 // never stop the signal emission, it seems to break the kbd handling
135 // inside the radiobox
136 (void)win->GetEventHandler()->ProcessEvent( event );
137 }
138
139 return FALSE;
140 }
141
142 static gint gtk_radiobutton_focus_out( GtkWidget *widget,
143 GdkEvent *WXUNUSED(event),
144 wxRadioBox *win )
145 {
146 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
147 // Replace with a warning, else we dump core a lot!
148 // if (!win->m_hasFocus)
149 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
150
151 // we might have lost the focus, but may be not - it may have just gone to
152 // another button in the same radiobox, so we'll check for it in the next
153 // idle iteration (leave m_hasFocus == TRUE for now)
154 win->m_lostFocus = TRUE;
155
156 return FALSE;
157 }
158
159 //-----------------------------------------------------------------------------
160 // wxRadioBox
161 //-----------------------------------------------------------------------------
162
163 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
164
165 void wxRadioBox::Init()
166 {
167 m_alreadySent = FALSE;
168 m_needParent = TRUE;
169 m_acceptsFocus = TRUE;
170
171 m_hasFocus =
172 m_lostFocus = FALSE;
173 }
174
175 bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
176 const wxPoint &pos, const wxSize &size,
177 int n, const wxString choices[], int majorDim,
178 long style, const wxValidator& validator,
179 const wxString &name )
180 {
181 if (!PreCreation( parent, pos, size ) ||
182 !CreateBase( parent, id, pos, size, style, validator, name ))
183 {
184 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
185 return FALSE;
186 }
187
188 m_widget = gtk_frame_new( title.mbc_str() );
189
190 m_majorDim = majorDim;
191
192 GtkRadioButton *m_radio = (GtkRadioButton*) NULL;
193
194 wxString label;
195 GSList *radio_button_group = (GSList *) NULL;
196 for (int i = 0; i < n; i++)
197 {
198 if ( i != 0 )
199 radio_button_group = gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio) );
200
201 label.Empty();
202 for ( const wxChar *pc = choices[i]; *pc; pc++ )
203 {
204 if ( *pc != wxT('&') )
205 label += *pc;
206 }
207
208 m_radio = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, label.mbc_str() ) );
209
210 gtk_signal_connect( GTK_OBJECT(m_radio), "key_press_event",
211 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback), (gpointer)this );
212
213 m_boxes.Append( (wxObject*) m_radio );
214
215 ConnectWidget( GTK_WIDGET(m_radio) );
216
217 if (!i) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio), TRUE );
218
219 gtk_signal_connect( GTK_OBJECT(m_radio), "clicked",
220 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
221
222 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_in_event",
223 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in), (gpointer)this );
224
225 gtk_signal_connect( GTK_OBJECT(m_radio), "focus_out_event",
226 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out), (gpointer)this );
227
228 gtk_pizza_put( GTK_PIZZA(m_parent->m_wxwindow),
229 GTK_WIDGET(m_radio),
230 m_x+10, m_y+10+(i*24), 10, 10 );
231 }
232
233 m_parent->DoAddChild( this );
234
235 PostCreation();
236
237 ApplyWidgetStyle();
238
239 SetLabel( title );
240
241 SetFont( parent->GetFont() );
242
243 wxSize ls = LayoutItems();
244
245 GtkRequisition req;
246 req.width = 2;
247 req.height = 2;
248 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request ) (m_widget, &req );
249 if (req.width > ls.x) ls.x = req.width;
250
251 wxSize newSize = size;
252 if (newSize.x == -1) newSize.x = ls.x;
253 if (newSize.y == -1) newSize.y = ls.y;
254 SetSize( newSize.x, newSize.y );
255
256 SetBackgroundColour( parent->GetBackgroundColour() );
257 SetForegroundColour( parent->GetForegroundColour() );
258
259 Show( TRUE );
260
261 return TRUE;
262 }
263
264 wxRadioBox::~wxRadioBox()
265 {
266 wxNode *node = m_boxes.First();
267 while (node)
268 {
269 GtkWidget *button = GTK_WIDGET( node->Data() );
270 gtk_widget_destroy( button );
271 node = node->Next();
272 }
273 }
274
275 void wxRadioBox::DoSetSize( int x, int y, int width, int height, int sizeFlags )
276 {
277 wxWindow::DoSetSize( x, y, width, height, sizeFlags );
278
279 LayoutItems();
280 }
281
282 wxSize wxRadioBox::LayoutItems()
283 {
284 int x = 7;
285 int y = 15;
286
287 if ( m_majorDim == 0 )
288 {
289 // avoid dividing by 0 below
290 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
291
292 m_majorDim = 1;
293 }
294
295 int num_per_major = (m_boxes.GetCount() - 1) / m_majorDim +1;
296
297 wxSize res( 0, 0 );
298
299 int num_of_cols = 0;
300 int num_of_rows = 0;
301 if (HasFlag(wxRA_SPECIFY_COLS))
302 {
303 num_of_cols = m_majorDim;
304 num_of_rows = num_per_major;
305 }
306 else
307 {
308 num_of_cols = num_per_major;
309 num_of_rows = m_majorDim;
310 }
311
312 if ( HasFlag(wxRA_SPECIFY_COLS) ||
313 (HasFlag(wxRA_SPECIFY_ROWS) && (num_of_cols > 1)) )
314 {
315 for (int j = 0; j < num_of_cols; j++)
316 {
317 y = 15;
318
319 int max_len = 0;
320 wxNode *node = m_boxes.Nth( j*num_of_rows );
321 for (int i1 = 0; i1< num_of_rows; i1++)
322 {
323 GtkWidget *button = GTK_WIDGET( node->Data() );
324
325 GtkRequisition req;
326 req.width = 2;
327 req.height = 2;
328 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button) )->size_request )
329 (button, &req );
330
331 if (req.width > max_len) max_len = req.width;
332
333 gtk_pizza_move( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y );
334 y += req.height;
335
336 node = node->Next();
337 if (!node) break;
338 }
339
340 // we don't know the max_len before
341
342 node = m_boxes.Nth( j*num_of_rows );
343 for (int i2 = 0; i2< num_of_rows; i2++)
344 {
345 GtkWidget *button = GTK_WIDGET( node->Data() );
346
347 gtk_pizza_resize( GTK_PIZZA(m_parent->m_wxwindow), button, max_len, 20 );
348
349 node = node->Next();
350 if (!node) break;
351 }
352
353 if (y > res.y) res.y = y;
354
355 x += max_len + 2;
356 }
357
358 res.x = x+4;
359 res.y += 4;
360 }
361 else
362 {
363 int max = 0;
364
365 wxNode *node = m_boxes.First();
366 while (node)
367 {
368 GtkWidget *button = GTK_WIDGET( node->Data() );
369
370 GtkRequisition req;
371 req.width = 2;
372 req.height = 2;
373 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button) )->size_request )
374 (button, &req );
375
376 if (req.width > max) max = req.width;
377
378 node = node->Next();
379 }
380
381 node = m_boxes.First();
382 while (node)
383 {
384 GtkWidget *button = GTK_WIDGET( node->Data() );
385
386 gtk_pizza_set_size( GTK_PIZZA(m_parent->m_wxwindow), button, m_x+x, m_y+y, max, 20 );
387 x += max;
388
389 node = node->Next();
390 }
391 res.x = x+4;
392 res.y = 40;
393 }
394
395 return res;
396 }
397
398 bool wxRadioBox::Show( bool show )
399 {
400 wxCHECK_MSG( m_widget != NULL, FALSE, wxT("invalid radiobox") );
401
402 if (!wxControl::Show(show))
403 {
404 // nothing to do
405 return FALSE;
406 }
407
408 if ((m_windowStyle & wxNO_BORDER) != 0)
409 gtk_widget_hide( m_widget );
410
411 wxNode *node = m_boxes.First();
412 while (node)
413 {
414 GtkWidget *button = GTK_WIDGET( node->Data() );
415
416 if (show) gtk_widget_show( button ); else gtk_widget_hide( button );
417
418 node = node->Next();
419 }
420
421 return TRUE;
422 }
423
424 int wxRadioBox::FindString( const wxString &s ) const
425 {
426 wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid radiobox") );
427
428 int count = 0;
429
430 wxNode *node = m_boxes.First();
431 while (node)
432 {
433 GtkButton *button = GTK_BUTTON( node->Data() );
434
435 GtkLabel *label = GTK_LABEL( button->child );
436 if (s == label->label) return count;
437 count++;
438
439 node = node->Next();
440 }
441
442 return -1;
443 }
444
445 void wxRadioBox::SetFocus()
446 {
447 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
448
449 if (m_boxes.GetCount() == 0) return;
450
451 wxNode *node = m_boxes.First();
452 while (node)
453 {
454 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
455 if (button->active)
456 {
457 gtk_widget_grab_focus( GTK_WIDGET(button) );
458 return;
459 }
460 node = node->Next();
461 }
462
463 }
464
465 void wxRadioBox::SetSelection( int n )
466 {
467 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
468
469 wxNode *node = m_boxes.Nth( n );
470
471 wxCHECK_RET( node, wxT("radiobox wrong index") );
472
473 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
474
475 GtkDisableEvents();
476
477 gtk_toggle_button_set_state( 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 wxNode *node = m_boxes.First();
489 while (node)
490 {
491 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
492 if (button->active) return count;
493 count++;
494 node = node->Next();
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 wxNode *node = m_boxes.Nth( n );
507
508 wxCHECK_MSG( node, wxT(""), wxT("radiobox wrong index") );
509
510 GtkButton *button = GTK_BUTTON( node->Data() );
511 GtkLabel *label = GTK_LABEL( button->child );
512
513 return wxString( label->label );
514 }
515
516 wxString wxRadioBox::GetLabel( int item ) const
517 {
518 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
519
520 return GetString( item );
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), wxControl::GetLabel().mbc_str() );
530 }
531
532 void wxRadioBox::SetLabel( int item, const wxString& label )
533 {
534 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
535
536 wxNode *node = m_boxes.Nth( item );
537
538 wxCHECK_RET( node, wxT("radiobox wrong index") );
539
540 GtkButton *button = GTK_BUTTON( node->Data() );
541 GtkLabel *g_label = GTK_LABEL( button->child );
542
543 gtk_label_set( g_label, label.mbc_str() );
544 }
545
546 void wxRadioBox::SetLabel( int WXUNUSED(item), wxBitmap *WXUNUSED(bitmap) )
547 {
548 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
549 }
550
551 bool wxRadioBox::Enable( bool enable )
552 {
553 if ( !wxControl::Enable( enable ) )
554 return FALSE;
555
556 wxNode *node = m_boxes.First();
557 while (node)
558 {
559 GtkButton *button = GTK_BUTTON( node->Data() );
560 GtkWidget *label = button->child;
561 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
562 gtk_widget_set_sensitive( label, enable );
563 node = node->Next();
564 }
565
566 return TRUE;
567 }
568
569 void wxRadioBox::Enable( int item, bool enable )
570 {
571 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
572
573 wxNode *node = m_boxes.Nth( item );
574
575 wxCHECK_RET( node, wxT("radiobox wrong index") );
576
577 GtkButton *button = GTK_BUTTON( node->Data() );
578 GtkWidget *label = button->child;
579 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
580 gtk_widget_set_sensitive( label, enable );
581 }
582
583 void wxRadioBox::Show( int item, bool show )
584 {
585 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
586
587 wxNode *node = m_boxes.Nth( item );
588
589 wxCHECK_RET( node, wxT("radiobox wrong index") );
590
591 GtkWidget *button = GTK_WIDGET( node->Data() );
592
593 if (show)
594 gtk_widget_show( button );
595 else
596 gtk_widget_hide( button );
597 }
598
599 wxString wxRadioBox::GetStringSelection() const
600 {
601 wxCHECK_MSG( m_widget != NULL, wxT(""), wxT("invalid radiobox") );
602
603 wxNode *node = m_boxes.First();
604 while (node)
605 {
606 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->Data() );
607 if (button->active)
608 {
609 GtkLabel *label = GTK_LABEL( GTK_BUTTON(button)->child );
610 return label->label;
611 }
612 node = node->Next();
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::Number() const
631 {
632 return m_boxes.Number();
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 wxNode *node = m_boxes.First();
648 while (node)
649 {
650 gtk_signal_disconnect_by_func( GTK_OBJECT(node->Data()),
651 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
652
653 node = node->Next();
654 }
655 }
656
657 void wxRadioBox::GtkEnableEvents()
658 {
659 wxNode *node = m_boxes.First();
660 while (node)
661 {
662 gtk_signal_connect( GTK_OBJECT(node->Data()), "clicked",
663 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback), (gpointer*)this );
664
665 node = node->Next();
666 }
667 }
668
669 void wxRadioBox::ApplyWidgetStyle()
670 {
671 SetWidgetStyle();
672
673 gtk_widget_set_style( m_widget, m_widgetStyle );
674
675 wxNode *node = m_boxes.First();
676 while (node)
677 {
678 GtkWidget *widget = GTK_WIDGET( node->Data() );
679 gtk_widget_set_style( widget, m_widgetStyle );
680
681 GtkButton *button = GTK_BUTTON( node->Data() );
682 gtk_widget_set_style( button->child, m_widgetStyle );
683
684 node = node->Next();
685 }
686 }
687
688 #if wxUSE_TOOLTIPS
689 void wxRadioBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
690 {
691 wxNode *node = m_boxes.First();
692 while (node)
693 {
694 GtkWidget *widget = GTK_WIDGET( node->Data() );
695 gtk_tooltips_set_tip( tips, widget, wxConvCurrent->cWX2MB(tip), (gchar*) NULL );
696 node = node->Next();
697 }
698 }
699 #endif // wxUSE_TOOLTIPS
700
701 bool wxRadioBox::IsOwnGtkWindow( GdkWindow *window )
702 {
703 if (window == m_widget->window) return TRUE;
704
705 wxNode *node = m_boxes.First();
706 while (node)
707 {
708 GtkWidget *button = GTK_WIDGET( node->Data() );
709
710 if (window == button->window) return TRUE;
711
712 node = node->Next();
713 }
714
715 return FALSE;
716 }
717
718 void wxRadioBox::OnInternalIdle()
719 {
720 if ( m_lostFocus )
721 {
722 m_hasFocus = FALSE;
723 m_lostFocus = FALSE;
724
725 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
726 event.SetEventObject( this );
727
728 (void)GetEventHandler()->ProcessEvent( event );
729 }
730 }
731
732 #endif // wxUSE_RADIOBOX
733