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"
18 #include "wx/tooltip.h"
21 #include "wx/gtk/private.h"
22 #include <gdk/gdkkeysyms.h>
24 //-----------------------------------------------------------------------------
25 // wxGTKRadioButtonInfo
26 //-----------------------------------------------------------------------------
27 // structure internally used by wxRadioBox to store its child buttons
29 class wxGTKRadioButtonInfo
: public wxObject
32 wxGTKRadioButtonInfo( GtkRadioButton
* abutton
, const wxRect
& arect
)
33 : button( abutton
), rect( arect
) {}
35 GtkRadioButton
* button
;
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 #include "wx/listimpl.cpp"
44 WX_DEFINE_LIST( wxRadioBoxButtonsInfoList
)
46 extern bool g_blockEventsOnDrag
;
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
53 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
55 if (!rb
->m_hasVMT
) return;
56 if (g_blockEventsOnDrag
) return;
58 if (!button
->active
) return;
60 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
61 event
.SetInt( rb
->GetSelection() );
62 event
.SetString( rb
->GetStringSelection() );
63 event
.SetEventObject( rb
);
64 rb
->HandleWindowEvent(event
);
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
73 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
75 if (!rb
->m_hasVMT
) return FALSE
;
76 if (g_blockEventsOnDrag
) return FALSE
;
78 if ( ((gdk_event
->keyval
== GDK_Tab
) ||
79 (gdk_event
->keyval
== GDK_ISO_Left_Tab
)) &&
80 rb
->GetParent() && (rb
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
82 wxNavigationKeyEvent new_event
;
83 new_event
.SetEventObject( rb
->GetParent() );
84 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
85 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
86 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
87 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
88 new_event
.SetCurrentFocus( rb
);
89 return rb
->GetParent()->HandleWindowEvent(new_event
);
92 if ((gdk_event
->keyval
!= GDK_Up
) &&
93 (gdk_event
->keyval
!= GDK_Down
) &&
94 (gdk_event
->keyval
!= GDK_Left
) &&
95 (gdk_event
->keyval
!= GDK_Right
))
100 wxRadioBoxButtonsInfoList::compatibility_iterator node
= rb
->m_buttonsInfo
.GetFirst();
101 while( node
&& GTK_WIDGET( node
->GetData()->button
) != widget
)
103 node
= node
->GetNext();
110 if ((gdk_event
->keyval
== GDK_Up
) ||
111 (gdk_event
->keyval
== GDK_Left
))
113 if (node
== rb
->m_buttonsInfo
.GetFirst())
114 node
= rb
->m_buttonsInfo
.GetLast();
116 node
= node
->GetPrevious();
120 if (node
== rb
->m_buttonsInfo
.GetLast())
121 node
= rb
->m_buttonsInfo
.GetFirst();
123 node
= node
->GetNext();
126 GtkWidget
*button
= (GtkWidget
*) node
->GetData()->button
;
128 gtk_widget_grab_focus( button
);
135 static gint
gtk_radiobutton_focus_out( GtkWidget
* WXUNUSED(widget
),
136 GdkEventFocus
*WXUNUSED(event
),
139 // NB: This control is composed of several GtkRadioButton widgets and
140 // when focus changes from one of them to another in the same
141 // wxRadioBox, we get a focus-out event followed by focus-in for
142 // another GtkRadioButton owned by the same control. We don't want
143 // to generate two spurious wxEVT_SET_FOCUS events in this case,
144 // so we defer sending wx events until idle time.
145 win
->GTKHandleFocusOut();
147 // never stop the signal emission, it seems to break the kbd handling
148 // inside the radiobox
154 static gint
gtk_radiobutton_focus_in( GtkWidget
* WXUNUSED(widget
),
155 GdkEventFocus
*WXUNUSED(event
),
158 win
->GTKHandleFocusIn();
160 // never stop the signal emission, it seems to break the kbd handling
161 // inside the radiobox
167 static void gtk_radiobutton_size_allocate( GtkWidget
*widget
,
168 GtkAllocation
* alloc
,
171 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= win
->m_buttonsInfo
.GetFirst();
173 node
= node
->GetNext())
175 if (widget
== GTK_WIDGET(node
->GetData()->button
))
177 const wxPoint origin
= win
->GetPosition();
178 wxRect rect
= wxRect( alloc
->x
- origin
.x
, alloc
->y
- origin
.y
,
179 alloc
->width
, alloc
->height
);
180 node
->GetData()->rect
= rect
;
188 //-----------------------------------------------------------------------------
190 //-----------------------------------------------------------------------------
192 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
194 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
195 const wxString
& title
,
196 const wxPoint
&pos
, const wxSize
&size
,
197 const wxArrayString
& choices
, int majorDim
,
198 long style
, const wxValidator
& validator
,
199 const wxString
&name
)
201 wxCArrayString
chs(choices
);
203 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
204 chs
.GetStrings(), majorDim
, style
, validator
, name
);
207 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
208 const wxPoint
&pos
, const wxSize
&size
,
209 int n
, const wxString choices
[], int majorDim
,
210 long style
, const wxValidator
& validator
,
211 const wxString
&name
)
213 if (!PreCreation( parent
, pos
, size
) ||
214 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
216 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
220 m_widget
= GTKCreateFrame(title
);
221 g_object_ref(m_widget
);
222 wxControl::SetLabel(title
);
223 if ( HasFlag(wxNO_BORDER
) )
225 // If we don't do this here, the wxNO_BORDER style is ignored in Show()
226 gtk_frame_set_shadow_type(GTK_FRAME(m_widget
), GTK_SHADOW_NONE
);
230 // majorDim may be 0 if all trailing parameters were omitted, so don't
231 // assert here but just use the correct value for it
232 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
235 unsigned int num_of_cols
= GetColumnCount();
236 unsigned int num_of_rows
= GetRowCount();
238 GtkRadioButton
*rbtn
= NULL
;
240 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
241 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
242 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
243 gtk_widget_show( table
);
244 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
247 GSList
*radio_button_group
= NULL
;
248 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
251 radio_button_group
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn
) );
254 for ( wxString::const_iterator pc
= choices
[i
].begin();
255 pc
!= choices
[i
].end(); ++pc
)
257 if ( *pc
!= wxT('&') )
261 rbtn
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
262 gtk_widget_show( GTK_WIDGET(rbtn
) );
264 g_signal_connect (rbtn
, "key_press_event",
265 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
267 m_buttonsInfo
.Append( new wxGTKRadioButtonInfo( rbtn
, wxRect() ) );
269 if (HasFlag(wxRA_SPECIFY_COLS
))
271 int left
= i%num_of_cols
;
272 int right
= (i%num_of_cols
) + 1;
273 int top
= i
/num_of_cols
;
274 int bottom
= (i
/num_of_cols
)+1;
275 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
276 GTK_FILL
, GTK_FILL
, 1, 1 );
280 int left
= i
/num_of_rows
;
281 int right
= (i
/num_of_rows
) + 1;
282 int top
= i%num_of_rows
;
283 int bottom
= (i%num_of_rows
)+1;
284 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
285 GTK_FILL
, GTK_FILL
, 1, 1 );
288 ConnectWidget( GTK_WIDGET(rbtn
) );
291 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn
), TRUE
);
293 g_signal_connect (rbtn
, "clicked",
294 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
295 g_signal_connect (rbtn
, "focus_in_event",
296 G_CALLBACK (gtk_radiobutton_focus_in
), this);
297 g_signal_connect (rbtn
, "focus_out_event",
298 G_CALLBACK (gtk_radiobutton_focus_out
), this);
299 g_signal_connect (rbtn
, "size_allocate",
300 G_CALLBACK (gtk_radiobutton_size_allocate
), this);
303 m_parent
->DoAddChild( this );
310 wxRadioBox::~wxRadioBox()
312 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
315 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
316 gtk_widget_destroy( button
);
317 node
= node
->GetNext();
319 WX_CLEAR_LIST( wxRadioBoxButtonsInfoList
, m_buttonsInfo
);
322 bool wxRadioBox::Show( bool show
)
324 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
326 if (!wxControl::Show(show
))
332 if ( HasFlag(wxNO_BORDER
) )
333 gtk_widget_hide( m_widget
);
335 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
338 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
341 gtk_widget_show( button
);
343 gtk_widget_hide( button
);
345 node
= node
->GetNext();
351 void wxRadioBox::SetSelection( int n
)
353 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
355 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
357 wxCHECK_RET( node
, wxT("radiobox wrong index") );
359 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
363 gtk_toggle_button_set_active( button
, 1 );
368 int wxRadioBox::GetSelection(void) const
370 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
374 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
377 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
378 if (button
->active
) return count
;
380 node
= node
->GetNext();
383 wxFAIL_MSG( wxT("wxRadioBox none selected") );
388 wxString
wxRadioBox::GetString(unsigned int n
) const
390 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
392 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
394 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
396 GtkLabel
*label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
398 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
403 void wxRadioBox::SetLabel( const wxString
& label
)
405 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
407 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
410 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
412 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
414 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
416 wxCHECK_RET( node
, wxT("radiobox wrong index") );
418 GtkLabel
*g_label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
420 gtk_label_set_text( g_label
, wxGTK_CONV( label
) );
423 bool wxRadioBox::Enable( bool enable
)
425 if ( !wxControl::Enable( enable
) )
428 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
431 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
432 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
434 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
435 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
436 node
= node
->GetNext();
445 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
447 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
449 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
451 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
453 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
454 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
456 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
457 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
462 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
464 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
466 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
468 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
470 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
472 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
473 // the parent radiobox is disabled
474 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
477 bool wxRadioBox::Show(unsigned int item
, bool show
)
479 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
481 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
483 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
485 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
488 gtk_widget_show( button
);
490 gtk_widget_hide( button
);
495 bool wxRadioBox::IsItemShown(unsigned int item
) const
497 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
499 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
501 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
503 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
505 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
508 unsigned int wxRadioBox::GetCount() const
510 return m_buttonsInfo
.GetCount();
513 void wxRadioBox::GtkDisableEvents()
515 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
518 g_signal_handlers_block_by_func(node
->GetData()->button
,
519 (gpointer
)gtk_radiobutton_clicked_callback
, this);
521 node
= node
->GetNext();
525 void wxRadioBox::GtkEnableEvents()
527 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
530 g_signal_handlers_unblock_by_func(node
->GetData()->button
,
531 (gpointer
)gtk_radiobutton_clicked_callback
, this);
533 node
= node
->GetNext();
537 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
539 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget
), style
);
541 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
544 GtkWidget
*widget
= GTK_WIDGET( node
->GetData()->button
);
546 gtk_widget_modify_style( widget
, style
);
547 gtk_widget_modify_style(GTK_BIN(widget
)->child
, style
);
549 node
= node
->GetNext();
553 bool wxRadioBox::GTKWidgetNeedsMnemonic() const
558 void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
560 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget
), w
);
564 void wxRadioBox::GTKApplyToolTip(GtkTooltips
* WXUNUSED(tips
), const gchar
*tip
)
566 // set this tooltip for all radiobuttons which don't have their own tips
568 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
570 node
= node
->GetNext(), n
++ )
572 if ( !GetItemToolTip(n
) )
574 wxToolTip::GTKApply(GTK_WIDGET(node
->GetData()->button
), tip
);
579 void wxRadioBox::DoSetItemToolTip(unsigned int n
, wxToolTip
*tooltip
)
583 tooltip
= GetToolTip();
585 buf
= wxGTK_CONV(tooltip
->GetTip());
587 wxToolTip::GTKApply(GTK_WIDGET(m_buttonsInfo
[n
]->button
), buf
);
590 #endif // wxUSE_TOOLTIPS
592 GdkWindow
*wxRadioBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
594 windows
.push_back(m_widget
->window
);
596 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
599 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
601 // don't put NULL pointers in the 'windows' array!
603 windows
.push_back(button
->window
);
605 node
= node
->GetNext();
613 wxRadioBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
615 wxVisualAttributes attr
;
616 // NB: we need toplevel window so that GTK+ can find the right style
617 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
618 GtkWidget
* widget
= gtk_radio_button_new_with_label(NULL
, "");
619 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
620 attr
= GetDefaultAttributesFromGTKWidget(widget
);
621 gtk_widget_destroy(wnd
);
625 int wxRadioBox::GetItemFromPoint(const wxPoint
& point
) const
627 const wxPoint pt
= ScreenToClient(point
);
629 for ( wxRadioBoxButtonsInfoList::compatibility_iterator
630 node
= m_buttonsInfo
.GetFirst(); node
; node
= node
->GetNext(), n
++ )
632 if ( m_buttonsInfo
[n
]->rect
.Contains(pt
) )
639 #endif // wxUSE_RADIOBOX