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