]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/radiobox.cpp
Added extra hit test style for more accurate reporting
[wxWidgets.git] / src / gtk / radiobox.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/gtk/radiobox.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if wxUSE_RADIOBOX
14
15#include "wx/radiobox.h"
16
17#ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/frame.h"
20 #include "wx/dialog.h"
21#endif
22
23#if wxUSE_TOOLTIPS
24 #include "wx/tooltip.h"
25#endif
26
27#include "wx/gtk/private.h"
28#include <gdk/gdkkeysyms.h>
29
30#include "wx/gtk/win_gtk.h"
31
32//-----------------------------------------------------------------------------
33// wxGTKRadioButtonInfo
34//-----------------------------------------------------------------------------
35// structure internally used by wxRadioBox to store its child buttons
36
37class wxGTKRadioButtonInfo : public wxObject
38{
39public:
40 wxGTKRadioButtonInfo( GtkRadioButton * abutton, const wxRect & arect )
41 : button( abutton ), rect( arect ) {}
42
43 GtkRadioButton * button;
44 wxRect rect;
45};
46
47//-----------------------------------------------------------------------------
48// data
49//-----------------------------------------------------------------------------
50
51#include "wx/listimpl.cpp"
52WX_DEFINE_LIST( wxRadioBoxButtonsInfoList );
53
54extern bool g_blockEventsOnDrag;
55
56//-----------------------------------------------------------------------------
57// "clicked"
58//-----------------------------------------------------------------------------
59
60extern "C" {
61static void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioBox *rb )
62{
63 if (g_isIdle) wxapp_install_idle_handler();
64
65 if (!rb->m_hasVMT) return;
66 if (g_blockEventsOnDrag) return;
67
68 if (!button->active) return;
69
70 wxCommandEvent event( wxEVT_COMMAND_RADIOBOX_SELECTED, rb->GetId() );
71 event.SetInt( rb->GetSelection() );
72 event.SetString( rb->GetStringSelection() );
73 event.SetEventObject( rb );
74 rb->GetEventHandler()->ProcessEvent(event);
75}
76}
77
78//-----------------------------------------------------------------------------
79// "key_press_event"
80//-----------------------------------------------------------------------------
81
82extern "C" {
83static gint gtk_radiobox_keypress_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxRadioBox *rb )
84{
85 // don't need to install idle handler, its done from "event" signal
86
87 if (!rb->m_hasVMT) return FALSE;
88 if (g_blockEventsOnDrag) return FALSE;
89
90 if ( ((gdk_event->keyval == GDK_Tab) ||
91 (gdk_event->keyval == GDK_ISO_Left_Tab)) &&
92 rb->GetParent() && (rb->GetParent()->HasFlag( wxTAB_TRAVERSAL)) )
93 {
94 wxNavigationKeyEvent new_event;
95 new_event.SetEventObject( rb->GetParent() );
96 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
97 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
98 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
99 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
100 new_event.SetCurrentFocus( rb );
101 return rb->GetParent()->GetEventHandler()->ProcessEvent( new_event );
102 }
103
104 if ((gdk_event->keyval != GDK_Up) &&
105 (gdk_event->keyval != GDK_Down) &&
106 (gdk_event->keyval != GDK_Left) &&
107 (gdk_event->keyval != GDK_Right))
108 {
109 return FALSE;
110 }
111
112 wxRadioBoxButtonsInfoList::compatibility_iterator node = rb->m_buttonsInfo.GetFirst();
113 while( node && GTK_WIDGET( node->GetData()->button ) != widget )
114 {
115 node = node->GetNext();
116 }
117 if (!node)
118 {
119 return FALSE;
120 }
121
122 if ((gdk_event->keyval == GDK_Up) ||
123 (gdk_event->keyval == GDK_Left))
124 {
125 if (node == rb->m_buttonsInfo.GetFirst())
126 node = rb->m_buttonsInfo.GetLast();
127 else
128 node = node->GetPrevious();
129 }
130 else
131 {
132 if (node == rb->m_buttonsInfo.GetLast())
133 node = rb->m_buttonsInfo.GetFirst();
134 else
135 node = node->GetNext();
136 }
137
138 GtkWidget *button = (GtkWidget*) node->GetData()->button;
139
140 gtk_widget_grab_focus( button );
141
142 return TRUE;
143}
144}
145
146extern "C" {
147static gint gtk_radiobutton_focus_in( GtkWidget *widget,
148 GdkEvent *WXUNUSED(event),
149 wxRadioBox *win )
150{
151 if ( win->m_lostFocus )
152 {
153 // no, we didn't really lose it
154 win->m_lostFocus = FALSE;
155 }
156 else if ( !win->m_hasFocus )
157 {
158 win->m_hasFocus = true;
159
160 wxFocusEvent event( wxEVT_SET_FOCUS, win->GetId() );
161 event.SetEventObject( win );
162
163 // never stop the signal emission, it seems to break the kbd handling
164 // inside the radiobox
165 (void)win->GetEventHandler()->ProcessEvent( event );
166 }
167
168 return FALSE;
169}
170}
171
172extern "C" {
173static gint gtk_radiobutton_focus_out( GtkWidget *widget,
174 GdkEvent *WXUNUSED(event),
175 wxRadioBox *win )
176{
177 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
178 // Replace with a warning, else we dump core a lot!
179 // if (!win->m_hasFocus)
180 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
181
182 // we might have lost the focus, but may be not - it may have just gone to
183 // another button in the same radiobox, so we'll check for it in the next
184 // idle iteration (leave m_hasFocus == true for now)
185 win->m_lostFocus = true;
186
187 return FALSE;
188}
189}
190
191extern "C" {
192static void gtk_radiobutton_size_allocate( GtkWidget *widget,
193 GtkAllocation * alloc,
194 wxRadioBox *win )
195{
196 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node = win->m_buttonsInfo.GetFirst();
197 node;
198 node = node->GetNext())
199 {
200 if (widget == GTK_WIDGET(node->GetData()->button))
201 {
202 const wxPoint origin = win->GetPosition();
203 wxRect rect = wxRect( alloc->x - origin.x, alloc->y - origin.y,
204 alloc->width, alloc->height );
205 node->GetData()->rect = rect;
206 break;
207 }
208 }
209}
210}
211
212
213//-----------------------------------------------------------------------------
214// wxRadioBox
215//-----------------------------------------------------------------------------
216
217IMPLEMENT_DYNAMIC_CLASS(wxRadioBox,wxControl)
218
219void wxRadioBox::Init()
220{
221 m_needParent = true;
222 m_acceptsFocus = true;
223
224 m_hasFocus =
225 m_lostFocus = false;
226}
227
228bool wxRadioBox::Create( wxWindow *parent, wxWindowID id,
229 const wxString& title,
230 const wxPoint &pos, const wxSize &size,
231 const wxArrayString& choices, int majorDim,
232 long style, const wxValidator& validator,
233 const wxString &name )
234{
235 wxCArrayString chs(choices);
236
237 return Create( parent, id, title, pos, size, chs.GetCount(),
238 chs.GetStrings(), majorDim, style, validator, name );
239}
240
241bool wxRadioBox::Create( wxWindow *parent, wxWindowID id, const wxString& title,
242 const wxPoint &pos, const wxSize &size,
243 int n, const wxString choices[], int majorDim,
244 long style, const wxValidator& validator,
245 const wxString &name )
246{
247 if (!PreCreation( parent, pos, size ) ||
248 !CreateBase( parent, id, pos, size, style, validator, name ))
249 {
250 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
251 return false;
252 }
253
254 m_widget = GTKCreateFrame(title);
255 wxControl::SetLabel(title);
256
257 // majorDim may be 0 if all trailing parameters were omitted, so don't
258 // assert here but just use the correct value for it
259 SetMajorDim(majorDim == 0 ? n : majorDim, style);
260
261
262 unsigned int num_of_cols = GetColumnCount();
263 unsigned int num_of_rows = GetRowCount();
264
265 GtkRadioButton *rbtn = (GtkRadioButton*) NULL;
266
267 GtkWidget *table = gtk_table_new( num_of_rows, num_of_cols, FALSE );
268 gtk_table_set_col_spacings( GTK_TABLE(table), 1 );
269 gtk_table_set_row_spacings( GTK_TABLE(table), 1 );
270 gtk_widget_show( table );
271 gtk_container_add( GTK_CONTAINER(m_widget), table );
272
273 wxString label;
274 GSList *radio_button_group = (GSList *) NULL;
275 for (unsigned int i = 0; i < (unsigned int)n; i++)
276 {
277 if ( i != 0 )
278 radio_button_group = gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn) );
279
280 label.Empty();
281 for ( const wxChar *pc = choices[i]; *pc; pc++ )
282 {
283 if ( *pc != wxT('&') )
284 label += *pc;
285 }
286
287 rbtn = GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group, wxGTK_CONV( label ) ) );
288 gtk_widget_show( GTK_WIDGET(rbtn) );
289
290 g_signal_connect (rbtn, "key_press_event",
291 G_CALLBACK (gtk_radiobox_keypress_callback), this);
292
293 m_buttonsInfo.Append( new wxGTKRadioButtonInfo( rbtn, wxRect() ) );
294
295 if (HasFlag(wxRA_SPECIFY_COLS))
296 {
297 int left = i%num_of_cols;
298 int right = (i%num_of_cols) + 1;
299 int top = i/num_of_cols;
300 int bottom = (i/num_of_cols)+1;
301 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(rbtn), left, right, top, bottom,
302 GTK_FILL, GTK_FILL, 1, 1 );
303 }
304 else
305 {
306 int left = i/num_of_rows;
307 int right = (i/num_of_rows) + 1;
308 int top = i%num_of_rows;
309 int bottom = (i%num_of_rows)+1;
310 gtk_table_attach( GTK_TABLE(table), GTK_WIDGET(rbtn), left, right, top, bottom,
311 GTK_FILL, GTK_FILL, 1, 1 );
312 }
313
314 ConnectWidget( GTK_WIDGET(rbtn) );
315
316 if (!i)
317 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn), TRUE );
318
319 g_signal_connect (rbtn, "clicked",
320 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
321 g_signal_connect (rbtn, "focus_in_event",
322 G_CALLBACK (gtk_radiobutton_focus_in), this);
323 g_signal_connect (rbtn, "focus_out_event",
324 G_CALLBACK (gtk_radiobutton_focus_out), this);
325 g_signal_connect (rbtn, "size_allocate",
326 G_CALLBACK (gtk_radiobutton_size_allocate), this);
327 }
328
329 m_parent->DoAddChild( this );
330
331 PostCreation(size);
332
333 return true;
334}
335
336wxRadioBox::~wxRadioBox()
337{
338 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
339 while (node)
340 {
341 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
342 gtk_widget_destroy( button );
343 node = node->GetNext();
344 }
345 WX_CLEAR_LIST( wxRadioBoxButtonsInfoList, m_buttonsInfo );
346}
347
348bool wxRadioBox::Show( bool show )
349{
350 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
351
352 if (!wxControl::Show(show))
353 {
354 // nothing to do
355 return false;
356 }
357
358 if ( HasFlag(wxNO_BORDER) )
359 gtk_widget_hide( m_widget );
360
361 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
362 while (node)
363 {
364 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
365
366 if (show)
367 gtk_widget_show( button );
368 else
369 gtk_widget_hide( button );
370
371 node = node->GetNext();
372 }
373
374 return true;
375}
376
377void wxRadioBox::SetFocus()
378{
379 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
380
381 if (m_buttonsInfo.GetCount() == 0) return;
382
383 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
384 while (node)
385 {
386 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData()->button );
387 if (button->active)
388 {
389 gtk_widget_grab_focus( GTK_WIDGET(button) );
390 return;
391 }
392 node = node->GetNext();
393 }
394}
395
396void wxRadioBox::SetSelection( int n )
397{
398 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
399
400 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( n );
401
402 wxCHECK_RET( node, wxT("radiobox wrong index") );
403
404 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData()->button );
405
406 GtkDisableEvents();
407
408 gtk_toggle_button_set_active( button, 1 );
409
410 GtkEnableEvents();
411}
412
413int wxRadioBox::GetSelection(void) const
414{
415 wxCHECK_MSG( m_widget != NULL, wxNOT_FOUND, wxT("invalid radiobox") );
416
417 int count = 0;
418
419 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
420 while (node)
421 {
422 GtkToggleButton *button = GTK_TOGGLE_BUTTON( node->GetData()->button );
423 if (button->active) return count;
424 count++;
425 node = node->GetNext();
426 }
427
428 wxFAIL_MSG( wxT("wxRadioBox none selected") );
429
430 return wxNOT_FOUND;
431}
432
433wxString wxRadioBox::GetString(unsigned int n) const
434{
435 wxCHECK_MSG( m_widget != NULL, wxEmptyString, wxT("invalid radiobox") );
436
437 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( n );
438
439 wxCHECK_MSG( node, wxEmptyString, wxT("radiobox wrong index") );
440
441 GtkLabel *label = GTK_LABEL(GTK_BIN(node->GetData()->button)->child);
442
443 wxString str( wxGTK_CONV_BACK( gtk_label_get_text(label) ) );
444
445 return str;
446}
447
448void wxRadioBox::SetLabel( const wxString& label )
449{
450 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
451
452 GTKSetLabelForFrame(GTK_FRAME(m_widget), label);
453}
454
455void wxRadioBox::SetString(unsigned int item, const wxString& label)
456{
457 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobox") );
458
459 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
460
461 wxCHECK_RET( node, wxT("radiobox wrong index") );
462
463 GtkLabel *g_label = GTK_LABEL(GTK_BIN(node->GetData()->button)->child);
464
465 gtk_label_set_text( g_label, wxGTK_CONV( label ) );
466}
467
468bool wxRadioBox::Enable( bool enable )
469{
470 if ( !wxControl::Enable( enable ) )
471 return false;
472
473 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
474 while (node)
475 {
476 GtkButton *button = GTK_BUTTON( node->GetData()->button );
477 GtkLabel *label = GTK_LABEL(GTK_BIN(button)->child);
478
479 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
480 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
481 node = node->GetNext();
482 }
483
484 return true;
485}
486
487bool wxRadioBox::Enable(unsigned int item, bool enable)
488{
489 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
490
491 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
492
493 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
494
495 GtkButton *button = GTK_BUTTON( node->GetData()->button );
496 GtkLabel *label = GTK_LABEL(GTK_BIN(button)->child);
497
498 gtk_widget_set_sensitive( GTK_WIDGET(button), enable );
499 gtk_widget_set_sensitive( GTK_WIDGET(label), enable );
500
501 return true;
502}
503
504bool wxRadioBox::IsItemEnabled(unsigned int item) const
505{
506 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
507
508 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
509
510 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
511
512 GtkButton *button = GTK_BUTTON( node->GetData()->button );
513
514 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
515 // the parent radiobox is disabled
516 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button));
517}
518
519bool wxRadioBox::Show(unsigned int item, bool show)
520{
521 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
522
523 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
524
525 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
526
527 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
528
529 if (show)
530 gtk_widget_show( button );
531 else
532 gtk_widget_hide( button );
533
534 return true;
535}
536
537bool wxRadioBox::IsItemShown(unsigned int item) const
538{
539 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobox") );
540
541 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.Item( item );
542
543 wxCHECK_MSG( node, false, wxT("radiobox wrong index") );
544
545 GtkButton *button = GTK_BUTTON( node->GetData()->button );
546
547 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button));
548}
549
550unsigned int wxRadioBox::GetCount() const
551{
552 return m_buttonsInfo.GetCount();
553}
554
555void wxRadioBox::GtkDisableEvents()
556{
557 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
558 while (node)
559 {
560 g_signal_handlers_disconnect_by_func (node->GetData()->button,
561 (gpointer) gtk_radiobutton_clicked_callback,
562 this);
563
564 node = node->GetNext();
565 }
566}
567
568void wxRadioBox::GtkEnableEvents()
569{
570 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
571 while (node)
572 {
573 g_signal_connect (node->GetData()->button, "clicked",
574 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
575
576 node = node->GetNext();
577 }
578}
579
580void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle *style)
581{
582 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget), style);
583
584 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
585 while (node)
586 {
587 GtkWidget *widget = GTK_WIDGET( node->GetData()->button );
588
589 gtk_widget_modify_style( widget, style );
590 gtk_widget_modify_style(GTK_BIN(widget)->child, style);
591
592 node = node->GetNext();
593 }
594}
595
596bool wxRadioBox::GTKWidgetNeedsMnemonic() const
597{
598 return true;
599}
600
601void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget* w)
602{
603 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget), w);
604}
605
606#if wxUSE_TOOLTIPS
607void wxRadioBox::ApplyToolTip(GtkTooltips * WXUNUSED(tips), const wxChar *tip)
608{
609 // set this tooltip for all radiobuttons which don't have their own tips
610 unsigned n = 0;
611 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
612 node;
613 node = node->GetNext(), n++ )
614 {
615 if ( !GetItemToolTip(n) )
616 {
617 wxToolTip::Apply(GTK_WIDGET(node->GetData()->button),
618 wxConvCurrent->cWX2MB(tip));
619 }
620 }
621}
622
623void wxRadioBox::DoSetItemToolTip(unsigned int n, wxToolTip *tooltip)
624{
625 wxCharBuffer buf;
626 if ( !tooltip )
627 tooltip = GetToolTip();
628 if ( tooltip )
629 buf = wxGTK_CONV(tooltip->GetTip());
630
631 wxToolTip::Apply(GTK_WIDGET(m_buttonsInfo[n]->button), buf);
632}
633
634#endif // wxUSE_TOOLTIPS
635
636GdkWindow *wxRadioBox::GTKGetWindow(wxArrayGdkWindows& windows) const
637{
638 windows.push_back(m_widget->window);
639
640 wxRadioBoxButtonsInfoList::compatibility_iterator node = m_buttonsInfo.GetFirst();
641 while (node)
642 {
643 GtkWidget *button = GTK_WIDGET( node->GetData()->button );
644
645 windows.push_back(button->window);
646
647 node = node->GetNext();
648 }
649
650 return NULL;
651}
652
653void wxRadioBox::OnInternalIdle()
654{
655 wxControl::OnInternalIdle();
656
657 if ( m_lostFocus )
658 {
659 m_hasFocus = false;
660 m_lostFocus = false;
661
662 wxFocusEvent event( wxEVT_KILL_FOCUS, GetId() );
663 event.SetEventObject( this );
664
665 (void)GetEventHandler()->ProcessEvent( event );
666 }
667}
668
669// static
670wxVisualAttributes
671wxRadioBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
672{
673 wxVisualAttributes attr;
674 // NB: we need toplevel window so that GTK+ can find the right style
675 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
676 GtkWidget* widget = gtk_radio_button_new_with_label(NULL, "");
677 gtk_container_add(GTK_CONTAINER(wnd), widget);
678 attr = GetDefaultAttributesFromGTKWidget(widget);
679 gtk_widget_destroy(wnd);
680 return attr;
681}
682
683int wxRadioBox::GetItemFromPoint(const wxPoint& point) const
684{
685 const wxPoint pt = ScreenToClient(point);
686 unsigned n = 0;
687 for ( wxRadioBoxButtonsInfoList::compatibility_iterator
688 node = m_buttonsInfo.GetFirst(); node; node = node->GetNext(), n++ )
689 {
690 if ( m_buttonsInfo[n]->rect.Contains(pt) )
691 return n;
692 }
693
694 return wxNOT_FOUND;
695}
696
697#endif // wxUSE_RADIOBOX