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