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();
198 (* GTK_WIDGET_CLASS( GTK_OBJECT(m_widget
)->klass
)->size_request
) (m_widget
, &req
);
199 if (req
.width
> ls
.x
) ls
.x
= req
.width
;
201 wxSize newSize
= size
;
202 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
203 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
204 SetSize( newSize
.x
, newSize
.y
);
206 SetBackgroundColour( parent
->GetBackgroundColour() );
207 SetForegroundColour( parent
->GetForegroundColour() );
214 wxRadioBox::~wxRadioBox()
216 wxNode
*node
= m_boxes
.First();
219 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
220 gtk_widget_destroy( button
);
225 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
227 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
232 wxSize
wxRadioBox::LayoutItems()
237 if ( m_majorDim
== 0 )
239 // avoid dividing by 0 below
240 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
245 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
251 if (HasFlag(wxRA_SPECIFY_COLS
))
253 num_of_cols
= m_majorDim
;
254 num_of_rows
= num_per_major
;
258 num_of_cols
= num_per_major
;
259 num_of_rows
= m_majorDim
;
262 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
263 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
265 for (int j
= 0; j
< num_of_cols
; j
++)
270 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
271 for (int i1
= 0; i1
< num_of_rows
; i1
++)
273 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
278 (* GTK_WIDGET_CLASS( GTK_OBJECT(button
)->klass
)->size_request
)
281 if (req
.width
> max_len
) max_len
= req
.width
;
283 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
290 // we don't know the max_len before
292 node
= m_boxes
.Nth( j
*num_of_rows
);
293 for (int i2
= 0; i2
< num_of_rows
; i2
++)
295 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
297 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
303 if (y
> res
.y
) res
.y
= y
;
315 wxNode
*node
= m_boxes
.First();
318 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
323 (* GTK_WIDGET_CLASS( GTK_OBJECT(button
)->klass
)->size_request
)
326 if (req
.width
> max
) max
= req
.width
;
331 node
= m_boxes
.First();
334 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
336 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
348 bool wxRadioBox::Show( bool show
)
350 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
352 if (!wxControl::Show(show
))
358 if ((m_windowStyle
& wxNO_BORDER
) != 0)
359 gtk_widget_hide( m_widget
);
361 wxNode
*node
= m_boxes
.First();
364 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
366 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
374 int wxRadioBox::FindString( const wxString
&s
) const
376 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
380 wxNode
*node
= m_boxes
.First();
383 GtkButton
*button
= GTK_BUTTON( node
->Data() );
385 GtkLabel
*label
= GTK_LABEL( button
->child
);
386 if (s
== label
->label
) return count
;
395 void wxRadioBox::SetFocus()
397 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
399 if (m_boxes
.GetCount() == 0) return;
401 wxNode
*node
= m_boxes
.First();
404 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
407 gtk_widget_grab_focus( GTK_WIDGET(button
) );
415 void wxRadioBox::SetSelection( int n
)
417 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
419 wxNode
*node
= m_boxes
.Nth( n
);
421 wxCHECK_RET( node
, wxT("radiobox wrong index") );
423 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
427 gtk_toggle_button_set_state( button
, 1 );
432 int wxRadioBox::GetSelection(void) const
434 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
438 wxNode
*node
= m_boxes
.First();
441 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
442 if (button
->active
) return count
;
447 wxFAIL_MSG( wxT("wxRadioBox none selected") );
452 wxString
wxRadioBox::GetString( int n
) const
454 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
456 wxNode
*node
= m_boxes
.Nth( n
);
458 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
460 GtkButton
*button
= GTK_BUTTON( node
->Data() );
461 GtkLabel
*label
= GTK_LABEL( button
->child
);
463 return wxString( label
->label
);
466 wxString
wxRadioBox::GetLabel( int item
) const
468 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
470 return GetString( item
);
473 void wxRadioBox::SetLabel( const wxString
& label
)
475 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
477 wxControl::SetLabel( label
);
479 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
482 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
484 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
486 wxNode
*node
= m_boxes
.Nth( item
);
488 wxCHECK_RET( node
, wxT("radiobox wrong index") );
490 GtkButton
*button
= GTK_BUTTON( node
->Data() );
491 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
493 gtk_label_set( g_label
, label
.mbc_str() );
496 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
498 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
501 bool wxRadioBox::Enable( bool enable
)
503 if ( !wxControl::Enable( enable
) )
506 wxNode
*node
= m_boxes
.First();
509 GtkButton
*button
= GTK_BUTTON( node
->Data() );
510 GtkWidget
*label
= button
->child
;
511 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
512 gtk_widget_set_sensitive( label
, enable
);
519 void wxRadioBox::Enable( int item
, bool enable
)
521 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
523 wxNode
*node
= m_boxes
.Nth( item
);
525 wxCHECK_RET( node
, wxT("radiobox wrong index") );
527 GtkButton
*button
= GTK_BUTTON( node
->Data() );
528 GtkWidget
*label
= button
->child
;
529 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
530 gtk_widget_set_sensitive( label
, enable
);
533 void wxRadioBox::Show( int item
, bool show
)
535 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
537 wxNode
*node
= m_boxes
.Nth( item
);
539 wxCHECK_RET( node
, wxT("radiobox wrong index") );
541 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
544 gtk_widget_show( button
);
546 gtk_widget_hide( button
);
549 wxString
wxRadioBox::GetStringSelection() const
551 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
553 wxNode
*node
= m_boxes
.First();
556 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
559 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
565 wxFAIL_MSG( wxT("wxRadioBox none selected") );
569 bool wxRadioBox::SetStringSelection( const wxString
&s
)
571 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
573 int res
= FindString( s
);
574 if (res
== -1) return FALSE
;
580 int wxRadioBox::Number() const
582 return m_boxes
.Number();
585 int wxRadioBox::GetNumberOfRowsOrCols() const
590 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
592 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
595 void wxRadioBox::GtkDisableEvents()
597 wxNode
*node
= m_boxes
.First();
600 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
601 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
607 void wxRadioBox::GtkEnableEvents()
609 wxNode
*node
= m_boxes
.First();
612 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
613 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
619 void wxRadioBox::ApplyWidgetStyle()
623 gtk_widget_set_style( m_widget
, m_widgetStyle
);
625 wxNode
*node
= m_boxes
.First();
628 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
629 gtk_widget_set_style( widget
, m_widgetStyle
);
631 GtkButton
*button
= GTK_BUTTON( node
->Data() );
632 gtk_widget_set_style( button
->child
, m_widgetStyle
);
639 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
641 wxNode
*node
= m_boxes
.First();
644 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
645 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
649 #endif // wxUSE_TOOLTIPS
651 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
653 if (window
== m_widget
->window
) return TRUE
;
655 wxNode
*node
= m_boxes
.First();
658 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
660 if (window
== button
->window
) return TRUE
;