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"
18 #include "wx/dialog.h"
23 #include "wx/gtk/win_gtk.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 extern void wxapp_install_idle_handler();
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
36 extern bool g_blockEventsOnDrag
;
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
44 if (g_isIdle
) wxapp_install_idle_handler();
46 if (!rb
->m_hasVMT
) return;
47 if (g_blockEventsOnDrag
) return;
49 if (rb
->m_alreadySent
)
51 rb
->m_alreadySent
= FALSE
;
55 rb
->m_alreadySent
= TRUE
;
57 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
58 event
.SetInt( rb
->GetSelection() );
59 event
.SetString( rb
->GetStringSelection() );
60 event
.SetEventObject( rb
);
61 rb
->GetEventHandler()->ProcessEvent(event
);
64 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
68 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
70 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
71 EVT_SIZE(wxRadioBox::OnSize
)
74 wxRadioBox::wxRadioBox()
78 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
79 const wxPoint
&pos
, const wxSize
&size
,
80 int n
, const wxString choices
[], int majorDim
,
81 long style
, const wxValidator
& validator
,
82 const wxString
&name
)
84 m_alreadySent
= FALSE
;
86 m_acceptsFocus
= TRUE
;
88 PreCreation( parent
, id
, pos
, size
, style
, name
);
90 SetValidator( validator
);
92 m_widget
= gtk_frame_new( title
.mbc_str() );
94 m_majorDim
= majorDim
;
96 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
98 GSList
*radio_button_group
= (GSList
*) NULL
;
99 for (int i
= 0; i
< n
; i
++)
101 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
103 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
].mbc_str() ) );
105 m_boxes
.Append( (wxObject
*) m_radio
);
107 ConnectWidget( GTK_WIDGET(m_radio
) );
109 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
111 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
112 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
114 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
),
116 m_x
+10, m_y
+10+(i
*24), 10, 10 );
119 wxSize ls
= LayoutItems();
121 wxSize newSize
= size
;
122 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
123 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
124 SetSize( newSize
.x
, newSize
.y
);
126 m_parent
->DoAddChild( this );
132 SetBackgroundColour( parent
->GetBackgroundColour() );
133 SetForegroundColour( parent
->GetForegroundColour() );
134 SetFont( parent
->GetFont() );
141 wxRadioBox::~wxRadioBox()
143 wxNode
*node
= m_boxes
.First();
146 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
147 gtk_widget_destroy( button
);
152 void wxRadioBox::OnSize( wxSizeEvent
&event
)
159 wxSize
wxRadioBox::LayoutItems()
164 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
168 if (m_windowStyle
& wxRA_HORIZONTAL
)
171 for (int j
= 0; j
< m_majorDim
; j
++)
176 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
177 for (int i1
= 0; i1
< num_per_major
; i1
++)
179 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
180 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
181 GdkFont
*font
= m_widget
->style
->font
;
182 int len
= 22+gdk_string_measure( font
, label
->label
);
183 if (len
> max_len
) max_len
= len
;
185 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
192 // we don't know the max_len before
194 node
= m_boxes
.Nth( j
*num_per_major
);
195 for (int i2
= 0; i2
< num_per_major
; i2
++)
197 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
199 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
205 if (y
> res
.y
) res
.y
= y
;
217 wxNode
*node
= m_boxes
.First();
220 GtkButton
*button
= GTK_BUTTON( node
->Data() );
221 GtkLabel
*label
= GTK_LABEL( button
->child
);
223 GdkFont
*font
= m_widget
->style
->font
;
224 int len
= 22+gdk_string_measure( font
, label
->label
);
225 if (len
> max
) max
= len
;
230 node
= m_boxes
.First();
233 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
235 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
247 bool wxRadioBox::Show( bool show
)
249 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
251 wxWindow::Show( show
);
253 if ((m_windowStyle
& wxNO_BORDER
) != 0)
254 gtk_widget_hide( m_widget
);
256 wxNode
*node
= m_boxes
.First();
259 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
261 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
269 int wxRadioBox::FindString( const wxString
&s
) const
271 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
275 wxNode
*node
= m_boxes
.First();
278 GtkButton
*button
= GTK_BUTTON( node
->Data() );
280 GtkLabel
*label
= GTK_LABEL( button
->child
);
281 if (s
== label
->label
) return count
;
290 void wxRadioBox::SetFocus()
292 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
294 if (m_boxes
.GetCount() == 0) return;
296 wxNode
*node
= m_boxes
.First();
299 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
302 gtk_widget_grab_focus( GTK_WIDGET(button
) );
311 void wxRadioBox::SetSelection( int n
)
313 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
315 wxNode
*node
= m_boxes
.Nth( n
);
317 wxCHECK_RET( node
, _T("radiobox wrong index") );
319 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
321 gtk_toggle_button_set_state( button
, 1 );
324 int wxRadioBox::GetSelection(void) const
326 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
330 wxNode
*node
= m_boxes
.First();
333 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
334 if (button
->active
) return count
;
339 wxFAIL_MSG( _T("wxRadioBox none selected") );
344 wxString
wxRadioBox::GetString( int n
) const
346 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
348 wxNode
*node
= m_boxes
.Nth( n
);
350 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
352 GtkButton
*button
= GTK_BUTTON( node
->Data() );
353 GtkLabel
*label
= GTK_LABEL( button
->child
);
355 return wxString( label
->label
);
358 wxString
wxRadioBox::GetLabel( int item
) const
360 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
362 return GetString( item
);
365 void wxRadioBox::SetLabel( const wxString
& label
)
367 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
369 wxControl::SetLabel( label
);
371 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
374 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
376 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
378 wxNode
*node
= m_boxes
.Nth( item
);
380 wxCHECK_RET( node
, _T("radiobox wrong index") );
382 GtkButton
*button
= GTK_BUTTON( node
->Data() );
383 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
385 gtk_label_set( g_label
, label
.mbc_str() );
388 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
390 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
393 bool wxRadioBox::Enable( bool enable
)
395 if ( !wxControl::Enable( enable
) )
398 wxNode
*node
= m_boxes
.First();
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
);
411 void wxRadioBox::Enable( int item
, bool enable
)
413 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
415 wxNode
*node
= m_boxes
.Nth( item
);
417 wxCHECK_RET( node
, _T("radiobox wrong index") );
419 GtkButton
*button
= GTK_BUTTON( node
->Data() );
420 GtkWidget
*label
= button
->child
;
421 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
422 gtk_widget_set_sensitive( label
, enable
);
425 void wxRadioBox::Show( int item
, bool show
)
427 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
429 wxNode
*node
= m_boxes
.Nth( item
);
431 wxCHECK_RET( node
, _T("radiobox wrong index") );
433 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
436 gtk_widget_show( button
);
438 gtk_widget_hide( button
);
441 wxString
wxRadioBox::GetStringSelection(void) const
443 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
445 wxNode
*node
= m_boxes
.First();
448 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
451 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
457 wxFAIL_MSG( _T("wxRadioBox none selected") );
461 bool wxRadioBox::SetStringSelection( const wxString
&s
)
463 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
465 int res
= FindString( s
);
466 if (res
== -1) return FALSE
;
472 int wxRadioBox::Number(void) const
474 return m_boxes
.Number();
477 int wxRadioBox::GetNumberOfRowsOrCols(void) const
482 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
484 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
487 void wxRadioBox::ApplyWidgetStyle()
491 gtk_widget_set_style( m_widget
, m_widgetStyle
);
493 wxNode
*node
= m_boxes
.First();
496 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
497 gtk_widget_set_style( widget
, m_widgetStyle
);
499 GtkButton
*button
= GTK_BUTTON( node
->Data() );
500 gtk_widget_set_style( button
->child
, m_widgetStyle
);
506 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
508 if (window
== m_widget
->window
) return TRUE
;
510 wxNode
*node
= m_boxes
.First();
513 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
515 if (window
== button
->window
) return TRUE
;