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
;
100 GSList
*radio_button_group
= (GSList
*) NULL
;
101 for (int i
= 0; i
< n
; i
++)
103 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
105 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
].mbc_str() ) );
107 m_boxes
.Append( (wxObject
*) m_radio
);
109 ConnectWidget( GTK_WIDGET(m_radio
) );
111 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
113 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
114 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
116 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
),
118 m_x
+10, m_y
+10+(i
*24), 10, 10 );
121 wxSize ls
= LayoutItems();
123 wxSize newSize
= size
;
124 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
125 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
126 SetSize( newSize
.x
, newSize
.y
);
128 m_parent
->DoAddChild( this );
134 SetBackgroundColour( parent
->GetBackgroundColour() );
135 SetForegroundColour( parent
->GetForegroundColour() );
136 SetFont( parent
->GetFont() );
143 wxRadioBox::~wxRadioBox()
145 wxNode
*node
= m_boxes
.First();
148 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
149 gtk_widget_destroy( button
);
154 void wxRadioBox::OnSize( wxSizeEvent
&event
)
161 wxSize
wxRadioBox::LayoutItems()
166 if ( m_majorDim
== 0 )
168 // avoid dividing by 0 below
169 wxFAIL_MSG( "dimension of radiobox should not be 0!" );
174 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
178 if (m_windowStyle
& wxRA_HORIZONTAL
)
181 for (int j
= 0; j
< m_majorDim
; j
++)
186 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
187 for (int i1
= 0; i1
< num_per_major
; i1
++)
189 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
190 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
191 GdkFont
*font
= m_widget
->style
->font
;
192 int len
= 22+gdk_string_measure( font
, label
->label
);
193 if (len
> max_len
) max_len
= len
;
195 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
202 // we don't know the max_len before
204 node
= m_boxes
.Nth( j
*num_per_major
);
205 for (int i2
= 0; i2
< num_per_major
; i2
++)
207 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
209 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
215 if (y
> res
.y
) res
.y
= y
;
227 wxNode
*node
= m_boxes
.First();
230 GtkButton
*button
= GTK_BUTTON( node
->Data() );
231 GtkLabel
*label
= GTK_LABEL( button
->child
);
233 GdkFont
*font
= m_widget
->style
->font
;
234 int len
= 22+gdk_string_measure( font
, label
->label
);
235 if (len
> max
) max
= len
;
240 node
= m_boxes
.First();
243 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
245 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
257 bool wxRadioBox::Show( bool show
)
259 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
261 wxWindow::Show( show
);
263 if ((m_windowStyle
& wxNO_BORDER
) != 0)
264 gtk_widget_hide( m_widget
);
266 wxNode
*node
= m_boxes
.First();
269 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
271 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
279 int wxRadioBox::FindString( const wxString
&s
) const
281 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
285 wxNode
*node
= m_boxes
.First();
288 GtkButton
*button
= GTK_BUTTON( node
->Data() );
290 GtkLabel
*label
= GTK_LABEL( button
->child
);
291 if (s
== label
->label
) return count
;
300 void wxRadioBox::SetFocus()
302 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
304 if (m_boxes
.GetCount() == 0) return;
306 wxNode
*node
= m_boxes
.First();
309 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
312 gtk_widget_grab_focus( GTK_WIDGET(button
) );
321 void wxRadioBox::SetSelection( int n
)
323 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
325 wxNode
*node
= m_boxes
.Nth( n
);
327 wxCHECK_RET( node
, _T("radiobox wrong index") );
329 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
333 gtk_toggle_button_set_state( button
, 1 );
338 int wxRadioBox::GetSelection(void) const
340 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
344 wxNode
*node
= m_boxes
.First();
347 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
348 if (button
->active
) return count
;
353 wxFAIL_MSG( _T("wxRadioBox none selected") );
358 wxString
wxRadioBox::GetString( int n
) const
360 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
362 wxNode
*node
= m_boxes
.Nth( n
);
364 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
366 GtkButton
*button
= GTK_BUTTON( node
->Data() );
367 GtkLabel
*label
= GTK_LABEL( button
->child
);
369 return wxString( label
->label
);
372 wxString
wxRadioBox::GetLabel( int item
) const
374 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
376 return GetString( item
);
379 void wxRadioBox::SetLabel( const wxString
& label
)
381 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
383 wxControl::SetLabel( label
);
385 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
388 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
390 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
392 wxNode
*node
= m_boxes
.Nth( item
);
394 wxCHECK_RET( node
, _T("radiobox wrong index") );
396 GtkButton
*button
= GTK_BUTTON( node
->Data() );
397 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
399 gtk_label_set( g_label
, label
.mbc_str() );
402 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
404 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
407 bool wxRadioBox::Enable( bool enable
)
409 if ( !wxControl::Enable( enable
) )
412 wxNode
*node
= m_boxes
.First();
415 GtkButton
*button
= GTK_BUTTON( node
->Data() );
416 GtkWidget
*label
= button
->child
;
417 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
418 gtk_widget_set_sensitive( label
, enable
);
425 void wxRadioBox::Enable( int item
, bool enable
)
427 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
429 wxNode
*node
= m_boxes
.Nth( item
);
431 wxCHECK_RET( node
, _T("radiobox wrong index") );
433 GtkButton
*button
= GTK_BUTTON( node
->Data() );
434 GtkWidget
*label
= button
->child
;
435 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
436 gtk_widget_set_sensitive( label
, enable
);
439 void wxRadioBox::Show( int item
, bool show
)
441 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
443 wxNode
*node
= m_boxes
.Nth( item
);
445 wxCHECK_RET( node
, _T("radiobox wrong index") );
447 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
450 gtk_widget_show( button
);
452 gtk_widget_hide( button
);
455 wxString
wxRadioBox::GetStringSelection(void) const
457 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
459 wxNode
*node
= m_boxes
.First();
462 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
465 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
471 wxFAIL_MSG( _T("wxRadioBox none selected") );
475 bool wxRadioBox::SetStringSelection( const wxString
&s
)
477 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
479 int res
= FindString( s
);
480 if (res
== -1) return FALSE
;
486 int wxRadioBox::Number(void) const
488 return m_boxes
.Number();
491 int wxRadioBox::GetNumberOfRowsOrCols(void) const
496 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
498 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
501 void wxRadioBox::DisableEvents()
503 wxNode
*node
= m_boxes
.First();
506 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
507 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
513 void wxRadioBox::EnableEvents()
515 wxNode
*node
= m_boxes
.First();
518 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
519 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
525 void wxRadioBox::ApplyWidgetStyle()
529 gtk_widget_set_style( m_widget
, m_widgetStyle
);
531 wxNode
*node
= m_boxes
.First();
534 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
535 gtk_widget_set_style( widget
, m_widgetStyle
);
537 GtkButton
*button
= GTK_BUTTON( node
->Data() );
538 gtk_widget_set_style( button
->child
, m_widgetStyle
);
544 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
546 if (window
== m_widget
->window
) return TRUE
;
548 wxNode
*node
= m_boxes
.First();
551 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
553 if (window
== button
->window
) return TRUE
;