renaming
[wxWidgets.git] / src / osx / carbon / radiobut.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$
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/mac/uma.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 Rect bounds = wxMacGetBoundsForControl( this, pos, size );
39
40 m_peer = new wxMacControl( this );
41 OSStatus err = CreateRadioButtonControl(
42 MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, CFSTR(""),
43 0, false /* no autotoggle */, m_peer->GetControlRefAddr() );
44 verify_noerr( err );
45
46 MacPostControlCreate( pos, size );
47
48 m_cycle = this;
49
50 if (HasFlag( wxRB_GROUP ))
51 {
52 AddInCycle( NULL );
53 }
54 else
55 {
56 // search backward for last group start
57 wxRadioButton *chief = NULL;
58 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
59 while (node)
60 {
61 wxWindow *child = node->GetData();
62 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ))
63 {
64 chief = (wxRadioButton*)child;
65 if (child->HasFlag( wxRB_GROUP ))
66 break;
67 }
68
69 node = node->GetPrevious();
70 }
71
72 AddInCycle( chief );
73 }
74
75 return true;
76 }
77
78 wxRadioButton::~wxRadioButton()
79 {
80 RemoveFromCycle();
81 }
82
83 void wxRadioButton::SetValue(bool val)
84 {
85 wxRadioButton *cycle;
86 if (m_peer->GetValue() == val)
87 return;
88
89 m_peer->SetValue( val );
90 if (val)
91 {
92 cycle = this->NextInCycle();
93 if (cycle != NULL)
94 {
95 while (cycle != this)
96 {
97 cycle->SetValue( false );
98 cycle = cycle->NextInCycle();
99 }
100 }
101 }
102 }
103
104 bool wxRadioButton::GetValue() const
105 {
106 return m_peer->GetValue();
107 }
108
109 void wxRadioButton::Command(wxCommandEvent& event)
110 {
111 SetValue( (event.GetInt() != 0) );
112 ProcessCommand( event );
113 }
114
115 wxInt32 wxRadioButton::MacControlHit( WXEVENTHANDLERREF WXUNUSED(handler), WXEVENTREF WXUNUSED(event) )
116 {
117 // if already set -> no action
118 if (GetValue())
119 return noErr;
120
121 wxRadioButton *cycle;
122 cycle = this->NextInCycle();
123 if (cycle != NULL)
124 {
125 while (cycle != this)
126 {
127 if (cycle->GetValue())
128 cycle->SetValue( false );
129
130 cycle = cycle->NextInCycle();
131 }
132 }
133
134 SetValue( true );
135
136 wxCommandEvent event2( wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId );
137 event2.SetEventObject( this );
138 event2.SetInt( true );
139 ProcessCommand( event2 );
140
141 return noErr;
142 }
143
144 wxRadioButton *wxRadioButton::AddInCycle(wxRadioButton *cycle)
145 {
146 wxRadioButton *current;
147
148 if (cycle == NULL)
149 {
150 m_cycle = this;
151 }
152 else
153 {
154 current = cycle;
155 while (current->m_cycle != cycle)
156 current = current->m_cycle;
157
158 m_cycle = cycle;
159 current->m_cycle = this;
160 }
161
162 return m_cycle;
163 }
164
165 void wxRadioButton::RemoveFromCycle()
166 {
167 if ((m_cycle == NULL) || (m_cycle == this))
168 return;
169
170 // Find the previous one and make it point to the next one
171 wxRadioButton* prev = this;
172 while (prev->m_cycle != this)
173 prev = prev->m_cycle;
174
175 prev->m_cycle = m_cycle;
176 }
177
178 #endif