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 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
170 if (m_windowStyle
& wxRA_HORIZONTAL
)
173 for (int j
= 0; j
< m_majorDim
; j
++)
178 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
179 for (int i1
= 0; i1
< num_per_major
; i1
++)
181 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
182 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
183 GdkFont
*font
= m_widget
->style
->font
;
184 int len
= 22+gdk_string_measure( font
, label
->label
);
185 if (len
> max_len
) max_len
= len
;
187 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
194 // we don't know the max_len before
196 node
= m_boxes
.Nth( j
*num_per_major
);
197 for (int i2
= 0; i2
< num_per_major
; i2
++)
199 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
201 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
207 if (y
> res
.y
) res
.y
= y
;
219 wxNode
*node
= m_boxes
.First();
222 GtkButton
*button
= GTK_BUTTON( node
->Data() );
223 GtkLabel
*label
= GTK_LABEL( button
->child
);
225 GdkFont
*font
= m_widget
->style
->font
;
226 int len
= 22+gdk_string_measure( font
, label
->label
);
227 if (len
> max
) max
= len
;
232 node
= m_boxes
.First();
235 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
237 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
249 bool wxRadioBox::Show( bool show
)
251 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
253 wxWindow::Show( show
);
255 if ((m_windowStyle
& wxNO_BORDER
) != 0)
256 gtk_widget_hide( m_widget
);
258 wxNode
*node
= m_boxes
.First();
261 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
263 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
271 int wxRadioBox::FindString( const wxString
&s
) const
273 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
277 wxNode
*node
= m_boxes
.First();
280 GtkButton
*button
= GTK_BUTTON( node
->Data() );
282 GtkLabel
*label
= GTK_LABEL( button
->child
);
283 if (s
== label
->label
) return count
;
292 void wxRadioBox::SetFocus()
294 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
296 if (m_boxes
.GetCount() == 0) return;
298 wxNode
*node
= m_boxes
.First();
301 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
304 gtk_widget_grab_focus( GTK_WIDGET(button
) );
313 void wxRadioBox::SetSelection( int n
)
315 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
317 wxNode
*node
= m_boxes
.Nth( n
);
319 wxCHECK_RET( node
, _T("radiobox wrong index") );
321 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
323 gtk_toggle_button_set_state( button
, 1 );
326 int wxRadioBox::GetSelection(void) const
328 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
332 wxNode
*node
= m_boxes
.First();
335 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
336 if (button
->active
) return count
;
341 wxFAIL_MSG( _T("wxRadioBox none selected") );
346 wxString
wxRadioBox::GetString( int n
) const
348 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
350 wxNode
*node
= m_boxes
.Nth( n
);
352 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
354 GtkButton
*button
= GTK_BUTTON( node
->Data() );
355 GtkLabel
*label
= GTK_LABEL( button
->child
);
357 return wxString( label
->label
);
360 wxString
wxRadioBox::GetLabel( int item
) const
362 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
364 return GetString( item
);
367 void wxRadioBox::SetLabel( const wxString
& label
)
369 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
371 wxControl::SetLabel( label
);
373 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
376 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
378 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
380 wxNode
*node
= m_boxes
.Nth( item
);
382 wxCHECK_RET( node
, _T("radiobox wrong index") );
384 GtkButton
*button
= GTK_BUTTON( node
->Data() );
385 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
387 gtk_label_set( g_label
, label
.mbc_str() );
390 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
392 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
395 bool wxRadioBox::Enable( bool enable
)
397 if ( !wxControl::Enable( enable
) )
400 wxNode
*node
= m_boxes
.First();
403 GtkButton
*button
= GTK_BUTTON( node
->Data() );
404 GtkWidget
*label
= button
->child
;
405 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
406 gtk_widget_set_sensitive( label
, enable
);
413 void wxRadioBox::Enable( int item
, bool enable
)
415 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
417 wxNode
*node
= m_boxes
.Nth( item
);
419 wxCHECK_RET( node
, _T("radiobox wrong index") );
421 GtkButton
*button
= GTK_BUTTON( node
->Data() );
422 GtkWidget
*label
= button
->child
;
423 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
424 gtk_widget_set_sensitive( label
, enable
);
427 void wxRadioBox::Show( int item
, bool show
)
429 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
431 wxNode
*node
= m_boxes
.Nth( item
);
433 wxCHECK_RET( node
, _T("radiobox wrong index") );
435 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
438 gtk_widget_show( button
);
440 gtk_widget_hide( button
);
443 wxString
wxRadioBox::GetStringSelection(void) const
445 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
447 wxNode
*node
= m_boxes
.First();
450 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
453 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
459 wxFAIL_MSG( _T("wxRadioBox none selected") );
463 bool wxRadioBox::SetStringSelection( const wxString
&s
)
465 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
467 int res
= FindString( s
);
468 if (res
== -1) return FALSE
;
474 int wxRadioBox::Number(void) const
476 return m_boxes
.Number();
479 int wxRadioBox::GetNumberOfRowsOrCols(void) const
484 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
486 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
489 void wxRadioBox::ApplyWidgetStyle()
493 gtk_widget_set_style( m_widget
, m_widgetStyle
);
495 wxNode
*node
= m_boxes
.First();
498 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
499 gtk_widget_set_style( widget
, m_widgetStyle
);
501 GtkButton
*button
= GTK_BUTTON( node
->Data() );
502 gtk_widget_set_style( button
->child
, m_widgetStyle
);
508 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
510 if (window
== m_widget
->window
) return TRUE
;
512 wxNode
*node
= m_boxes
.First();
515 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
517 if (window
== button
->window
) return TRUE
;