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
);
84 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
86 // if (((m_style & wxRA_VERTICAL) == wxRA_VERTICAL) && (n > 0))
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
= 22+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
108 if (tmp
> maxLen
) maxLen
= tmp
;
110 int width
= m_width
-10;
111 if (size
.x
== -1) width
= tmp
;
112 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
120 wxSize newSize
= size
;
121 if (newSize
.x
== -1) newSize
.x
= maxLen
+10;
122 if (newSize
.y
== -1) newSize
.y
= height
;
123 SetSize( newSize
.x
, newSize
.y
);
129 SetBackgroundColour( parent
->GetBackgroundColour() );
136 wxRadioBox::~wxRadioBox(void)
138 wxNode
*node
= m_boxes
.First();
141 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
142 gtk_widget_destroy( button
);
147 void wxRadioBox::OnSize( wxSizeEvent
&event
)
149 wxControl::OnSize( event
);
154 wxNode
*node
= m_boxes
.First();
157 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
159 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
164 gtk_widget_set_usize( button
, w
, 20 );
170 bool wxRadioBox::Show( bool show
)
172 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
174 wxWindow::Show( show
);
176 wxNode
*node
= m_boxes
.First();
179 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
181 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
189 int wxRadioBox::FindString( const wxString
&s
) const
191 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
195 wxNode
*node
= m_boxes
.First();
198 GtkButton
*button
= GTK_BUTTON( node
->Data() );
200 GtkLabel
*label
= GTK_LABEL( button
->child
);
201 if (s
== label
->label
) return count
;
210 void wxRadioBox::SetSelection( int n
)
212 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
214 wxNode
*node
= m_boxes
.Nth( n
);
218 wxFAIL_MSG( "wxRadioBox wrong index" );
222 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
224 gtk_toggle_button_set_state( button
, 1 );
227 int wxRadioBox::GetSelection(void) const
229 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
233 wxNode
*node
= m_boxes
.First();
236 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
237 if (button
->active
) return count
;
242 wxFAIL_MSG( "wxRadioBox none selected" );
247 wxString
wxRadioBox::GetString( int n
) const
249 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
251 wxNode
*node
= m_boxes
.Nth( n
);
255 wxFAIL_MSG( "wxRadioBox wrong index" );
259 GtkButton
*button
= GTK_BUTTON( node
->Data() );
260 GtkLabel
*label
= GTK_LABEL( button
->child
);
262 return wxString( label
->label
);
265 wxString
wxRadioBox::GetLabel( int item
) const
267 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
269 return GetString( item
);
272 void wxRadioBox::SetLabel( const wxString
& label
)
274 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
276 wxControl::SetLabel( label
);
278 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
281 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
283 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
285 wxNode
*node
= m_boxes
.Nth( item
);
289 wxFAIL_MSG( "wxRadioBox wrong index" );
293 GtkButton
*button
= GTK_BUTTON( node
->Data() );
294 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
296 gtk_label_set( g_label
, label
);
299 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
301 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
304 void wxRadioBox::Enable( bool enable
)
306 wxControl::Enable( enable
);
308 wxNode
*node
= m_boxes
.First();
311 GtkButton
*button
= GTK_BUTTON( node
->Data() );
312 GtkWidget
*label
= button
->child
;
313 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
314 gtk_widget_set_sensitive( label
, enable
);
319 void wxRadioBox::Enable( int item
, bool enable
)
321 wxNode
*node
= m_boxes
.Nth( item
);
325 wxFAIL_MSG( "wxRadioBox wrong index" );
329 GtkButton
*button
= GTK_BUTTON( node
->Data() );
330 GtkWidget
*label
= button
->child
;
331 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
332 gtk_widget_set_sensitive( label
, enable
);
335 void wxRadioBox::Show( int item
, bool show
)
337 wxNode
*node
= m_boxes
.Nth( item
);
341 wxFAIL_MSG( "wxRadioBox wrong index" );
345 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
348 gtk_widget_show( button
);
350 gtk_widget_hide( button
);
353 wxString
wxRadioBox::GetStringSelection(void) const
355 wxNode
*node
= m_boxes
.First();
358 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
361 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
367 wxFAIL_MSG( "wxRadioBox none selected" );
371 bool wxRadioBox::SetStringSelection( const wxString
&s
)
373 int res
= FindString( s
);
374 if (res
== -1) return FALSE
;
379 int wxRadioBox::Number(void) const
381 return m_boxes
.Number();
384 int wxRadioBox::GetNumberOfRowsOrCols(void) const
389 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
391 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
394 void wxRadioBox::SetFont( const wxFont
&font
)
396 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
398 wxControl::SetFont( font
);
400 gtk_widget_set_style( m_widget
, m_widgetStyle
);
402 wxNode
*node
= m_boxes
.First();
405 GtkButton
*button
= GTK_BUTTON( node
->Data() );
407 gtk_widget_set_style( button
->child
, m_widgetStyle
);
413 void wxRadioBox::SetBackgroundColour( const wxColour
&colour
)
415 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
417 wxControl::SetBackgroundColour( colour
);
419 if (!m_backgroundColour
.Ok()) return;
421 gtk_widget_set_style( m_widget
, m_widgetStyle
);
423 wxNode
*node
= m_boxes
.First();
426 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
428 gtk_widget_set_style( button
, m_widgetStyle
);
434 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
436 if (window
== m_widget
->window
) return TRUE
;
438 wxNode
*node
= m_boxes
.First();
441 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
443 if (window
== button
->window
) return TRUE
;