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_Up
) &&
76 (gdk_event
->keyval
!= GDK_Down
) &&
77 (gdk_event
->keyval
!= GDK_Left
) &&
78 (gdk_event
->keyval
!= GDK_Right
))
83 wxList::compatibility_iterator node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
89 g_signal_stop_emission_by_name (widget
, "key_press_event");
91 if ((gdk_event
->keyval
== GDK_Up
) ||
92 (gdk_event
->keyval
== GDK_Left
))
94 if (node
== rb
->m_boxes
.GetFirst())
95 node
= rb
->m_boxes
.GetLast();
97 node
= node
->GetPrevious();
101 if (node
== rb
->m_boxes
.GetLast())
102 node
= rb
->m_boxes
.GetFirst();
104 node
= node
->GetNext();
107 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
109 gtk_widget_grab_focus( button
);
116 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
117 GdkEvent
*WXUNUSED(event
),
120 if ( win
->m_lostFocus
)
122 // no, we didn't really lose it
123 win
->m_lostFocus
= FALSE
;
125 else if ( !win
->m_hasFocus
)
127 win
->m_hasFocus
= true;
129 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
130 event
.SetEventObject( win
);
132 // never stop the signal emission, it seems to break the kbd handling
133 // inside the radiobox
134 (void)win
->GetEventHandler()->ProcessEvent( event
);
142 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
143 GdkEvent
*WXUNUSED(event
),
146 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
147 // Replace with a warning, else we dump core a lot!
148 // if (!win->m_hasFocus)
149 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
151 // we might have lost the focus, but may be not - it may have just gone to
152 // another button in the same radiobox, so we'll check for it in the next
153 // idle iteration (leave m_hasFocus == true for now)
154 win
->m_lostFocus
= true;
160 //-----------------------------------------------------------------------------
162 //-----------------------------------------------------------------------------
164 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
166 void wxRadioBox::Init()
169 m_acceptsFocus
= true;
175 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
176 const wxString
& title
,
177 const wxPoint
&pos
, const wxSize
&size
,
178 const wxArrayString
& choices
, int majorDim
,
179 long style
, const wxValidator
& validator
,
180 const wxString
&name
)
182 wxCArrayString
chs(choices
);
184 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
185 chs
.GetStrings(), majorDim
, style
, validator
, name
);
188 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
189 const wxPoint
&pos
, const wxSize
&size
,
190 int n
, const wxString choices
[], int majorDim
,
191 long style
, const wxValidator
& validator
,
192 const wxString
&name
)
194 if (!PreCreation( parent
, pos
, size
) ||
195 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
197 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
201 m_widget
= gtk_frame_new(NULL
);
204 // majorDim may be 0 if all trailing parameters were omitted, so don't
205 // assert here but just use the correct value for it
206 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
209 int num_of_cols
= GetColumnCount();
210 int num_of_rows
= GetRowCount();
212 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
214 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
215 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
216 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
217 gtk_widget_show( table
);
218 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
221 GSList
*radio_button_group
= (GSList
*) NULL
;
222 for (int i
= 0; i
< n
; i
++)
225 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
228 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
230 if ( *pc
!= wxT('&') )
234 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
235 gtk_widget_show( GTK_WIDGET(m_radio
) );
237 g_signal_connect (m_radio
, "key_press_event",
238 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
240 m_boxes
.Append( (wxObject
*) m_radio
);
242 if (HasFlag(wxRA_SPECIFY_COLS
))
244 int left
= i%num_of_cols
;
245 int right
= (i%num_of_cols
) + 1;
246 int top
= i
/num_of_cols
;
247 int bottom
= (i
/num_of_cols
)+1;
248 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
249 GTK_FILL
, GTK_FILL
, 1, 1 );
253 int left
= i
/num_of_rows
;
254 int right
= (i
/num_of_rows
) + 1;
255 int top
= i%num_of_rows
;
256 int bottom
= (i%num_of_rows
)+1;
257 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(m_radio
), left
, right
, top
, bottom
,
258 GTK_FILL
, GTK_FILL
, 1, 1 );
261 ConnectWidget( GTK_WIDGET(m_radio
) );
263 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
265 g_signal_connect (m_radio
, "clicked",
266 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
267 g_signal_connect (m_radio
, "focus_in_event",
268 G_CALLBACK (gtk_radiobutton_focus_in
), this);
269 g_signal_connect (m_radio
, "focus_out_event",
270 G_CALLBACK (gtk_radiobutton_focus_out
), this);
273 m_parent
->DoAddChild( this );
280 wxRadioBox::~wxRadioBox()
282 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
285 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
286 gtk_widget_destroy( button
);
287 node
= node
->GetNext();
291 bool wxRadioBox::Show( bool show
)
293 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
295 if (!wxControl::Show(show
))
301 if ( HasFlag(wxNO_BORDER
) )
302 gtk_widget_hide( m_widget
);
304 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
307 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
310 gtk_widget_show( button
);
312 gtk_widget_hide( button
);
314 node
= node
->GetNext();
320 void wxRadioBox::SetFocus()
322 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
324 if (m_boxes
.GetCount() == 0) return;
326 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
329 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
332 gtk_widget_grab_focus( GTK_WIDGET(button
) );
335 node
= node
->GetNext();
339 void wxRadioBox::SetSelection( int n
)
341 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
343 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
345 wxCHECK_RET( node
, wxT("radiobox wrong index") );
347 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
351 gtk_toggle_button_set_active( button
, 1 );
356 int wxRadioBox::GetSelection(void) const
358 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
362 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
365 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
366 if (button
->active
) return count
;
368 node
= node
->GetNext();
371 wxFAIL_MSG( wxT("wxRadioBox none selected") );
376 wxString
wxRadioBox::GetString( int n
) const
378 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
380 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
382 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
384 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
386 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
391 void wxRadioBox::SetLabel( const wxString
& label
)
393 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
395 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
398 void wxRadioBox::SetString( int item
, const wxString
& label
)
400 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
402 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
404 wxCHECK_RET( node
, wxT("radiobox wrong index") );
406 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
408 gtk_label_set( g_label
, wxGTK_CONV( label
) );
411 bool wxRadioBox::Enable( bool enable
)
413 if ( !wxControl::Enable( enable
) )
416 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
419 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
420 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
422 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
423 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
424 node
= node
->GetNext();
430 bool wxRadioBox::Enable( int item
, bool enable
)
432 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
434 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
436 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
438 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
439 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
441 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
442 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
447 bool wxRadioBox::IsItemEnabled(int item
) const
449 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
451 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
453 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
455 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
457 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
458 // the parent radiobox is disabled
459 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
462 bool wxRadioBox::Show( int item
, bool show
)
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 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
473 gtk_widget_show( button
);
475 gtk_widget_hide( button
);
480 bool wxRadioBox::IsItemShown(int item
) const
482 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
484 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
486 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
488 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
490 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
493 int wxRadioBox::GetCount() const
495 return m_boxes
.GetCount();
498 void wxRadioBox::GtkDisableEvents()
500 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
503 g_signal_handlers_disconnect_by_func (node
->GetData(),
504 (gpointer
) gtk_radiobutton_clicked_callback
,
507 node
= node
->GetNext();
511 void wxRadioBox::GtkEnableEvents()
513 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
516 g_signal_connect (node
->GetData(), "clicked",
517 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
519 node
= node
->GetNext();
523 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
525 gtk_widget_modify_style( m_widget
, style
);
526 gtk_widget_modify_style(GTK_FRAME(m_widget
)->label_widget
, style
);
528 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
531 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
533 gtk_widget_modify_style( widget
, style
);
534 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
536 node
= node
->GetNext();
541 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
543 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
546 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
547 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
548 node
= node
->GetNext();
551 #endif // wxUSE_TOOLTIPS
553 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
555 if (window
== m_widget
->window
)
558 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
561 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
563 if (window
== button
->window
)
566 node
= node
->GetNext();
572 void wxRadioBox::OnInternalIdle()
579 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
580 event
.SetEventObject( this );
582 (void)GetEventHandler()->ProcessEvent( event
);
585 if (g_delayedFocus
== this)
587 if (GTK_WIDGET_REALIZED(m_widget
))
589 g_delayedFocus
= NULL
;
597 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
599 wxVisualAttributes attr
;
600 // NB: we need toplevel window so that GTK+ can find the right style
601 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
602 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
603 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
604 attr
= GetDefaultAttributesFromGTKWidget(widget
);
605 gtk_widget_destroy(wnd
);
609 #endif // wxUSE_RADIOBOX