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