1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/radiobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/radiobox.h"
20 #include "wx/dialog.h"
24 #include "wx/tooltip.h"
27 #include "wx/gtk/private.h"
28 #include <gdk/gdkkeysyms.h>
30 //-----------------------------------------------------------------------------
31 // wxGTKRadioButtonInfo
32 //-----------------------------------------------------------------------------
33 // structure internally used by wxRadioBox to store its child buttons
35 class wxGTKRadioButtonInfo
: public wxObject
38 wxGTKRadioButtonInfo( GtkRadioButton
* abutton
, const wxRect
& arect
)
39 : button( abutton
), rect( arect
) {}
41 GtkRadioButton
* button
;
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 #include "wx/listimpl.cpp"
50 WX_DEFINE_LIST( wxRadioBoxButtonsInfoList
)
52 extern bool g_blockEventsOnDrag
;
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
59 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
61 if (!rb
->m_hasVMT
) return;
62 if (g_blockEventsOnDrag
) return;
64 if (!button
->active
) return;
66 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
67 event
.SetInt( rb
->GetSelection() );
68 event
.SetString( rb
->GetStringSelection() );
69 event
.SetEventObject( rb
);
70 rb
->HandleWindowEvent(event
);
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
79 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
81 if (!rb
->m_hasVMT
) return FALSE
;
82 if (g_blockEventsOnDrag
) return FALSE
;
84 if ( ((gdk_event
->keyval
== GDK_Tab
) ||
85 (gdk_event
->keyval
== GDK_ISO_Left_Tab
)) &&
86 rb
->GetParent() && (rb
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
88 wxNavigationKeyEvent new_event
;
89 new_event
.SetEventObject( rb
->GetParent() );
90 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
91 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
92 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
93 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
94 new_event
.SetCurrentFocus( rb
);
95 return rb
->GetParent()->HandleWindowEvent(new_event
);
98 if ((gdk_event
->keyval
!= GDK_Up
) &&
99 (gdk_event
->keyval
!= GDK_Down
) &&
100 (gdk_event
->keyval
!= GDK_Left
) &&
101 (gdk_event
->keyval
!= GDK_Right
))
106 wxRadioBoxButtonsInfoList::compatibility_iterator node
= rb
->m_buttonsInfo
.GetFirst();
107 while( node
&& GTK_WIDGET( node
->GetData()->button
) != widget
)
109 node
= node
->GetNext();
116 if ((gdk_event
->keyval
== GDK_Up
) ||
117 (gdk_event
->keyval
== GDK_Left
))
119 if (node
== rb
->m_buttonsInfo
.GetFirst())
120 node
= rb
->m_buttonsInfo
.GetLast();
122 node
= node
->GetPrevious();
126 if (node
== rb
->m_buttonsInfo
.GetLast())
127 node
= rb
->m_buttonsInfo
.GetFirst();
129 node
= node
->GetNext();
132 GtkWidget
*button
= (GtkWidget
*) node
->GetData()->button
;
134 gtk_widget_grab_focus( button
);
141 static gint
gtk_radiobutton_focus_in( GtkWidget
* WXUNUSED(widget
),
142 GdkEvent
*WXUNUSED(event
),
145 if ( win
->m_lostFocus
)
147 // no, we didn't really lose it
148 win
->m_lostFocus
= FALSE
;
150 else if ( !win
->m_hasFocus
)
152 win
->m_hasFocus
= true;
154 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
155 event
.SetEventObject( win
);
157 // never stop the signal emission, it seems to break the kbd handling
158 // inside the radiobox
159 (void)win
->HandleWindowEvent( event
);
167 static gint
gtk_radiobutton_focus_out( GtkWidget
* WXUNUSED(widget
),
168 GdkEvent
*WXUNUSED(event
),
171 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
172 // Replace with a warning, else we dump core a lot!
173 // if (!win->m_hasFocus)
174 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
176 // we might have lost the focus, but may be not - it may have just gone to
177 // another button in the same radiobox, so we'll check for it in the next
178 // idle iteration (leave m_hasFocus == true for now)
179 win
->m_lostFocus
= true;
186 static void gtk_radiobutton_size_allocate( GtkWidget
*widget
,
187 GtkAllocation
* alloc
,
190 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= win
->m_buttonsInfo
.GetFirst();
192 node
= node
->GetNext())
194 if (widget
== GTK_WIDGET(node
->GetData()->button
))
196 const wxPoint origin
= win
->GetPosition();
197 wxRect rect
= wxRect( alloc
->x
- origin
.x
, alloc
->y
- origin
.y
,
198 alloc
->width
, alloc
->height
);
199 node
->GetData()->rect
= rect
;
207 //-----------------------------------------------------------------------------
209 //-----------------------------------------------------------------------------
211 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
213 void wxRadioBox::Init()
219 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
220 const wxString
& title
,
221 const wxPoint
&pos
, const wxSize
&size
,
222 const wxArrayString
& choices
, int majorDim
,
223 long style
, const wxValidator
& validator
,
224 const wxString
&name
)
226 wxCArrayString
chs(choices
);
228 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
229 chs
.GetStrings(), majorDim
, style
, validator
, name
);
232 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
233 const wxPoint
&pos
, const wxSize
&size
,
234 int n
, const wxString choices
[], int majorDim
,
235 long style
, const wxValidator
& validator
,
236 const wxString
&name
)
238 if (!PreCreation( parent
, pos
, size
) ||
239 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
241 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
245 m_widget
= GTKCreateFrame(title
);
246 wxControl::SetLabel(title
);
247 if ( HasFlag(wxNO_BORDER
) )
249 // If we don't do this here, the wxNO_BORDER style is ignored in Show()
250 gtk_frame_set_shadow_type(GTK_FRAME(m_widget
), GTK_SHADOW_NONE
);
254 // majorDim may be 0 if all trailing parameters were omitted, so don't
255 // assert here but just use the correct value for it
256 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
259 unsigned int num_of_cols
= GetColumnCount();
260 unsigned int num_of_rows
= GetRowCount();
262 GtkRadioButton
*rbtn
= (GtkRadioButton
*) NULL
;
264 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
265 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
266 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
267 gtk_widget_show( table
);
268 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
271 GSList
*radio_button_group
= (GSList
*) NULL
;
272 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
275 radio_button_group
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn
) );
278 for ( wxString::const_iterator pc
= choices
[i
].begin();
279 pc
!= choices
[i
].end(); ++pc
)
281 if ( *pc
!= wxT('&') )
285 rbtn
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
286 gtk_widget_show( GTK_WIDGET(rbtn
) );
288 g_signal_connect (rbtn
, "key_press_event",
289 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
291 m_buttonsInfo
.Append( new wxGTKRadioButtonInfo( rbtn
, wxRect() ) );
293 if (HasFlag(wxRA_SPECIFY_COLS
))
295 int left
= i%num_of_cols
;
296 int right
= (i%num_of_cols
) + 1;
297 int top
= i
/num_of_cols
;
298 int bottom
= (i
/num_of_cols
)+1;
299 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
300 GTK_FILL
, GTK_FILL
, 1, 1 );
304 int left
= i
/num_of_rows
;
305 int right
= (i
/num_of_rows
) + 1;
306 int top
= i%num_of_rows
;
307 int bottom
= (i%num_of_rows
)+1;
308 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
309 GTK_FILL
, GTK_FILL
, 1, 1 );
312 ConnectWidget( GTK_WIDGET(rbtn
) );
315 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn
), TRUE
);
317 g_signal_connect (rbtn
, "clicked",
318 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
319 g_signal_connect (rbtn
, "focus_in_event",
320 G_CALLBACK (gtk_radiobutton_focus_in
), this);
321 g_signal_connect (rbtn
, "focus_out_event",
322 G_CALLBACK (gtk_radiobutton_focus_out
), this);
323 g_signal_connect (rbtn
, "size_allocate",
324 G_CALLBACK (gtk_radiobutton_size_allocate
), this);
327 m_parent
->DoAddChild( this );
334 wxRadioBox::~wxRadioBox()
336 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
339 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
340 gtk_widget_destroy( button
);
341 node
= node
->GetNext();
343 WX_CLEAR_LIST( wxRadioBoxButtonsInfoList
, m_buttonsInfo
);
346 bool wxRadioBox::Show( bool show
)
348 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
350 if (!wxControl::Show(show
))
356 if ( HasFlag(wxNO_BORDER
) )
357 gtk_widget_hide( m_widget
);
359 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
362 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
365 gtk_widget_show( button
);
367 gtk_widget_hide( button
);
369 node
= node
->GetNext();
375 void wxRadioBox::SetFocus()
377 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
379 if (m_buttonsInfo
.GetCount() == 0) return;
381 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
384 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
387 gtk_widget_grab_focus( GTK_WIDGET(button
) );
390 node
= node
->GetNext();
394 void wxRadioBox::SetSelection( int n
)
396 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
398 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
400 wxCHECK_RET( node
, wxT("radiobox wrong index") );
402 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
406 gtk_toggle_button_set_active( button
, 1 );
411 int wxRadioBox::GetSelection(void) const
413 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
417 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
420 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
421 if (button
->active
) return count
;
423 node
= node
->GetNext();
426 wxFAIL_MSG( wxT("wxRadioBox none selected") );
431 wxString
wxRadioBox::GetString(unsigned int n
) const
433 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
435 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
437 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
439 GtkLabel
*label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
441 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
446 void wxRadioBox::SetLabel( const wxString
& label
)
448 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
450 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
453 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
455 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
457 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
459 wxCHECK_RET( node
, wxT("radiobox wrong index") );
461 GtkLabel
*g_label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
463 gtk_label_set_text( g_label
, wxGTK_CONV( label
) );
466 bool wxRadioBox::Enable( bool enable
)
468 if ( !wxControl::Enable( enable
) )
471 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
474 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
475 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
477 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
478 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
479 node
= node
->GetNext();
485 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
487 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
489 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
491 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
493 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
494 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
496 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
497 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
502 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
504 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
506 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
508 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
510 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
512 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
513 // the parent radiobox is disabled
514 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
517 bool wxRadioBox::Show(unsigned int item
, bool show
)
519 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
521 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
523 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
525 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
528 gtk_widget_show( button
);
530 gtk_widget_hide( button
);
535 bool wxRadioBox::IsItemShown(unsigned int item
) const
537 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
539 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
541 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
543 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
545 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
548 unsigned int wxRadioBox::GetCount() const
550 return m_buttonsInfo
.GetCount();
553 void wxRadioBox::GtkDisableEvents()
555 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
558 g_signal_handlers_block_by_func(node
->GetData()->button
,
559 (gpointer
)gtk_radiobutton_clicked_callback
, this);
561 node
= node
->GetNext();
565 void wxRadioBox::GtkEnableEvents()
567 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
570 g_signal_handlers_unblock_by_func(node
->GetData()->button
,
571 (gpointer
)gtk_radiobutton_clicked_callback
, this);
573 node
= node
->GetNext();
577 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
579 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget
), style
);
581 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
584 GtkWidget
*widget
= GTK_WIDGET( node
->GetData()->button
);
586 gtk_widget_modify_style( widget
, style
);
587 gtk_widget_modify_style(GTK_BIN(widget
)->child
, style
);
589 node
= node
->GetNext();
593 bool wxRadioBox::GTKWidgetNeedsMnemonic() const
598 void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
600 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget
), w
);
604 void wxRadioBox::ApplyToolTip(GtkTooltips
* WXUNUSED(tips
), const gchar
*tip
)
606 // set this tooltip for all radiobuttons which don't have their own tips
608 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
610 node
= node
->GetNext(), n
++ )
612 if ( !GetItemToolTip(n
) )
614 wxToolTip::Apply(GTK_WIDGET(node
->GetData()->button
), tip
);
619 void wxRadioBox::DoSetItemToolTip(unsigned int n
, wxToolTip
*tooltip
)
623 tooltip
= GetToolTip();
625 buf
= wxGTK_CONV(tooltip
->GetTip());
627 wxToolTip::Apply(GTK_WIDGET(m_buttonsInfo
[n
]->button
), buf
);
630 #endif // wxUSE_TOOLTIPS
632 GdkWindow
*wxRadioBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
634 windows
.push_back(m_widget
->window
);
636 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
639 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
641 windows
.push_back(button
->window
);
643 node
= node
->GetNext();
649 void wxRadioBox::OnInternalIdle()
651 wxControl::OnInternalIdle();
658 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
659 event
.SetEventObject( this );
661 (void)HandleWindowEvent( event
);
667 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
669 wxVisualAttributes attr
;
670 // NB: we need toplevel window so that GTK+ can find the right style
671 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
672 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
673 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
674 attr
= GetDefaultAttributesFromGTKWidget(widget
);
675 gtk_widget_destroy(wnd
);
679 int wxRadioBox::GetItemFromPoint(const wxPoint
& point
) const
681 const wxPoint pt
= ScreenToClient(point
);
683 for ( wxRadioBoxButtonsInfoList::compatibility_iterator
684 node
= m_buttonsInfo
.GetFirst(); node
; node
= node
->GetNext(), n
++ )
686 if ( m_buttonsInfo
[n
]->rect
.Contains(pt
) )
693 #endif // wxUSE_RADIOBOX