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 event
.SetString( rb
->GetStringSelection() );
48 event
.SetEventObject( rb
);
49 rb
->GetEventHandler()->ProcessEvent(event
);
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
58 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
59 EVT_SIZE(wxRadioBox::OnSize
)
62 wxRadioBox::wxRadioBox(void)
66 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
67 const wxPoint
&pos
, const wxSize
&size
,
68 int n
, const wxString choices
[], int 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
.mbc_str() );
82 m_majorDim
= majorDim
;
84 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
86 GSList
*radio_button_group
= (GSList
*) NULL
;
87 for (int i
= 0; i
< n
; i
++)
89 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
91 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
].mbc_str() ) );
93 m_boxes
.Append( (wxObject
*) m_radio
);
95 ConnectWidget( GTK_WIDGET(m_radio
) );
97 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
99 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
100 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
102 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
), GTK_WIDGET(m_radio
), m_x
+10, m_y
+10+(i
*24) );
106 wxSize ls
= LayoutItems();
108 wxSize newSize
= size
;
109 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
110 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
111 SetSize( newSize
.x
, newSize
.y
);
113 m_parent
->AddChild( this );
115 (m_parent
->m_insertCallback
)( m_parent
, this );
121 SetBackgroundColour( parent
->GetBackgroundColour() );
122 SetForegroundColour( parent
->GetForegroundColour() );
123 SetFont( parent
->GetFont() );
130 wxRadioBox::~wxRadioBox(void)
132 wxNode
*node
= m_boxes
.First();
135 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
136 gtk_widget_destroy( button
);
141 void wxRadioBox::OnSize( wxSizeEvent
&event
)
143 wxControl::OnSize( event
);
148 wxSize
wxRadioBox::LayoutItems()
153 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
157 if (m_windowStyle
& wxRA_HORIZONTAL
)
160 for (int j
= 0; j
< m_majorDim
; j
++)
165 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
166 for (int i1
= 0; i1
< num_per_major
; i1
++)
168 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
169 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
170 GdkFont
*font
= m_widget
->style
->font
;
171 int len
= 22+gdk_string_measure( font
, label
->label
);
172 if (len
> max_len
) max_len
= len
;
174 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
181 // we don't know the max_len before
183 node
= m_boxes
.Nth( j
*num_per_major
);
184 for (int i2
= 0; i2
< num_per_major
; i2
++)
186 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
188 gtk_widget_set_usize( button
, max_len
, 20 );
194 if (y
> res
.y
) res
.y
= y
;
206 wxNode
*node
= m_boxes
.First();
209 GtkButton
*button
= GTK_BUTTON( node
->Data() );
210 GtkLabel
*label
= GTK_LABEL( button
->child
);
212 GdkFont
*font
= m_widget
->style
->font
;
213 int len
= 22+gdk_string_measure( font
, label
->label
);
214 if (len
> max
) max
= len
;
219 node
= m_boxes
.First();
222 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
224 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
226 gtk_widget_set_usize( button
, max
, 20 );
237 bool wxRadioBox::Show( bool show
)
239 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
241 wxWindow::Show( show
);
243 wxNode
*node
= m_boxes
.First();
246 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
248 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
256 int wxRadioBox::FindString( const wxString
&s
) const
258 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
262 wxNode
*node
= m_boxes
.First();
265 GtkButton
*button
= GTK_BUTTON( node
->Data() );
267 GtkLabel
*label
= GTK_LABEL( button
->child
);
268 if (s
== label
->label
) return count
;
277 void wxRadioBox::SetFocus()
279 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
281 if (m_boxes
.GetCount() == 0) return;
283 wxNode
*node
= m_boxes
.First();
286 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
289 gtk_widget_grab_focus( GTK_WIDGET(button
) );
298 void wxRadioBox::SetSelection( int n
)
300 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
302 wxNode
*node
= m_boxes
.Nth( n
);
304 wxCHECK_RET( node
, _T("radiobox wrong index") );
306 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
308 gtk_toggle_button_set_state( button
, 1 );
311 int wxRadioBox::GetSelection(void) const
313 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
317 wxNode
*node
= m_boxes
.First();
320 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
321 if (button
->active
) return count
;
326 wxFAIL_MSG( _T("wxRadioBox none selected") );
331 wxString
wxRadioBox::GetString( int n
) const
333 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
335 wxNode
*node
= m_boxes
.Nth( n
);
337 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
339 GtkButton
*button
= GTK_BUTTON( node
->Data() );
340 GtkLabel
*label
= GTK_LABEL( button
->child
);
342 return wxString( label
->label
);
345 wxString
wxRadioBox::GetLabel( int item
) const
347 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
349 return GetString( item
);
352 void wxRadioBox::SetLabel( const wxString
& label
)
354 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
356 wxControl::SetLabel( label
);
358 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
361 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
363 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
365 wxNode
*node
= m_boxes
.Nth( item
);
367 wxCHECK_RET( node
, _T("radiobox wrong index") );
369 GtkButton
*button
= GTK_BUTTON( node
->Data() );
370 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
372 gtk_label_set( g_label
, label
.mbc_str() );
375 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
377 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
380 void wxRadioBox::Enable( bool enable
)
382 wxControl::Enable( enable
);
384 wxNode
*node
= m_boxes
.First();
387 GtkButton
*button
= GTK_BUTTON( node
->Data() );
388 GtkWidget
*label
= button
->child
;
389 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
390 gtk_widget_set_sensitive( label
, enable
);
395 void wxRadioBox::Enable( int item
, bool enable
)
397 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
399 wxNode
*node
= m_boxes
.Nth( item
);
401 wxCHECK_RET( node
, _T("radiobox wrong index") );
403 GtkButton
*button
= GTK_BUTTON( node
->Data() );
404 GtkWidget
*label
= button
->child
;
405 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
406 gtk_widget_set_sensitive( label
, enable
);
409 void wxRadioBox::Show( int item
, bool show
)
411 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
413 wxNode
*node
= m_boxes
.Nth( item
);
415 wxCHECK_RET( node
, _T("radiobox wrong index") );
417 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
420 gtk_widget_show( button
);
422 gtk_widget_hide( button
);
425 wxString
wxRadioBox::GetStringSelection(void) const
427 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
429 wxNode
*node
= m_boxes
.First();
432 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
435 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
441 wxFAIL_MSG( _T("wxRadioBox none selected") );
445 bool wxRadioBox::SetStringSelection( const wxString
&s
)
447 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
449 int res
= FindString( s
);
450 if (res
== -1) return FALSE
;
456 int wxRadioBox::Number(void) const
458 return m_boxes
.Number();
461 int wxRadioBox::GetNumberOfRowsOrCols(void) const
466 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
468 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
471 void wxRadioBox::ApplyWidgetStyle()
475 gtk_widget_set_style( m_widget
, m_widgetStyle
);
477 wxNode
*node
= m_boxes
.First();
480 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
481 gtk_widget_set_style( widget
, m_widgetStyle
);
483 GtkButton
*button
= GTK_BUTTON( node
->Data() );
484 gtk_widget_set_style( button
->child
, m_widgetStyle
);
490 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
492 if (window
== m_widget
->window
) return TRUE
;
494 wxNode
*node
= m_boxes
.First();
497 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
499 if (window
== button
->window
) return TRUE
;