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
);
166 SetBackgroundColour( parent
->GetBackgroundColour() );
167 SetForegroundColour( parent
->GetForegroundColour() );
174 wxRadioBox::~wxRadioBox(void)
176 wxNode
*node
= m_boxes
.First();
179 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
180 gtk_widget_destroy( button
);
185 void wxRadioBox::OnSize( wxSizeEvent
&event
)
187 wxControl::OnSize( event
);
192 if (m_windowStyle
& wxRA_VERTICAL
)
194 wxNode
*node
= m_boxes
.First();
197 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
199 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
204 gtk_widget_set_usize( button
, w
, 20 );
213 wxNode
*node
= m_boxes
.First();
216 GtkButton
*button
= GTK_BUTTON( node
->Data() );
217 GtkLabel
*label
= GTK_LABEL( button
->child
);
219 GdkFont
*font
= m_widget
->style
->font
;
220 int len
= 27+gdk_string_measure( font
, label
->label
);
221 if (len
> max
) max
= len
;
226 node
= m_boxes
.First();
229 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
231 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
233 gtk_widget_set_usize( button
, max
, 20 );
240 bool wxRadioBox::Show( bool show
)
242 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
244 wxWindow::Show( show
);
246 wxNode
*node
= m_boxes
.First();
249 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
251 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
259 int wxRadioBox::FindString( const wxString
&s
) const
261 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
265 wxNode
*node
= m_boxes
.First();
268 GtkButton
*button
= GTK_BUTTON( node
->Data() );
270 GtkLabel
*label
= GTK_LABEL( button
->child
);
271 if (s
== label
->label
) return count
;
280 void wxRadioBox::SetSelection( int n
)
282 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
284 wxNode
*node
= m_boxes
.Nth( n
);
288 wxFAIL_MSG( "wxRadioBox wrong index" );
292 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
294 gtk_toggle_button_set_state( button
, 1 );
297 int wxRadioBox::GetSelection(void) const
299 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
303 wxNode
*node
= m_boxes
.First();
306 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
307 if (button
->active
) return count
;
312 wxFAIL_MSG( "wxRadioBox none selected" );
317 wxString
wxRadioBox::GetString( int n
) const
319 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
321 wxNode
*node
= m_boxes
.Nth( n
);
325 wxFAIL_MSG( "wxRadioBox wrong index" );
329 GtkButton
*button
= GTK_BUTTON( node
->Data() );
330 GtkLabel
*label
= GTK_LABEL( button
->child
);
332 return wxString( label
->label
);
335 wxString
wxRadioBox::GetLabel( int item
) const
337 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
339 return GetString( item
);
342 void wxRadioBox::SetLabel( const wxString
& label
)
344 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
346 wxControl::SetLabel( label
);
348 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
351 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
353 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
355 wxNode
*node
= m_boxes
.Nth( item
);
359 wxFAIL_MSG( "wxRadioBox wrong index" );
363 GtkButton
*button
= GTK_BUTTON( node
->Data() );
364 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
366 gtk_label_set( g_label
, label
);
369 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
371 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
374 void wxRadioBox::Enable( bool enable
)
376 wxControl::Enable( enable
);
378 wxNode
*node
= m_boxes
.First();
381 GtkButton
*button
= GTK_BUTTON( node
->Data() );
382 GtkWidget
*label
= button
->child
;
383 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
384 gtk_widget_set_sensitive( label
, enable
);
389 void wxRadioBox::Enable( int item
, bool enable
)
391 wxNode
*node
= m_boxes
.Nth( item
);
395 wxFAIL_MSG( "wxRadioBox wrong index" );
399 GtkButton
*button
= GTK_BUTTON( node
->Data() );
400 GtkWidget
*label
= button
->child
;
401 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
402 gtk_widget_set_sensitive( label
, enable
);
405 void wxRadioBox::Show( int item
, bool show
)
407 wxNode
*node
= m_boxes
.Nth( item
);
411 wxFAIL_MSG( "wxRadioBox wrong index" );
415 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
418 gtk_widget_show( button
);
420 gtk_widget_hide( button
);
423 wxString
wxRadioBox::GetStringSelection(void) const
425 wxNode
*node
= m_boxes
.First();
428 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
431 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
437 wxFAIL_MSG( "wxRadioBox none selected" );
441 bool wxRadioBox::SetStringSelection( const wxString
&s
)
443 int res
= FindString( s
);
444 if (res
== -1) return FALSE
;
449 int wxRadioBox::Number(void) const
451 return m_boxes
.Number();
454 int wxRadioBox::GetNumberOfRowsOrCols(void) const
459 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
461 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
464 void wxRadioBox::ApplyWidgetStyle()
468 gtk_widget_set_style( m_widget
, m_widgetStyle
);
470 wxNode
*node
= m_boxes
.First();
473 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
474 gtk_widget_set_style( widget
, m_widgetStyle
);
476 GtkButton
*button
= GTK_BUTTON( node
->Data() );
477 gtk_widget_set_style( button
->child
, m_widgetStyle
);
483 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
485 if (window
== m_widget
->window
) return TRUE
;
487 wxNode
*node
= m_boxes
.First();
490 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
492 if (window
== button
->window
) return TRUE
;