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
;
74 m_acceptsFocus
= TRUE
;
76 PreCreation( parent
, id
, pos
, size
, style
, name
);
78 SetValidator( validator
);
80 m_widget
= gtk_frame_new( title
);
88 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
90 if (m_windowStyle
& wxRA_HORIZONTAL
)
92 GSList
*radio_button_group
= (GSList
*) NULL
;
93 for (int i
= 0; i
< n
; i
++)
95 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
97 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
99 m_boxes
.Append( (wxObject
*) m_radio
);
101 ConnectWidget( GTK_WIDGET(m_radio
) );
103 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
105 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
106 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
108 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
110 int tmp
= 25+gdk_string_measure( GTK_WIDGET(m_radio
)->style
->font
, choices
[i
] );
111 if (tmp
> maxLen
) maxLen
= tmp
;
114 if (size
.x
== -1) width
= tmp
;
115 gtk_widget_set_usize( GTK_WIDGET(m_radio
), width
, 20 );
125 for (int i
= 0; i
< n
; i
++)
127 GdkFont
*font
= m_widget
->style
->font
;
128 int len
= 27+gdk_string_measure( font
, choices
[i
] );
129 if (len
> max
) max
= len
;
132 GSList
*radio_button_group
= (GSList
*) NULL
;
133 for (int i
= 0; i
< n
; i
++)
135 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
137 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
139 m_boxes
.Append( (wxObject
*) m_radio
);
141 ConnectWidget( GTK_WIDGET(m_radio
) );
143 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
145 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
146 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
148 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), x
, y
);
150 gtk_widget_set_usize( GTK_WIDGET(m_radio
), max
, 20 );
159 wxSize newSize
= size
;
160 if (newSize
.x
== -1) newSize
.x
= width
;
161 if (newSize
.y
== -1) newSize
.y
= height
;
162 SetSize( newSize
.x
, newSize
.y
);
164 m_parent
->AddChild( this );
166 (m_parent
->m_insertCallback
)( m_parent
, this );
172 SetBackgroundColour( parent
->GetBackgroundColour() );
173 SetForegroundColour( parent
->GetForegroundColour() );
180 wxRadioBox::~wxRadioBox(void)
182 wxNode
*node
= m_boxes
.First();
185 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
186 gtk_widget_destroy( button
);
191 void wxRadioBox::OnSize( wxSizeEvent
&event
)
193 wxControl::OnSize( event
);
198 if (m_windowStyle
& wxRA_HORIZONTAL
)
200 wxNode
*node
= m_boxes
.First();
203 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
205 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
210 gtk_widget_set_usize( button
, w
, 20 );
219 wxNode
*node
= m_boxes
.First();
222 GtkButton
*button
= GTK_BUTTON( node
->Data() );
223 GtkLabel
*label
= GTK_LABEL( button
->child
);
225 GdkFont
*font
= m_widget
->style
->font
;
226 int len
= 27+gdk_string_measure( font
, label
->label
);
227 if (len
> max
) max
= len
;
232 node
= m_boxes
.First();
235 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
237 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
239 gtk_widget_set_usize( button
, max
, 20 );
246 bool wxRadioBox::Show( bool show
)
248 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
250 wxWindow::Show( show
);
252 wxNode
*node
= m_boxes
.First();
255 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
257 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
265 int wxRadioBox::FindString( const wxString
&s
) const
267 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
271 wxNode
*node
= m_boxes
.First();
274 GtkButton
*button
= GTK_BUTTON( node
->Data() );
276 GtkLabel
*label
= GTK_LABEL( button
->child
);
277 if (s
== label
->label
) return count
;
286 void wxRadioBox::SetFocus()
288 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
290 if (m_boxes
.GetCount() == 0) return;
292 wxNode
*node
= m_boxes
.First();
295 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
298 gtk_widget_grab_focus( GTK_WIDGET(button
) );
307 void wxRadioBox::SetSelection( int n
)
309 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
311 wxNode
*node
= m_boxes
.Nth( n
);
313 wxCHECK_RET( node
, "radiobox wrong index" );
315 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
317 gtk_toggle_button_set_state( button
, 1 );
320 int wxRadioBox::GetSelection(void) const
322 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
326 wxNode
*node
= m_boxes
.First();
329 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
330 if (button
->active
) return count
;
335 wxFAIL_MSG( "wxRadioBox none selected" );
340 wxString
wxRadioBox::GetString( int n
) const
342 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
344 wxNode
*node
= m_boxes
.Nth( n
);
346 wxCHECK_MSG( node
, "", "radiobox wrong index" );
348 GtkButton
*button
= GTK_BUTTON( node
->Data() );
349 GtkLabel
*label
= GTK_LABEL( button
->child
);
351 return wxString( label
->label
);
354 wxString
wxRadioBox::GetLabel( int item
) const
356 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
358 return GetString( item
);
361 void wxRadioBox::SetLabel( const wxString
& label
)
363 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
365 wxControl::SetLabel( label
);
367 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
370 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
372 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
374 wxNode
*node
= m_boxes
.Nth( item
);
376 wxCHECK_RET( node
, "radiobox wrong index" );
378 GtkButton
*button
= GTK_BUTTON( node
->Data() );
379 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
381 gtk_label_set( g_label
, label
);
384 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
386 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
389 void wxRadioBox::Enable( bool enable
)
391 wxControl::Enable( enable
);
393 wxNode
*node
= m_boxes
.First();
396 GtkButton
*button
= GTK_BUTTON( node
->Data() );
397 GtkWidget
*label
= button
->child
;
398 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
399 gtk_widget_set_sensitive( label
, enable
);
404 void wxRadioBox::Enable( int item
, bool enable
)
406 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
408 wxNode
*node
= m_boxes
.Nth( item
);
410 wxCHECK_RET( node
, "radiobox wrong index" );
412 GtkButton
*button
= GTK_BUTTON( node
->Data() );
413 GtkWidget
*label
= button
->child
;
414 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
415 gtk_widget_set_sensitive( label
, enable
);
418 void wxRadioBox::Show( int item
, bool show
)
420 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
422 wxNode
*node
= m_boxes
.Nth( item
);
424 wxCHECK_RET( node
, "radiobox wrong index" );
426 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
429 gtk_widget_show( button
);
431 gtk_widget_hide( button
);
434 wxString
wxRadioBox::GetStringSelection(void) const
436 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
438 wxNode
*node
= m_boxes
.First();
441 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
444 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
450 wxFAIL_MSG( "wxRadioBox none selected" );
454 bool wxRadioBox::SetStringSelection( const wxString
&s
)
456 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
458 int res
= FindString( s
);
459 if (res
== -1) return FALSE
;
465 int wxRadioBox::Number(void) const
467 return m_boxes
.Number();
470 int wxRadioBox::GetNumberOfRowsOrCols(void) const
475 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
477 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
480 void wxRadioBox::ApplyWidgetStyle()
484 gtk_widget_set_style( m_widget
, m_widgetStyle
);
486 wxNode
*node
= m_boxes
.First();
489 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
490 gtk_widget_set_style( widget
, m_widgetStyle
);
492 GtkButton
*button
= GTK_BUTTON( node
->Data() );
493 gtk_widget_set_style( button
->child
, m_widgetStyle
);
499 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
501 if (window
== m_widget
->window
) return TRUE
;
503 wxNode
*node
= m_boxes
.First();
506 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
508 if (window
== button
->window
) return TRUE
;