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() );
331 gtk_toggle_button_set_state( button
, 1 );
334 int wxRadioBox::GetSelection(void) const
336 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
340 wxNode
*node
= m_boxes
.First();
343 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
344 if (button
->active
) return count
;
349 wxFAIL_MSG( _T("wxRadioBox none selected") );
354 wxString
wxRadioBox::GetString( int n
) const
356 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
358 wxNode
*node
= m_boxes
.Nth( n
);
360 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
362 GtkButton
*button
= GTK_BUTTON( node
->Data() );
363 GtkLabel
*label
= GTK_LABEL( button
->child
);
365 return wxString( label
->label
);
368 wxString
wxRadioBox::GetLabel( int item
) const
370 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
372 return GetString( item
);
375 void wxRadioBox::SetLabel( const wxString
& label
)
377 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
379 wxControl::SetLabel( label
);
381 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
384 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
386 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
388 wxNode
*node
= m_boxes
.Nth( item
);
390 wxCHECK_RET( node
, _T("radiobox wrong index") );
392 GtkButton
*button
= GTK_BUTTON( node
->Data() );
393 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
395 gtk_label_set( g_label
, label
.mbc_str() );
398 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
400 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
403 bool wxRadioBox::Enable( bool enable
)
405 if ( !wxControl::Enable( enable
) )
408 wxNode
*node
= m_boxes
.First();
411 GtkButton
*button
= GTK_BUTTON( node
->Data() );
412 GtkWidget
*label
= button
->child
;
413 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
414 gtk_widget_set_sensitive( label
, enable
);
421 void wxRadioBox::Enable( int item
, bool enable
)
423 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
425 wxNode
*node
= m_boxes
.Nth( item
);
427 wxCHECK_RET( node
, _T("radiobox wrong index") );
429 GtkButton
*button
= GTK_BUTTON( node
->Data() );
430 GtkWidget
*label
= button
->child
;
431 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
432 gtk_widget_set_sensitive( label
, enable
);
435 void wxRadioBox::Show( int item
, bool show
)
437 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
439 wxNode
*node
= m_boxes
.Nth( item
);
441 wxCHECK_RET( node
, _T("radiobox wrong index") );
443 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
446 gtk_widget_show( button
);
448 gtk_widget_hide( button
);
451 wxString
wxRadioBox::GetStringSelection(void) const
453 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
455 wxNode
*node
= m_boxes
.First();
458 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
461 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
467 wxFAIL_MSG( _T("wxRadioBox none selected") );
471 bool wxRadioBox::SetStringSelection( const wxString
&s
)
473 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
475 int res
= FindString( s
);
476 if (res
== -1) return FALSE
;
482 int wxRadioBox::Number(void) const
484 return m_boxes
.Number();
487 int wxRadioBox::GetNumberOfRowsOrCols(void) const
492 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
494 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
497 void wxRadioBox::ApplyWidgetStyle()
501 gtk_widget_set_style( m_widget
, m_widgetStyle
);
503 wxNode
*node
= m_boxes
.First();
506 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
507 gtk_widget_set_style( widget
, m_widgetStyle
);
509 GtkButton
*button
= GTK_BUTTON( node
->Data() );
510 gtk_widget_set_style( button
->child
, m_widgetStyle
);
516 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
518 if (window
== m_widget
->window
) return TRUE
;
520 wxNode
*node
= m_boxes
.First();
523 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
525 if (window
== button
->window
) return TRUE
;