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 //-----------------------------------------------------------------------------
31 // wxGTKRadioButtonInfo
32 //-----------------------------------------------------------------------------
33 // structure internally used by wxRadioBox to store its child buttons
35 class wxGTKRadioButtonInfo
: public wxObject
38 wxGTKRadioButtonInfo( GtkRadioButton
* abutton
, const wxRect
& arect
)
39 : button( abutton
), rect( arect
) {}
41 GtkRadioButton
* button
;
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 #include "wx/listimpl.cpp"
50 WX_DEFINE_LIST( wxRadioBoxButtonsInfoList
)
52 extern bool g_blockEventsOnDrag
;
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
59 static void gtk_radiobutton_clicked_callback( GtkToggleButton
*button
, wxRadioBox
*rb
)
61 if (!rb
->m_hasVMT
) return;
62 if (g_blockEventsOnDrag
) return;
64 if (!button
->active
) return;
66 wxCommandEvent
event( wxEVT_COMMAND_RADIOBOX_SELECTED
, rb
->GetId() );
67 event
.SetInt( rb
->GetSelection() );
68 event
.SetString( rb
->GetStringSelection() );
69 event
.SetEventObject( rb
);
70 rb
->HandleWindowEvent(event
);
74 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
79 static gint
gtk_radiobox_keypress_callback( GtkWidget
*widget
, GdkEventKey
*gdk_event
, wxRadioBox
*rb
)
81 if (!rb
->m_hasVMT
) return FALSE
;
82 if (g_blockEventsOnDrag
) return FALSE
;
84 if ( ((gdk_event
->keyval
== GDK_Tab
) ||
85 (gdk_event
->keyval
== GDK_ISO_Left_Tab
)) &&
86 rb
->GetParent() && (rb
->GetParent()->HasFlag( wxTAB_TRAVERSAL
)) )
88 wxNavigationKeyEvent new_event
;
89 new_event
.SetEventObject( rb
->GetParent() );
90 // GDK reports GDK_ISO_Left_Tab for SHIFT-TAB
91 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
92 // CTRL-TAB changes the (parent) window, i.e. switch notebook page
93 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
94 new_event
.SetCurrentFocus( rb
);
95 return rb
->GetParent()->HandleWindowEvent(new_event
);
98 if ((gdk_event
->keyval
!= GDK_Up
) &&
99 (gdk_event
->keyval
!= GDK_Down
) &&
100 (gdk_event
->keyval
!= GDK_Left
) &&
101 (gdk_event
->keyval
!= GDK_Right
))
106 wxRadioBoxButtonsInfoList::compatibility_iterator node
= rb
->m_buttonsInfo
.GetFirst();
107 while( node
&& GTK_WIDGET( node
->GetData()->button
) != widget
)
109 node
= node
->GetNext();
116 if ((gdk_event
->keyval
== GDK_Up
) ||
117 (gdk_event
->keyval
== GDK_Left
))
119 if (node
== rb
->m_buttonsInfo
.GetFirst())
120 node
= rb
->m_buttonsInfo
.GetLast();
122 node
= node
->GetPrevious();
126 if (node
== rb
->m_buttonsInfo
.GetLast())
127 node
= rb
->m_buttonsInfo
.GetFirst();
129 node
= node
->GetNext();
132 GtkWidget
*button
= (GtkWidget
*) node
->GetData()->button
;
134 gtk_widget_grab_focus( button
);
141 static gint
gtk_radiobutton_focus_out( GtkWidget
* WXUNUSED(widget
),
142 GdkEventFocus
*WXUNUSED(event
),
145 // NB: This control is composed of several GtkRadioButton widgets and
146 // when focus changes from one of them to another in the same
147 // wxRadioBox, we get a focus-out event followed by focus-in for
148 // another GtkRadioButton owned by the same control. We don't want
149 // to generate two spurious wxEVT_SET_FOCUS events in this case,
150 // so we defer sending wx events until idle time.
151 win
->GTKHandleFocusOut();
153 // never stop the signal emission, it seems to break the kbd handling
154 // inside the radiobox
160 static gint
gtk_radiobutton_focus_in( GtkWidget
* WXUNUSED(widget
),
161 GdkEventFocus
*WXUNUSED(event
),
164 win
->GTKHandleFocusIn();
166 // never stop the signal emission, it seems to break the kbd handling
167 // inside the radiobox
173 static void gtk_radiobutton_size_allocate( GtkWidget
*widget
,
174 GtkAllocation
* alloc
,
177 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= win
->m_buttonsInfo
.GetFirst();
179 node
= node
->GetNext())
181 if (widget
== GTK_WIDGET(node
->GetData()->button
))
183 const wxPoint origin
= win
->GetPosition();
184 wxRect rect
= wxRect( alloc
->x
- origin
.x
, alloc
->y
- origin
.y
,
185 alloc
->width
, alloc
->height
);
186 node
->GetData()->rect
= rect
;
194 //-----------------------------------------------------------------------------
196 //-----------------------------------------------------------------------------
198 IMPLEMENT_DYNAMIC_CLASS(wxRadioBox
,wxControl
)
200 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
,
201 const wxString
& title
,
202 const wxPoint
&pos
, const wxSize
&size
,
203 const wxArrayString
& choices
, int majorDim
,
204 long style
, const wxValidator
& validator
,
205 const wxString
&name
)
207 wxCArrayString
chs(choices
);
209 return Create( parent
, id
, title
, pos
, size
, chs
.GetCount(),
210 chs
.GetStrings(), majorDim
, style
, validator
, name
);
213 bool wxRadioBox::Create( wxWindow
*parent
, wxWindowID id
, const wxString
& title
,
214 const wxPoint
&pos
, const wxSize
&size
,
215 int n
, const wxString choices
[], int majorDim
,
216 long style
, const wxValidator
& validator
,
217 const wxString
&name
)
219 if (!PreCreation( parent
, pos
, size
) ||
220 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
222 wxFAIL_MSG( wxT("wxRadioBox creation failed") );
226 m_widget
= GTKCreateFrame(title
);
227 wxControl::SetLabel(title
);
228 if ( HasFlag(wxNO_BORDER
) )
230 // If we don't do this here, the wxNO_BORDER style is ignored in Show()
231 gtk_frame_set_shadow_type(GTK_FRAME(m_widget
), GTK_SHADOW_NONE
);
235 // majorDim may be 0 if all trailing parameters were omitted, so don't
236 // assert here but just use the correct value for it
237 SetMajorDim(majorDim
== 0 ? n
: majorDim
, style
);
240 unsigned int num_of_cols
= GetColumnCount();
241 unsigned int num_of_rows
= GetRowCount();
243 GtkRadioButton
*rbtn
= (GtkRadioButton
*) NULL
;
245 GtkWidget
*table
= gtk_table_new( num_of_rows
, num_of_cols
, FALSE
);
246 gtk_table_set_col_spacings( GTK_TABLE(table
), 1 );
247 gtk_table_set_row_spacings( GTK_TABLE(table
), 1 );
248 gtk_widget_show( table
);
249 gtk_container_add( GTK_CONTAINER(m_widget
), table
);
252 GSList
*radio_button_group
= (GSList
*) NULL
;
253 for (unsigned int i
= 0; i
< (unsigned int)n
; i
++)
256 radio_button_group
= gtk_radio_button_get_group( GTK_RADIO_BUTTON(rbtn
) );
259 for ( wxString::const_iterator pc
= choices
[i
].begin();
260 pc
!= choices
[i
].end(); ++pc
)
262 if ( *pc
!= wxT('&') )
266 rbtn
= GTK_RADIO_BUTTON( gtk_radio_button_new_with_label( radio_button_group
, wxGTK_CONV( label
) ) );
267 gtk_widget_show( GTK_WIDGET(rbtn
) );
269 g_signal_connect (rbtn
, "key_press_event",
270 G_CALLBACK (gtk_radiobox_keypress_callback
), this);
272 m_buttonsInfo
.Append( new wxGTKRadioButtonInfo( rbtn
, wxRect() ) );
274 if (HasFlag(wxRA_SPECIFY_COLS
))
276 int left
= i%num_of_cols
;
277 int right
= (i%num_of_cols
) + 1;
278 int top
= i
/num_of_cols
;
279 int bottom
= (i
/num_of_cols
)+1;
280 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
281 GTK_FILL
, GTK_FILL
, 1, 1 );
285 int left
= i
/num_of_rows
;
286 int right
= (i
/num_of_rows
) + 1;
287 int top
= i%num_of_rows
;
288 int bottom
= (i%num_of_rows
)+1;
289 gtk_table_attach( GTK_TABLE(table
), GTK_WIDGET(rbtn
), left
, right
, top
, bottom
,
290 GTK_FILL
, GTK_FILL
, 1, 1 );
293 ConnectWidget( GTK_WIDGET(rbtn
) );
296 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(rbtn
), TRUE
);
298 g_signal_connect (rbtn
, "clicked",
299 G_CALLBACK (gtk_radiobutton_clicked_callback
), this);
300 g_signal_connect (rbtn
, "focus_in_event",
301 G_CALLBACK (gtk_radiobutton_focus_in
), this);
302 g_signal_connect (rbtn
, "focus_out_event",
303 G_CALLBACK (gtk_radiobutton_focus_out
), this);
304 g_signal_connect (rbtn
, "size_allocate",
305 G_CALLBACK (gtk_radiobutton_size_allocate
), this);
308 m_parent
->DoAddChild( this );
315 wxRadioBox::~wxRadioBox()
317 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
320 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
321 gtk_widget_destroy( button
);
322 node
= node
->GetNext();
324 WX_CLEAR_LIST( wxRadioBoxButtonsInfoList
, m_buttonsInfo
);
327 bool wxRadioBox::Show( bool show
)
329 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
331 if (!wxControl::Show(show
))
337 if ( HasFlag(wxNO_BORDER
) )
338 gtk_widget_hide( m_widget
);
340 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
343 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
346 gtk_widget_show( button
);
348 gtk_widget_hide( button
);
350 node
= node
->GetNext();
356 void wxRadioBox::SetSelection( int n
)
358 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
360 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
362 wxCHECK_RET( node
, wxT("radiobox wrong index") );
364 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
368 gtk_toggle_button_set_active( button
, 1 );
373 int wxRadioBox::GetSelection(void) const
375 wxCHECK_MSG( m_widget
!= NULL
, wxNOT_FOUND
, wxT("invalid radiobox") );
379 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
382 GtkToggleButton
*button
= GTK_TOGGLE_BUTTON( node
->GetData()->button
);
383 if (button
->active
) return count
;
385 node
= node
->GetNext();
388 wxFAIL_MSG( wxT("wxRadioBox none selected") );
393 wxString
wxRadioBox::GetString(unsigned int n
) const
395 wxCHECK_MSG( m_widget
!= NULL
, wxEmptyString
, wxT("invalid radiobox") );
397 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( n
);
399 wxCHECK_MSG( node
, wxEmptyString
, wxT("radiobox wrong index") );
401 GtkLabel
*label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
403 wxString
str( wxGTK_CONV_BACK( gtk_label_get_text(label
) ) );
408 void wxRadioBox::SetLabel( const wxString
& label
)
410 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
412 GTKSetLabelForFrame(GTK_FRAME(m_widget
), label
);
415 void wxRadioBox::SetString(unsigned int item
, const wxString
& label
)
417 wxCHECK_RET( m_widget
!= NULL
, wxT("invalid radiobox") );
419 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
421 wxCHECK_RET( node
, wxT("radiobox wrong index") );
423 GtkLabel
*g_label
= GTK_LABEL(GTK_BIN(node
->GetData()->button
)->child
);
425 gtk_label_set_text( g_label
, wxGTK_CONV( label
) );
428 bool wxRadioBox::Enable( bool enable
)
430 if ( !wxControl::Enable( enable
) )
433 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
436 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
437 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
439 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
440 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
441 node
= node
->GetNext();
447 bool wxRadioBox::Enable(unsigned int item
, bool enable
)
449 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
451 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
453 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
455 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
456 GtkLabel
*label
= GTK_LABEL(GTK_BIN(button
)->child
);
458 gtk_widget_set_sensitive( GTK_WIDGET(button
), enable
);
459 gtk_widget_set_sensitive( GTK_WIDGET(label
), enable
);
464 bool wxRadioBox::IsItemEnabled(unsigned int item
) const
466 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
468 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
470 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
472 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
474 // don't use GTK_WIDGET_IS_SENSITIVE() here, we want to return true even if
475 // the parent radiobox is disabled
476 return GTK_WIDGET_SENSITIVE(GTK_WIDGET(button
));
479 bool wxRadioBox::Show(unsigned int item
, bool show
)
481 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
483 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
485 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
487 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
490 gtk_widget_show( button
);
492 gtk_widget_hide( button
);
497 bool wxRadioBox::IsItemShown(unsigned int item
) const
499 wxCHECK_MSG( m_widget
!= NULL
, false, wxT("invalid radiobox") );
501 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.Item( item
);
503 wxCHECK_MSG( node
, false, wxT("radiobox wrong index") );
505 GtkButton
*button
= GTK_BUTTON( node
->GetData()->button
);
507 return GTK_WIDGET_VISIBLE(GTK_WIDGET(button
));
510 unsigned int wxRadioBox::GetCount() const
512 return m_buttonsInfo
.GetCount();
515 void wxRadioBox::GtkDisableEvents()
517 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
520 g_signal_handlers_block_by_func(node
->GetData()->button
,
521 (gpointer
)gtk_radiobutton_clicked_callback
, this);
523 node
= node
->GetNext();
527 void wxRadioBox::GtkEnableEvents()
529 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
532 g_signal_handlers_unblock_by_func(node
->GetData()->button
,
533 (gpointer
)gtk_radiobutton_clicked_callback
, this);
535 node
= node
->GetNext();
539 void wxRadioBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
541 GTKFrameApplyWidgetStyle(GTK_FRAME(m_widget
), style
);
543 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
546 GtkWidget
*widget
= GTK_WIDGET( node
->GetData()->button
);
548 gtk_widget_modify_style( widget
, style
);
549 gtk_widget_modify_style(GTK_BIN(widget
)->child
, style
);
551 node
= node
->GetNext();
555 bool wxRadioBox::GTKWidgetNeedsMnemonic() const
560 void wxRadioBox::GTKWidgetDoSetMnemonic(GtkWidget
* w
)
562 GTKFrameSetMnemonicWidget(GTK_FRAME(m_widget
), w
);
566 void wxRadioBox::ApplyToolTip(GtkTooltips
* WXUNUSED(tips
), const gchar
*tip
)
568 // set this tooltip for all radiobuttons which don't have their own tips
570 for ( wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
572 node
= node
->GetNext(), n
++ )
574 if ( !GetItemToolTip(n
) )
576 wxToolTip::Apply(GTK_WIDGET(node
->GetData()->button
), tip
);
581 void wxRadioBox::DoSetItemToolTip(unsigned int n
, wxToolTip
*tooltip
)
585 tooltip
= GetToolTip();
587 buf
= wxGTK_CONV(tooltip
->GetTip());
589 wxToolTip::Apply(GTK_WIDGET(m_buttonsInfo
[n
]->button
), buf
);
592 #endif // wxUSE_TOOLTIPS
594 GdkWindow
*wxRadioBox::GTKGetWindow(wxArrayGdkWindows
& windows
) const
596 windows
.push_back(m_widget
->window
);
598 wxRadioBoxButtonsInfoList::compatibility_iterator node
= m_buttonsInfo
.GetFirst();
601 GtkWidget
*button
= GTK_WIDGET( node
->GetData()->button
);
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