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 if (!PreCreation( parent
, pos
, size
) ||
89 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
91 wxFAIL_MSG( T("wxRadioBox creation failed") );
95 m_widget
= gtk_frame_new( title
.mbc_str() );
97 m_majorDim
= majorDim
;
99 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
102 GSList
*radio_button_group
= (GSList
*) NULL
;
103 for (int i
= 0; i
< n
; i
++)
106 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
109 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
115 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
117 m_boxes
.Append( (wxObject
*) m_radio
);
119 ConnectWidget( GTK_WIDGET(m_radio
) );
121 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
123 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
124 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
126 gtk_myfixed_put( GTK_MYFIXED(m_parent
->m_wxwindow
),
128 m_x
+10, m_y
+10+(i
*24), 10, 10 );
131 wxSize ls
= LayoutItems();
133 wxSize newSize
= size
;
134 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
135 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
136 SetSize( newSize
.x
, newSize
.y
);
138 m_parent
->DoAddChild( this );
144 SetBackgroundColour( parent
->GetBackgroundColour() );
145 SetForegroundColour( parent
->GetForegroundColour() );
146 SetFont( parent
->GetFont() );
153 wxRadioBox::~wxRadioBox()
155 wxNode
*node
= m_boxes
.First();
158 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
159 gtk_widget_destroy( button
);
164 void wxRadioBox::OnSize( wxSizeEvent
&event
)
171 wxSize
wxRadioBox::LayoutItems()
176 if ( m_majorDim
== 0 )
178 // avoid dividing by 0 below
179 wxFAIL_MSG( T("dimension of radiobox should not be 0!") );
184 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
190 if (HasFlag(wxRA_SPECIFY_COLS
))
192 num_of_cols
= m_majorDim
;
193 num_of_rows
= num_per_major
;
197 num_of_cols
= num_per_major
;
198 num_of_rows
= m_majorDim
;
201 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
202 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
204 for (int j
= 0; j
< num_of_cols
; j
++)
209 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
210 for (int i1
= 0; i1
< num_of_rows
; i1
++)
212 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
213 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
214 GdkFont
*font
= m_widget
->style
->font
;
215 int len
= 22+gdk_string_measure( font
, label
->label
);
216 if (len
> max_len
) max_len
= len
;
218 gtk_myfixed_move( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
225 // we don't know the max_len before
227 node
= m_boxes
.Nth( j
*num_of_rows
);
228 for (int i2
= 0; i2
< num_of_rows
; i2
++)
230 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
232 gtk_myfixed_resize( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, max_len
, 20 );
238 if (y
> res
.y
) res
.y
= y
;
250 wxNode
*node
= m_boxes
.First();
253 GtkButton
*button
= GTK_BUTTON( node
->Data() );
254 GtkLabel
*label
= GTK_LABEL( button
->child
);
256 GdkFont
*font
= m_widget
->style
->font
;
257 int len
= 22+gdk_string_measure( font
, label
->label
);
258 if (len
> max
) max
= len
;
263 node
= m_boxes
.First();
266 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
268 gtk_myfixed_set_size( GTK_MYFIXED(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
280 bool wxRadioBox::Show( bool show
)
282 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, T("invalid radiobox") );
284 wxWindow::Show( show
);
286 if ((m_windowStyle
& wxNO_BORDER
) != 0)
287 gtk_widget_hide( m_widget
);
289 wxNode
*node
= m_boxes
.First();
292 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
294 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
302 int wxRadioBox::FindString( const wxString
&s
) const
304 wxCHECK_MSG( m_widget
!= NULL
, -1, T("invalid radiobox") );
308 wxNode
*node
= m_boxes
.First();
311 GtkButton
*button
= GTK_BUTTON( node
->Data() );
313 GtkLabel
*label
= GTK_LABEL( button
->child
);
314 if (s
== label
->label
) return count
;
323 void wxRadioBox::SetFocus()
325 wxCHECK_RET( m_widget
!= NULL
, T("invalid radiobox") );
327 if (m_boxes
.GetCount() == 0) return;
329 wxNode
*node
= m_boxes
.First();
332 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
335 gtk_widget_grab_focus( GTK_WIDGET(button
) );
344 void wxRadioBox::SetSelection( int n
)
346 wxCHECK_RET( m_widget
!= NULL
, T("invalid radiobox") );
348 wxNode
*node
= m_boxes
.Nth( n
);
350 wxCHECK_RET( node
, T("radiobox wrong index") );
352 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
356 gtk_toggle_button_set_state( button
, 1 );
361 int wxRadioBox::GetSelection(void) const
363 wxCHECK_MSG( m_widget
!= NULL
, -1, T("invalid radiobox") );
367 wxNode
*node
= m_boxes
.First();
370 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
371 if (button
->active
) return count
;
376 wxFAIL_MSG( T("wxRadioBox none selected") );
381 wxString
wxRadioBox::GetString( int n
) const
383 wxCHECK_MSG( m_widget
!= NULL
, T(""), T("invalid radiobox") );
385 wxNode
*node
= m_boxes
.Nth( n
);
387 wxCHECK_MSG( node
, T(""), T("radiobox wrong index") );
389 GtkButton
*button
= GTK_BUTTON( node
->Data() );
390 GtkLabel
*label
= GTK_LABEL( button
->child
);
392 return wxString( label
->label
);
395 wxString
wxRadioBox::GetLabel( int item
) const
397 wxCHECK_MSG( m_widget
!= NULL
, T(""), T("invalid radiobox") );
399 return GetString( item
);
402 void wxRadioBox::SetLabel( const wxString
& label
)
404 wxCHECK_RET( m_widget
!= NULL
, T("invalid radiobox") );
406 wxControl::SetLabel( label
);
408 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
411 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
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 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
422 gtk_label_set( g_label
, label
.mbc_str() );
425 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
427 wxFAIL_MSG(T("wxRadioBox::SetLabel not implemented."));
430 bool wxRadioBox::Enable( bool enable
)
432 if ( !wxControl::Enable( enable
) )
435 wxNode
*node
= m_boxes
.First();
438 GtkButton
*button
= GTK_BUTTON( node
->Data() );
439 GtkWidget
*label
= button
->child
;
440 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
441 gtk_widget_set_sensitive( label
, enable
);
448 void wxRadioBox::Enable( int item
, bool enable
)
450 wxCHECK_RET( m_widget
!= NULL
, T("invalid radiobox") );
452 wxNode
*node
= m_boxes
.Nth( item
);
454 wxCHECK_RET( node
, T("radiobox wrong index") );
456 GtkButton
*button
= GTK_BUTTON( node
->Data() );
457 GtkWidget
*label
= button
->child
;
458 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
459 gtk_widget_set_sensitive( label
, enable
);
462 void wxRadioBox::Show( int item
, bool show
)
464 wxCHECK_RET( m_widget
!= NULL
, T("invalid radiobox") );
466 wxNode
*node
= m_boxes
.Nth( item
);
468 wxCHECK_RET( node
, T("radiobox wrong index") );
470 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
473 gtk_widget_show( button
);
475 gtk_widget_hide( button
);
478 wxString
wxRadioBox::GetStringSelection() const
480 wxCHECK_MSG( m_widget
!= NULL
, T(""), T("invalid radiobox") );
482 wxNode
*node
= m_boxes
.First();
485 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
488 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
494 wxFAIL_MSG( T("wxRadioBox none selected") );
498 bool wxRadioBox::SetStringSelection( const wxString
&s
)
500 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, T("invalid radiobox") );
502 int res
= FindString( s
);
503 if (res
== -1) return FALSE
;
509 int wxRadioBox::Number() const
511 return m_boxes
.Number();
514 int wxRadioBox::GetNumberOfRowsOrCols() const
519 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
521 wxFAIL_MSG(T("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
524 void wxRadioBox::DisableEvents()
526 wxNode
*node
= m_boxes
.First();
529 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
530 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
536 void wxRadioBox::EnableEvents()
538 wxNode
*node
= m_boxes
.First();
541 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
542 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
548 void wxRadioBox::ApplyWidgetStyle()
552 gtk_widget_set_style( m_widget
, m_widgetStyle
);
554 wxNode
*node
= m_boxes
.First();
557 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
558 gtk_widget_set_style( widget
, m_widgetStyle
);
560 GtkButton
*button
= GTK_BUTTON( node
->Data() );
561 gtk_widget_set_style( button
->child
, m_widgetStyle
);
567 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
569 if (window
== m_widget
->window
) return TRUE
;
571 wxNode
*node
= m_boxes
.First();
574 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
576 if (window
== button
->window
) return TRUE
;