1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "radiobox.h"
14 #include "wx/radiobox.h"
15 #include "wx/dialog.h"
20 #include "wx/gtk/win_gtk.h"
22 //-----------------------------------------------------------------------------
24 //-----------------------------------------------------------------------------
26 extern bool g_blockEventsOnDrag
;
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
34 if (!rb
->HasVMT()) return;
35 if (g_blockEventsOnDrag
) return;
37 if (rb
->m_alreadySent
)
39 rb
->m_alreadySent
= FALSE
;
43 rb
->m_alreadySent
= TRUE
;
45 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
46 event
.SetInt( rb
->GetSelection() );
47 wxString
tmp( rb
->GetStringSelection() );
48 event
.SetString( WXSTRINGCAST(tmp
) );
49 event
.SetEventObject( rb
);
50 rb
->GetEventHandler()->ProcessEvent(event
);
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
59 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
60 EVT_SIZE(wxRadioBox::OnSize
)
63 wxRadioBox::wxRadioBox(void)
67 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
68 const wxPoint
&pos
, const wxSize
&size
,
69 int n
, const wxString choices
[], int majorDim
,
70 long style
, const wxValidator
& validator
,
71 const wxString
&name
)
73 m_alreadySent
= FALSE
;
75 m_acceptsFocus
= TRUE
;
77 PreCreation( parent
, id
, pos
, size
, style
, name
);
79 SetValidator( validator
);
81 m_widget
= gtk_frame_new( title
);
83 m_majorDim
= majorDim
;
85 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
87 GSList
*radio_button_group
= (GSList
*) NULL
;
88 for (int i
= 0; i
< n
; i
++)
90 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
92 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
] ) );
94 m_boxes
.Append( (wxObject
*) m_radio
);
96 ConnectWidget( GTK_WIDGET(m_radio
) );
98 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
100 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
101 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
103 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), m_x
+10, m_y
+10+(i
*24) );
107 wxSize ls
= LayoutItems();
109 wxSize newSize
= size
;
110 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
111 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
112 SetSize( newSize
.x
, newSize
.y
);
114 m_parent
->AddChild( this );
116 (m_parent
->m_insertCallback
)( m_parent
, this );
122 SetBackgroundColour( parent
->GetBackgroundColour() );
123 SetForegroundColour( parent
->GetForegroundColour() );
124 SetFont( parent
->GetFont() );
131 wxRadioBox::~wxRadioBox(void)
133 wxNode
*node
= m_boxes
.First();
136 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
137 gtk_widget_destroy( button
);
142 void wxRadioBox::OnSize( wxSizeEvent
&event
)
144 wxControl::OnSize( event
);
149 wxSize
wxRadioBox::LayoutItems()
154 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
158 if (m_windowStyle
& wxRA_HORIZONTAL
)
161 for (int j
= 0; j
< m_majorDim
; j
++)
166 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
167 for (int i1
= 0; i1
< num_per_major
; i1
++)
169 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
170 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
171 GdkFont
*font
= m_widget
->style
->font
;
172 int len
= 22+gdk_string_measure( font
, label
->label
);
173 if (len
> max_len
) max_len
= len
;
175 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
182 // we don't know the max_len before
184 node
= m_boxes
.Nth( j
*num_per_major
);
185 for (int i2
= 0; i2
< num_per_major
; i2
++)
187 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
189 gtk_widget_set_usize( button
, max_len
, 20 );
195 if (y
> res
.y
) res
.y
= y
;
207 wxNode
*node
= m_boxes
.First();
210 GtkButton
*button
= GTK_BUTTON( node
->Data() );
211 GtkLabel
*label
= GTK_LABEL( button
->child
);
213 GdkFont
*font
= m_widget
->style
->font
;
214 int len
= 22+gdk_string_measure( font
, label
->label
);
215 if (len
> max
) max
= len
;
220 node
= m_boxes
.First();
223 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
225 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
227 gtk_widget_set_usize( button
, max
, 20 );
238 bool wxRadioBox::Show( bool show
)
240 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
242 wxWindow::Show( show
);
244 wxNode
*node
= m_boxes
.First();
247 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
249 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
257 int wxRadioBox::FindString( const wxString
&s
) const
259 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
263 wxNode
*node
= m_boxes
.First();
266 GtkButton
*button
= GTK_BUTTON( node
->Data() );
268 GtkLabel
*label
= GTK_LABEL( button
->child
);
269 if (s
== label
->label
) return count
;
278 void wxRadioBox::SetFocus()
280 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
282 if (m_boxes
.GetCount() == 0) return;
284 wxNode
*node
= m_boxes
.First();
287 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
290 gtk_widget_grab_focus( GTK_WIDGET(button
) );
299 void wxRadioBox::SetSelection( int n
)
301 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
303 wxNode
*node
= m_boxes
.Nth( n
);
305 wxCHECK_RET( node
, "radiobox wrong index" );
307 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
309 gtk_toggle_button_set_state( button
, 1 );
312 int wxRadioBox::GetSelection(void) const
314 wxCHECK_MSG( m_widget
!= NULL
, -1, "invalid radiobox" );
318 wxNode
*node
= m_boxes
.First();
321 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
322 if (button
->active
) return count
;
327 wxFAIL_MSG( "wxRadioBox none selected" );
332 wxString
wxRadioBox::GetString( int n
) const
334 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
336 wxNode
*node
= m_boxes
.Nth( n
);
338 wxCHECK_MSG( node
, "", "radiobox wrong index" );
340 GtkButton
*button
= GTK_BUTTON( node
->Data() );
341 GtkLabel
*label
= GTK_LABEL( button
->child
);
343 return wxString( label
->label
);
346 wxString
wxRadioBox::GetLabel( int item
) const
348 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
350 return GetString( item
);
353 void wxRadioBox::SetLabel( const wxString
& label
)
355 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
357 wxControl::SetLabel( label
);
359 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel() );
362 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
364 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
366 wxNode
*node
= m_boxes
.Nth( item
);
368 wxCHECK_RET( node
, "radiobox wrong index" );
370 GtkButton
*button
= GTK_BUTTON( node
->Data() );
371 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
373 gtk_label_set( g_label
, label
);
376 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
378 wxFAIL_MSG("wxRadioBox::SetLabel not implemented.");
381 void wxRadioBox::Enable( bool enable
)
383 wxControl::Enable( enable
);
385 wxNode
*node
= m_boxes
.First();
388 GtkButton
*button
= GTK_BUTTON( node
->Data() );
389 GtkWidget
*label
= button
->child
;
390 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
391 gtk_widget_set_sensitive( label
, enable
);
396 void wxRadioBox::Enable( int item
, bool enable
)
398 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
400 wxNode
*node
= m_boxes
.Nth( item
);
402 wxCHECK_RET( node
, "radiobox wrong index" );
404 GtkButton
*button
= GTK_BUTTON( node
->Data() );
405 GtkWidget
*label
= button
->child
;
406 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
407 gtk_widget_set_sensitive( label
, enable
);
410 void wxRadioBox::Show( int item
, bool show
)
412 wxCHECK_RET( m_widget
!= NULL
, "invalid radiobox" );
414 wxNode
*node
= m_boxes
.Nth( item
);
416 wxCHECK_RET( node
, "radiobox 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 wxCHECK_MSG( m_widget
!= NULL
, "", "invalid radiobox" );
430 wxNode
*node
= m_boxes
.First();
433 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
436 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
442 wxFAIL_MSG( "wxRadioBox none selected" );
446 bool wxRadioBox::SetStringSelection( const wxString
&s
)
448 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, "invalid radiobox" );
450 int res
= FindString( s
);
451 if (res
== -1) return FALSE
;
457 int wxRadioBox::Number(void) const
459 return m_boxes
.Number();
462 int wxRadioBox::GetNumberOfRowsOrCols(void) const
467 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
469 wxFAIL_MSG("wxRadioBox::SetNumberOfRowsOrCols not implemented.");
472 void wxRadioBox::ApplyWidgetStyle()
476 gtk_widget_set_style( m_widget
, m_widgetStyle
);
478 wxNode
*node
= m_boxes
.First();
481 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
482 gtk_widget_set_style( widget
, m_widgetStyle
);
484 GtkButton
*button
= GTK_BUTTON( node
->Data() );
485 gtk_widget_set_style( button
->child
, m_widgetStyle
);
491 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
493 if (window
== m_widget
->window
) return TRUE
;
495 wxNode
*node
= m_boxes
.First();
498 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
500 if (window
== button
->window
) return TRUE
;