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 "gdk/gdkkeysyms.h"
24 #include "wx/gtk/win_gtk.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 extern void wxapp_install_idle_handler();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern bool g_blockEventsOnDrag
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
45 if (g_isIdle
) wxapp_install_idle_handler();
47 if (!rb
->m_hasVMT
) return;
48 if (g_blockEventsOnDrag
) return;
50 if (rb
->m_alreadySent
)
52 rb
->m_alreadySent
= FALSE
;
56 rb
->m_alreadySent
= TRUE
;
58 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
59 event
.SetInt( rb
->GetSelection() );
60 event
.SetString( rb
->GetStringSelection() );
61 event
.SetEventObject( rb
);
62 rb
->GetEventHandler()->ProcessEvent(event
);
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
69 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
71 wxRadioBox::wxRadioBox()
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 if (!PreCreation( parent
, pos
, size
) ||
86 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
88 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
92 m_widget
= gtk_frame_new( title
.mbc_str() );
94 m_majorDim
= majorDim
;
96 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
99 GSList
*radio_button_group
= (GSList
*) NULL
;
100 for (int i
= 0; i
< n
; i
++)
103 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
106 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
108 if ( *pc
!= wxT('&') )
112 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
114 m_boxes
.Append( (wxObject
*) m_radio
);
116 ConnectWidget( GTK_WIDGET(m_radio
) );
118 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
120 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
121 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
123 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
125 m_x
+10, m_y
+10+(i
*24), 10, 10 );
128 wxSize ls
= LayoutItems();
130 wxSize newSize
= size
;
131 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
132 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
133 SetSize( newSize
.x
, newSize
.y
);
135 m_parent
->DoAddChild( this );
141 SetBackgroundColour( parent
->GetBackgroundColour() );
142 SetForegroundColour( parent
->GetForegroundColour() );
143 SetFont( parent
->GetFont() );
150 wxRadioBox::~wxRadioBox()
152 wxNode
*node
= m_boxes
.First();
155 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
156 gtk_widget_destroy( button
);
161 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
163 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
168 wxSize
wxRadioBox::LayoutItems()
173 if ( m_majorDim
== 0 )
175 // avoid dividing by 0 below
176 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
181 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
187 if (HasFlag(wxRA_SPECIFY_COLS
))
189 num_of_cols
= m_majorDim
;
190 num_of_rows
= num_per_major
;
194 num_of_cols
= num_per_major
;
195 num_of_rows
= m_majorDim
;
198 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
199 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
201 for (int j
= 0; j
< num_of_cols
; j
++)
206 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
207 for (int i1
= 0; i1
< num_of_rows
; i1
++)
209 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
210 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
211 GdkFont
*font
= m_widget
->style
->font
;
212 int len
= 22+gdk_string_measure( font
, label
->label
);
213 if (len
> max_len
) max_len
= len
;
215 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
222 // we don't know the max_len before
224 node
= m_boxes
.Nth( j
*num_of_rows
);
225 for (int i2
= 0; i2
< num_of_rows
; i2
++)
227 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
229 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
235 if (y
> res
.y
) res
.y
= y
;
247 wxNode
*node
= m_boxes
.First();
250 GtkButton
*button
= GTK_BUTTON( node
->Data() );
251 GtkLabel
*label
= GTK_LABEL( button
->child
);
253 GdkFont
*font
= m_widget
->style
->font
;
254 int len
= 22+gdk_string_measure( font
, label
->label
);
255 if (len
> max
) max
= len
;
260 node
= m_boxes
.First();
263 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
265 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
277 bool wxRadioBox::Show( bool show
)
279 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
281 if (!wxControl::Show(show
))
287 if ((m_windowStyle
& wxNO_BORDER
) != 0)
288 gtk_widget_hide( m_widget
);
290 wxNode
*node
= m_boxes
.First();
293 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
295 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
303 int wxRadioBox::FindString( const wxString
&s
) const
305 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
309 wxNode
*node
= m_boxes
.First();
312 GtkButton
*button
= GTK_BUTTON( node
->Data() );
314 GtkLabel
*label
= GTK_LABEL( button
->child
);
315 if (s
== label
->label
) return count
;
324 void wxRadioBox::SetFocus()
326 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
328 if (m_boxes
.GetCount() == 0) return;
330 wxNode
*node
= m_boxes
.First();
333 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
336 gtk_widget_grab_focus( GTK_WIDGET(button
) );
345 void wxRadioBox::SetSelection( int n
)
347 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
349 wxNode
*node
= m_boxes
.Nth( n
);
351 wxCHECK_RET( node
, wxT("radiobox wrong index") );
353 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
357 gtk_toggle_button_set_state( button
, 1 );
362 int wxRadioBox::GetSelection(void) const
364 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
368 wxNode
*node
= m_boxes
.First();
371 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
372 if (button
->active
) return count
;
377 wxFAIL_MSG( wxT("wxRadioBox none selected") );
382 wxString
wxRadioBox::GetString( int n
) const
384 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
386 wxNode
*node
= m_boxes
.Nth( n
);
388 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
390 GtkButton
*button
= GTK_BUTTON( node
->Data() );
391 GtkLabel
*label
= GTK_LABEL( button
->child
);
393 return wxString( label
->label
);
396 wxString
wxRadioBox::GetLabel( int item
) const
398 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
400 return GetString( item
);
403 void wxRadioBox::SetLabel( const wxString
& label
)
405 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
407 wxControl::SetLabel( label
);
409 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
412 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
414 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
416 wxNode
*node
= m_boxes
.Nth( item
);
418 wxCHECK_RET( node
, wxT("radiobox wrong index") );
420 GtkButton
*button
= GTK_BUTTON( node
->Data() );
421 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
423 gtk_label_set( g_label
, label
.mbc_str() );
426 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
428 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
431 bool wxRadioBox::Enable( bool enable
)
433 if ( !wxControl::Enable( enable
) )
436 wxNode
*node
= m_boxes
.First();
439 GtkButton
*button
= GTK_BUTTON( node
->Data() );
440 GtkWidget
*label
= button
->child
;
441 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
442 gtk_widget_set_sensitive( label
, enable
);
449 void wxRadioBox::Enable( int item
, bool enable
)
451 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
453 wxNode
*node
= m_boxes
.Nth( item
);
455 wxCHECK_RET( node
, wxT("radiobox wrong index") );
457 GtkButton
*button
= GTK_BUTTON( node
->Data() );
458 GtkWidget
*label
= button
->child
;
459 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
460 gtk_widget_set_sensitive( label
, enable
);
463 void wxRadioBox::Show( int item
, bool show
)
465 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
467 wxNode
*node
= m_boxes
.Nth( item
);
469 wxCHECK_RET( node
, wxT("radiobox wrong index") );
471 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
474 gtk_widget_show( button
);
476 gtk_widget_hide( button
);
479 wxString
wxRadioBox::GetStringSelection() const
481 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
483 wxNode
*node
= m_boxes
.First();
486 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
489 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
495 wxFAIL_MSG( wxT("wxRadioBox none selected") );
499 bool wxRadioBox::SetStringSelection( const wxString
&s
)
501 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
503 int res
= FindString( s
);
504 if (res
== -1) return FALSE
;
510 int wxRadioBox::Number() const
512 return m_boxes
.Number();
515 int wxRadioBox::GetNumberOfRowsOrCols() const
520 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
522 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
525 void wxRadioBox::DisableEvents()
527 wxNode
*node
= m_boxes
.First();
530 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
531 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
537 void wxRadioBox::EnableEvents()
539 wxNode
*node
= m_boxes
.First();
542 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
543 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
549 void wxRadioBox::ApplyWidgetStyle()
553 gtk_widget_set_style( m_widget
, m_widgetStyle
);
555 wxNode
*node
= m_boxes
.First();
558 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
559 gtk_widget_set_style( widget
, m_widgetStyle
);
561 GtkButton
*button
= GTK_BUTTON( node
->Data() );
562 gtk_widget_set_style( button
->child
, m_widgetStyle
);
568 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
570 if (window
== m_widget
->window
) return TRUE
;
572 wxNode
*node
= m_boxes
.First();
575 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
577 if (window
== button
->window
) return TRUE
;