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
[],
68 int WXUNUSED(majorDim
),
69 long style
, const wxValidator
& validator
,
70 const wxString
&name
)
72 m_alreadySent
= FALSE
;
75 PreCreation( parent
, id
, pos
, size
, style
, name
);
77 SetValidator( validator
);
79 m_widget
= gtk_frame_new( title
);
87 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
89 if (m_windowStyle
& wxRA_HORIZONTAL
)
91 GSList
*radio_button_group
= (GSList
*) NULL
;
92 for (int i
= 0; i
< n
; i
++)
94 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
96 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
98 m_boxes
.Append( (wxObject
*) m_radio
);
100 ConnectWidget( GTK_WIDGET(m_radio
) );
102 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
104 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
105 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
107 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
109 int tmp
= 25+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
110 if (tmp
> maxLen
) maxLen
= tmp
;
113 if (size
.x
== -1) width
= tmp
;
114 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
124 for (int i
= 0; i
< n
; i
++)
126 GdkFont
*font
= m_widget
->style
->font
;
127 int len
= 27+gdk_string_measure( font
, choices
[i
] );
128 if (len
> max
) max
= len
;
131 GSList
*radio_button_group
= (GSList
*) NULL
;
132 for (int i
= 0; i
< n
; i
++)
134 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
136 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
138 m_boxes
.Append( (wxObject
*) m_radio
);
140 ConnectWidget( GTK_WIDGET(m_radio
) );
142 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
144 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
145 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
147 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
149 gtk_widget_set_usize( GTK_WIDGET(m_radio
), max
, 20 );
158 wxSize newSize
= size
;
159 if (newSize
.x
== -1) newSize
.x
= width
;
160 if (newSize
.y
== -1) newSize
.y
= height
;
161 SetSize( newSize
.x
, newSize
.y
);
163 m_parent
->AddChild( this );
165 (m_parent
->m_insertCallback
)( m_parent
, this );
171 SetBackgroundColour( parent
->GetBackgroundColour() );
172 SetForegroundColour( parent
->GetForegroundColour() );
179 wxRadioBox::~wxRadioBox(void)
181 wxNode
*node
= m_boxes
.First();
184 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
185 gtk_widget_destroy( button
);
190 void wxRadioBox::OnSize( wxSizeEvent
&event
)
192 wxControl::OnSize( event
);
197 if (m_windowStyle
& wxRA_HORIZONTAL
)
199 wxNode
*node
= m_boxes
.First();
202 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
204 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
209 gtk_widget_set_usize( button
, w
, 20 );
218 wxNode
*node
= m_boxes
.First();
221 GtkButton
*button
= GTK_BUTTON( node
->Data() );
222 GtkLabel
*label
= GTK_LABEL( button
->child
);
224 GdkFont
*font
= m_widget
->style
->font
;
225 int len
= 27+gdk_string_measure( font
, label
->label
);
226 if (len
> max
) max
= len
;
231 node
= m_boxes
.First();
234 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
236 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
238 gtk_widget_set_usize( button
, max
, 20 );
245 bool wxRadioBox::Show( bool show
)
247 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
249 wxWindow::Show( show
);
251 wxNode
*node
= m_boxes
.First();
254 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
256 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
264 int wxRadioBox::FindString( const wxString
&s
) const
266 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
270 wxNode
*node
= m_boxes
.First();
273 GtkButton
*button
= GTK_BUTTON( node
->Data() );
275 GtkLabel
*label
= GTK_LABEL( button
->child
);
276 if (s
== label
->label
) return count
;
285 void wxRadioBox::SetSelection( int n
)
287 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
289 wxNode
*node
= m_boxes
.Nth( n
);
291 wxCHECK_RET( node
, "radiobox wrong index" );
293 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
295 gtk_toggle_button_set_state( button
, 1 );
298 int wxRadioBox::GetSelection(void) const
300 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
304 wxNode
*node
= m_boxes
.First();
307 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
308 if (button
->active
) return count
;
313 wxFAIL_MSG( "wxRadioBox none selected" );
318 wxString
wxRadioBox::GetString( int n
) const
320 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
322 wxNode
*node
= m_boxes
.Nth( n
);
324 wxCHECK_MSG( node
, "", "radiobox wrong index" );
326 GtkButton
*button
= GTK_BUTTON( node
->Data() );
327 GtkLabel
*label
= GTK_LABEL( button
->child
);
329 return wxString( label
->label
);
332 wxString
wxRadioBox::GetLabel( int item
) const
334 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
336 return GetString( item
);
339 void wxRadioBox::SetLabel( const wxString
& label
)
341 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
343 wxControl::SetLabel( label
);
345 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
348 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
350 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
352 wxNode
*node
= m_boxes
.Nth( item
);
354 wxCHECK_RET( node
, "radiobox wrong index" );
356 GtkButton
*button
= GTK_BUTTON( node
->Data() );
357 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
359 gtk_label_set( g_label
, label
);
362 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
364 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
367 void wxRadioBox::Enable( bool enable
)
369 wxControl::Enable( enable
);
371 wxNode
*node
= m_boxes
.First();
374 GtkButton
*button
= GTK_BUTTON( node
->Data() );
375 GtkWidget
*label
= button
->child
;
376 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
377 gtk_widget_set_sensitive( label
, enable
);
382 void wxRadioBox::Enable( int item
, bool enable
)
384 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
386 wxNode
*node
= m_boxes
.Nth( item
);
388 wxCHECK_RET( node
, "radiobox wrong index" );
390 GtkButton
*button
= GTK_BUTTON( node
->Data() );
391 GtkWidget
*label
= button
->child
;
392 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
393 gtk_widget_set_sensitive( label
, enable
);
396 void wxRadioBox::Show( int item
, bool show
)
398 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
400 wxNode
*node
= m_boxes
.Nth( item
);
402 wxCHECK_RET( node
, "radiobox wrong index" );
404 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
407 gtk_widget_show( button
);
409 gtk_widget_hide( button
);
412 wxString
wxRadioBox::GetStringSelection(void) const
414 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
416 wxNode
*node
= m_boxes
.First();
419 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
422 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
428 wxFAIL_MSG( "wxRadioBox none selected" );
432 bool wxRadioBox::SetStringSelection( const wxString
&s
)
434 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
436 int res
= FindString( s
);
437 if (res
== -1) return FALSE
;
443 int wxRadioBox::Number(void) const
445 return m_boxes
.Number();
448 int wxRadioBox::GetNumberOfRowsOrCols(void) const
453 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
455 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
458 void wxRadioBox::ApplyWidgetStyle()
462 gtk_widget_set_style( m_widget
, m_widgetStyle
);
464 wxNode
*node
= m_boxes
.First();
467 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
468 gtk_widget_set_style( widget
, m_widgetStyle
);
470 GtkButton
*button
= GTK_BUTTON( node
->Data() );
471 gtk_widget_set_style( button
->child
, m_widgetStyle
);
477 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
479 if (window
== m_widget
->window
) return TRUE
;
481 wxNode
*node
= m_boxes
.First();
484 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
486 if (window
== button
->window
) return TRUE
;