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"
17 #include "wx/dialog.h"
21 #include "wx/gtk/private.h"
22 #include <gdk/gdkkeysyms.h>
24 #include "wx/gtk/win_gtk.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
38 extern wxWindowGTK
*g_delayedFocus
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
45 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
47 if (g_isIdle
) wxapp_install_idle_handler();
49 if (!rb
->m_hasVMT
) return;
50 if (g_blockEventsOnDrag
) return;
52 if (!button
->active
) return;
54 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
55 event
.SetInt( rb
->GetSelection() );
56 event
.SetString( rb
->GetStringSelection() );
57 event
.SetEventObject( rb
);
58 rb
->GetEventHandler()->ProcessEvent(event
);
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
67 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
70 wxapp_install_idle_handler();
72 if (!rb
->m_hasVMT
) return FALSE
;
73 if (g_blockEventsOnDrag
) return FALSE
;
75 if ( ((gdk_event
->keyval
== GDK_Tab
) ||
76 (gdk_event
->keyval
== GDK_ISO_Left_Tab
)) &&
77 rb
->GetParent() && (rb
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
79 wxNavigationKeyEvent new_event
;
80 new_event
.SetEventObject( rb
->GetParent() );
81 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
82 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
83 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
84 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
85 new_event
.SetCurrentFocus( rb
);
86 return rb
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
89 if ((gdk_event
->keyval
!= GDK_Up
) &&
90 (gdk_event
->keyval
!= GDK_Down
) &&
91 (gdk_event
->keyval
!= GDK_Left
) &&
92 (gdk_event
->keyval
!= GDK_Right
))
97 wxList::compatibility_iterator node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
103 g_signal_stop_emission_by_name (widget
, "key_press_event");
105 if ((gdk_event
->keyval
== GDK_Up
) ||
106 (gdk_event
->keyval
== GDK_Left
))
108 if (node
== rb
->m_boxes
.GetFirst())
109 node
= rb
->m_boxes
.GetLast();
111 node
= node
->GetPrevious();
115 if (node
== rb
->m_boxes
.GetLast())
116 node
= rb
->m_boxes
.GetFirst();
118 node
= node
->GetNext();
121 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
123 gtk_widget_grab_focus( button
);
130 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
131 GdkEvent
*WXUNUSED(event
),
134 if ( win
->m_lostFocus
)
136 // no, we didn't really lose it
137 win
->m_lostFocus
= FALSE
;
139 else if ( !win
->m_hasFocus
)
141 win
->m_hasFocus
= true;
143 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
144 event
.SetEventObject( win
);
146 // never stop the signal emission, it seems to break the kbd handling
147 // inside the radiobox
148 (void)win
->GetEventHandler()->ProcessEvent( event
);
156 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
157 GdkEvent
*WXUNUSED(event
),
160 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
161 // Replace with a warning, else we dump core a lot!
162 // if (!win->m_hasFocus)
163 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
165 // we might have lost the focus, but may be not - it may have just gone to
166 // another button in the same radiobox, so we'll check for it in the next
167 // idle iteration (leave m_hasFocus == true for now)
168 win
->m_lostFocus
= true;
174 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
178 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
180 void wxRadioBox::Init()
183 m_acceptsFocus
= true;
189 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
190 const wxString
& title
,
191 const wxPoint
&pos
, const wxSize
&size
,
192 const wxArrayString
& choices
, int majorDim
,
193 long style
, const wxValidator
& validator
,
194 const wxString
&name
)
196 wxCArrayString
chs(choices
);
198 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
199 chs
.GetStrings(), majorDim
, style
, validator
, name
);
202 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
203 const wxPoint
&pos
, const wxSize
&size
,
204 int n
, const wxString choices
[], int majorDim
,
205 long style
, const wxValidator
& validator
,
206 const wxString
&name
)
208 if (!PreCreation( parent
, pos
, size
) ||
209 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
211 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
215 m_widget
= gtk_frame_new(NULL
);
218 // majorDim may be 0 if all trailing parameters were omitted, so don't
219 // assert here but just use the correct value for it
220 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
223 int num_of_cols
= GetColumnCount();
224 int num_of_rows
= GetRowCount();
226 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
228 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
229 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
230 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
231 gtk_widget_show( table
);
232 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
235 GSList
*radio_button_group
= (GSList
*) NULL
;
236 for (int i
= 0; i
< n
; i
++)
239 radio_button_group
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(m_radio
) );
242 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
244 if ( *pc
!= wxT('&') )
248 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
249 gtk_widget_show( GTK_WIDGET(m_radio
) );
251 g_signal_connect (m_radio
, "key_press_event",
252 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
254 m_boxes
.Append( (wxObject
*) m_radio
);
256 if (HasFlag(wxRA_SPECIFY_COLS
))
258 int left
= i%num_of_cols
;
259 int right
= (i%num_of_cols
) + 1;
260 int top
= i
/num_of_cols
;
261 int bottom
= (i
/num_of_cols
)+1;
262 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
263 GTK_FILL
, GTK_FILL
, 1, 1 );
267 int left
= i
/num_of_rows
;
268 int right
= (i
/num_of_rows
) + 1;
269 int top
= i%num_of_rows
;
270 int bottom
= (i%num_of_rows
)+1;
271 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
272 GTK_FILL
, GTK_FILL
, 1, 1 );
275 ConnectWidget( GTK_WIDGET(m_radio
) );
278 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
280 g_signal_connect (m_radio
, "clicked",
281 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
282 g_signal_connect (m_radio
, "focus_in_event",
283 G_CALLBACK (gtk_radiobutton_focus_in
), this);
284 g_signal_connect (m_radio
, "focus_out_event",
285 G_CALLBACK (gtk_radiobutton_focus_out
), this);
288 m_parent
->DoAddChild( this );
295 wxRadioBox::~wxRadioBox()
297 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
300 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
301 gtk_widget_destroy( button
);
302 node
= node
->GetNext();
306 bool wxRadioBox::Show( bool show
)
308 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
310 if (!wxControl::Show(show
))
316 if ( HasFlag(wxNO_BORDER
) )
317 gtk_widget_hide( m_widget
);
319 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
322 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
325 gtk_widget_show( button
);
327 gtk_widget_hide( button
);
329 node
= node
->GetNext();
335 void wxRadioBox::SetFocus()
337 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
339 if (m_boxes
.GetCount() == 0) return;
341 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
344 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
347 gtk_widget_grab_focus( GTK_WIDGET(button
) );
350 node
= node
->GetNext();
354 void wxRadioBox::SetSelection( int n
)
356 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
358 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
360 wxCHECK_RET( node
, wxT("radiobox wrong index") );
362 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
366 gtk_toggle_button_set_active( button
, 1 );
371 int wxRadioBox::GetSelection(void) const
373 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
377 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
380 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
381 if (button
->active
) return count
;
383 node
= node
->GetNext();
386 wxFAIL_MSG( wxT("wxRadioBox none selected") );
391 wxString
wxRadioBox::GetString( int n
) const
393 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
395 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
397 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
399 GtkLabel
*label
= GTK_LABEL(GTK_BIN(node
->GetData())->child
);
401 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
406 void wxRadioBox::SetLabel( const wxString
& label
)
408 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
410 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
413 void wxRadioBox::SetString( int item
, const wxString
& label
)
415 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
417 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
419 wxCHECK_RET( node
, wxT("radiobox wrong index") );
421 GtkLabel
*g_label
= GTK_LABEL(GTK_BIN(node
->GetData())->child
);
423 gtk_label_set_text( g_label
, wxGTK_CONV( label
) );
426 bool wxRadioBox::Enable( bool enable
)
428 if ( !wxControl::Enable( enable
) )
431 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
434 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
435 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
437 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
438 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
439 node
= node
->GetNext();
445 bool wxRadioBox::Enable( int item
, bool enable
)
447 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
449 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
451 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
453 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
454 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
456 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
457 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
462 bool wxRadioBox::IsItemEnabled(int item
) const
464 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
466 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
468 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
470 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
472 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
473 // the parent radiobox is disabled
474 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
477 bool wxRadioBox::Show( int item
, bool show
)
479 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
481 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
483 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
485 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
488 gtk_widget_show( button
);
490 gtk_widget_hide( button
);
495 bool wxRadioBox::IsItemShown(int item
) const
497 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
499 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
501 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
503 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
505 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
508 int wxRadioBox::GetCount() const
510 return m_boxes
.GetCount();
513 void wxRadioBox::GtkDisableEvents()
515 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
518 g_signal_handlers_disconnect_by_func (node
->GetData(),
519 (gpointer
) gtk_radiobutton_clicked_callback
,
522 node
= node
->GetNext();
526 void wxRadioBox::GtkEnableEvents()
528 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
531 g_signal_connect (node
->GetData(), "clicked",
532 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
534 node
= node
->GetNext();
538 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
540 gtk_widget_modify_style( m_widget
, style
);
541 gtk_widget_modify_style(GTK_FRAME(m_widget
)->label_widget
, style
);
543 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
546 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
548 gtk_widget_modify_style( widget
, style
);
549 gtk_widget_modify_style(GTK_BIN(widget
)->child
, style
);
551 node
= node
->GetNext();
556 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
558 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
561 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
562 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
563 node
= node
->GetNext();
566 #endif // wxUSE_TOOLTIPS
568 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
570 if (window
== m_widget
->window
)
573 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
576 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
578 if (window
== button
->window
)
581 node
= node
->GetNext();
587 void wxRadioBox::OnInternalIdle()
594 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
595 event
.SetEventObject( this );
597 (void)GetEventHandler()->ProcessEvent( event
);
600 if (g_delayedFocus
== this)
602 if (GTK_WIDGET_REALIZED(m_widget
))
604 g_delayedFocus
= NULL
;
612 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
614 wxVisualAttributes attr
;
615 // NB: we need toplevel window so that GTK+ can find the right style
616 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
617 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
618 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
619 attr
= GetDefaultAttributesFromGTKWidget(widget
);
620 gtk_widget_destroy(wnd
);
624 #endif // wxUSE_RADIOBOX