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;
189 if (HasFlag(wxRA_SPECIFY_COLS
))
191 num_of_cols
= m_majorDim
;
192 num_of_rows
= num_per_major
;
196 num_of_cols
= num_per_major
;
197 num_of_rows
= m_majorDim
;
200 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
201 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
203 for (int j
= 0; j
< num_of_cols
; j
++)
208 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
209 for (int i1
= 0; i1
< num_of_rows
; i1
++)
211 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
212 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
213 GdkFont
*font
= m_widget
->style
->font
;
214 int len
= 22+gdk_string_measure( font
, label
->label
);
215 if (len
> max_len
) max_len
= len
;
217 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
224 // we don't know the max_len before
226 node
= m_boxes
.Nth( j
*num_of_rows
);
227 for (int i2
= 0; i2
< num_of_rows
; i2
++)
229 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
231 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
237 if (y
> res
.y
) res
.y
= y
;
249 wxNode
*node
= m_boxes
.First();
252 GtkButton
*button
= GTK_BUTTON( node
->Data() );
253 GtkLabel
*label
= GTK_LABEL( button
->child
);
255 GdkFont
*font
= m_widget
->style
->font
;
256 int len
= 22+gdk_string_measure( font
, label
->label
);
257 if (len
> max
) max
= len
;
262 node
= m_boxes
.First();
265 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
267 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
279 bool wxRadioBox::Show( bool show
)
281 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
283 wxWindow::Show( show
);
285 if ((m_windowStyle
& wxNO_BORDER
) != 0)
286 gtk_widget_hide( m_widget
);
288 wxNode
*node
= m_boxes
.First();
291 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
293 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
301 int wxRadioBox::FindString( const wxString
&s
) const
303 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
307 wxNode
*node
= m_boxes
.First();
310 GtkButton
*button
= GTK_BUTTON( node
->Data() );
312 GtkLabel
*label
= GTK_LABEL( button
->child
);
313 if (s
== label
->label
) return count
;
322 void wxRadioBox::SetFocus()
324 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
326 if (m_boxes
.GetCount() == 0) return;
328 wxNode
*node
= m_boxes
.First();
331 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
334 gtk_widget_grab_focus( GTK_WIDGET(button
) );
343 void wxRadioBox::SetSelection( int n
)
345 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
347 wxNode
*node
= m_boxes
.Nth( n
);
349 wxCHECK_RET( node
, _T("radiobox wrong index") );
351 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
355 gtk_toggle_button_set_state( button
, 1 );
360 int wxRadioBox::GetSelection(void) const
362 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
366 wxNode
*node
= m_boxes
.First();
369 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
370 if (button
->active
) return count
;
375 wxFAIL_MSG( _T("wxRadioBox none selected") );
380 wxString
wxRadioBox::GetString( int n
) const
382 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
384 wxNode
*node
= m_boxes
.Nth( n
);
386 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
388 GtkButton
*button
= GTK_BUTTON( node
->Data() );
389 GtkLabel
*label
= GTK_LABEL( button
->child
);
391 return wxString( label
->label
);
394 wxString
wxRadioBox::GetLabel( int item
) const
396 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
398 return GetString( item
);
401 void wxRadioBox::SetLabel( const wxString
& label
)
403 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
405 wxControl::SetLabel( label
);
407 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
410 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
412 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
414 wxNode
*node
= m_boxes
.Nth( item
);
416 wxCHECK_RET( node
, _T("radiobox wrong index") );
418 GtkButton
*button
= GTK_BUTTON( node
->Data() );
419 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
421 gtk_label_set( g_label
, label
.mbc_str() );
424 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
426 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
429 bool wxRadioBox::Enable( bool enable
)
431 if ( !wxControl::Enable( enable
) )
434 wxNode
*node
= m_boxes
.First();
437 GtkButton
*button
= GTK_BUTTON( node
->Data() );
438 GtkWidget
*label
= button
->child
;
439 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
440 gtk_widget_set_sensitive( label
, enable
);
447 void wxRadioBox::Enable( int item
, bool enable
)
449 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
451 wxNode
*node
= m_boxes
.Nth( item
);
453 wxCHECK_RET( node
, _T("radiobox wrong index") );
455 GtkButton
*button
= GTK_BUTTON( node
->Data() );
456 GtkWidget
*label
= button
->child
;
457 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
458 gtk_widget_set_sensitive( label
, enable
);
461 void wxRadioBox::Show( int item
, bool show
)
463 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
465 wxNode
*node
= m_boxes
.Nth( item
);
467 wxCHECK_RET( node
, _T("radiobox wrong index") );
469 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
472 gtk_widget_show( button
);
474 gtk_widget_hide( button
);
477 wxString
wxRadioBox::GetStringSelection(void) const
479 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
481 wxNode
*node
= m_boxes
.First();
484 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
487 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
493 wxFAIL_MSG( _T("wxRadioBox none selected") );
497 bool wxRadioBox::SetStringSelection( const wxString
&s
)
499 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
501 int res
= FindString( s
);
502 if (res
== -1) return FALSE
;
508 int wxRadioBox::Number(void) const
510 return m_boxes
.Number();
513 int wxRadioBox::GetNumberOfRowsOrCols(void) const
518 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
520 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
523 void wxRadioBox::DisableEvents()
525 wxNode
*node
= m_boxes
.First();
528 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
529 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
535 void wxRadioBox::EnableEvents()
537 wxNode
*node
= m_boxes
.First();
540 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
541 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
547 void wxRadioBox::ApplyWidgetStyle()
551 gtk_widget_set_style( m_widget
, m_widgetStyle
);
553 wxNode
*node
= m_boxes
.First();
556 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
557 gtk_widget_set_style( widget
, m_widgetStyle
);
559 GtkButton
*button
= GTK_BUTTON( node
->Data() );
560 gtk_widget_set_style( button
->child
, m_widgetStyle
);
566 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
568 if (window
== m_widget
->window
) return TRUE
;
570 wxNode
*node
= m_boxes
.First();
573 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
575 if (window
== button
->window
) return TRUE
;