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>
25 #include "wx/gtk/win_gtk.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern void wxapp_install_idle_handler();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 extern bool g_blockEventsOnDrag
;
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static void gtk_radiobutton_clicked_callback( GtkWidget
*WXUNUSED(widget
), wxRadioBox
*rb
)
46 if (g_isIdle
) wxapp_install_idle_handler();
48 if (!rb
->m_hasVMT
) return;
49 if (g_blockEventsOnDrag
) return;
51 if (rb
->m_alreadySent
)
53 rb
->m_alreadySent
= FALSE
;
57 rb
->m_alreadySent
= TRUE
;
59 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
60 event
.SetInt( rb
->GetSelection() );
61 event
.SetString( rb
->GetStringSelection() );
62 event
.SetEventObject( rb
);
63 rb
->GetEventHandler()->ProcessEvent(event
);
66 //-----------------------------------------------------------------------------
68 //-----------------------------------------------------------------------------
70 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
73 wxapp_install_idle_handler();
75 if (!rb
->m_hasVMT
) return FALSE
;
76 if (g_blockEventsOnDrag
) return FALSE
;
78 if ((gdk_event
->keyval
!= GDK_Up
) &&
79 (gdk_event
->keyval
!= GDK_Down
) &&
80 (gdk_event
->keyval
!= GDK_Left
) &&
81 (gdk_event
->keyval
!= GDK_Right
))
86 wxNode
*node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
92 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
94 if ((gdk_event
->keyval
== GDK_Up
) ||
95 (gdk_event
->keyval
== GDK_Left
))
97 if (node
== rb
->m_boxes
.First())
98 node
= rb
->m_boxes
.Last();
100 node
= node
->Previous();
104 if (node
== rb
->m_boxes
.Last())
105 node
= rb
->m_boxes
.First();
110 GtkWidget
*button
= (GtkWidget
*) node
->Data();
112 gtk_widget_grab_focus( button
);
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
123 wxRadioBox::wxRadioBox()
127 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
128 const wxPoint
&pos
, const wxSize
&size
,
129 int n
, const wxString choices
[], int majorDim
,
130 long style
, const wxValidator
& validator
,
131 const wxString
&name
)
133 m_alreadySent
= FALSE
;
135 m_acceptsFocus
= TRUE
;
137 if (!PreCreation( parent
, pos
, size
) ||
138 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
140 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
144 m_widget
= gtk_frame_new( title
.mbc_str() );
146 m_majorDim
= majorDim
;
148 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
151 GSList
*radio_button_group
= (GSList
*) NULL
;
152 for (int i
= 0; i
< n
; i
++)
155 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
158 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
160 if ( *pc
!= wxT('&') )
164 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
166 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
167 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
169 m_boxes
.Append( (wxObject
*) m_radio
);
171 ConnectWidget( GTK_WIDGET(m_radio
) );
173 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
175 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
176 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
178 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
180 m_x
+10, m_y
+10+(i
*24), 10, 10 );
183 m_parent
->DoAddChild( this );
191 SetFont( parent
->GetFont() );
193 wxSize ls
= LayoutItems();
195 wxSize newSize
= size
;
196 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
197 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
198 SetSize( newSize
.x
, newSize
.y
);
200 SetBackgroundColour( parent
->GetBackgroundColour() );
201 SetForegroundColour( parent
->GetForegroundColour() );
208 wxRadioBox::~wxRadioBox()
210 wxNode
*node
= m_boxes
.First();
213 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
214 gtk_widget_destroy( button
);
219 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
221 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
226 wxSize
wxRadioBox::LayoutItems()
231 if ( m_majorDim
== 0 )
233 // avoid dividing by 0 below
234 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
239 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
245 if (HasFlag(wxRA_SPECIFY_COLS
))
247 num_of_cols
= m_majorDim
;
248 num_of_rows
= num_per_major
;
252 num_of_cols
= num_per_major
;
253 num_of_rows
= m_majorDim
;
256 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
257 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
259 for (int j
= 0; j
< num_of_cols
; j
++)
264 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
265 for (int i1
= 0; i1
< num_of_rows
; i1
++)
267 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
268 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
269 GdkFont
*font
= m_widget
->style
->font
;
270 int len
= 22+gdk_string_measure( font
, label
->label
);
271 if (len
> max_len
) max_len
= len
;
273 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
280 // we don't know the max_len before
282 node
= m_boxes
.Nth( j
*num_of_rows
);
283 for (int i2
= 0; i2
< num_of_rows
; i2
++)
285 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
287 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
293 if (y
> res
.y
) res
.y
= y
;
305 wxNode
*node
= m_boxes
.First();
308 GtkButton
*button
= GTK_BUTTON( node
->Data() );
309 GtkLabel
*label
= GTK_LABEL( button
->child
);
311 GdkFont
*font
= m_widget
->style
->font
;
312 int len
= 22+gdk_string_measure( font
, label
->label
);
313 if (len
> max
) max
= len
;
318 node
= m_boxes
.First();
321 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
323 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
335 bool wxRadioBox::Show( bool show
)
337 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
339 if (!wxControl::Show(show
))
345 if ((m_windowStyle
& wxNO_BORDER
) != 0)
346 gtk_widget_hide( m_widget
);
348 wxNode
*node
= m_boxes
.First();
351 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
353 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
361 int wxRadioBox::FindString( const wxString
&s
) const
363 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
367 wxNode
*node
= m_boxes
.First();
370 GtkButton
*button
= GTK_BUTTON( node
->Data() );
372 GtkLabel
*label
= GTK_LABEL( button
->child
);
373 if (s
== label
->label
) return count
;
382 void wxRadioBox::SetFocus()
384 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
386 if (m_boxes
.GetCount() == 0) return;
388 wxNode
*node
= m_boxes
.First();
391 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
394 gtk_widget_grab_focus( GTK_WIDGET(button
) );
402 void wxRadioBox::SetSelection( int n
)
404 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
406 wxNode
*node
= m_boxes
.Nth( n
);
408 wxCHECK_RET( node
, wxT("radiobox wrong index") );
410 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
414 gtk_toggle_button_set_state( button
, 1 );
419 int wxRadioBox::GetSelection(void) const
421 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
425 wxNode
*node
= m_boxes
.First();
428 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
429 if (button
->active
) return count
;
434 wxFAIL_MSG( wxT("wxRadioBox none selected") );
439 wxString
wxRadioBox::GetString( int n
) const
441 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
443 wxNode
*node
= m_boxes
.Nth( n
);
445 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
447 GtkButton
*button
= GTK_BUTTON( node
->Data() );
448 GtkLabel
*label
= GTK_LABEL( button
->child
);
450 return wxString( label
->label
);
453 wxString
wxRadioBox::GetLabel( int item
) const
455 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
457 return GetString( item
);
460 void wxRadioBox::SetLabel( const wxString
& label
)
462 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
464 wxControl::SetLabel( label
);
466 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
469 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
471 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
473 wxNode
*node
= m_boxes
.Nth( item
);
475 wxCHECK_RET( node
, wxT("radiobox wrong index") );
477 GtkButton
*button
= GTK_BUTTON( node
->Data() );
478 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
480 gtk_label_set( g_label
, label
.mbc_str() );
483 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
485 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
488 bool wxRadioBox::Enable( bool enable
)
490 if ( !wxControl::Enable( enable
) )
493 wxNode
*node
= m_boxes
.First();
496 GtkButton
*button
= GTK_BUTTON( node
->Data() );
497 GtkWidget
*label
= button
->child
;
498 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
499 gtk_widget_set_sensitive( label
, enable
);
506 void wxRadioBox::Enable( int item
, bool enable
)
508 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
510 wxNode
*node
= m_boxes
.Nth( item
);
512 wxCHECK_RET( node
, wxT("radiobox wrong index") );
514 GtkButton
*button
= GTK_BUTTON( node
->Data() );
515 GtkWidget
*label
= button
->child
;
516 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
517 gtk_widget_set_sensitive( label
, enable
);
520 void wxRadioBox::Show( int item
, bool show
)
522 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
524 wxNode
*node
= m_boxes
.Nth( item
);
526 wxCHECK_RET( node
, wxT("radiobox wrong index") );
528 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
531 gtk_widget_show( button
);
533 gtk_widget_hide( button
);
536 wxString
wxRadioBox::GetStringSelection() const
538 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
540 wxNode
*node
= m_boxes
.First();
543 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
546 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
552 wxFAIL_MSG( wxT("wxRadioBox none selected") );
556 bool wxRadioBox::SetStringSelection( const wxString
&s
)
558 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
560 int res
= FindString( s
);
561 if (res
== -1) return FALSE
;
567 int wxRadioBox::Number() const
569 return m_boxes
.Number();
572 int wxRadioBox::GetNumberOfRowsOrCols() const
577 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
579 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
582 void wxRadioBox::GtkDisableEvents()
584 wxNode
*node
= m_boxes
.First();
587 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
588 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
594 void wxRadioBox::GtkEnableEvents()
596 wxNode
*node
= m_boxes
.First();
599 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
600 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
606 void wxRadioBox::ApplyWidgetStyle()
610 gtk_widget_set_style( m_widget
, m_widgetStyle
);
612 wxNode
*node
= m_boxes
.First();
615 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
616 gtk_widget_set_style( widget
, m_widgetStyle
);
618 GtkButton
*button
= GTK_BUTTON( node
->Data() );
619 gtk_widget_set_style( button
->child
, m_widgetStyle
);
626 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
628 wxNode
*node
= m_boxes
.First();
631 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
632 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
636 #endif // wxUSE_TOOLTIPS
638 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
640 if (window
== m_widget
->window
) return TRUE
;
642 wxNode
*node
= m_boxes
.First();
645 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
647 if (window
== button
->window
) return TRUE
;