1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
23 #include "wx/gtk1/private.h"
24 #include <gdk/gdkkeysyms.h>
26 #include "wx/gtk1/win_gtk.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern void wxapp_install_idle_handler();
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 extern bool g_blockEventsOnDrag
;
40 extern wxWindowGTK
*g_delayedFocus
;
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
47 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
49 if (g_isIdle
) wxapp_install_idle_handler();
51 if (!rb
->m_hasVMT
) return;
52 if (g_blockEventsOnDrag
) return;
54 if (!button
->active
) return;
56 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
57 event
.SetInt( rb
->GetSelection() );
58 event
.SetString( rb
->GetStringSelection() );
59 event
.SetEventObject( rb
);
60 rb
->GetEventHandler()->ProcessEvent(event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
69 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
72 wxapp_install_idle_handler();
74 if (!rb
->m_hasVMT
) return FALSE
;
75 if (g_blockEventsOnDrag
) return FALSE
;
77 if ((gdk_event
->keyval
!= GDK_Up
) &&
78 (gdk_event
->keyval
!= GDK_Down
) &&
79 (gdk_event
->keyval
!= GDK_Left
) &&
80 (gdk_event
->keyval
!= GDK_Right
))
85 wxList::compatibility_iterator node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
91 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
93 if ((gdk_event
->keyval
== GDK_Up
) ||
94 (gdk_event
->keyval
== GDK_Left
))
96 if (node
== rb
->m_boxes
.GetFirst())
97 node
= rb
->m_boxes
.GetLast();
99 node
= node
->GetPrevious();
103 if (node
== rb
->m_boxes
.GetLast())
104 node
= rb
->m_boxes
.GetFirst();
106 node
= node
->GetNext();
109 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
111 gtk_widget_grab_focus( button
);
118 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
119 GdkEvent
*WXUNUSED(event
),
122 if ( win
->m_lostFocus
)
124 // no, we didn't really lose it
125 win
->m_lostFocus
= FALSE
;
127 else if ( !win
->m_hasFocus
)
129 win
->m_hasFocus
= true;
131 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
132 event
.SetEventObject( win
);
134 // never stop the signal emission, it seems to break the kbd handling
135 // inside the radiobox
136 (void)win
->GetEventHandler()->ProcessEvent( event
);
144 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
145 GdkEvent
*WXUNUSED(event
),
148 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
149 // Replace with a warning, else we dump core a lot!
150 // if (!win->m_hasFocus)
151 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
153 // we might have lost the focus, but may be not - it may have just gone to
154 // another button in the same radiobox, so we'll check for it in the next
155 // idle iteration (leave m_hasFocus == true for now)
156 win
->m_lostFocus
= true;
162 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
168 void wxRadioBox::Init()
171 m_acceptsFocus
= true;
177 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
178 const wxString
& title
,
179 const wxPoint
&pos
, const wxSize
&size
,
180 const wxArrayString
& choices
, int majorDim
,
181 long style
, const wxValidator
& validator
,
182 const wxString
&name
)
184 wxCArrayString
chs(choices
);
186 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
187 chs
.GetStrings(), majorDim
, style
, validator
, name
);
190 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
191 const wxPoint
&pos
, const wxSize
&size
,
192 int n
, const wxString choices
[], int majorDim
,
193 long style
, const wxValidator
& validator
,
194 const wxString
&name
)
196 if (!PreCreation( parent
, pos
, size
) ||
197 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
199 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
203 m_widget
= gtk_frame_new(NULL
);
206 // majorDim may be 0 if all trailing parameters were omitted, so don't
207 // assert here but just use the correct value for it
208 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
211 unsigned int num_of_cols
= GetColumnCount();
212 unsigned int num_of_rows
= GetRowCount();
214 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
216 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
217 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
218 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
219 gtk_widget_show( table
);
220 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
223 GSList
*radio_button_group
= (GSList
*) NULL
;
224 for (int i
= 0; i
< n
; i
++)
227 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
230 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
232 if ( *pc
!= wxT('&') )
236 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
237 gtk_widget_show( GTK_WIDGET(m_radio
) );
239 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
240 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
242 m_boxes
.Append( (wxObject
*) m_radio
);
244 if (HasFlag(wxRA_SPECIFY_COLS
))
246 int left
= i%num_of_cols
;
247 int right
= (i%num_of_cols
) + 1;
248 int top
= i
/num_of_cols
;
249 int bottom
= (i
/num_of_cols
)+1;
250 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
251 GTK_FILL
, GTK_FILL
, 1, 1 );
255 int left
= i
/num_of_rows
;
256 int right
= (i
/num_of_rows
) + 1;
257 int top
= i%num_of_rows
;
258 int bottom
= (i%num_of_rows
)+1;
259 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
260 GTK_FILL
, GTK_FILL
, 1, 1 );
263 ConnectWidget( GTK_WIDGET(m_radio
) );
265 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
267 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
268 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
270 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
271 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
273 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
274 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
277 m_parent
->DoAddChild( this );
284 wxRadioBox::~wxRadioBox()
286 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
289 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
290 gtk_widget_destroy( button
);
291 node
= node
->GetNext();
295 bool wxRadioBox::Show(bool show
)
297 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
299 if (!wxControl::Show(show
))
305 if ( HasFlag(wxNO_BORDER
) )
306 gtk_widget_hide( m_widget
);
308 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
311 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
314 gtk_widget_show( button
);
316 gtk_widget_hide( button
);
318 node
= node
->GetNext();
324 void wxRadioBox::SetFocus()
326 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
328 if (m_boxes
.GetCount() == 0) return;
330 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
333 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
336 gtk_widget_grab_focus( GTK_WIDGET(button
) );
339 node
= node
->GetNext();
343 void wxRadioBox::SetSelection( int n
)
345 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
347 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
349 wxCHECK_RET( node
, wxT("radiobox wrong index") );
351 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
355 gtk_toggle_button_set_active( button
, 1 );
360 int wxRadioBox::GetSelection(void) const
362 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
366 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
369 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
370 if (button
->active
) return count
;
372 node
= node
->GetNext();
375 wxFAIL_MSG( wxT("wxRadioBox none selected") );
380 wxString
wxRadioBox::GetString(unsigned int n
) const
382 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
384 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
386 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
388 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
390 wxString
str( label
->label
);
395 void wxRadioBox::SetLabel( const wxString
& label
)
397 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
399 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
402 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
404 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
406 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
408 wxCHECK_RET( node
, wxT("radiobox wrong index") );
410 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
412 gtk_label_set( g_label
, wxGTK_CONV( label
) );
415 bool wxRadioBox::Enable( bool enable
)
417 if ( !wxControl::Enable( enable
) )
420 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
423 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
424 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
426 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
427 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
428 node
= node
->GetNext();
434 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
436 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
438 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
440 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
442 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
443 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
445 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
446 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
451 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
453 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
455 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
457 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
459 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
461 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
462 // the parent radiobox is disabled
463 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
466 bool wxRadioBox::Show(unsigned int item
, bool show
)
468 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
470 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
472 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
474 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
477 gtk_widget_show( button
);
479 gtk_widget_hide( button
);
484 bool wxRadioBox::IsItemShown(unsigned int item
) const
486 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
488 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
490 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
492 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
494 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
497 unsigned int wxRadioBox::GetCount() const
499 return m_boxes
.GetCount();
502 void wxRadioBox::GtkDisableEvents()
504 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
507 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
508 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
510 node
= node
->GetNext();
514 void wxRadioBox::GtkEnableEvents()
516 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
519 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
520 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
522 node
= node
->GetNext();
526 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
528 gtk_widget_modify_style( m_widget
, style
);
530 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
533 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
535 gtk_widget_modify_style( widget
, style
);
536 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
538 node
= node
->GetNext();
543 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
545 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
548 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
549 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
550 node
= node
->GetNext();
553 #endif // wxUSE_TOOLTIPS
555 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
557 if (window
== m_widget
->window
)
560 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
563 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
565 if (window
== button
->window
)
568 node
= node
->GetNext();
574 void wxRadioBox::OnInternalIdle()
581 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
582 event
.SetEventObject( this );
584 (void)GetEventHandler()->ProcessEvent( event
);
587 if (g_delayedFocus
== this)
589 if (GTK_WIDGET_REALIZED(m_widget
))
591 g_delayedFocus
= NULL
;
599 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
601 wxVisualAttributes attr
;
602 // NB: we need toplevel window so that GTK+ can find the right style
603 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
604 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
605 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
606 attr
= GetDefaultAttributesFromGTKWidget(widget
);
607 gtk_widget_destroy(wnd
);
611 #endif // wxUSE_RADIOBOX