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 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
72 wxapp_install_idle_handler();
74 if (!rb
->m_hasVMT
) return FALSE
;
75 if (g_blockEventsOnDrag
) return FALSE
;
77 if ((gdk_event
->keyval
!= GDK_Up
) &&
78 (gdk_event
->keyval
!= GDK_Down
) &&
79 (gdk_event
->keyval
!= GDK_Left
) &&
80 (gdk_event
->keyval
!= GDK_Right
))
85 wxNode
*node
= rb
->m_boxes
.Find( (wxObject
*) widget
);
91 gtk_signal_emit_stop_by_name( GTK_OBJECT(widget
), "key_press_event" );
93 if ((gdk_event
->keyval
== GDK_Up
) ||
94 (gdk_event
->keyval
== GDK_Left
))
96 if (node
== rb
->m_boxes
.First())
97 node
= rb
->m_boxes
.Last();
99 node
= node
->Previous();
103 if (node
== rb
->m_boxes
.Last())
104 node
= rb
->m_boxes
.First();
109 GtkWidget
*button
= (GtkWidget
*) node
->Data();
111 gtk_widget_grab_focus( button
);
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
122 wxRadioBox::wxRadioBox()
126 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
127 const wxPoint
&pos
, const wxSize
&size
,
128 int n
, const wxString choices
[], int majorDim
,
129 long style
, const wxValidator
& validator
,
130 const wxString
&name
)
132 m_alreadySent
= FALSE
;
134 m_acceptsFocus
= TRUE
;
136 if (!PreCreation( parent
, pos
, size
) ||
137 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
139 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
143 m_widget
= gtk_frame_new( title
.mbc_str() );
145 m_majorDim
= majorDim
;
147 GtkRadioButton
*m_radio
= (GtkRadioButton
*) NULL
;
150 GSList
*radio_button_group
= (GSList
*) NULL
;
151 for (int i
= 0; i
< n
; i
++)
154 radio_button_group
= gtk_radio_button_group( GTK_RADIO_BUTTON(m_radio
) );
157 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
159 if ( *pc
!= wxT('&') )
163 m_radio
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, label
.mbc_str() ) );
165 gtk_signal_connect( GTK_OBJECT(m_radio
), "key_press_event",
166 GTK_SIGNAL_FUNC(gtk_radiobox_keypress_callback
), (gpointer
)this );
168 m_boxes
.Append( (wxObject
*) m_radio
);
170 ConnectWidget( GTK_WIDGET(m_radio
) );
172 if (!i
) gtk_toggle_button_set_state( GTK_TOGGLE_BUTTON(m_radio
), TRUE
);
174 gtk_signal_connect( GTK_OBJECT(m_radio
), "clicked",
175 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
177 gtk_pizza_put( GTK_PIZZA(m_parent
->m_wxwindow
),
179 m_x
+10, m_y
+10+(i
*24), 10, 10 );
182 wxSize ls
= LayoutItems();
184 wxSize newSize
= size
;
185 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
186 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
187 SetSize( newSize
.x
, newSize
.y
);
189 m_parent
->DoAddChild( this );
195 SetBackgroundColour( parent
->GetBackgroundColour() );
196 SetForegroundColour( parent
->GetForegroundColour() );
197 SetFont( parent
->GetFont() );
204 wxRadioBox::~wxRadioBox()
206 wxNode
*node
= m_boxes
.First();
209 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
210 gtk_widget_destroy( button
);
215 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
217 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
222 wxSize
wxRadioBox::LayoutItems()
227 if ( m_majorDim
== 0 )
229 // avoid dividing by 0 below
230 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
235 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
241 if (HasFlag(wxRA_SPECIFY_COLS
))
243 num_of_cols
= m_majorDim
;
244 num_of_rows
= num_per_major
;
248 num_of_cols
= num_per_major
;
249 num_of_rows
= m_majorDim
;
252 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
253 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
255 for (int j
= 0; j
< num_of_cols
; j
++)
260 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
261 for (int i1
= 0; i1
< num_of_rows
; i1
++)
263 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
264 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
265 GdkFont
*font
= m_widget
->style
->font
;
266 int len
= 22+gdk_string_measure( font
, label
->label
);
267 if (len
> max_len
) max_len
= len
;
269 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
276 // we don't know the max_len before
278 node
= m_boxes
.Nth( j
*num_of_rows
);
279 for (int i2
= 0; i2
< num_of_rows
; i2
++)
281 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
283 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
289 if (y
> res
.y
) res
.y
= y
;
301 wxNode
*node
= m_boxes
.First();
304 GtkButton
*button
= GTK_BUTTON( node
->Data() );
305 GtkLabel
*label
= GTK_LABEL( button
->child
);
307 GdkFont
*font
= m_widget
->style
->font
;
308 int len
= 22+gdk_string_measure( font
, label
->label
);
309 if (len
> max
) max
= len
;
314 node
= m_boxes
.First();
317 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
319 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
331 bool wxRadioBox::Show( bool show
)
333 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
335 if (!wxControl::Show(show
))
341 if ((m_windowStyle
& wxNO_BORDER
) != 0)
342 gtk_widget_hide( m_widget
);
344 wxNode
*node
= m_boxes
.First();
347 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
349 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
357 int wxRadioBox::FindString( const wxString
&s
) const
359 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
363 wxNode
*node
= m_boxes
.First();
366 GtkButton
*button
= GTK_BUTTON( node
->Data() );
368 GtkLabel
*label
= GTK_LABEL( button
->child
);
369 if (s
== label
->label
) return count
;
378 void wxRadioBox::SetFocus()
380 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
382 if (m_boxes
.GetCount() == 0) return;
384 wxNode
*node
= m_boxes
.First();
387 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
390 gtk_widget_grab_focus( GTK_WIDGET(button
) );
398 void wxRadioBox::SetSelection( int n
)
400 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
402 wxNode
*node
= m_boxes
.Nth( n
);
404 wxCHECK_RET( node
, wxT("radiobox wrong index") );
406 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
410 gtk_toggle_button_set_state( button
, 1 );
415 int wxRadioBox::GetSelection(void) const
417 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
421 wxNode
*node
= m_boxes
.First();
424 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
425 if (button
->active
) return count
;
430 wxFAIL_MSG( wxT("wxRadioBox none selected") );
435 wxString
wxRadioBox::GetString( int n
) const
437 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
439 wxNode
*node
= m_boxes
.Nth( n
);
441 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
443 GtkButton
*button
= GTK_BUTTON( node
->Data() );
444 GtkLabel
*label
= GTK_LABEL( button
->child
);
446 return wxString( label
->label
);
449 wxString
wxRadioBox::GetLabel( int item
) const
451 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
453 return GetString( item
);
456 void wxRadioBox::SetLabel( const wxString
& label
)
458 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
460 wxControl::SetLabel( label
);
462 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
465 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
467 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
469 wxNode
*node
= m_boxes
.Nth( item
);
471 wxCHECK_RET( node
, wxT("radiobox wrong index") );
473 GtkButton
*button
= GTK_BUTTON( node
->Data() );
474 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
476 gtk_label_set( g_label
, label
.mbc_str() );
479 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
481 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
484 bool wxRadioBox::Enable( bool enable
)
486 if ( !wxControl::Enable( enable
) )
489 wxNode
*node
= m_boxes
.First();
492 GtkButton
*button
= GTK_BUTTON( node
->Data() );
493 GtkWidget
*label
= button
->child
;
494 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
495 gtk_widget_set_sensitive( label
, enable
);
502 void wxRadioBox::Enable( int item
, bool enable
)
504 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
506 wxNode
*node
= m_boxes
.Nth( item
);
508 wxCHECK_RET( node
, wxT("radiobox wrong index") );
510 GtkButton
*button
= GTK_BUTTON( node
->Data() );
511 GtkWidget
*label
= button
->child
;
512 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
513 gtk_widget_set_sensitive( label
, enable
);
516 void wxRadioBox::Show( int item
, bool show
)
518 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
520 wxNode
*node
= m_boxes
.Nth( item
);
522 wxCHECK_RET( node
, wxT("radiobox wrong index") );
524 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
527 gtk_widget_show( button
);
529 gtk_widget_hide( button
);
532 wxString
wxRadioBox::GetStringSelection() const
534 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
536 wxNode
*node
= m_boxes
.First();
539 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
542 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
548 wxFAIL_MSG( wxT("wxRadioBox none selected") );
552 bool wxRadioBox::SetStringSelection( const wxString
&s
)
554 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
556 int res
= FindString( s
);
557 if (res
== -1) return FALSE
;
563 int wxRadioBox::Number() const
565 return m_boxes
.Number();
568 int wxRadioBox::GetNumberOfRowsOrCols() const
573 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
575 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
578 void wxRadioBox::GtkDisableEvents()
580 wxNode
*node
= m_boxes
.First();
583 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
584 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
590 void wxRadioBox::GtkEnableEvents()
592 wxNode
*node
= m_boxes
.First();
595 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
596 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
602 void wxRadioBox::ApplyWidgetStyle()
606 gtk_widget_set_style( m_widget
, m_widgetStyle
);
608 wxNode
*node
= m_boxes
.First();
611 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
612 gtk_widget_set_style( widget
, m_widgetStyle
);
614 GtkButton
*button
= GTK_BUTTON( node
->Data() );
615 gtk_widget_set_style( button
->child
, m_widgetStyle
);
622 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
624 wxNode
*node
= m_boxes
.First();
627 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
628 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
632 #endif // wxUSE_TOOLTIPS
634 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
636 if (window
== m_widget
->window
) return TRUE
;
638 wxNode
*node
= m_boxes
.First();
641 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
643 if (window
== button
->window
) return TRUE
;