1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "radiobox.h"
14 #include "wx/radiobox.h"
18 #include "wx/dialog.h"
23 #include <gdk/gdkkeysyms.h>
25 #include "wx/gtk/win_gtk.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!rb
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 if (rb
->m_alreadySent
)
53 rb
->m_alreadySent
= FALSE
;
57 rb
->m_alreadySent
= TRUE
;
59 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
60 event
.SetInt( rb
->GetSelection() );
61 event
.SetString( rb
->GetStringSelection() );
62 event
.SetEventObject( rb
);
63 rb
->GetEventHandler()->ProcessEvent(event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
73 wxapp_install_idle_handler();
75 if (!rb
->m_hasVMT
) return FALSE
;
76 if (g_blockEventsOnDrag
) return FALSE
;
78 if ((gdk_event
->keyval
!= GDK_Up
) &&
79 (gdk_event
->keyval
!= GDK_Down
) &&
80 (gdk_event
->keyval
!= GDK_Left
) &&
81 (gdk_event
->keyval
!= GDK_Right
))
86 wxNode
*node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
92 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
94 if ((gdk_event
->keyval
== GDK_Up
) ||
95 (gdk_event
->keyval
== GDK_Left
))
97 if (node
== rb
->m_boxes
.First())
98 node
= rb
->m_boxes
.Last();
100 node
= node
->Previous();
104 if (node
== rb
->m_boxes
.Last())
105 node
= rb
->m_boxes
.First();
110 GtkWidget
*button
= (GtkWidget
*) node
->Data();
112 gtk_widget_grab_focus( button
);
117 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
118 GdkEvent
*WXUNUSED(event
),
121 if ( win
->m_lostFocus
)
123 // no, we didn't really lose it
124 win
->m_lostFocus
= FALSE
;
126 else if ( !win
->m_hasFocus
)
128 win
->m_hasFocus
= TRUE
;
130 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
131 event
.SetEventObject( win
);
133 // never stop the signal emission, it seems to break the kbd handling
134 // inside the radiobox
135 (void)win
->GetEventHandler()->ProcessEvent( event
);
141 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
142 GdkEvent
*WXUNUSED(event
),
145 wxASSERT_MSG( win
->m_hasFocus
, _T("got focus out without any focus in?") );
147 // we might have lost the focus, but may be not - it may have just gone to
148 // another button in the same radiobox, so we'll check for it in the next
149 // idle iteration (leave m_hasFocus == TRUE for now)
150 win
->m_lostFocus
= TRUE
;
155 //-----------------------------------------------------------------------------
157 //-----------------------------------------------------------------------------
159 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
161 void wxRadioBox::Init()
163 m_alreadySent
= FALSE
;
165 m_acceptsFocus
= TRUE
;
171 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
172 const wxPoint
&pos
, const wxSize
&size
,
173 int n
, const wxString choices
[], int majorDim
,
174 long style
, const wxValidator
& validator
,
175 const wxString
&name
)
177 if (!PreCreation( parent
, pos
, size
) ||
178 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
180 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
184 m_widget
= gtk_frame_new( title
.mbc_str() );
186 m_majorDim
= majorDim
;
188 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
191 GSList
*radio_button_group
= (GSList
*) NULL
;
192 for (int i
= 0; i
< n
; i
++)
195 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
198 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
200 if ( *pc
!= wxT('&') )
204 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
206 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
207 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
209 m_boxes
.Append( (wxObject
*) m_radio
);
211 ConnectWidget( GTK_WIDGET(m_radio
) );
213 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
215 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
216 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
218 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_in_event",
219 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_in
), (gpointer
)this );
221 gtk_signal_connect( GTK_OBJECT(m_radio
), "focus_out_event",
222 GTK_SIGNAL_FUNC(gtk_radiobutton_focus_out
), (gpointer
)this );
224 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
226 m_x
+10, m_y
+10+(i
*24), 10, 10 );
229 m_parent
->DoAddChild( this );
237 SetFont( parent
->GetFont() );
239 wxSize ls
= LayoutItems();
244 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget
) )->size_request
) (m_widget
, &req
);
245 if (req
.width
> ls
.x
) ls
.x
= req
.width
;
247 wxSize newSize
= size
;
248 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
249 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
250 SetSize( newSize
.x
, newSize
.y
);
252 SetBackgroundColour( parent
->GetBackgroundColour() );
253 SetForegroundColour( parent
->GetForegroundColour() );
260 wxRadioBox::~wxRadioBox()
262 wxNode
*node
= m_boxes
.First();
265 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
266 gtk_widget_destroy( button
);
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::LayoutItems()
283 if ( m_majorDim
== 0 )
285 // avoid dividing by 0 below
286 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
291 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
297 if (HasFlag(wxRA_SPECIFY_COLS
))
299 num_of_cols
= m_majorDim
;
300 num_of_rows
= num_per_major
;
304 num_of_cols
= num_per_major
;
305 num_of_rows
= m_majorDim
;
308 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
309 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
311 for (int j
= 0; j
< num_of_cols
; j
++)
316 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
317 for (int i1
= 0; i1
< num_of_rows
; i1
++)
319 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
324 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
327 if (req
.width
> max_len
) max_len
= req
.width
;
329 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
336 // we don't know the max_len before
338 node
= m_boxes
.Nth( j
*num_of_rows
);
339 for (int i2
= 0; i2
< num_of_rows
; i2
++)
341 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
343 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
349 if (y
> res
.y
) res
.y
= y
;
361 wxNode
*node
= m_boxes
.First();
364 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
369 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(button
) )->size_request
)
372 if (req
.width
> max
) max
= req
.width
;
377 node
= m_boxes
.First();
380 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
382 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
394 bool wxRadioBox::Show( bool show
)
396 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
398 if (!wxControl::Show(show
))
404 if ((m_windowStyle
& wxNO_BORDER
) != 0)
405 gtk_widget_hide( m_widget
);
407 wxNode
*node
= m_boxes
.First();
410 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
412 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
420 int wxRadioBox::FindString( const wxString
&s
) const
422 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
426 wxNode
*node
= m_boxes
.First();
429 GtkButton
*button
= GTK_BUTTON( node
->Data() );
431 GtkLabel
*label
= GTK_LABEL( button
->child
);
432 if (s
== label
->label
) return count
;
441 void wxRadioBox::SetFocus()
443 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
445 if (m_boxes
.GetCount() == 0) return;
447 wxNode
*node
= m_boxes
.First();
450 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
453 gtk_widget_grab_focus( GTK_WIDGET(button
) );
461 void wxRadioBox::SetSelection( int n
)
463 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
465 wxNode
*node
= m_boxes
.Nth( n
);
467 wxCHECK_RET( node
, wxT("radiobox wrong index") );
469 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
473 gtk_toggle_button_set_state( button
, 1 );
478 int wxRadioBox::GetSelection(void) const
480 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
484 wxNode
*node
= m_boxes
.First();
487 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
488 if (button
->active
) return count
;
493 wxFAIL_MSG( wxT("wxRadioBox none selected") );
498 wxString
wxRadioBox::GetString( int n
) const
500 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
502 wxNode
*node
= m_boxes
.Nth( n
);
504 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
506 GtkButton
*button
= GTK_BUTTON( node
->Data() );
507 GtkLabel
*label
= GTK_LABEL( button
->child
);
509 return wxString( label
->label
);
512 wxString
wxRadioBox::GetLabel( int item
) const
514 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
516 return GetString( item
);
519 void wxRadioBox::SetLabel( const wxString
& label
)
521 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
523 wxControl::SetLabel( label
);
525 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
528 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
530 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
532 wxNode
*node
= m_boxes
.Nth( item
);
534 wxCHECK_RET( node
, wxT("radiobox wrong index") );
536 GtkButton
*button
= GTK_BUTTON( node
->Data() );
537 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
539 gtk_label_set( g_label
, label
.mbc_str() );
542 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
544 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
547 bool wxRadioBox::Enable( bool enable
)
549 if ( !wxControl::Enable( enable
) )
552 wxNode
*node
= m_boxes
.First();
555 GtkButton
*button
= GTK_BUTTON( node
->Data() );
556 GtkWidget
*label
= button
->child
;
557 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
558 gtk_widget_set_sensitive( label
, enable
);
565 void wxRadioBox::Enable( int item
, bool enable
)
567 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
569 wxNode
*node
= m_boxes
.Nth( item
);
571 wxCHECK_RET( node
, wxT("radiobox wrong index") );
573 GtkButton
*button
= GTK_BUTTON( node
->Data() );
574 GtkWidget
*label
= button
->child
;
575 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
576 gtk_widget_set_sensitive( label
, enable
);
579 void wxRadioBox::Show( int item
, bool show
)
581 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
583 wxNode
*node
= m_boxes
.Nth( item
);
585 wxCHECK_RET( node
, wxT("radiobox wrong index") );
587 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
590 gtk_widget_show( button
);
592 gtk_widget_hide( button
);
595 wxString
wxRadioBox::GetStringSelection() const
597 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
599 wxNode
*node
= m_boxes
.First();
602 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
605 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
611 wxFAIL_MSG( wxT("wxRadioBox none selected") );
615 bool wxRadioBox::SetStringSelection( const wxString
&s
)
617 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
619 int res
= FindString( s
);
620 if (res
== -1) return FALSE
;
626 int wxRadioBox::Number() const
628 return m_boxes
.Number();
631 int wxRadioBox::GetNumberOfRowsOrCols() const
636 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
638 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
641 void wxRadioBox::GtkDisableEvents()
643 wxNode
*node
= m_boxes
.First();
646 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
647 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
653 void wxRadioBox::GtkEnableEvents()
655 wxNode
*node
= m_boxes
.First();
658 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
659 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
665 void wxRadioBox::ApplyWidgetStyle()
669 gtk_widget_set_style( m_widget
, m_widgetStyle
);
671 wxNode
*node
= m_boxes
.First();
674 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
675 gtk_widget_set_style( widget
, m_widgetStyle
);
677 GtkButton
*button
= GTK_BUTTON( node
->Data() );
678 gtk_widget_set_style( button
->child
, m_widgetStyle
);
685 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
687 wxNode
*node
= m_boxes
.First();
690 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
691 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
695 #endif // wxUSE_TOOLTIPS
697 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
699 if (window
== m_widget
->window
) return TRUE
;
701 wxNode
*node
= m_boxes
.First();
704 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
706 if (window
== button
->window
) return TRUE
;
714 void wxRadioBox::OnInternalIdle()
721 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
722 event
.SetEventObject( this );
724 (void)GetEventHandler()->ProcessEvent( event
);
728 #endif // wxUSE_RADIOBOX