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
), GTK_WIDGET(m_radio
), m_x
+10, m_y
+10+(i
*24) );
115 wxSize ls
= LayoutItems();
117 wxSize newSize
= size
;
118 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
119 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
120 SetSize( newSize
.x
, newSize
.y
);
122 m_parent
->AddChild( this );
124 (m_parent
->m_insertCallback
)( m_parent
, this );
130 SetBackgroundColour( parent
->GetBackgroundColour() );
131 SetForegroundColour( parent
->GetForegroundColour() );
132 SetFont( parent
->GetFont() );
139 wxRadioBox::~wxRadioBox(void)
141 wxNode
*node
= m_boxes
.First();
144 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
145 gtk_widget_destroy( button
);
150 void wxRadioBox::OnSize( wxSizeEvent
&event
)
152 wxControl::OnSize( event
);
157 wxSize
wxRadioBox::LayoutItems()
162 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
166 if (m_windowStyle
& wxRA_HORIZONTAL
)
169 for (int j
= 0; j
< m_majorDim
; j
++)
174 wxNode
*node
= m_boxes
.Nth( j
*num_per_major
);
175 for (int i1
= 0; i1
< num_per_major
; i1
++)
177 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
178 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
179 GdkFont
*font
= m_widget
->style
->font
;
180 int len
= 22+gdk_string_measure( font
, label
->label
);
181 if (len
> max_len
) max_len
= len
;
183 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
190 // we don't know the max_len before
192 node
= m_boxes
.Nth( j
*num_per_major
);
193 for (int i2
= 0; i2
< num_per_major
; i2
++)
195 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
197 gtk_widget_set_usize( button
, max_len
, 20 );
203 if (y
> res
.y
) res
.y
= y
;
215 wxNode
*node
= m_boxes
.First();
218 GtkButton
*button
= GTK_BUTTON( node
->Data() );
219 GtkLabel
*label
= GTK_LABEL( button
->child
);
221 GdkFont
*font
= m_widget
->style
->font
;
222 int len
= 22+gdk_string_measure( font
, label
->label
);
223 if (len
> max
) max
= len
;
228 node
= m_boxes
.First();
231 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
233 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
235 gtk_widget_set_usize( button
, max
, 20 );
246 bool wxRadioBox::Show( bool show
)
248 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
250 wxWindow::Show( show
);
252 wxNode
*node
= m_boxes
.First();
255 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
257 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
265 int wxRadioBox::FindString( const wxString
&s
) const
267 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
271 wxNode
*node
= m_boxes
.First();
274 GtkButton
*button
= GTK_BUTTON( node
->Data() );
276 GtkLabel
*label
= GTK_LABEL( button
->child
);
277 if (s
== label
->label
) return count
;
286 void wxRadioBox::SetFocus()
288 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
290 if (m_boxes
.GetCount() == 0) return;
292 wxNode
*node
= m_boxes
.First();
295 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
298 gtk_widget_grab_focus( GTK_WIDGET(button
) );
307 void wxRadioBox::SetSelection( int n
)
309 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
311 wxNode
*node
= m_boxes
.Nth( n
);
313 wxCHECK_RET( node
, _T("radiobox wrong index") );
315 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
317 gtk_toggle_button_set_state( button
, 1 );
320 int wxRadioBox::GetSelection(void) const
322 wxCHECK_MSG( m_widget
!= NULL
, -1, _T("invalid radiobox") );
326 wxNode
*node
= m_boxes
.First();
329 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
330 if (button
->active
) return count
;
335 wxFAIL_MSG( _T("wxRadioBox none selected") );
340 wxString
wxRadioBox::GetString( int n
) const
342 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
344 wxNode
*node
= m_boxes
.Nth( n
);
346 wxCHECK_MSG( node
, _T(""), _T("radiobox wrong index") );
348 GtkButton
*button
= GTK_BUTTON( node
->Data() );
349 GtkLabel
*label
= GTK_LABEL( button
->child
);
351 return wxString( label
->label
);
354 wxString
wxRadioBox::GetLabel( int item
) const
356 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
358 return GetString( item
);
361 void wxRadioBox::SetLabel( const wxString
& label
)
363 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
365 wxControl::SetLabel( label
);
367 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
370 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
372 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
374 wxNode
*node
= m_boxes
.Nth( item
);
376 wxCHECK_RET( node
, _T("radiobox wrong index") );
378 GtkButton
*button
= GTK_BUTTON( node
->Data() );
379 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
381 gtk_label_set( g_label
, label
.mbc_str() );
384 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
386 wxFAIL_MSG(_T("wxRadioBox::SetLabel not implemented."));
389 void wxRadioBox::Enable( bool enable
)
391 wxControl::Enable( enable
);
393 wxNode
*node
= m_boxes
.First();
396 GtkButton
*button
= GTK_BUTTON( node
->Data() );
397 GtkWidget
*label
= button
->child
;
398 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
399 gtk_widget_set_sensitive( label
, enable
);
404 void wxRadioBox::Enable( int item
, bool enable
)
406 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
408 wxNode
*node
= m_boxes
.Nth( item
);
410 wxCHECK_RET( node
, _T("radiobox wrong index") );
412 GtkButton
*button
= GTK_BUTTON( node
->Data() );
413 GtkWidget
*label
= button
->child
;
414 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
415 gtk_widget_set_sensitive( label
, enable
);
418 void wxRadioBox::Show( int item
, bool show
)
420 wxCHECK_RET( m_widget
!= NULL
, _T("invalid radiobox") );
422 wxNode
*node
= m_boxes
.Nth( item
);
424 wxCHECK_RET( node
, _T("radiobox wrong index") );
426 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
429 gtk_widget_show( button
);
431 gtk_widget_hide( button
);
434 wxString
wxRadioBox::GetStringSelection(void) const
436 wxCHECK_MSG( m_widget
!= NULL
, _T(""), _T("invalid radiobox") );
438 wxNode
*node
= m_boxes
.First();
441 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
444 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
450 wxFAIL_MSG( _T("wxRadioBox none selected") );
454 bool wxRadioBox::SetStringSelection( const wxString
&s
)
456 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, _T("invalid radiobox") );
458 int res
= FindString( s
);
459 if (res
== -1) return FALSE
;
465 int wxRadioBox::Number(void) const
467 return m_boxes
.Number();
470 int wxRadioBox::GetNumberOfRowsOrCols(void) const
475 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
477 wxFAIL_MSG(_T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
480 void wxRadioBox::ApplyWidgetStyle()
484 gtk_widget_set_style( m_widget
, m_widgetStyle
);
486 wxNode
*node
= m_boxes
.First();
489 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
490 gtk_widget_set_style( widget
, m_widgetStyle
);
492 GtkButton
*button
= GTK_BUTTON( node
->Data() );
493 gtk_widget_set_style( button
->child
, m_widgetStyle
);
499 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
501 if (window
== m_widget
->window
) return TRUE
;
503 wxNode
*node
= m_boxes
.First();
506 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
508 if (window
== button
->window
) return TRUE
;