1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "radiobox.h"
15 #include "wx/radiobox.h"
16 #include "wx/dialog.h"
18 #include "wx/gtk/win_gtk.h"
20 //-----------------------------------------------------------------------------
22 //-----------------------------------------------------------------------------
24 extern bool g_blockEventsOnDrag
;
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
32 if (!rb
->HasVMT()) return;
33 if (g_blockEventsOnDrag
) return;
35 if (rb
->m_alreadySent
)
37 rb
->m_alreadySent
= FALSE
;
41 rb
->m_alreadySent
= TRUE
;
43 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
44 event
.SetInt( rb
->GetSelection() );
45 wxString
tmp( rb
->GetStringSelection() );
46 event
.SetString( WXSTRINGCAST(tmp
) );
47 event
.SetEventObject( rb
);
48 rb
->GetEventHandler()->ProcessEvent(event
);
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
57 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
58 EVT_SIZE(wxRadioBox::OnSize
)
61 wxRadioBox::wxRadioBox(void)
65 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
66 const wxPoint
&pos
, const wxSize
&size
,
67 int n
, const wxString choices
[], int WXUNUSED(majorDim
),
68 long style
, const wxValidator
& validator
, const wxString
&name
)
70 m_alreadySent
= FALSE
;
73 PreCreation( parent
, id
, pos
, size
, style
, name
);
75 SetValidator( validator
);
77 m_widget
= gtk_frame_new( title
);
85 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
87 if (m_windowStyle
& wxRA_VERTICAL
)
89 GSList
*radio_button_group
= (GSList
*) NULL
;
90 for (int i
= 0; i
< n
; i
++)
92 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
94 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
96 m_boxes
.Append( (wxObject
*) m_radio
);
98 ConnectWidget( GTK_WIDGET(m_radio
) );
100 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
102 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
103 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
105 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
107 int tmp
= 25+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
108 if (tmp
> maxLen
) maxLen
= tmp
;
111 if (size
.x
== -1) width
= tmp
;
112 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
123 for (int i
= 0; i
< n
; i
++)
125 GdkFont
*font
= m_widget
->style
->font
;
126 int len
= 27+gdk_string_measure( font
, choices
[i
] );
127 if (len
> max
) max
= len
;
130 GSList
*radio_button_group
= (GSList
*) NULL
;
131 for (int i
= 0; i
< n
; i
++)
133 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
135 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
137 m_boxes
.Append( (wxObject
*) m_radio
);
139 ConnectWidget( GTK_WIDGET(m_radio
) );
141 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
143 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
144 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
146 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
148 gtk_widget_set_usize( GTK_WIDGET(m_radio
), max
, 20 );
157 wxSize newSize
= size
;
158 if (newSize
.x
== -1) newSize
.x
= width
;
159 if (newSize
.y
== -1) newSize
.y
= height
;
160 SetSize( newSize
.x
, newSize
.y
);
162 m_parent
->AddChild( this );
164 (m_parent
->m_insertCallback
)( m_parent
, this );
170 SetBackgroundColour( parent
->GetBackgroundColour() );
171 SetForegroundColour( parent
->GetForegroundColour() );
178 wxRadioBox::~wxRadioBox(void)
180 wxNode
*node
= m_boxes
.First();
183 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
184 gtk_widget_destroy( button
);
189 void wxRadioBox::OnSize( wxSizeEvent
&event
)
191 wxControl::OnSize( event
);
196 if (m_windowStyle
& wxRA_VERTICAL
)
198 wxNode
*node
= m_boxes
.First();
201 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
203 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
208 gtk_widget_set_usize( button
, w
, 20 );
217 wxNode
*node
= m_boxes
.First();
220 GtkButton
*button
= GTK_BUTTON( node
->Data() );
221 GtkLabel
*label
= GTK_LABEL( button
->child
);
223 GdkFont
*font
= m_widget
->style
->font
;
224 int len
= 27+gdk_string_measure( font
, label
->label
);
225 if (len
> max
) max
= len
;
230 node
= m_boxes
.First();
233 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
235 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
237 gtk_widget_set_usize( button
, max
, 20 );
244 bool wxRadioBox::Show( bool show
)
246 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
248 wxWindow::Show( show
);
250 wxNode
*node
= m_boxes
.First();
253 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
255 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
263 int wxRadioBox::FindString( const wxString
&s
) const
265 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
269 wxNode
*node
= m_boxes
.First();
272 GtkButton
*button
= GTK_BUTTON( node
->Data() );
274 GtkLabel
*label
= GTK_LABEL( button
->child
);
275 if (s
== label
->label
) return count
;
284 void wxRadioBox::SetSelection( int n
)
286 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
288 wxNode
*node
= m_boxes
.Nth( n
);
292 wxFAIL_MSG( "wxRadioBox wrong index" );
296 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
298 gtk_toggle_button_set_state( button
, 1 );
301 int wxRadioBox::GetSelection(void) const
303 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
307 wxNode
*node
= m_boxes
.First();
310 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
311 if (button
->active
) return count
;
316 wxFAIL_MSG( "wxRadioBox none selected" );
321 wxString
wxRadioBox::GetString( int n
) const
323 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
325 wxNode
*node
= m_boxes
.Nth( n
);
329 wxFAIL_MSG( "wxRadioBox wrong index" );
333 GtkButton
*button
= GTK_BUTTON( node
->Data() );
334 GtkLabel
*label
= GTK_LABEL( button
->child
);
336 return wxString( label
->label
);
339 wxString
wxRadioBox::GetLabel( int item
) const
341 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
343 return GetString( item
);
346 void wxRadioBox::SetLabel( const wxString
& label
)
348 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
350 wxControl::SetLabel( label
);
352 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
355 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
357 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
359 wxNode
*node
= m_boxes
.Nth( item
);
363 wxFAIL_MSG( "wxRadioBox wrong index" );
367 GtkButton
*button
= GTK_BUTTON( node
->Data() );
368 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
370 gtk_label_set( g_label
, label
);
373 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
375 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
378 void wxRadioBox::Enable( bool enable
)
380 wxControl::Enable( enable
);
382 wxNode
*node
= m_boxes
.First();
385 GtkButton
*button
= GTK_BUTTON( node
->Data() );
386 GtkWidget
*label
= button
->child
;
387 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
388 gtk_widget_set_sensitive( label
, enable
);
393 void wxRadioBox::Enable( int item
, bool enable
)
395 wxNode
*node
= m_boxes
.Nth( item
);
399 wxFAIL_MSG( "wxRadioBox wrong index" );
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
);
409 void wxRadioBox::Show( int item
, bool show
)
411 wxNode
*node
= m_boxes
.Nth( item
);
415 wxFAIL_MSG( "wxRadioBox wrong index" );
419 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
422 gtk_widget_show( button
);
424 gtk_widget_hide( button
);
427 wxString
wxRadioBox::GetStringSelection(void) const
429 wxNode
*node
= m_boxes
.First();
432 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
435 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
441 wxFAIL_MSG( "wxRadioBox none selected" );
445 bool wxRadioBox::SetStringSelection( const wxString
&s
)
447 int res
= FindString( s
);
448 if (res
== -1) return FALSE
;
453 int wxRadioBox::Number(void) const
455 return m_boxes
.Number();
458 int wxRadioBox::GetNumberOfRowsOrCols(void) const
463 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
465 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
468 void wxRadioBox::ApplyWidgetStyle()
472 gtk_widget_set_style( m_widget
, m_widgetStyle
);
474 wxNode
*node
= m_boxes
.First();
477 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
478 gtk_widget_set_style( widget
, m_widgetStyle
);
480 GtkButton
*button
= GTK_BUTTON( node
->Data() );
481 gtk_widget_set_style( button
->child
, m_widgetStyle
);
487 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
489 if (window
== m_widget
->window
) return TRUE
;
491 wxNode
*node
= m_boxes
.First();
494 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
496 if (window
== button
->window
) return TRUE
;