simplify code to return from the end of the function
[wxWidgets.git] / src / gtk / radiobut.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/gtk/radiobut.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
f96aa4d9
RR
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
dcf924a3 12
d968078a 13#if wxUSE_RADIOBTN
dcf924a3 14
1e6feb95
VZ
15#include "wx/radiobut.h"
16
9dc44eff 17#include <gtk/gtk.h>
9e691f46 18#include "wx/gtk/private.h"
9dc44eff 19#include "wx/gtk/private/gtk2-compat.h"
c801d85f 20
6de97a3b
RR
21//-----------------------------------------------------------------------------
22// data
23//-----------------------------------------------------------------------------
24
d7fa7eaa 25extern bool g_blockEventsOnDrag;
6de97a3b
RR
26
27//-----------------------------------------------------------------------------
bb4549de 28// "clicked"
6de97a3b
RR
29//-----------------------------------------------------------------------------
30
865bb325 31extern "C" {
b2ff89d6 32static
e2762ff0 33void gtk_radiobutton_clicked_callback( GtkToggleButton *button, wxRadioButton *rb )
6de97a3b 34{
f5d29b39 35 if (g_blockEventsOnDrag) return;
b2ff89d6 36
385e8575 37 if (!gtk_toggle_button_get_active(button)) return;
b2ff89d6 38
f5d29b39
RR
39 wxCommandEvent event( wxEVT_COMMAND_RADIOBUTTON_SELECTED, rb->GetId());
40 event.SetInt( rb->GetValue() );
41 event.SetEventObject( rb );
937013e0 42 rb->HandleWindowEvent( event );
6de97a3b 43}
865bb325 44}
6de97a3b 45
bb4549de
RR
46//-----------------------------------------------------------------------------
47// wxRadioButton
48//-----------------------------------------------------------------------------
49
2b4f3c9f
VZ
50bool wxRadioButton::Create( wxWindow *parent,
51 wxWindowID id,
52 const wxString& label,
53 const wxPoint& pos,
54 const wxSize& size,
55 long style,
56 const wxValidator& validator,
57 const wxString& name )
6de97a3b 58{
4dcaf11a
RR
59 if (!PreCreation( parent, pos, size ) ||
60 !CreateBase( parent, id, pos, size, style, validator, name ))
61 {
223d09f6 62 wxFAIL_MSG( wxT("wxRadioButton creation failed") );
e8375af8 63 return false;
4dcaf11a 64 }
953704c1 65
a45c6a89
VZ
66 // Check if this radio button should be put into an existing group. This
67 // shouldn't be done if it's given a style to explicitly start a new group
68 // or if it's not meant to be a part of a group at all.
739b7529 69 GSList* radioButtonGroup = NULL;
f938aa99 70 if (!HasFlag(wxRB_GROUP) && !HasFlag(wxRB_SINGLE))
953704c1 71 {
9864c56d 72 // search backward for last group start
222ed1d6 73 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
d968078a 74 for (; node; node = node->GetPrevious())
e2762ff0
RR
75 {
76 wxWindow *child = node->GetData();
a45c6a89
VZ
77
78 // We stop at the first previous radio button in any case as it
79 // wouldn't make sense to put this button in a group with another
80 // one if there is a radio button that is not part of the same
81 // group between them.
82 if (wxIsKindOf(child, wxRadioButton))
e2762ff0 83 {
a45c6a89
VZ
84 // Any preceding radio button can be used to get its group, not
85 // necessarily one with wxRB_GROUP style, but exclude
86 // wxRB_SINGLE ones as their group should never be shared.
87 if (!child->HasFlag(wxRB_SINGLE))
88 {
89 radioButtonGroup = gtk_radio_button_get_group(
90 GTK_RADIO_BUTTON(child->m_widget));
91 }
92
d968078a 93 break;
e2762ff0 94 }
e2762ff0 95 }
953704c1
RR
96 }
97
739b7529 98 m_widget = gtk_radio_button_new_with_label( radioButtonGroup, wxGTK_CONV( label ) );
9ff9d30c 99 g_object_ref(m_widget);
b2ff89d6 100
f5d29b39 101 SetLabel(label);
6de97a3b 102
e9a7fde5
RR
103 g_signal_connect_after (m_widget, "clicked",
104 G_CALLBACK (gtk_radiobutton_clicked_callback), this);
b2ff89d6 105
f03fc89f 106 m_parent->DoAddChild( this );
b2ff89d6 107
abdeb9e7 108 PostCreation(size);
6de97a3b 109
d968078a 110 return true;
6de97a3b
RR
111}
112
113void wxRadioButton::SetLabel( const wxString& label )
114{
223d09f6 115 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
b2ff89d6 116
6ea2bc50
VZ
117 // save the original label
118 wxControlBase::SetLabel(label);
119
385e8575 120 GTKSetLabelForLabel(GTK_LABEL(gtk_bin_get_child(GTK_BIN(m_widget))), label);
6de97a3b
RR
121}
122
123void wxRadioButton::SetValue( bool val )
124{
223d09f6 125 wxCHECK_RET( m_widget != NULL, wxT("invalid radiobutton") );
f6bcfd97 126
953704c1 127 if (val == GetValue())
0659e7ee
RR
128 return;
129
d968078a
PC
130 g_signal_handlers_block_by_func(
131 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
953704c1 132
f5d29b39 133 if (val)
953704c1 134 {
e2762ff0 135 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(m_widget), TRUE );
953704c1 136 }
f5d29b39 137 else
953704c1
RR
138 {
139 // should give an assert
f6bcfd97
BP
140 // RL - No it shouldn't. A wxGenericValidator might try to set it
141 // as FALSE. Failing silently is probably TRTTD here.
953704c1 142 }
f6bcfd97 143
d968078a
PC
144 g_signal_handlers_unblock_by_func(
145 m_widget, (void*)gtk_radiobutton_clicked_callback, this);
6de97a3b
RR
146}
147
eb082a08 148bool wxRadioButton::GetValue() const
6de97a3b 149{
d968078a 150 wxCHECK_MSG( m_widget != NULL, false, wxT("invalid radiobutton") );
b2ff89d6 151
385e8575 152 return gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_widget)) != 0;
6de97a3b
RR
153}
154
f03fc89f 155bool wxRadioButton::Enable( bool enable )
d3904ceb 156{
b545684e 157 if (!base_type::Enable(enable))
d968078a 158 return false;
b2ff89d6 159
385e8575 160 gtk_widget_set_sensitive(gtk_bin_get_child(GTK_BIN(m_widget)), enable);
f03fc89f 161
b545684e 162 if (enable)
ad60f9e7 163 GTKFixSensitivity();
ad60f9e7 164
d968078a 165 return true;
d3904ceb
RR
166}
167
f40fdaa3 168void wxRadioButton::DoApplyWidgetStyle(GtkRcStyle *style)
868a2826 169{
9dc44eff
PC
170 GTKApplyStyle(m_widget, style);
171 GTKApplyStyle(gtk_bin_get_child(GTK_BIN(m_widget)), style);
f96aa4d9 172}
dcf924a3 173
ef5c70f9
VZ
174GdkWindow *
175wxRadioButton::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
2f073eb2 176{
9dc44eff 177 return gtk_button_get_event_window(GTK_BUTTON(m_widget));
2f073eb2
RR
178}
179
9d522606
RD
180// static
181wxVisualAttributes
182wxRadioButton::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
183{
7fff16b8 184 return GetDefaultAttributesFromGTKWidget(gtk_radio_button_new_with_label(NULL, ""));
9d522606
RD
185}
186
187
dcf924a3 188#endif