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 "wx/gtk/win_gtk.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!rb
->m_hasVMT
) return;
47 if (g_blockEventsOnDrag
) return;
49 if (rb
->m_alreadySent
)
51 rb
->m_alreadySent
= FALSE
;
55 rb
->m_alreadySent
= TRUE
;
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 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
70 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
71 EVT_SIZE(wxRadioBox::OnSize
)
74 wxRadioBox::wxRadioBox()
78 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
79 const wxPoint
&pos
, const wxSize
&size
,
80 int n
, const wxString choices
[], int majorDim
,
81 long style
, const wxValidator
& validator
,
82 const wxString
&name
)
84 m_alreadySent
= FALSE
;
86 m_acceptsFocus
= TRUE
;
88 PreCreation( parent
, id
, pos
, size
, style
, name
);
91 SetValidator( validator
);
94 m_widget
= gtk_frame_new( title
.mbc_str() );
96 m_majorDim
= majorDim
;
98 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
101 GSList
*radio_button_group
= (GSList
*) NULL
;
102 for (int i
= 0; i
< n
; i
++)
105 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
108 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
110 if ( *pc
!= _T('&') )
114 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
116 m_boxes
.Append( (wxObject
*) m_radio
);
118 ConnectWidget( GTK_WIDGET(m_radio
) );
120 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
122 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
123 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
125 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
),
127 m_x
+10, m_y
+10+(i
*24), 10, 10 );
130 wxSize ls
= LayoutItems();
132 wxSize newSize
= size
;
133 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
134 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
135 SetSize( newSize
.x
, newSize
.y
);
137 m_parent
->DoAddChild( this );
143 SetBackgroundColour( parent
->GetBackgroundColour() );
144 SetForegroundColour( parent
->GetForegroundColour() );
145 SetFont( parent
->GetFont() );
152 wxRadioBox::~wxRadioBox()
154 wxNode
*node
= m_boxes
.First();
157 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
158 gtk_widget_destroy( button
);
163 void wxRadioBox::OnSize( wxSizeEvent
&event
)
170 wxSize
wxRadioBox::LayoutItems()
175 if ( m_majorDim
== 0 )
177 // avoid dividing by 0 below
178 wxFAIL_MSG( _T("dimension of radiobox should not be 0!") );
183 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
187 if (m_windowStyle
& wxRA_HORIZONTAL
)
190 for (int j
= 0; j
< m_majorDim
; j
++)
195 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
196 for (int i1
= 0; i1
< num_per_major
; i1
++)
198 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
199 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
200 GdkFont
*font
= m_widget
->style
->font
;
201 int len
= 22+gdk_string_measure( font
, label
->label
);
202 if (len
> max_len
) max_len
= len
;
204 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
211 // we don't know the max_len before
213 node
= m_boxes
.Nth( j
*num_per_major
);
214 for (int i2
= 0; i2
< num_per_major
; i2
++)
216 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
218 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
224 if (y
> res
.y
) res
.y
= y
;
236 wxNode
*node
= m_boxes
.First();
239 GtkButton
*button
= GTK_BUTTON( node
->Data() );
240 GtkLabel
*label
= GTK_LABEL( button
->child
);
242 GdkFont
*font
= m_widget
->style
->font
;
243 int len
= 22+gdk_string_measure( font
, label
->label
);
244 if (len
> max
) max
= len
;
249 node
= m_boxes
.First();
252 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
254 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
266 bool wxRadioBox::Show( bool show
)
268 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
270 wxWindow::Show( show
);
272 if ((m_windowStyle
& wxNO_BORDER
) != 0)
273 gtk_widget_hide( m_widget
);
275 wxNode
*node
= m_boxes
.First();
278 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
280 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
288 int wxRadioBox::FindString( const wxString
&s
) const
290 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
294 wxNode
*node
= m_boxes
.First();
297 GtkButton
*button
= GTK_BUTTON( node
->Data() );
299 GtkLabel
*label
= GTK_LABEL( button
->child
);
300 if (s
== label
->label
) return count
;
309 void wxRadioBox::SetFocus()
311 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
313 if (m_boxes
.GetCount() == 0) return;
315 wxNode
*node
= m_boxes
.First();
318 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
321 gtk_widget_grab_focus( GTK_WIDGET(button
) );
330 void wxRadioBox::SetSelection( int n
)
332 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
334 wxNode
*node
= m_boxes
.Nth( n
);
336 wxCHECK_RET( node
, _T("radiobox wrong index") );
338 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
342 gtk_toggle_button_set_state( button
, 1 );
347 int wxRadioBox::GetSelection(void) const
349 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
353 wxNode
*node
= m_boxes
.First();
356 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
357 if (button
->active
) return count
;
362 wxFAIL_MSG( _T("wxRadioBox none selected") );
367 wxString
wxRadioBox::GetString( int n
) const
369 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
371 wxNode
*node
= m_boxes
.Nth( n
);
373 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
375 GtkButton
*button
= GTK_BUTTON( node
->Data() );
376 GtkLabel
*label
= GTK_LABEL( button
->child
);
378 return wxString( label
->label
);
381 wxString
wxRadioBox::GetLabel( int item
) const
383 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
385 return GetString( item
);
388 void wxRadioBox::SetLabel( const wxString
& label
)
390 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
392 wxControl::SetLabel( label
);
394 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
397 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
399 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
401 wxNode
*node
= m_boxes
.Nth( item
);
403 wxCHECK_RET( node
, _T("radiobox wrong index") );
405 GtkButton
*button
= GTK_BUTTON( node
->Data() );
406 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
408 gtk_label_set( g_label
, label
.mbc_str() );
411 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
413 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
416 bool wxRadioBox::Enable( bool enable
)
418 if ( !wxControl::Enable( enable
) )
421 wxNode
*node
= m_boxes
.First();
424 GtkButton
*button
= GTK_BUTTON( node
->Data() );
425 GtkWidget
*label
= button
->child
;
426 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
427 gtk_widget_set_sensitive( label
, enable
);
434 void wxRadioBox::Enable( int item
, bool enable
)
436 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
438 wxNode
*node
= m_boxes
.Nth( item
);
440 wxCHECK_RET( node
, _T("radiobox wrong index") );
442 GtkButton
*button
= GTK_BUTTON( node
->Data() );
443 GtkWidget
*label
= button
->child
;
444 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
445 gtk_widget_set_sensitive( label
, enable
);
448 void wxRadioBox::Show( int item
, bool show
)
450 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
452 wxNode
*node
= m_boxes
.Nth( item
);
454 wxCHECK_RET( node
, _T("radiobox wrong index") );
456 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
459 gtk_widget_show( button
);
461 gtk_widget_hide( button
);
464 wxString
wxRadioBox::GetStringSelection(void) const
466 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
468 wxNode
*node
= m_boxes
.First();
471 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
474 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
480 wxFAIL_MSG( _T("wxRadioBox none selected") );
484 bool wxRadioBox::SetStringSelection( const wxString
&s
)
486 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
488 int res
= FindString( s
);
489 if (res
== -1) return FALSE
;
495 int wxRadioBox::Number(void) const
497 return m_boxes
.Number();
500 int wxRadioBox::GetNumberOfRowsOrCols(void) const
505 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
507 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
510 void wxRadioBox::DisableEvents()
512 wxNode
*node
= m_boxes
.First();
515 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
516 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
522 void wxRadioBox::EnableEvents()
524 wxNode
*node
= m_boxes
.First();
527 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
528 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
534 void wxRadioBox::ApplyWidgetStyle()
538 gtk_widget_set_style( m_widget
, m_widgetStyle
);
540 wxNode
*node
= m_boxes
.First();
543 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
544 gtk_widget_set_style( widget
, m_widgetStyle
);
546 GtkButton
*button
= GTK_BUTTON( node
->Data() );
547 gtk_widget_set_style( button
->child
, m_widgetStyle
);
553 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
555 if (window
== m_widget
->window
) return TRUE
;
557 wxNode
*node
= m_boxes
.First();
560 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
562 if (window
== button
->window
) return TRUE
;