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 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), m_widget
, m_x
, m_y
);
163 gtk_widget_set_usize( m_widget
, m_width
, m_height
);
169 SetBackgroundColour( parent
->GetBackgroundColour() );
170 SetForegroundColour( parent
->GetForegroundColour() );
177 wxRadioBox::~wxRadioBox(void)
179 wxNode
*node
= m_boxes
.First();
182 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
183 gtk_widget_destroy( button
);
188 void wxRadioBox::OnSize( wxSizeEvent
&event
)
190 wxControl::OnSize( event
);
195 if (m_windowStyle
& wxRA_VERTICAL
)
197 wxNode
*node
= m_boxes
.First();
200 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
202 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
207 gtk_widget_set_usize( button
, w
, 20 );
216 wxNode
*node
= m_boxes
.First();
219 GtkButton
*button
= GTK_BUTTON( node
->Data() );
220 GtkLabel
*label
= GTK_LABEL( button
->child
);
222 GdkFont
*font
= m_widget
->style
->font
;
223 int len
= 27+gdk_string_measure( font
, label
->label
);
224 if (len
> max
) max
= len
;
229 node
= m_boxes
.First();
232 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
234 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
236 gtk_widget_set_usize( button
, max
, 20 );
243 bool wxRadioBox::Show( bool show
)
245 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
247 wxWindow::Show( show
);
249 wxNode
*node
= m_boxes
.First();
252 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
254 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
262 int wxRadioBox::FindString( const wxString
&s
) const
264 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
268 wxNode
*node
= m_boxes
.First();
271 GtkButton
*button
= GTK_BUTTON( node
->Data() );
273 GtkLabel
*label
= GTK_LABEL( button
->child
);
274 if (s
== label
->label
) return count
;
283 void wxRadioBox::SetSelection( int n
)
285 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
287 wxNode
*node
= m_boxes
.Nth( n
);
291 wxFAIL_MSG( "wxRadioBox wrong index" );
295 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
297 gtk_toggle_button_set_state( button
, 1 );
300 int wxRadioBox::GetSelection(void) const
302 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
306 wxNode
*node
= m_boxes
.First();
309 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
310 if (button
->active
) return count
;
315 wxFAIL_MSG( "wxRadioBox none selected" );
320 wxString
wxRadioBox::GetString( int n
) const
322 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
324 wxNode
*node
= m_boxes
.Nth( n
);
328 wxFAIL_MSG( "wxRadioBox wrong index" );
332 GtkButton
*button
= GTK_BUTTON( node
->Data() );
333 GtkLabel
*label
= GTK_LABEL( button
->child
);
335 return wxString( label
->label
);
338 wxString
wxRadioBox::GetLabel( int item
) const
340 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
342 return GetString( item
);
345 void wxRadioBox::SetLabel( const wxString
& label
)
347 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
349 wxControl::SetLabel( label
);
351 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
354 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
356 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
358 wxNode
*node
= m_boxes
.Nth( item
);
362 wxFAIL_MSG( "wxRadioBox wrong index" );
366 GtkButton
*button
= GTK_BUTTON( node
->Data() );
367 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
369 gtk_label_set( g_label
, label
);
372 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
374 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
377 void wxRadioBox::Enable( bool enable
)
379 wxControl::Enable( enable
);
381 wxNode
*node
= m_boxes
.First();
384 GtkButton
*button
= GTK_BUTTON( node
->Data() );
385 GtkWidget
*label
= button
->child
;
386 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
387 gtk_widget_set_sensitive( label
, enable
);
392 void wxRadioBox::Enable( int item
, bool enable
)
394 wxNode
*node
= m_boxes
.Nth( item
);
398 wxFAIL_MSG( "wxRadioBox wrong index" );
402 GtkButton
*button
= GTK_BUTTON( node
->Data() );
403 GtkWidget
*label
= button
->child
;
404 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
405 gtk_widget_set_sensitive( label
, enable
);
408 void wxRadioBox::Show( int item
, bool show
)
410 wxNode
*node
= m_boxes
.Nth( item
);
414 wxFAIL_MSG( "wxRadioBox wrong index" );
418 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
421 gtk_widget_show( button
);
423 gtk_widget_hide( button
);
426 wxString
wxRadioBox::GetStringSelection(void) const
428 wxNode
*node
= m_boxes
.First();
431 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
434 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
440 wxFAIL_MSG( "wxRadioBox none selected" );
444 bool wxRadioBox::SetStringSelection( const wxString
&s
)
446 int res
= FindString( s
);
447 if (res
== -1) return FALSE
;
452 int wxRadioBox::Number(void) const
454 return m_boxes
.Number();
457 int wxRadioBox::GetNumberOfRowsOrCols(void) const
462 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
464 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
467 void wxRadioBox::ApplyWidgetStyle()
471 gtk_widget_set_style( m_widget
, m_widgetStyle
);
473 wxNode
*node
= m_boxes
.First();
476 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
477 gtk_widget_set_style( widget
, m_widgetStyle
);
479 GtkButton
*button
= GTK_BUTTON( node
->Data() );
480 gtk_widget_set_style( button
->child
, m_widgetStyle
);
486 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
488 if (window
== m_widget
->window
) return TRUE
;
490 wxNode
*node
= m_boxes
.First();
493 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
495 if (window
== button
->window
) return TRUE
;