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