common implementation files
[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();
101 }
102
103 void wxRadioButton::Command(wxCommandEvent& event)
104 {
105 SetValue( (event.GetInt() != 0) );
106 ProcessCommand( event );
107 }
108
109 bool wxRadioButton::HandleClicked( double timestampsec )
110 {
111 // if already set -> no action
112 if (GetValue())
113 return true;
114
115 wxRadioButton *cycle;
116 cycle = this->NextInCycle();
117 if (cycle != NULL)
118 {
119 while (cycle != this)
120 {
121 if (cycle->GetValue())
122 cycle->SetValue( false );
123
124 cycle = cycle->NextInCycle();
125 }
126 }
127
128 SetValue( true );
129
130 wxCommandEvent event2( wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId );
131 event2.SetEventObject( this );
132 event2.SetInt( true );
133 ProcessCommand( event2 );
134
135 return true;
136 }
137
138 wxRadioButton *wxRadioButton::AddInCycle(wxRadioButton *cycle)
139 {
140 wxRadioButton *current;
141
142 if (cycle == NULL)
143 {
144 m_cycle = this;
145 }
146 else
147 {
148 current = cycle;
149 while (current->m_cycle != cycle)
150 current = current->m_cycle;
151
152 m_cycle = cycle;
153 current->m_cycle = this;
154 }
155
156 return m_cycle;
157 }
158
159 void wxRadioButton::RemoveFromCycle()
160 {
161 if ((m_cycle == NULL) || (m_cycle == this))
162 return;
163
164 // Find the previous one and make it point to the next one
165 wxRadioButton* prev = this;
166 while (prev->m_cycle != this)
167 prev = prev->m_cycle;
168
169 prev->m_cycle = m_cycle;
170 }
171
172 #endif