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 #include "wx/gtk/win_gtk.h"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
37 extern wxWindowGTK
*g_delayedFocus
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
44 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!rb
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 if (!button
->active
) return;
53 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
54 event
.SetInt( rb
->GetSelection() );
55 event
.SetString( rb
->GetStringSelection() );
56 event
.SetEventObject( rb
);
57 rb
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
66 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
69 wxapp_install_idle_handler();
71 if (!rb
->m_hasVMT
) return FALSE
;
72 if (g_blockEventsOnDrag
) return FALSE
;
74 if ( ((gdk_event
->keyval
== GDK_Tab
) ||
75 (gdk_event
->keyval
== GDK_ISO_Left_Tab
)) &&
76 rb
->GetParent() && (rb
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
78 wxNavigationKeyEvent new_event
;
79 new_event
.SetEventObject( rb
->GetParent() );
80 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
81 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
82 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
83 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
84 new_event
.SetCurrentFocus( rb
);
85 return rb
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
88 if ((gdk_event
->keyval
!= GDK_Up
) &&
89 (gdk_event
->keyval
!= GDK_Down
) &&
90 (gdk_event
->keyval
!= GDK_Left
) &&
91 (gdk_event
->keyval
!= GDK_Right
))
96 wxList::compatibility_iterator node
= rb
->m_buttons
.Find( (wxObject
*) widget
);
102 g_signal_stop_emission_by_name (widget
, "key_press_event");
104 if ((gdk_event
->keyval
== GDK_Up
) ||
105 (gdk_event
->keyval
== GDK_Left
))
107 if (node
== rb
->m_buttons
.GetFirst())
108 node
= rb
->m_buttons
.GetLast();
110 node
= node
->GetPrevious();
114 if (node
== rb
->m_buttons
.GetLast())
115 node
= rb
->m_buttons
.GetFirst();
117 node
= node
->GetNext();
120 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
122 gtk_widget_grab_focus( button
);
129 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
130 GdkEvent
*WXUNUSED(event
),
133 if ( win
->m_lostFocus
)
135 // no, we didn't really lose it
136 win
->m_lostFocus
= FALSE
;
138 else if ( !win
->m_hasFocus
)
140 win
->m_hasFocus
= true;
142 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
143 event
.SetEventObject( win
);
145 // never stop the signal emission, it seems to break the kbd handling
146 // inside the radiobox
147 (void)win
->GetEventHandler()->ProcessEvent( event
);
155 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
156 GdkEvent
*WXUNUSED(event
),
159 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
160 // Replace with a warning, else we dump core a lot!
161 // if (!win->m_hasFocus)
162 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
164 // we might have lost the focus, but may be not - it may have just gone to
165 // another button in the same radiobox, so we'll check for it in the next
166 // idle iteration (leave m_hasFocus == true for now)
167 win
->m_lostFocus
= true;
173 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
177 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
179 void wxRadioBox::Init()
182 m_acceptsFocus
= true;
188 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
189 const wxString
& title
,
190 const wxPoint
&pos
, const wxSize
&size
,
191 const wxArrayString
& choices
, int majorDim
,
192 long style
, const wxValidator
& validator
,
193 const wxString
&name
)
195 wxCArrayString
chs(choices
);
197 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
198 chs
.GetStrings(), majorDim
, style
, validator
, name
);
201 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
202 const wxPoint
&pos
, const wxSize
&size
,
203 int n
, const wxString choices
[], int majorDim
,
204 long style
, const wxValidator
& validator
,
205 const wxString
&name
)
207 if (!PreCreation( parent
, pos
, size
) ||
208 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
210 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
214 m_widget
= GTKCreateFrame(title
);
215 wxControl::SetLabel(title
);
217 // majorDim may be 0 if all trailing parameters were omitted, so don't
218 // assert here but just use the correct value for it
219 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
222 unsigned int num_of_cols
= GetColumnCount();
223 unsigned int num_of_rows
= GetRowCount();
225 GtkRadioButton
*rbtn
= (GtkRadioButton
*) NULL
;
227 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
228 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
229 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
230 gtk_widget_show( table
);
231 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
234 GSList
*radio_button_group
= (GSList
*) NULL
;
235 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
238 radio_button_group
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn
) );
241 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
243 if ( *pc
!= wxT('&') )
247 rbtn
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
248 gtk_widget_show( GTK_WIDGET(rbtn
) );
250 g_signal_connect (rbtn
, "key_press_event",
251 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
253 m_buttons
.Append( (wxObject
*) rbtn
);
255 if (HasFlag(wxRA_SPECIFY_COLS
))
257 int left
= i%num_of_cols
;
258 int right
= (i%num_of_cols
) + 1;
259 int top
= i
/num_of_cols
;
260 int bottom
= (i
/num_of_cols
)+1;
261 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
262 GTK_FILL
, GTK_FILL
, 1, 1 );
266 int left
= i
/num_of_rows
;
267 int right
= (i
/num_of_rows
) + 1;
268 int top
= i%num_of_rows
;
269 int bottom
= (i%num_of_rows
)+1;
270 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
271 GTK_FILL
, GTK_FILL
, 1, 1 );
274 ConnectWidget( GTK_WIDGET(rbtn
) );
277 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn
), TRUE
);
279 g_signal_connect (rbtn
, "clicked",
280 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
281 g_signal_connect (rbtn
, "focus_in_event",
282 G_CALLBACK (gtk_radiobutton_focus_in
), this);
283 g_signal_connect (rbtn
, "focus_out_event",
284 G_CALLBACK (gtk_radiobutton_focus_out
), this);
287 m_parent
->DoAddChild( this );
294 wxRadioBox::~wxRadioBox()
296 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
299 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
300 gtk_widget_destroy( button
);
301 node
= node
->GetNext();
305 bool wxRadioBox::Show( bool show
)
307 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
309 if (!wxControl::Show(show
))
315 if ( HasFlag(wxNO_BORDER
) )
316 gtk_widget_hide( m_widget
);
318 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
321 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
324 gtk_widget_show( button
);
326 gtk_widget_hide( button
);
328 node
= node
->GetNext();
334 void wxRadioBox::SetFocus()
336 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
338 if (m_buttons
.GetCount() == 0) return;
340 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
343 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
346 gtk_widget_grab_focus( GTK_WIDGET(button
) );
349 node
= node
->GetNext();
353 void wxRadioBox::SetSelection( int n
)
355 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
357 wxList::compatibility_iterator node
= m_buttons
.Item( n
);
359 wxCHECK_RET( node
, wxT("radiobox wrong index") );
361 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
365 gtk_toggle_button_set_active( button
, 1 );
370 int wxRadioBox::GetSelection(void) const
372 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
376 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
379 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
380 if (button
->active
) return count
;
382 node
= node
->GetNext();
385 wxFAIL_MSG( wxT("wxRadioBox none selected") );
390 wxString
wxRadioBox::GetString(unsigned int n
) const
392 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
394 wxList::compatibility_iterator node
= m_buttons
.Item( n
);
396 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
398 GtkLabel
*label
= GTK_LABEL(GTK_BIN(node
->GetData())->child
);
400 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
405 void wxRadioBox::SetLabel( const wxString
& label
)
407 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
409 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
412 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
414 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
416 wxList::compatibility_iterator node
= m_buttons
.Item( item
);
418 wxCHECK_RET( node
, wxT("radiobox wrong index") );
420 GtkLabel
*g_label
= GTK_LABEL(GTK_BIN(node
->GetData())->child
);
422 gtk_label_set_text( g_label
, wxGTK_CONV( label
) );
425 bool wxRadioBox::Enable( bool enable
)
427 if ( !wxControl::Enable( enable
) )
430 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
433 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
434 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
436 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
437 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
438 node
= node
->GetNext();
444 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
446 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
448 wxList::compatibility_iterator node
= m_buttons
.Item( item
);
450 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
452 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
453 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
455 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
456 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
461 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
463 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
465 wxList::compatibility_iterator node
= m_buttons
.Item( item
);
467 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
469 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
471 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
472 // the parent radiobox is disabled
473 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
476 bool wxRadioBox::Show(unsigned int item
, bool show
)
478 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
480 wxList::compatibility_iterator node
= m_buttons
.Item( item
);
482 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
484 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
487 gtk_widget_show( button
);
489 gtk_widget_hide( button
);
494 bool wxRadioBox::IsItemShown(unsigned int item
) const
496 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
498 wxList::compatibility_iterator node
= m_buttons
.Item( item
);
500 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
502 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
504 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
507 unsigned int wxRadioBox::GetCount() const
509 return m_buttons
.GetCount();
512 void wxRadioBox::GtkDisableEvents()
514 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
517 g_signal_handlers_disconnect_by_func (node
->GetData(),
518 (gpointer
) gtk_radiobutton_clicked_callback
,
521 node
= node
->GetNext();
525 void wxRadioBox::GtkEnableEvents()
527 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
530 g_signal_connect (node
->GetData(), "clicked",
531 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
533 node
= node
->GetNext();
537 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
539 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget
), style
);
541 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
544 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
546 gtk_widget_modify_style( widget
, style
);
547 gtk_widget_modify_style(GTK_BIN(widget
)->child
, style
);
549 node
= node
->GetNext();
553 bool wxRadioBox::GTKWidgetNeedsMnemonic() const
558 void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
560 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget
), w
);
564 void wxRadioBox::ApplyToolTip(GtkTooltips
* WXUNUSED(tips
), const wxChar
*tip
)
566 // set this tooltip for all radiobuttons which don't have their own tips
568 for ( wxList::compatibility_iterator node
= m_buttons
.GetFirst();
570 node
= node
->GetNext(), n
++ )
572 if ( !GetItemToolTip(n
) )
574 wxToolTip::Apply(GTK_WIDGET(node
->GetData()),
575 wxConvCurrent
->cWX2MB(tip
));
580 void wxRadioBox::DoSetItemToolTip(unsigned int n
, wxToolTip
*tooltip
)
584 tooltip
= GetToolTip();
586 buf
= wxGTK_CONV(tooltip
->GetTip());
588 wxToolTip::Apply(GTK_WIDGET(m_buttons
[n
]), buf
);
591 #endif // wxUSE_TOOLTIPS
593 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
595 if (window
== m_widget
->window
)
598 wxList::compatibility_iterator node
= m_buttons
.GetFirst();
601 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
603 if (window
== button
->window
)
606 node
= node
->GetNext();
612 void wxRadioBox::OnInternalIdle()
619 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
620 event
.SetEventObject( this );
622 (void)GetEventHandler()->ProcessEvent( event
);
625 if (g_delayedFocus
== this)
627 if (GTK_WIDGET_REALIZED(m_widget
))
629 g_delayedFocus
= NULL
;
637 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
639 wxVisualAttributes attr
;
640 // NB: we need toplevel window so that GTK+ can find the right style
641 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
642 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
643 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
644 attr
= GetDefaultAttributesFromGTKWidget(widget
);
645 gtk_widget_destroy(wnd
);
649 #endif // wxUSE_RADIOBOX