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 void wxapp_install_idle_handler();
29 //-----------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------
33 extern bool g_blockEventsOnDrag
;
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
41 if (g_isIdle
) wxapp_install_idle_handler();
43 if (!rb
->HasVMT()) return;
44 if (g_blockEventsOnDrag
) return;
46 if (rb
->m_alreadySent
)
48 rb
->m_alreadySent
= FALSE
;
52 rb
->m_alreadySent
= TRUE
;
54 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
55 event
.SetInt( rb
->GetSelection() );
56 event
.SetString( rb
->GetStringSelection() );
57 event
.SetEventObject( rb
);
58 rb
->GetEventHandler()->ProcessEvent(event
);
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
67 BEGIN_EVENT_TABLE(wxRadioBox
, wxControl
)
68 EVT_SIZE(wxRadioBox::OnSize
)
71 wxRadioBox::wxRadioBox(void)
75 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
76 const wxPoint
&pos
, const wxSize
&size
,
77 int n
, const wxString choices
[], int majorDim
,
78 long style
, const wxValidator
& validator
,
79 const wxString
&name
)
81 m_alreadySent
= FALSE
;
83 m_acceptsFocus
= TRUE
;
85 PreCreation( parent
, id
, pos
, size
, style
, name
);
87 SetValidator( validator
);
89 m_widget
= gtk_frame_new( title
.mbc_str() );
91 m_majorDim
= majorDim
;
93 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
95 GSList
*radio_button_group
= (GSList
*) NULL
;
96 for (int i
= 0; i
< n
; i
++)
98 if (i
) radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
100 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, choices
[i
].mbc_str() ) );
102 m_boxes
.Append( (wxObject
*) m_radio
);
104 ConnectWidget( GTK_WIDGET(m_radio
) );
106 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
108 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
109 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
111 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
),
113 m_x
+10, m_y
+10+(i
*24), 10, 10 );
116 wxSize ls
= LayoutItems();
118 wxSize newSize
= size
;
119 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
120 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
121 SetSize( newSize
.x
, newSize
.y
);
123 m_parent
->AddChild( this );
125 (m_parent
->m_insertCallback
)( m_parent
, this );
131 SetBackgroundColour( parent
->GetBackgroundColour() );
132 SetForegroundColour( parent
->GetForegroundColour() );
133 SetFont( parent
->GetFont() );
140 wxRadioBox::~wxRadioBox(void)
142 wxNode
*node
= m_boxes
.First();
145 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
146 gtk_widget_destroy( button
);
151 void wxRadioBox::OnSize( wxSizeEvent
&event
)
153 wxControl::OnSize( event
);
158 wxSize
wxRadioBox::LayoutItems()
163 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
167 if (m_windowStyle
& wxRA_HORIZONTAL
)
170 for (int j
= 0; j
< m_majorDim
; j
++)
175 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
176 for (int i1
= 0; i1
< num_per_major
; i1
++)
178 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
179 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
180 GdkFont
*font
= m_widget
->style
->font
;
181 int len
= 22+gdk_string_measure( font
, label
->label
);
182 if (len
> max_len
) max_len
= len
;
184 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
191 // we don't know the max_len before
193 node
= m_boxes
.Nth( j
*num_per_major
);
194 for (int i2
= 0; i2
< num_per_major
; i2
++)
196 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
198 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
204 if (y
> res
.y
) res
.y
= y
;
216 wxNode
*node
= m_boxes
.First();
219 GtkButton
*button
= GTK_BUTTON( node
->Data() );
220 GtkLabel
*label
= GTK_LABEL( button
->child
);
222 GdkFont
*font
= m_widget
->style
->font
;
223 int len
= 22+gdk_string_measure( font
, label
->label
);
224 if (len
> max
) max
= len
;
229 node
= m_boxes
.First();
232 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
234 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
246 bool wxRadioBox::Show( bool show
)
248 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
250 wxWindow::Show( show
);
252 if ((m_windowStyle
& wxNO_BORDER
) == 0)
253 gtk_widget_hide( m_widget
);
255 wxNode
*node
= m_boxes
.First();
258 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
260 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
268 int wxRadioBox::FindString( const wxString
&s
) const
270 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
274 wxNode
*node
= m_boxes
.First();
277 GtkButton
*button
= GTK_BUTTON( node
->Data() );
279 GtkLabel
*label
= GTK_LABEL( button
->child
);
280 if (s
== label
->label
) return count
;
289 void wxRadioBox::SetFocus()
291 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
293 if (m_boxes
.GetCount() == 0) return;
295 wxNode
*node
= m_boxes
.First();
298 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
301 gtk_widget_grab_focus( GTK_WIDGET(button
) );
310 void wxRadioBox::SetSelection( int n
)
312 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
314 wxNode
*node
= m_boxes
.Nth( n
);
316 wxCHECK_RET( node
, _T("radiobox wrong index") );
318 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
320 gtk_toggle_button_set_state( button
, 1 );
323 int wxRadioBox::GetSelection(void) const
325 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
329 wxNode
*node
= m_boxes
.First();
332 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
333 if (button
->active
) return count
;
338 wxFAIL_MSG( _T("wxRadioBox none selected") );
343 wxString
wxRadioBox::GetString( int n
) const
345 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
347 wxNode
*node
= m_boxes
.Nth( n
);
349 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
351 GtkButton
*button
= GTK_BUTTON( node
->Data() );
352 GtkLabel
*label
= GTK_LABEL( button
->child
);
354 return wxString( label
->label
);
357 wxString
wxRadioBox::GetLabel( int item
) const
359 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
361 return GetString( item
);
364 void wxRadioBox::SetLabel( const wxString
& label
)
366 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
368 wxControl::SetLabel( label
);
370 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
373 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
375 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
377 wxNode
*node
= m_boxes
.Nth( item
);
379 wxCHECK_RET( node
, _T("radiobox wrong index") );
381 GtkButton
*button
= GTK_BUTTON( node
->Data() );
382 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
384 gtk_label_set( g_label
, label
.mbc_str() );
387 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
389 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
392 void wxRadioBox::Enable( bool enable
)
394 wxControl::Enable( enable
);
396 wxNode
*node
= m_boxes
.First();
399 GtkButton
*button
= GTK_BUTTON( node
->Data() );
400 GtkWidget
*label
= button
->child
;
401 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
402 gtk_widget_set_sensitive( label
, enable
);
407 void wxRadioBox::Enable( int item
, bool enable
)
409 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
411 wxNode
*node
= m_boxes
.Nth( item
);
413 wxCHECK_RET( node
, _T("radiobox wrong index") );
415 GtkButton
*button
= GTK_BUTTON( node
->Data() );
416 GtkWidget
*label
= button
->child
;
417 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
418 gtk_widget_set_sensitive( label
, enable
);
421 void wxRadioBox::Show( int item
, bool show
)
423 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
425 wxNode
*node
= m_boxes
.Nth( item
);
427 wxCHECK_RET( node
, _T("radiobox wrong index") );
429 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
432 gtk_widget_show( button
);
434 gtk_widget_hide( button
);
437 wxString
wxRadioBox::GetStringSelection(void) const
439 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
441 wxNode
*node
= m_boxes
.First();
444 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
447 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
453 wxFAIL_MSG( _T("wxRadioBox none selected") );
457 bool wxRadioBox::SetStringSelection( const wxString
&s
)
459 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
461 int res
= FindString( s
);
462 if (res
== -1) return FALSE
;
468 int wxRadioBox::Number(void) const
470 return m_boxes
.Number();
473 int wxRadioBox::GetNumberOfRowsOrCols(void) const
478 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
480 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
483 void wxRadioBox::ApplyWidgetStyle()
487 gtk_widget_set_style( m_widget
, m_widgetStyle
);
489 wxNode
*node
= m_boxes
.First();
492 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
493 gtk_widget_set_style( widget
, m_widgetStyle
);
495 GtkButton
*button
= GTK_BUTTON( node
->Data() );
496 gtk_widget_set_style( button
->child
, m_widgetStyle
);
502 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
504 if (window
== m_widget
->window
) return TRUE
;
506 wxNode
*node
= m_boxes
.First();
509 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
511 if (window
== button
->window
) return TRUE
;