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"
15 #include "wx/dialog.h"
20 #include "wx/gtk/win_gtk.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
41 if (g_isIdle
) wxapp_install_idle_handler();
43 if (!rb
->m_hasVMT
) return;
44 if (g_blockEventsOnDrag
) return;
46 if (rb
->m_alreadySent
)
48 rb
->m_alreadySent
= FALSE
;
52 rb
->m_alreadySent
= TRUE
;
54 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
55 event
.SetInt( rb
->GetSelection() );
56 event
.SetString( rb
->GetStringSelection() );
57 event
.SetEventObject( rb
);
58 rb
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
67 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
68 EVT_SIZE(wxRadioBox::OnSize
)
71 wxRadioBox::wxRadioBox()
75 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
76 const wxPoint
&pos
, const wxSize
&size
,
77 int n
, const wxString choices
[], int majorDim
,
78 long style
, const wxValidator
& validator
,
79 const wxString
&name
)
81 m_alreadySent
= FALSE
;
83 m_acceptsFocus
= TRUE
;
85 PreCreation( parent
, id
, pos
, size
, style
, name
);
87 SetValidator( validator
);
89 m_widget
= gtk_frame_new( title
.mbc_str() );
91 m_majorDim
= majorDim
;
93 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
95 GSList
*radio_button_group
= (GSList
*) NULL
;
96 for (int i
= 0; i
< n
; i
++)
98 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
100 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
].mbc_str() ) );
102 m_boxes
.Append( (wxObject
*) m_radio
);
104 ConnectWidget( GTK_WIDGET(m_radio
) );
106 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
108 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
109 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
111 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
),
113 m_x
+10, m_y
+10+(i
*24), 10, 10 );
116 wxSize ls
= LayoutItems();
118 wxSize newSize
= size
;
119 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
120 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
121 SetSize( newSize
.x
, newSize
.y
);
123 m_parent
->DoAddChild( this );
129 SetBackgroundColour( parent
->GetBackgroundColour() );
130 SetForegroundColour( parent
->GetForegroundColour() );
131 SetFont( parent
->GetFont() );
138 wxRadioBox::~wxRadioBox()
140 wxNode
*node
= m_boxes
.First();
143 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
144 gtk_widget_destroy( button
);
149 void wxRadioBox::OnSize( wxSizeEvent
&event
)
156 wxSize
wxRadioBox::LayoutItems()
161 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
165 if (m_windowStyle
& wxRA_HORIZONTAL
)
168 for (int j
= 0; j
< m_majorDim
; j
++)
173 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
174 for (int i1
= 0; i1
< num_per_major
; i1
++)
176 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
177 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
178 GdkFont
*font
= m_widget
->style
->font
;
179 int len
= 22+gdk_string_measure( font
, label
->label
);
180 if (len
> max_len
) max_len
= len
;
182 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
189 // we don't know the max_len before
191 node
= m_boxes
.Nth( j
*num_per_major
);
192 for (int i2
= 0; i2
< num_per_major
; i2
++)
194 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
196 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
202 if (y
> res
.y
) res
.y
= y
;
214 wxNode
*node
= m_boxes
.First();
217 GtkButton
*button
= GTK_BUTTON( node
->Data() );
218 GtkLabel
*label
= GTK_LABEL( button
->child
);
220 GdkFont
*font
= m_widget
->style
->font
;
221 int len
= 22+gdk_string_measure( font
, label
->label
);
222 if (len
> max
) max
= len
;
227 node
= m_boxes
.First();
230 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
232 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
244 bool wxRadioBox::Show( bool show
)
246 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
248 wxWindow::Show( show
);
250 if ((m_windowStyle
& wxNO_BORDER
) != 0)
251 gtk_widget_hide( m_widget
);
253 wxNode
*node
= m_boxes
.First();
256 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
258 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
266 int wxRadioBox::FindString( const wxString
&s
) const
268 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
272 wxNode
*node
= m_boxes
.First();
275 GtkButton
*button
= GTK_BUTTON( node
->Data() );
277 GtkLabel
*label
= GTK_LABEL( button
->child
);
278 if (s
== label
->label
) return count
;
287 void wxRadioBox::SetFocus()
289 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
291 if (m_boxes
.GetCount() == 0) return;
293 wxNode
*node
= m_boxes
.First();
296 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
299 gtk_widget_grab_focus( GTK_WIDGET(button
) );
308 void wxRadioBox::SetSelection( int n
)
310 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
312 wxNode
*node
= m_boxes
.Nth( n
);
314 wxCHECK_RET( node
, _T("radiobox wrong index") );
316 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
318 gtk_toggle_button_set_state( button
, 1 );
321 int wxRadioBox::GetSelection(void) const
323 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
327 wxNode
*node
= m_boxes
.First();
330 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
331 if (button
->active
) return count
;
336 wxFAIL_MSG( _T("wxRadioBox none selected") );
341 wxString
wxRadioBox::GetString( int n
) const
343 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
345 wxNode
*node
= m_boxes
.Nth( n
);
347 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
349 GtkButton
*button
= GTK_BUTTON( node
->Data() );
350 GtkLabel
*label
= GTK_LABEL( button
->child
);
352 return wxString( label
->label
);
355 wxString
wxRadioBox::GetLabel( int item
) const
357 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
359 return GetString( item
);
362 void wxRadioBox::SetLabel( const wxString
& label
)
364 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
366 wxControl::SetLabel( label
);
368 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
371 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
373 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
375 wxNode
*node
= m_boxes
.Nth( item
);
377 wxCHECK_RET( node
, _T("radiobox wrong index") );
379 GtkButton
*button
= GTK_BUTTON( node
->Data() );
380 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
382 gtk_label_set( g_label
, label
.mbc_str() );
385 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
387 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
390 bool wxRadioBox::Enable( bool enable
)
392 if ( !wxControl::Enable( enable
) )
395 wxNode
*node
= m_boxes
.First();
398 GtkButton
*button
= GTK_BUTTON( node
->Data() );
399 GtkWidget
*label
= button
->child
;
400 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
401 gtk_widget_set_sensitive( label
, enable
);
408 void wxRadioBox::Enable( int item
, bool enable
)
410 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
412 wxNode
*node
= m_boxes
.Nth( item
);
414 wxCHECK_RET( node
, _T("radiobox wrong index") );
416 GtkButton
*button
= GTK_BUTTON( node
->Data() );
417 GtkWidget
*label
= button
->child
;
418 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
419 gtk_widget_set_sensitive( label
, enable
);
422 void wxRadioBox::Show( int item
, bool show
)
424 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
426 wxNode
*node
= m_boxes
.Nth( item
);
428 wxCHECK_RET( node
, _T("radiobox wrong index") );
430 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
433 gtk_widget_show( button
);
435 gtk_widget_hide( button
);
438 wxString
wxRadioBox::GetStringSelection(void) const
440 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
442 wxNode
*node
= m_boxes
.First();
445 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
448 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
454 wxFAIL_MSG( _T("wxRadioBox none selected") );
458 bool wxRadioBox::SetStringSelection( const wxString
&s
)
460 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
462 int res
= FindString( s
);
463 if (res
== -1) return FALSE
;
469 int wxRadioBox::Number(void) const
471 return m_boxes
.Number();
474 int wxRadioBox::GetNumberOfRowsOrCols(void) const
479 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
481 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
484 void wxRadioBox::ApplyWidgetStyle()
488 gtk_widget_set_style( m_widget
, m_widgetStyle
);
490 wxNode
*node
= m_boxes
.First();
493 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
494 gtk_widget_set_style( widget
, m_widgetStyle
);
496 GtkButton
*button
= GTK_BUTTON( node
->Data() );
497 gtk_widget_set_style( button
->child
, m_widgetStyle
);
503 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
505 if (window
== m_widget
->window
) return TRUE
;
507 wxNode
*node
= m_boxes
.First();
510 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
512 if (window
== button
->window
) return TRUE
;