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 majorDim
,
68 long style
, const wxValidator
& validator
,
69 const wxString
&name
)
71 m_alreadySent
= FALSE
;
73 m_acceptsFocus
= TRUE
;
75 PreCreation( parent
, id
, pos
, size
, style
, name
);
77 SetValidator( validator
);
79 m_widget
= gtk_frame_new( title
);
81 m_majorDim
= majorDim
;
83 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
85 GSList
*radio_button_group
= (GSList
*) NULL
;
86 for (int i
= 0; i
< n
; i
++)
88 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
90 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
92 m_boxes
.Append( (wxObject
*) m_radio
);
94 ConnectWidget( GTK_WIDGET(m_radio
) );
96 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
98 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
99 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
101 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), m_x
+10, m_y
+10+(i
*24) );
105 wxSize ls
= LayoutItems();
107 wxSize newSize
= size
;
108 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
109 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
110 SetSize( newSize
.x
, newSize
.y
);
112 m_parent
->AddChild( this );
114 (m_parent
->m_insertCallback
)( m_parent
, this );
120 SetBackgroundColour( parent
->GetBackgroundColour() );
121 SetForegroundColour( parent
->GetForegroundColour() );
128 wxRadioBox::~wxRadioBox(void)
130 wxNode
*node
= m_boxes
.First();
133 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
134 gtk_widget_destroy( button
);
139 void wxRadioBox::OnSize( wxSizeEvent
&event
)
141 wxControl::OnSize( event
);
146 wxSize
wxRadioBox::LayoutItems()
151 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
155 if (m_windowStyle
& wxRA_HORIZONTAL
)
158 for (int j
= 0; j
< m_majorDim
; j
++)
163 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
164 for (int i
= 0; i
< num_per_major
; i
++)
166 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
167 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
168 GdkFont
*font
= m_widget
->style
->font
;
169 int len
= 27+gdk_string_measure( font
, label
->label
);
170 if (len
> max_len
) max_len
= len
;
172 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
179 // we don't know the max_len before
181 node
= m_boxes
.Nth( j
*num_per_major
);
182 for (int i
= 0; i
< num_per_major
; i
++)
184 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
186 gtk_widget_set_usize( button
, max_len
, 22 );
192 if (y
> res
.y
) res
.y
= y
;
204 wxNode
*node
= m_boxes
.First();
207 GtkButton
*button
= GTK_BUTTON( node
->Data() );
208 GtkLabel
*label
= GTK_LABEL( button
->child
);
210 GdkFont
*font
= m_widget
->style
->font
;
211 int len
= 27+gdk_string_measure( font
, label
->label
);
212 if (len
> max
) max
= len
;
217 node
= m_boxes
.First();
220 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
222 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, x
, y
);
224 gtk_widget_set_usize( button
, max
, 20 );
235 bool wxRadioBox::Show( bool show
)
237 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
239 wxWindow::Show( show
);
241 wxNode
*node
= m_boxes
.First();
244 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
246 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
254 int wxRadioBox::FindString( const wxString
&s
) const
256 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
260 wxNode
*node
= m_boxes
.First();
263 GtkButton
*button
= GTK_BUTTON( node
->Data() );
265 GtkLabel
*label
= GTK_LABEL( button
->child
);
266 if (s
== label
->label
) return count
;
275 void wxRadioBox::SetFocus()
277 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
279 if (m_boxes
.GetCount() == 0) return;
281 wxNode
*node
= m_boxes
.First();
284 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
287 gtk_widget_grab_focus( GTK_WIDGET(button
) );
296 void wxRadioBox::SetSelection( int n
)
298 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
300 wxNode
*node
= m_boxes
.Nth( n
);
302 wxCHECK_RET( node
, "radiobox wrong index" );
304 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
306 gtk_toggle_button_set_state( button
, 1 );
309 int wxRadioBox::GetSelection(void) const
311 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
315 wxNode
*node
= m_boxes
.First();
318 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
319 if (button
->active
) return count
;
324 wxFAIL_MSG( "wxRadioBox none selected" );
329 wxString
wxRadioBox::GetString( int n
) const
331 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
333 wxNode
*node
= m_boxes
.Nth( n
);
335 wxCHECK_MSG( node
, "", "radiobox wrong index" );
337 GtkButton
*button
= GTK_BUTTON( node
->Data() );
338 GtkLabel
*label
= GTK_LABEL( button
->child
);
340 return wxString( label
->label
);
343 wxString
wxRadioBox::GetLabel( int item
) const
345 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
347 return GetString( item
);
350 void wxRadioBox::SetLabel( const wxString
& label
)
352 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
354 wxControl::SetLabel( label
);
356 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
359 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
361 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
363 wxNode
*node
= m_boxes
.Nth( item
);
365 wxCHECK_RET( node
, "radiobox wrong index" );
367 GtkButton
*button
= GTK_BUTTON( node
->Data() );
368 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
370 gtk_label_set( g_label
, label
);
373 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
375 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
378 void wxRadioBox::Enable( bool enable
)
380 wxControl::Enable( enable
);
382 wxNode
*node
= m_boxes
.First();
385 GtkButton
*button
= GTK_BUTTON( node
->Data() );
386 GtkWidget
*label
= button
->child
;
387 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
388 gtk_widget_set_sensitive( label
, enable
);
393 void wxRadioBox::Enable( int item
, bool enable
)
395 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
397 wxNode
*node
= m_boxes
.Nth( item
);
399 wxCHECK_RET( node
, "radiobox wrong index" );
401 GtkButton
*button
= GTK_BUTTON( node
->Data() );
402 GtkWidget
*label
= button
->child
;
403 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
404 gtk_widget_set_sensitive( label
, enable
);
407 void wxRadioBox::Show( int item
, bool show
)
409 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
411 wxNode
*node
= m_boxes
.Nth( item
);
413 wxCHECK_RET( node
, "radiobox 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 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
427 wxNode
*node
= m_boxes
.First();
430 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
433 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
439 wxFAIL_MSG( "wxRadioBox none selected" );
443 bool wxRadioBox::SetStringSelection( const wxString
&s
)
445 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
447 int res
= FindString( s
);
448 if (res
== -1) return FALSE
;
454 int wxRadioBox::Number(void) const
456 return m_boxes
.Number();
459 int wxRadioBox::GetNumberOfRowsOrCols(void) const
464 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
466 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
469 void wxRadioBox::ApplyWidgetStyle()
473 gtk_widget_set_style( m_widget
, m_widgetStyle
);
475 wxNode
*node
= m_boxes
.First();
478 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
479 gtk_widget_set_style( widget
, m_widgetStyle
);
481 GtkButton
*button
= GTK_BUTTON( node
->Data() );
482 gtk_widget_set_style( button
->child
, m_widgetStyle
);
488 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
490 if (window
== m_widget
->window
) return TRUE
;
492 wxNode
*node
= m_boxes
.First();
495 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
497 if (window
== button
->window
) return TRUE
;