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