1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "radiobox.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
19 #include "wx/radiobox.h"
21 #include "wx/dialog.h"
25 #include "wx/gtk/private.h"
26 #include <gdk/gdkkeysyms.h>
28 #include "wx/gtk/win_gtk.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern void wxapp_install_idle_handler();
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern bool g_blockEventsOnDrag
;
42 extern wxWindowGTK
*g_delayedFocus
;
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
50 if (g_isIdle
) wxapp_install_idle_handler();
52 if (!rb
->m_hasVMT
) return;
53 if (g_blockEventsOnDrag
) return;
55 if (!button
->active
) return;
57 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
58 event
.SetInt( rb
->GetSelection() );
59 event
.SetString( rb
->GetStringSelection() );
60 event
.SetEventObject( rb
);
61 rb
->GetEventHandler()->ProcessEvent(event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
71 wxapp_install_idle_handler();
73 if (!rb
->m_hasVMT
) return FALSE
;
74 if (g_blockEventsOnDrag
) return FALSE
;
76 if ((gdk_event
->keyval
!= GDK_Up
) &&
77 (gdk_event
->keyval
!= GDK_Down
) &&
78 (gdk_event
->keyval
!= GDK_Left
) &&
79 (gdk_event
->keyval
!= GDK_Right
))
84 wxList::compatibility_iterator node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
90 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
92 if ((gdk_event
->keyval
== GDK_Up
) ||
93 (gdk_event
->keyval
== GDK_Left
))
95 if (node
== rb
->m_boxes
.GetFirst())
96 node
= rb
->m_boxes
.GetLast();
98 node
= node
->GetPrevious();
102 if (node
== rb
->m_boxes
.GetLast())
103 node
= rb
->m_boxes
.GetFirst();
105 node
= node
->GetNext();
108 GtkWidget
*button
= (GtkWidget
*) node
->GetData();
110 gtk_widget_grab_focus( button
);
115 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
116 GdkEvent
*WXUNUSED(event
),
119 if ( win
->m_lostFocus
)
121 // no, we didn't really lose it
122 win
->m_lostFocus
= FALSE
;
124 else if ( !win
->m_hasFocus
)
126 win
->m_hasFocus
= true;
128 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
129 event
.SetEventObject( win
);
131 // never stop the signal emission, it seems to break the kbd handling
132 // inside the radiobox
133 (void)win
->GetEventHandler()->ProcessEvent( event
);
139 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
140 GdkEvent
*WXUNUSED(event
),
143 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
144 // Replace with a warning, else we dump core a lot!
145 // if (!win->m_hasFocus)
146 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
148 // we might have lost the focus, but may be not - it may have just gone to
149 // another button in the same radiobox, so we'll check for it in the next
150 // idle iteration (leave m_hasFocus == true for now)
151 win
->m_lostFocus
= true;
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
162 void wxRadioBox::Init()
165 m_acceptsFocus
= true;
171 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
172 const wxString
& title
,
173 const wxPoint
&pos
, const wxSize
&size
,
174 const wxArrayString
& choices
, int majorDim
,
175 long style
, const wxValidator
& validator
,
176 const wxString
&name
)
178 wxCArrayString
chs(choices
);
180 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
181 chs
.GetStrings(), majorDim
, style
, validator
, name
);
184 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
185 const wxPoint
&pos
, const wxSize
&size
,
186 int n
, const wxString choices
[], int majorDim
,
187 long style
, const wxValidator
& validator
,
188 const wxString
&name
)
190 if (!PreCreation( parent
, pos
, size
) ||
191 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
193 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
197 m_widget
= gtk_frame_new( wxGTK_CONV( title
) );
199 // majorDim may be 0 if all trailing parameters were omitted, so don't
200 // assert here but just use the correct value for it
201 m_majorDim
= majorDim
== 0 ? n
: majorDim
;
203 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
206 GSList
*radio_button_group
= (GSList
*) NULL
;
207 for (int i
= 0; i
< n
; i
++)
210 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
213 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
215 if ( *pc
!= wxT('&') )
219 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
221 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
222 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
224 m_boxes
.Append( (wxObject
*) m_radio
);
226 ConnectWidget( GTK_WIDGET(m_radio
) );
228 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
230 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
231 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
233 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
234 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
236 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
237 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
239 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
241 m_x
+10, m_y
+10+(i
*24), 10, 10 );
244 m_parent
->DoAddChild( this );
246 bool wasShown
= IsShown();
248 Hide(); // prevent PostCreation() from showing us
260 wxRadioBox::~wxRadioBox()
262 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
265 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
266 gtk_widget_destroy( button
);
267 node
= node
->GetNext();
271 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
273 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
278 wxSize
wxRadioBox::DoGetBestSize() const
280 wxSize size
= LayoutItems(true);
285 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
) (m_widget
, &req
);
286 if (req
.width
> size
.x
)
293 wxSize
wxRadioBox::LayoutItems(bool justCalc
) const
297 // avoid dividing by 0 below
298 wxCHECK_MSG( m_majorDim
, res
, wxT("dimension of radiobox should not be 0!") );
300 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
307 if (HasFlag(wxRA_SPECIFY_COLS
))
309 num_of_cols
= m_majorDim
;
310 num_of_rows
= num_per_major
;
314 num_of_cols
= num_per_major
;
315 num_of_rows
= m_majorDim
;
318 int lineheight
= GetCharHeight()+2;
320 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
321 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
323 for (int j
= 0; j
< num_of_cols
; j
++)
329 wxList::compatibility_iterator node
= m_boxes
.Item( j
*num_of_rows
);
330 for (int i1
= 0; i1
< num_of_rows
; i1
++)
332 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
337 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
340 if (req
.width
> max_len
) max_len
= req
.width
;
343 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
346 node
= node
->GetNext();
350 // we don't know the max_len before
352 node
= m_boxes
.Item( j
*num_of_rows
);
353 for (int i2
= 0; i2
< num_of_rows
; i2
++)
355 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
358 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, lineheight
);
360 node
= node
->GetNext();
364 if (y
> res
.y
) res
.y
= y
;
376 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
379 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
384 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
387 if (req
.width
> max
) max
= req
.width
;
389 node
= node
->GetNext();
392 node
= m_boxes
.GetFirst();
395 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
398 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, lineheight
);
401 node
= node
->GetNext();
410 bool wxRadioBox::Show( bool show
)
412 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
414 if (!wxControl::Show(show
))
420 if ( HasFlag(wxNO_BORDER
) )
421 gtk_widget_hide( m_widget
);
423 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
426 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
428 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
430 node
= node
->GetNext();
436 int wxRadioBox::FindString( const wxString
&find
) const
438 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
442 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
445 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
447 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
449 wxString
str( label
->label
);
456 node
= node
->GetNext();
462 void wxRadioBox::SetFocus()
464 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
466 if (m_boxes
.GetCount() == 0) return;
468 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
471 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
474 gtk_widget_grab_focus( GTK_WIDGET(button
) );
477 node
= node
->GetNext();
481 void wxRadioBox::SetSelection( int n
)
483 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
485 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
487 wxCHECK_RET( node
, wxT("radiobox wrong index") );
489 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
493 gtk_toggle_button_set_active( button
, 1 );
498 int wxRadioBox::GetSelection(void) const
500 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
504 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
507 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
508 if (button
->active
) return count
;
510 node
= node
->GetNext();
513 wxFAIL_MSG( wxT("wxRadioBox none selected") );
518 wxString
wxRadioBox::GetString( int n
) const
520 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
522 wxList::compatibility_iterator node
= m_boxes
.Item( n
);
524 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
526 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
529 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
531 wxString
str( label
->label
);
537 void wxRadioBox::SetLabel( const wxString
& label
)
539 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
541 wxControl::SetLabel( label
);
543 gtk_frame_set_label( GTK_FRAME(m_widget
), wxGTK_CONV( wxControl::GetLabel() ) );
546 void wxRadioBox::SetString( int item
, const wxString
& label
)
548 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
550 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
552 wxCHECK_RET( node
, wxT("radiobox wrong index") );
554 GtkLabel
*g_label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
556 gtk_label_set( g_label
, wxGTK_CONV( label
) );
559 bool wxRadioBox::Enable( bool enable
)
561 if ( !wxControl::Enable( enable
) )
564 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
567 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
568 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
570 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
571 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
572 node
= node
->GetNext();
578 bool wxRadioBox::Enable( int item
, bool enable
)
580 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
582 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
584 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
586 GtkButton
*button
= GTK_BUTTON( node
->GetData() );
587 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(button
) );
589 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
590 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
595 bool wxRadioBox::Show( int item
, bool show
)
597 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
599 wxList::compatibility_iterator node
= m_boxes
.Item( item
);
601 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
603 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
606 gtk_widget_show( button
);
608 gtk_widget_hide( button
);
613 wxString
wxRadioBox::GetStringSelection() const
615 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
617 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
620 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData() );
623 GtkLabel
*label
= GTK_LABEL( BUTTON_CHILD(node
->GetData()) );
626 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
628 wxString
str( label
->label
);
632 node
= node
->GetNext();
635 wxFAIL_MSG( wxT("wxRadioBox none selected") );
636 return wxEmptyString
;
639 bool wxRadioBox::SetStringSelection( const wxString
&s
)
641 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
643 int res
= FindString( s
);
644 if (res
== wxNOT_FOUND
) return false;
650 int wxRadioBox::GetCount() const
652 return m_boxes
.GetCount();
655 void wxRadioBox::GtkDisableEvents()
657 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
660 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->GetData()),
661 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
663 node
= node
->GetNext();
667 void wxRadioBox::GtkEnableEvents()
669 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
672 gtk_signal_connect( GTK_OBJECT(node
->GetData()), "clicked",
673 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
675 node
= node
->GetNext();
679 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
681 gtk_widget_modify_style( m_widget
, style
);
684 gtk_widget_modify_style(GTK_FRAME(m_widget
)->label_widget
, style
);
687 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
690 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
692 gtk_widget_modify_style( widget
, style
);
693 gtk_widget_modify_style( BUTTON_CHILD(node
->GetData()), style
);
695 node
= node
->GetNext();
700 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
702 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
705 GtkWidget
*widget
= GTK_WIDGET( node
->GetData() );
706 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
707 node
= node
->GetNext();
710 #endif // wxUSE_TOOLTIPS
712 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
714 if (window
== m_widget
->window
) return true;
716 wxList::compatibility_iterator node
= m_boxes
.GetFirst();
719 GtkWidget
*button
= GTK_WIDGET( node
->GetData() );
721 if (window
== button
->window
) return true;
723 node
= node
->GetNext();
729 void wxRadioBox::OnInternalIdle()
736 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
737 event
.SetEventObject( this );
739 (void)GetEventHandler()->ProcessEvent( event
);
742 if (g_delayedFocus
== this)
744 if (GTK_WIDGET_REALIZED(m_widget
))
746 g_delayedFocus
= NULL
;
754 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
756 wxVisualAttributes attr
;
757 // NB: we need toplevel window so that GTK+ can find the right style
758 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
759 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
760 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
761 attr
= GetDefaultAttributesFromGTKWidget(widget
);
762 gtk_widget_destroy(wnd
);
766 #if WXWIN_COMPATIBILITY_2_2
768 int wxRadioBox::Number() const
773 wxString
wxRadioBox::GetLabel(int n
) const
778 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
780 SetString(item
, label
);
783 #endif // WXWIN_COMPATIBILITY_2_2
785 #endif // wxUSE_RADIOBOX