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"
18 #include "wx/tooltip.h"
21 #include "wx/gtk/private.h"
23 #include <gdk/gdkkeysyms.h>
24 #if GTK_CHECK_VERSION(3,0,0)
25 #include <gdk/gdkkeysyms-compat.h>
28 //-----------------------------------------------------------------------------
29 // wxGTKRadioButtonInfo
30 //-----------------------------------------------------------------------------
31 // structure internally used by wxRadioBox to store its child buttons
33 class wxGTKRadioButtonInfo
: public wxObject
36 wxGTKRadioButtonInfo( GtkRadioButton
* abutton
, const wxRect
& arect
)
37 : button( abutton
), rect( arect
) {}
39 GtkRadioButton
* button
;
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 #include "wx/listimpl.cpp"
48 WX_DEFINE_LIST( wxRadioBoxButtonsInfoList
)
50 extern bool g_blockEventsOnDrag
;
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
57 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
59 if (!rb
->m_hasVMT
) return;
60 if (g_blockEventsOnDrag
) return;
62 if (!gtk_toggle_button_get_active(button
)) return;
64 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
65 event
.SetInt( rb
->GetSelection() );
66 event
.SetString( rb
->GetStringSelection() );
67 event
.SetEventObject( rb
);
68 rb
->HandleWindowEvent(event
);
72 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
77 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
79 if (!rb
->m_hasVMT
) return FALSE
;
80 if (g_blockEventsOnDrag
) return FALSE
;
82 if ( ((gdk_event
->keyval
== GDK_Tab
) ||
83 (gdk_event
->keyval
== GDK_ISO_Left_Tab
)) &&
84 rb
->GetParent() && (rb
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
86 wxNavigationKeyEvent new_event
;
87 new_event
.SetEventObject( rb
->GetParent() );
88 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
89 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
90 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
91 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
92 new_event
.SetCurrentFocus( rb
);
93 return rb
->GetParent()->HandleWindowEvent(new_event
);
96 if ((gdk_event
->keyval
!= GDK_Up
) &&
97 (gdk_event
->keyval
!= GDK_Down
) &&
98 (gdk_event
->keyval
!= GDK_Left
) &&
99 (gdk_event
->keyval
!= GDK_Right
))
104 wxRadioBoxButtonsInfoList::compatibility_iterator node
= rb
->m_buttonsInfo
.GetFirst();
105 while( node
&& GTK_WIDGET( node
->GetData()->button
) != widget
)
107 node
= node
->GetNext();
114 if ((gdk_event
->keyval
== GDK_Up
) ||
115 (gdk_event
->keyval
== GDK_Left
))
117 if (node
== rb
->m_buttonsInfo
.GetFirst())
118 node
= rb
->m_buttonsInfo
.GetLast();
120 node
= node
->GetPrevious();
124 if (node
== rb
->m_buttonsInfo
.GetLast())
125 node
= rb
->m_buttonsInfo
.GetFirst();
127 node
= node
->GetNext();
130 GtkWidget
*button
= (GtkWidget
*) node
->GetData()->button
;
132 gtk_widget_grab_focus( button
);
139 static gint
gtk_radiobutton_focus_out( GtkWidget
* WXUNUSED(widget
),
140 GdkEventFocus
*WXUNUSED(event
),
143 // NB: This control is composed of several GtkRadioButton widgets and
144 // when focus changes from one of them to another in the same
145 // wxRadioBox, we get a focus-out event followed by focus-in for
146 // another GtkRadioButton owned by the same control. We don't want
147 // to generate two spurious wxEVT_SET_FOCUS events in this case,
148 // so we defer sending wx events until idle time.
149 win
->GTKHandleFocusOut();
151 // never stop the signal emission, it seems to break the kbd handling
152 // inside the radiobox
158 static gint
gtk_radiobutton_focus_in( GtkWidget
* WXUNUSED(widget
),
159 GdkEventFocus
*WXUNUSED(event
),
162 win
->GTKHandleFocusIn();
164 // never stop the signal emission, it seems to break the kbd handling
165 // inside the radiobox
171 static void gtk_radiobutton_size_allocate( GtkWidget
*widget
,
172 GtkAllocation
* alloc
,
175 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= win
->m_buttonsInfo
.GetFirst();
177 node
= node
->GetNext())
179 if (widget
== GTK_WIDGET(node
->GetData()->button
))
181 const wxPoint origin
= win
->GetPosition();
182 wxRect rect
= wxRect( alloc
->x
- origin
.x
, alloc
->y
- origin
.y
,
183 alloc
->width
, alloc
->height
);
184 node
->GetData()->rect
= rect
;
192 //-----------------------------------------------------------------------------
194 //-----------------------------------------------------------------------------
196 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
198 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
199 const wxString
& title
,
200 const wxPoint
&pos
, const wxSize
&size
,
201 const wxArrayString
& choices
, int majorDim
,
202 long style
, const wxValidator
& validator
,
203 const wxString
&name
)
205 wxCArrayString
chs(choices
);
207 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
208 chs
.GetStrings(), majorDim
, style
, validator
, name
);
211 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
212 const wxPoint
&pos
, const wxSize
&size
,
213 int n
, const wxString choices
[], int majorDim
,
214 long style
, const wxValidator
& validator
,
215 const wxString
&name
)
217 if (!PreCreation( parent
, pos
, size
) ||
218 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
220 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
224 m_widget
= GTKCreateFrame(title
);
225 g_object_ref(m_widget
);
226 wxControl::SetLabel(title
);
227 if ( HasFlag(wxNO_BORDER
) )
229 // If we don't do this here, the wxNO_BORDER style is ignored in Show()
230 gtk_frame_set_shadow_type(GTK_FRAME(m_widget
), GTK_SHADOW_NONE
);
234 // majorDim may be 0 if all trailing parameters were omitted, so don't
235 // assert here but just use the correct value for it
236 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
239 unsigned int num_of_cols
= GetColumnCount();
240 unsigned int num_of_rows
= GetRowCount();
242 GtkRadioButton
*rbtn
= NULL
;
244 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
245 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
246 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
247 gtk_widget_show( table
);
248 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
251 GSList
*radio_button_group
= NULL
;
252 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
255 radio_button_group
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn
) );
258 for ( wxString::const_iterator pc
= choices
[i
].begin();
259 pc
!= choices
[i
].end(); ++pc
)
261 if ( *pc
!= wxT('&') )
265 rbtn
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
266 gtk_widget_show( GTK_WIDGET(rbtn
) );
268 g_signal_connect (rbtn
, "key_press_event",
269 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
271 m_buttonsInfo
.Append( new wxGTKRadioButtonInfo( rbtn
, wxRect() ) );
273 if (HasFlag(wxRA_SPECIFY_COLS
))
275 int left
= i%num_of_cols
;
276 int right
= (i%num_of_cols
) + 1;
277 int top
= i
/num_of_cols
;
278 int bottom
= (i
/num_of_cols
)+1;
279 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
280 GTK_FILL
, GTK_FILL
, 1, 1 );
284 int left
= i
/num_of_rows
;
285 int right
= (i
/num_of_rows
) + 1;
286 int top
= i%num_of_rows
;
287 int bottom
= (i%num_of_rows
)+1;
288 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
289 GTK_FILL
, GTK_FILL
, 1, 1 );
292 ConnectWidget( GTK_WIDGET(rbtn
) );
295 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn
), TRUE
);
297 g_signal_connect (rbtn
, "clicked",
298 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
299 g_signal_connect (rbtn
, "focus_in_event",
300 G_CALLBACK (gtk_radiobutton_focus_in
), this);
301 g_signal_connect (rbtn
, "focus_out_event",
302 G_CALLBACK (gtk_radiobutton_focus_out
), this);
303 g_signal_connect (rbtn
, "size_allocate",
304 G_CALLBACK (gtk_radiobutton_size_allocate
), this);
307 m_parent
->DoAddChild( this );
314 wxRadioBox::~wxRadioBox()
316 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
319 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
320 gtk_widget_destroy( button
);
321 node
= node
->GetNext();
323 WX_CLEAR_LIST( wxRadioBoxButtonsInfoList
, m_buttonsInfo
);
326 bool wxRadioBox::Show( bool show
)
328 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
330 if (!wxControl::Show(show
))
336 if ( HasFlag(wxNO_BORDER
) )
337 gtk_widget_hide( m_widget
);
339 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
342 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
345 gtk_widget_show( button
);
347 gtk_widget_hide( button
);
349 node
= node
->GetNext();
355 void wxRadioBox::SetSelection( int n
)
357 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
359 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
361 wxCHECK_RET( node
, wxT("radiobox wrong index") );
363 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
367 gtk_toggle_button_set_active( button
, 1 );
372 int wxRadioBox::GetSelection(void) const
374 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
378 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
381 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
382 if (gtk_toggle_button_get_active(button
)) return count
;
384 node
= node
->GetNext();
387 wxFAIL_MSG( wxT("wxRadioBox none selected") );
392 wxString
wxRadioBox::GetString(unsigned int n
) const
394 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
396 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
398 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
400 GtkLabel
* label
= GTK_LABEL(gtk_bin_get_child(GTK_BIN(node
->GetData()->button
)));
402 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
407 void wxRadioBox::SetLabel( const wxString
& label
)
409 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
411 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
414 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
416 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
418 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
420 wxCHECK_RET( node
, wxT("radiobox wrong index") );
422 GtkLabel
* g_label
= GTK_LABEL(gtk_bin_get_child(GTK_BIN(node
->GetData()->button
)));
424 gtk_label_set_text( g_label
, wxGTK_CONV( label
) );
427 bool wxRadioBox::Enable( bool enable
)
429 if ( !wxControl::Enable( enable
) )
432 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
435 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
436 GtkLabel
*label
= GTK_LABEL(gtk_bin_get_child(GTK_BIN(button
)));
438 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
439 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
440 node
= node
->GetNext();
449 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
451 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
453 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
455 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
457 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
458 GtkLabel
*label
= GTK_LABEL(gtk_bin_get_child(GTK_BIN(button
)));
460 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
461 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
466 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
468 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
470 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
472 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
474 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
476 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
477 // the parent radiobox is disabled
478 return gtk_widget_get_sensitive(GTK_WIDGET(button
));
481 bool wxRadioBox::Show(unsigned int item
, bool show
)
483 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
485 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
487 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
489 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
492 gtk_widget_show( button
);
494 gtk_widget_hide( button
);
499 bool wxRadioBox::IsItemShown(unsigned int item
) const
501 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
503 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
505 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
507 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
509 return gtk_widget_get_visible(GTK_WIDGET(button
));
512 unsigned int wxRadioBox::GetCount() const
514 return m_buttonsInfo
.GetCount();
517 void wxRadioBox::GtkDisableEvents()
519 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
522 g_signal_handlers_block_by_func(node
->GetData()->button
,
523 (gpointer
)gtk_radiobutton_clicked_callback
, this);
525 node
= node
->GetNext();
529 void wxRadioBox::GtkEnableEvents()
531 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
534 g_signal_handlers_unblock_by_func(node
->GetData()->button
,
535 (gpointer
)gtk_radiobutton_clicked_callback
, this);
537 node
= node
->GetNext();
541 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
543 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget
), style
);
545 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
548 GtkWidget
*widget
= GTK_WIDGET( node
->GetData()->button
);
550 gtk_widget_modify_style( widget
, style
);
551 gtk_widget_modify_style(gtk_bin_get_child(GTK_BIN(widget
)), style
);
553 node
= node
->GetNext();
557 bool wxRadioBox::GTKWidgetNeedsMnemonic() const
562 void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
564 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget
), w
);
568 void wxRadioBox::GTKApplyToolTip(const char* tip
)
570 // set this tooltip for all radiobuttons which don't have their own tips
572 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
574 node
= node
->GetNext(), n
++ )
576 if ( !GetItemToolTip(n
) )
578 wxToolTip::GTKApply(GTK_WIDGET(node
->GetData()->button
), tip
);
583 void wxRadioBox::DoSetItemToolTip(unsigned int n
, wxToolTip
*tooltip
)
587 tooltip
= GetToolTip();
589 buf
= wxGTK_CONV(tooltip
->GetTip());
591 wxToolTip::GTKApply(GTK_WIDGET(m_buttonsInfo
[n
]->button
), buf
);
594 #endif // wxUSE_TOOLTIPS
596 GdkWindow
*wxRadioBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
598 windows
.push_back(gtk_widget_get_window(m_widget
));
600 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
603 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
605 // don't put NULL pointers in the 'windows' array!
606 if (gtk_widget_get_window(button
))
607 windows
.push_back(gtk_widget_get_window(button
));
609 node
= node
->GetNext();
617 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
619 wxVisualAttributes attr
;
620 // NB: we need toplevel window so that GTK+ can find the right style
621 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
622 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
623 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
624 attr
= GetDefaultAttributesFromGTKWidget(widget
);
625 gtk_widget_destroy(wnd
);
629 int wxRadioBox::GetItemFromPoint(const wxPoint
& point
) const
631 const wxPoint pt
= ScreenToClient(point
);
633 for ( wxRadioBoxButtonsInfoList::compatibility_iterator
634 node
= m_buttonsInfo
.GetFirst(); node
; node
= node
->GetNext(), n
++ )
636 if ( m_buttonsInfo
[n
]->rect
.Contains(pt
) )
643 #endif // wxUSE_RADIOBOX