1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/radiobox.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/radiobox.h"
20 #include "wx/dialog.h"
24 #include "wx/tooltip.h"
27 #include "wx/gtk/private.h"
28 #include <gdk/gdkkeysyms.h>
30 #include "wx/gtk/win_gtk.h"
32 //-----------------------------------------------------------------------------
33 // wxGTKRadioButtonInfo
34 //-----------------------------------------------------------------------------
35 // structure internally used by wxRadioBox to store its child buttons
37 class wxGTKRadioButtonInfo
: public wxObject
40 wxGTKRadioButtonInfo( GtkRadioButton
* abutton
, const wxRect
& arect
)
41 : button( abutton
), rect( arect
) {}
43 GtkRadioButton
* button
;
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST( wxRadioBoxButtonsInfoList
);
54 extern bool g_blockEventsOnDrag
;
56 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
61 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
63 if (g_isIdle
) wxapp_install_idle_handler();
65 if (!rb
->m_hasVMT
) return;
66 if (g_blockEventsOnDrag
) return;
68 if (!button
->active
) return;
70 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
71 event
.SetInt( rb
->GetSelection() );
72 event
.SetString( rb
->GetStringSelection() );
73 event
.SetEventObject( rb
);
74 rb
->GetEventHandler()->ProcessEvent(event
);
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
83 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
85 // don't need to install idle handler, its done from "event" signal
87 if (!rb
->m_hasVMT
) return FALSE
;
88 if (g_blockEventsOnDrag
) return FALSE
;
90 if ( ((gdk_event
->keyval
== GDK_Tab
) ||
91 (gdk_event
->keyval
== GDK_ISO_Left_Tab
)) &&
92 rb
->GetParent() && (rb
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
94 wxNavigationKeyEvent new_event
;
95 new_event
.SetEventObject( rb
->GetParent() );
96 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
97 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
98 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
99 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
100 new_event
.SetCurrentFocus( rb
);
101 return rb
->GetParent()->GetEventHandler()->ProcessEvent( new_event
);
104 if ((gdk_event
->keyval
!= GDK_Up
) &&
105 (gdk_event
->keyval
!= GDK_Down
) &&
106 (gdk_event
->keyval
!= GDK_Left
) &&
107 (gdk_event
->keyval
!= GDK_Right
))
112 wxRadioBoxButtonsInfoList::compatibility_iterator node
= rb
->m_buttonsInfo
.GetFirst();
113 while( node
&& GTK_WIDGET( node
->GetData()->button
) != widget
)
115 node
= node
->GetNext();
122 if ((gdk_event
->keyval
== GDK_Up
) ||
123 (gdk_event
->keyval
== GDK_Left
))
125 if (node
== rb
->m_buttonsInfo
.GetFirst())
126 node
= rb
->m_buttonsInfo
.GetLast();
128 node
= node
->GetPrevious();
132 if (node
== rb
->m_buttonsInfo
.GetLast())
133 node
= rb
->m_buttonsInfo
.GetFirst();
135 node
= node
->GetNext();
138 GtkWidget
*button
= (GtkWidget
*) node
->GetData()->button
;
140 gtk_widget_grab_focus( button
);
147 static gint
gtk_radiobutton_focus_in( GtkWidget
*widget
,
148 GdkEvent
*WXUNUSED(event
),
151 if ( win
->m_lostFocus
)
153 // no, we didn't really lose it
154 win
->m_lostFocus
= FALSE
;
156 else if ( !win
->m_hasFocus
)
158 win
->m_hasFocus
= true;
160 wxFocusEvent
event( wxEVT_SET_FOCUS
, win
->GetId() );
161 event
.SetEventObject( win
);
163 // never stop the signal emission, it seems to break the kbd handling
164 // inside the radiobox
165 (void)win
->GetEventHandler()->ProcessEvent( event
);
173 static gint
gtk_radiobutton_focus_out( GtkWidget
*widget
,
174 GdkEvent
*WXUNUSED(event
),
177 // wxASSERT_MSG( win->m_hasFocus, _T("got focus out without any focus in?") );
178 // Replace with a warning, else we dump core a lot!
179 // if (!win->m_hasFocus)
180 // wxLogWarning(_T("Radiobox got focus out without any focus in.") );
182 // we might have lost the focus, but may be not - it may have just gone to
183 // another button in the same radiobox, so we'll check for it in the next
184 // idle iteration (leave m_hasFocus == true for now)
185 win
->m_lostFocus
= true;
192 static void gtk_radiobutton_size_allocate( GtkWidget
*widget
,
193 GtkAllocation
* alloc
,
196 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= win
->m_buttonsInfo
.GetFirst();
198 node
= node
->GetNext())
200 if (widget
== GTK_WIDGET(node
->GetData()->button
))
202 const wxPoint origin
= win
->GetPosition();
203 wxRect rect
= wxRect( alloc
->x
- origin
.x
, alloc
->y
- origin
.y
,
204 alloc
->width
, alloc
->height
);
205 node
->GetData()->rect
= rect
;
213 //-----------------------------------------------------------------------------
215 //-----------------------------------------------------------------------------
217 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
219 void wxRadioBox::Init()
227 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
228 const wxString
& title
,
229 const wxPoint
&pos
, const wxSize
&size
,
230 const wxArrayString
& choices
, int majorDim
,
231 long style
, const wxValidator
& validator
,
232 const wxString
&name
)
234 wxCArrayString
chs(choices
);
236 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
237 chs
.GetStrings(), majorDim
, style
, validator
, name
);
240 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
241 const wxPoint
&pos
, const wxSize
&size
,
242 int n
, const wxString choices
[], int majorDim
,
243 long style
, const wxValidator
& validator
,
244 const wxString
&name
)
246 if (!PreCreation( parent
, pos
, size
) ||
247 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
249 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
253 m_widget
= GTKCreateFrame(title
);
254 wxControl::SetLabel(title
);
255 if ( HasFlag(wxNO_BORDER
) )
257 // If we don't do this here, the wxNO_BORDER style is ignored in Show()
258 gtk_frame_set_shadow_type(GTK_FRAME(m_widget
), GTK_SHADOW_NONE
);
262 // majorDim may be 0 if all trailing parameters were omitted, so don't
263 // assert here but just use the correct value for it
264 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
267 unsigned int num_of_cols
= GetColumnCount();
268 unsigned int num_of_rows
= GetRowCount();
270 GtkRadioButton
*rbtn
= (GtkRadioButton
*) NULL
;
272 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
273 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
274 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
275 gtk_widget_show( table
);
276 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
279 GSList
*radio_button_group
= (GSList
*) NULL
;
280 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
283 radio_button_group
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn
) );
286 for ( const wxChar
*pc
= choices
[i
]; *pc
; pc
++ )
288 if ( *pc
!= wxT('&') )
292 rbtn
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
293 gtk_widget_show( GTK_WIDGET(rbtn
) );
295 g_signal_connect (rbtn
, "key_press_event",
296 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
298 m_buttonsInfo
.Append( new wxGTKRadioButtonInfo( rbtn
, wxRect() ) );
300 if (HasFlag(wxRA_SPECIFY_COLS
))
302 int left
= i%num_of_cols
;
303 int right
= (i%num_of_cols
) + 1;
304 int top
= i
/num_of_cols
;
305 int bottom
= (i
/num_of_cols
)+1;
306 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
307 GTK_FILL
, GTK_FILL
, 1, 1 );
311 int left
= i
/num_of_rows
;
312 int right
= (i
/num_of_rows
) + 1;
313 int top
= i%num_of_rows
;
314 int bottom
= (i%num_of_rows
)+1;
315 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
316 GTK_FILL
, GTK_FILL
, 1, 1 );
319 ConnectWidget( GTK_WIDGET(rbtn
) );
322 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn
), TRUE
);
324 g_signal_connect (rbtn
, "clicked",
325 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
326 g_signal_connect (rbtn
, "focus_in_event",
327 G_CALLBACK (gtk_radiobutton_focus_in
), this);
328 g_signal_connect (rbtn
, "focus_out_event",
329 G_CALLBACK (gtk_radiobutton_focus_out
), this);
330 g_signal_connect (rbtn
, "size_allocate",
331 G_CALLBACK (gtk_radiobutton_size_allocate
), this);
334 m_parent
->DoAddChild( this );
341 wxRadioBox::~wxRadioBox()
343 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
346 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
347 gtk_widget_destroy( button
);
348 node
= node
->GetNext();
350 WX_CLEAR_LIST( wxRadioBoxButtonsInfoList
, m_buttonsInfo
);
353 bool wxRadioBox::Show( bool show
)
355 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
357 if (!wxControl::Show(show
))
363 if ( HasFlag(wxNO_BORDER
) )
364 gtk_widget_hide( m_widget
);
366 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
369 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
372 gtk_widget_show( button
);
374 gtk_widget_hide( button
);
376 node
= node
->GetNext();
382 void wxRadioBox::SetFocus()
384 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
386 if (m_buttonsInfo
.GetCount() == 0) return;
388 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
391 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
394 gtk_widget_grab_focus( GTK_WIDGET(button
) );
397 node
= node
->GetNext();
401 void wxRadioBox::SetSelection( int n
)
403 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
405 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
407 wxCHECK_RET( node
, wxT("radiobox wrong index") );
409 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
413 gtk_toggle_button_set_active( button
, 1 );
418 int wxRadioBox::GetSelection(void) const
420 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
424 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
427 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
428 if (button
->active
) return count
;
430 node
= node
->GetNext();
433 wxFAIL_MSG( wxT("wxRadioBox none selected") );
438 wxString
wxRadioBox::GetString(unsigned int n
) const
440 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
442 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
444 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
446 GtkLabel
*label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
448 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
453 void wxRadioBox::SetLabel( const wxString
& label
)
455 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
457 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
460 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
462 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
464 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
466 wxCHECK_RET( node
, wxT("radiobox wrong index") );
468 GtkLabel
*g_label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
470 gtk_label_set_text( g_label
, wxGTK_CONV( label
) );
473 bool wxRadioBox::Enable( bool enable
)
475 if ( !wxControl::Enable( enable
) )
478 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
481 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
482 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
484 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
485 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
486 node
= node
->GetNext();
492 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
494 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
496 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
498 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
500 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
501 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
503 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
504 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
509 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
511 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
513 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
515 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
517 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
519 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
520 // the parent radiobox is disabled
521 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
524 bool wxRadioBox::Show(unsigned int item
, bool show
)
526 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
528 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
530 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
532 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
535 gtk_widget_show( button
);
537 gtk_widget_hide( button
);
542 bool wxRadioBox::IsItemShown(unsigned int item
) const
544 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
546 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
548 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
550 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
552 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
555 unsigned int wxRadioBox::GetCount() const
557 return m_buttonsInfo
.GetCount();
560 void wxRadioBox::GtkDisableEvents()
562 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
565 g_signal_handlers_disconnect_by_func (node
->GetData()->button
,
566 (gpointer
) gtk_radiobutton_clicked_callback
,
569 node
= node
->GetNext();
573 void wxRadioBox::GtkEnableEvents()
575 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
578 g_signal_connect (node
->GetData()->button
, "clicked",
579 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
581 node
= node
->GetNext();
585 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
587 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget
), style
);
589 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
592 GtkWidget
*widget
= GTK_WIDGET( node
->GetData()->button
);
594 gtk_widget_modify_style( widget
, style
);
595 gtk_widget_modify_style(GTK_BIN(widget
)->child
, style
);
597 node
= node
->GetNext();
601 bool wxRadioBox::GTKWidgetNeedsMnemonic() const
606 void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
608 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget
), w
);
612 void wxRadioBox::ApplyToolTip(GtkTooltips
* WXUNUSED(tips
), const wxChar
*tip
)
614 // set this tooltip for all radiobuttons which don't have their own tips
616 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
618 node
= node
->GetNext(), n
++ )
620 if ( !GetItemToolTip(n
) )
622 wxToolTip::Apply(GTK_WIDGET(node
->GetData()->button
),
623 wxConvCurrent
->cWX2MB(tip
));
628 void wxRadioBox::DoSetItemToolTip(unsigned int n
, wxToolTip
*tooltip
)
632 tooltip
= GetToolTip();
634 buf
= wxGTK_CONV(tooltip
->GetTip());
636 wxToolTip::Apply(GTK_WIDGET(m_buttonsInfo
[n
]->button
), buf
);
639 #endif // wxUSE_TOOLTIPS
641 GdkWindow
*wxRadioBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
643 windows
.push_back(m_widget
->window
);
645 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
648 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
650 windows
.push_back(button
->window
);
652 node
= node
->GetNext();
658 void wxRadioBox::OnInternalIdle()
660 wxControl::OnInternalIdle();
667 wxFocusEvent
event( wxEVT_KILL_FOCUS
, GetId() );
668 event
.SetEventObject( this );
670 (void)GetEventHandler()->ProcessEvent( event
);
676 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
678 wxVisualAttributes attr
;
679 // NB: we need toplevel window so that GTK+ can find the right style
680 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
681 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
682 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
683 attr
= GetDefaultAttributesFromGTKWidget(widget
);
684 gtk_widget_destroy(wnd
);
688 int wxRadioBox::GetItemFromPoint(const wxPoint
& point
) const
690 const wxPoint pt
= ScreenToClient(point
);
692 for ( wxRadioBoxButtonsInfoList::compatibility_iterator
693 node
= m_buttonsInfo
.GetFirst(); node
; node
= node
->GetNext(), n
++ )
695 if ( m_buttonsInfo
[n
]->rect
.Contains(pt
) )
702 #endif // wxUSE_RADIOBOX