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
= 22+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() );
173 wxRadioBox::~wxRadioBox(void)
175 wxNode
*node
= m_boxes
.First();
178 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
179 gtk_widget_destroy( button
);
184 void wxRadioBox::OnSize( wxSizeEvent
&event
)
186 wxControl::OnSize( event
);
191 if (m_windowStyle
& wxRA_VERTICAL
)
193 wxNode
*node
= m_boxes
.First();
196 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
198 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
203 gtk_widget_set_usize( button
, w
, 20 );
212 wxNode
*node
= m_boxes
.First();
215 GtkButton
*button
= GTK_BUTTON( node
->Data() );
216 GtkLabel
*label
= GTK_LABEL( button
->child
);
218 GdkFont
*font
= m_widget
->style
->font
;
219 int len
= 27+gdk_string_measure( font
, label
->label
);
220 if (len
> max
) max
= len
;
225 node
= m_boxes
.First();
228 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
230 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
232 gtk_widget_set_usize( button
, max
, 20 );
239 bool wxRadioBox::Show( bool show
)
241 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
243 wxWindow::Show( show
);
245 wxNode
*node
= m_boxes
.First();
248 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
250 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
258 int wxRadioBox::FindString( const wxString
&s
) const
260 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
264 wxNode
*node
= m_boxes
.First();
267 GtkButton
*button
= GTK_BUTTON( node
->Data() );
269 GtkLabel
*label
= GTK_LABEL( button
->child
);
270 if (s
== label
->label
) return count
;
279 void wxRadioBox::SetSelection( int n
)
281 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
283 wxNode
*node
= m_boxes
.Nth( n
);
287 wxFAIL_MSG( "wxRadioBox wrong index" );
291 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
293 gtk_toggle_button_set_state( button
, 1 );
296 int wxRadioBox::GetSelection(void) const
298 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
302 wxNode
*node
= m_boxes
.First();
305 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
306 if (button
->active
) return count
;
311 wxFAIL_MSG( "wxRadioBox none selected" );
316 wxString
wxRadioBox::GetString( int n
) const
318 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
320 wxNode
*node
= m_boxes
.Nth( n
);
324 wxFAIL_MSG( "wxRadioBox wrong index" );
328 GtkButton
*button
= GTK_BUTTON( node
->Data() );
329 GtkLabel
*label
= GTK_LABEL( button
->child
);
331 return wxString( label
->label
);
334 wxString
wxRadioBox::GetLabel( int item
) const
336 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
338 return GetString( item
);
341 void wxRadioBox::SetLabel( const wxString
& label
)
343 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
345 wxControl::SetLabel( label
);
347 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
350 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
352 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
354 wxNode
*node
= m_boxes
.Nth( item
);
358 wxFAIL_MSG( "wxRadioBox wrong index" );
362 GtkButton
*button
= GTK_BUTTON( node
->Data() );
363 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
365 gtk_label_set( g_label
, label
);
368 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
370 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
373 void wxRadioBox::Enable( bool enable
)
375 wxControl::Enable( enable
);
377 wxNode
*node
= m_boxes
.First();
380 GtkButton
*button
= GTK_BUTTON( node
->Data() );
381 GtkWidget
*label
= button
->child
;
382 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
383 gtk_widget_set_sensitive( label
, enable
);
388 void wxRadioBox::Enable( int item
, bool enable
)
390 wxNode
*node
= m_boxes
.Nth( item
);
394 wxFAIL_MSG( "wxRadioBox wrong index" );
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
);
404 void wxRadioBox::Show( int item
, bool show
)
406 wxNode
*node
= m_boxes
.Nth( item
);
410 wxFAIL_MSG( "wxRadioBox wrong index" );
414 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
417 gtk_widget_show( button
);
419 gtk_widget_hide( button
);
422 wxString
wxRadioBox::GetStringSelection(void) const
424 wxNode
*node
= m_boxes
.First();
427 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
430 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
436 wxFAIL_MSG( "wxRadioBox none selected" );
440 bool wxRadioBox::SetStringSelection( const wxString
&s
)
442 int res
= FindString( s
);
443 if (res
== -1) return FALSE
;
448 int wxRadioBox::Number(void) const
450 return m_boxes
.Number();
453 int wxRadioBox::GetNumberOfRowsOrCols(void) const
458 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
460 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
463 void wxRadioBox::SetFont( const wxFont
&font
)
465 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
467 wxControl::SetFont( font
);
469 gtk_widget_set_style( m_widget
, m_widgetStyle
);
471 wxNode
*node
= m_boxes
.First();
474 GtkButton
*button
= GTK_BUTTON( node
->Data() );
476 gtk_widget_set_style( button
->child
, m_widgetStyle
);
482 void wxRadioBox::SetBackgroundColour( const wxColour
&colour
)
484 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
486 wxControl::SetBackgroundColour( colour
);
488 if (!m_backgroundColour
.Ok()) return;
490 gtk_widget_set_style( m_widget
, m_widgetStyle
);
492 wxNode
*node
= m_boxes
.First();
495 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
497 gtk_widget_set_style( button
, 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
;