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 wxSize ls
= LayoutItems();
185 wxSize newSize
= size
;
186 if (newSize
.x
== -1) newSize
.x
= ls
.x
;
187 if (newSize
.y
== -1) newSize
.y
= ls
.y
;
188 SetSize( newSize
.x
, newSize
.y
);
190 m_parent
->DoAddChild( this );
196 SetBackgroundColour( parent
->GetBackgroundColour() );
197 SetForegroundColour( parent
->GetForegroundColour() );
198 SetFont( parent
->GetFont() );
205 wxRadioBox::~wxRadioBox()
207 wxNode
*node
= m_boxes
.First();
210 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
211 gtk_widget_destroy( button
);
216 void wxRadioBox::DoSetSize( int x
, int y
, int width
, int height
, int sizeFlags
)
218 wxWindow::DoSetSize( x
, y
, width
, height
, sizeFlags
);
223 wxSize
wxRadioBox::LayoutItems()
228 if ( m_majorDim
== 0 )
230 // avoid dividing by 0 below
231 wxFAIL_MSG( wxT("dimension of radiobox should not be 0!") );
236 int num_per_major
= (m_boxes
.GetCount() - 1) / m_majorDim
+1;
242 if (HasFlag(wxRA_SPECIFY_COLS
))
244 num_of_cols
= m_majorDim
;
245 num_of_rows
= num_per_major
;
249 num_of_cols
= num_per_major
;
250 num_of_rows
= m_majorDim
;
253 if ( HasFlag(wxRA_SPECIFY_COLS
) ||
254 (HasFlag(wxRA_SPECIFY_ROWS
) && (num_of_cols
> 1)) )
256 for (int j
= 0; j
< num_of_cols
; j
++)
261 wxNode
*node
= m_boxes
.Nth( j
*num_of_rows
);
262 for (int i1
= 0; i1
< num_of_rows
; i1
++)
264 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
265 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
266 GdkFont
*font
= m_widget
->style
->font
;
267 int len
= 22+gdk_string_measure( font
, label
->label
);
268 if (len
> max_len
) max_len
= len
;
270 gtk_pizza_move( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
);
277 // we don't know the max_len before
279 node
= m_boxes
.Nth( j
*num_of_rows
);
280 for (int i2
= 0; i2
< num_of_rows
; i2
++)
282 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
284 gtk_pizza_resize( GTK_PIZZA(m_parent
->m_wxwindow
), button
, max_len
, 20 );
290 if (y
> res
.y
) res
.y
= y
;
302 wxNode
*node
= m_boxes
.First();
305 GtkButton
*button
= GTK_BUTTON( node
->Data() );
306 GtkLabel
*label
= GTK_LABEL( button
->child
);
308 GdkFont
*font
= m_widget
->style
->font
;
309 int len
= 22+gdk_string_measure( font
, label
->label
);
310 if (len
> max
) max
= len
;
315 node
= m_boxes
.First();
318 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
320 gtk_pizza_set_size( GTK_PIZZA(m_parent
->m_wxwindow
), button
, m_x
+x
, m_y
+y
, max
, 20 );
332 bool wxRadioBox::Show( bool show
)
334 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
336 if (!wxControl::Show(show
))
342 if ((m_windowStyle
& wxNO_BORDER
) != 0)
343 gtk_widget_hide( m_widget
);
345 wxNode
*node
= m_boxes
.First();
348 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
350 if (show
) gtk_widget_show( button
); else gtk_widget_hide( button
);
358 int wxRadioBox::FindString( const wxString
&s
) const
360 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
364 wxNode
*node
= m_boxes
.First();
367 GtkButton
*button
= GTK_BUTTON( node
->Data() );
369 GtkLabel
*label
= GTK_LABEL( button
->child
);
370 if (s
== label
->label
) return count
;
379 void wxRadioBox::SetFocus()
381 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
383 if (m_boxes
.GetCount() == 0) return;
385 wxNode
*node
= m_boxes
.First();
388 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
391 gtk_widget_grab_focus( GTK_WIDGET(button
) );
399 void wxRadioBox::SetSelection( int n
)
401 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
403 wxNode
*node
= m_boxes
.Nth( n
);
405 wxCHECK_RET( node
, wxT("radiobox wrong index") );
407 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
411 gtk_toggle_button_set_state( button
, 1 );
416 int wxRadioBox::GetSelection(void) const
418 wxCHECK_MSG( m_widget
!= NULL
, -1, wxT("invalid radiobox") );
422 wxNode
*node
= m_boxes
.First();
425 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
426 if (button
->active
) return count
;
431 wxFAIL_MSG( wxT("wxRadioBox none selected") );
436 wxString
wxRadioBox::GetString( int n
) const
438 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
440 wxNode
*node
= m_boxes
.Nth( n
);
442 wxCHECK_MSG( node
, wxT(""), wxT("radiobox wrong index") );
444 GtkButton
*button
= GTK_BUTTON( node
->Data() );
445 GtkLabel
*label
= GTK_LABEL( button
->child
);
447 return wxString( label
->label
);
450 wxString
wxRadioBox::GetLabel( int item
) const
452 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
454 return GetString( item
);
457 void wxRadioBox::SetLabel( const wxString
& label
)
459 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
461 wxControl::SetLabel( label
);
463 gtk_frame_set_label( GTK_FRAME(m_widget
), wxControl::GetLabel().mbc_str() );
466 void wxRadioBox::SetLabel( int item
, const wxString
& label
)
468 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
470 wxNode
*node
= m_boxes
.Nth( item
);
472 wxCHECK_RET( node
, wxT("radiobox wrong index") );
474 GtkButton
*button
= GTK_BUTTON( node
->Data() );
475 GtkLabel
*g_label
= GTK_LABEL( button
->child
);
477 gtk_label_set( g_label
, label
.mbc_str() );
480 void wxRadioBox::SetLabel( int WXUNUSED(item
), wxBitmap
*WXUNUSED(bitmap
) )
482 wxFAIL_MSG(wxT("wxRadioBox::SetLabel not implemented."));
485 bool wxRadioBox::Enable( bool enable
)
487 if ( !wxControl::Enable( enable
) )
490 wxNode
*node
= m_boxes
.First();
493 GtkButton
*button
= GTK_BUTTON( node
->Data() );
494 GtkWidget
*label
= button
->child
;
495 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
496 gtk_widget_set_sensitive( label
, enable
);
503 void wxRadioBox::Enable( int item
, bool enable
)
505 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
507 wxNode
*node
= m_boxes
.Nth( item
);
509 wxCHECK_RET( node
, wxT("radiobox wrong index") );
511 GtkButton
*button
= GTK_BUTTON( node
->Data() );
512 GtkWidget
*label
= button
->child
;
513 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
514 gtk_widget_set_sensitive( label
, enable
);
517 void wxRadioBox::Show( int item
, bool show
)
519 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
521 wxNode
*node
= m_boxes
.Nth( item
);
523 wxCHECK_RET( node
, wxT("radiobox wrong index") );
525 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
528 gtk_widget_show( button
);
530 gtk_widget_hide( button
);
533 wxString
wxRadioBox::GetStringSelection() const
535 wxCHECK_MSG( m_widget
!= NULL
, wxT(""), wxT("invalid radiobox") );
537 wxNode
*node
= m_boxes
.First();
540 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->Data() );
543 GtkLabel
*label
= GTK_LABEL( GTK_BUTTON(button
)->child
);
549 wxFAIL_MSG( wxT("wxRadioBox none selected") );
553 bool wxRadioBox::SetStringSelection( const wxString
&s
)
555 wxCHECK_MSG( m_widget
!= NULL
, FALSE
, wxT("invalid radiobox") );
557 int res
= FindString( s
);
558 if (res
== -1) return FALSE
;
564 int wxRadioBox::Number() const
566 return m_boxes
.Number();
569 int wxRadioBox::GetNumberOfRowsOrCols() const
574 void wxRadioBox::SetNumberOfRowsOrCols( int WXUNUSED(n
) )
576 wxFAIL_MSG(wxT("wxRadioBox::SetNumberOfRowsOrCols not implemented."));
579 void wxRadioBox::GtkDisableEvents()
581 wxNode
*node
= m_boxes
.First();
584 gtk_signal_disconnect_by_func( GTK_OBJECT(node
->Data()),
585 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
591 void wxRadioBox::GtkEnableEvents()
593 wxNode
*node
= m_boxes
.First();
596 gtk_signal_connect( GTK_OBJECT(node
->Data()), "clicked",
597 GTK_SIGNAL_FUNC(gtk_radiobutton_clicked_callback
), (gpointer
*)this );
603 void wxRadioBox::ApplyWidgetStyle()
607 gtk_widget_set_style( m_widget
, m_widgetStyle
);
609 wxNode
*node
= m_boxes
.First();
612 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
613 gtk_widget_set_style( widget
, m_widgetStyle
);
615 GtkButton
*button
= GTK_BUTTON( node
->Data() );
616 gtk_widget_set_style( button
->child
, m_widgetStyle
);
623 void wxRadioBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
625 wxNode
*node
= m_boxes
.First();
628 GtkWidget
*widget
= GTK_WIDGET( node
->Data() );
629 gtk_tooltips_set_tip( tips
, widget
, wxConvCurrent
->cWX2MB(tip
), (gchar
*) NULL
);
633 #endif // wxUSE_TOOLTIPS
635 bool wxRadioBox::IsOwnGtkWindow( GdkWindow
*window
)
637 if (window
== m_widget
->window
) return TRUE
;
639 wxNode
*node
= m_boxes
.First();
642 GtkWidget
*button
= GTK_WIDGET( node
->Data() );
644 if (window
== button
->window
) return TRUE
;