use bool expression rather than implicit conversion
[wxWidgets.git] / src / osx / radiobut_osx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: radiobut.cpp
3 // Purpose: wxRadioButton
4 // Author: AUTHOR
5 // Modified by: JS Lair (99/11/15) adding the cyclic group notion for radiobox
6 // Created: ??/??/98
7 // RCS-ID: $Id: radiobut.cpp 54129 2008-06-11 19:30:52Z SC $
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_RADIOBTN
15
16 #include "wx/radiobut.h"
17 #include "wx/osx/private.h"
18
19 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
20
21
22 bool wxRadioButton::Create( wxWindow *parent,
23 wxWindowID id,
24 const wxString& label,
25 const wxPoint& pos,
26 const wxSize& size,
27 long style,
28 const wxValidator& validator,
29 const wxString& name )
30 {
31 m_macIsUserPane = false;
32
33 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
34 return false;
35
36 m_labelOrig = m_label = label;
37
38 m_peer = wxWidgetImpl::CreateRadioButton( this, parent, id, label, pos, size, style, GetExtraStyle() );
39
40 MacPostControlCreate( pos, size );
41
42 m_cycle = this;
43
44 if (HasFlag( wxRB_GROUP ))
45 {
46 AddInCycle( NULL );
47 }
48 else
49 {
50 // search backward for last group start
51 wxRadioButton *chief = NULL;
52 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
53 while (node)
54 {
55 wxWindow *child = node->GetData();
56 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ))
57 {
58 chief = (wxRadioButton*)child;
59 if (child->HasFlag( wxRB_GROUP ))
60 break;
61 }
62
63 node = node->GetPrevious();
64 }
65
66 AddInCycle( chief );
67 }
68
69 return true;
70 }
71
72 wxRadioButton::~wxRadioButton()
73 {
74 RemoveFromCycle();
75 }
76
77 void wxRadioButton::SetValue(bool val)
78 {
79 wxRadioButton *cycle;
80 if (m_peer->GetValue() == val)
81 return;
82
83 m_peer->SetValue( val );
84 if (val)
85 {
86 cycle = this->NextInCycle();
87 if (cycle != NULL)
88 {
89 while (cycle != this)
90 {
91 cycle->SetValue( false );
92 cycle = cycle->NextInCycle();
93 }
94 }
95 }
96 }
97
98 bool wxRadioButton::GetValue() const
99 {
100 return m_peer->GetValue() != 0;
101 }
102
103 void wxRadioButton::Command(wxCommandEvent& event)
104 {
105 SetValue( (event.GetInt() != 0) );
106 ProcessCommand( event );
107 }
108
109 bool wxRadioButton::OSXHandleClicked( double timestampsec )
110 {
111 if ( !m_peer->ButtonClickDidStateChange() )
112 {
113 // if already set -> no action
114 if (GetValue())
115 return true;
116 }
117
118 wxRadioButton *cycle;
119 cycle = this->NextInCycle();
120 if (cycle != NULL)
121 {
122 while (cycle != this)
123 {
124 if (cycle->GetValue())
125 cycle->SetValue( false );
126
127 cycle = cycle->NextInCycle();
128 }
129 }
130
131 SetValue( true );
132
133 wxCommandEvent event2( wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId );
134 event2.SetEventObject( this );
135 event2.SetInt( true );
136 ProcessCommand( event2 );
137
138 return true;
139 }
140
141 wxRadioButton *wxRadioButton::AddInCycle(wxRadioButton *cycle)
142 {
143 wxRadioButton *current;
144
145 if (cycle == NULL)
146 {
147 m_cycle = this;
148 }
149 else
150 {
151 current = cycle;
152 while (current->m_cycle != cycle)
153 current = current->m_cycle;
154
155 m_cycle = cycle;
156 current->m_cycle = this;
157 }
158
159 return m_cycle;
160 }
161
162 void wxRadioButton::RemoveFromCycle()
163 {
164 if ((m_cycle == NULL) || (m_cycle == this))
165 return;
166
167 // Find the previous one and make it point to the next one
168 wxRadioButton* prev = this;
169 while (prev->m_cycle != this)
170 prev = prev->m_cycle;
171
172 prev->m_cycle = m_cycle;
173 }
174
175 #endif