]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/radiobut.cpp
must be static of course, otherwise items just get appended for every menu-build
[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 #if !USE_SHARED_LIBRARY
23 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
24 #endif
25
26 #include "wx/mac/uma.h"
27
28 bool wxRadioButton::Create(wxWindow *parent, wxWindowID id,
29 const wxString& label,
30 const wxPoint& pos,
31 const wxSize& size, long style,
32 const wxValidator& validator,
33 const wxString& name)
34 {
35 m_macIsUserPane = FALSE ;
36
37 if ( !wxControl::Create(parent, id, pos, size, style, validator, name) )
38 return false;
39
40 m_label = label ;
41
42 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
43
44 m_peer = new wxMacControl(this) ;
45 verify_noerr ( CreateRadioButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") ,
46 0 , false /* no autotoggle */ , m_peer->GetControlRefAddr() ) );
47
48
49 MacPostControlCreate(pos,size) ;
50
51 m_cycle = this ;
52
53 if (HasFlag(wxRB_GROUP))
54 {
55 AddInCycle( NULL ) ;
56 }
57 else
58 {
59 /* search backward for last group start */
60 wxRadioButton *chief = (wxRadioButton*) NULL;
61 wxWindowList::compatibility_iterator node = parent->GetChildren().GetLast();
62 while (node)
63 {
64 wxWindow *child = node->GetData();
65 if (child->IsKindOf( CLASSINFO( wxRadioButton ) ) )
66 {
67 chief = (wxRadioButton*) child;
68 if (child->HasFlag(wxRB_GROUP)) break;
69 }
70 node = node->GetPrevious();
71 }
72 AddInCycle( chief ) ;
73 }
74 return TRUE;
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 wxInt32 wxRadioButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
110 {
111 // if already set -> no action
112 if ( GetValue() )
113 return noErr;
114
115 wxRadioButton *cycle, *old = NULL ;
116 cycle=this->NextInCycle();
117 if (cycle!=NULL) {
118 while (cycle!=this) {
119 if ( cycle->GetValue() ) {
120 old = cycle ;
121 cycle->SetValue(false);
122 }
123 cycle=cycle->NextInCycle();
124 }
125 }
126
127 SetValue(true) ;
128
129 if ( old ) {
130 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, old->m_windowId );
131 event.SetEventObject(old);
132 event.SetInt( false );
133 old->ProcessCommand(event);
134 }
135 wxCommandEvent event2(wxEVT_COMMAND_RADIOBUTTON_SELECTED, m_windowId );
136 event2.SetEventObject(this);
137 event2.SetInt( true );
138 ProcessCommand(event2);
139 return noErr ;
140 }
141
142 wxRadioButton *wxRadioButton::AddInCycle(wxRadioButton *cycle)
143 {
144 wxRadioButton *next,*current;
145
146 if (cycle==NULL) {
147 m_cycle=this;
148 return(this);
149 }
150 else {
151 current=cycle;
152 while ((next=current->m_cycle)!=cycle)
153 current=current->m_cycle;
154 m_cycle=cycle;
155 current->m_cycle=this;
156 return(cycle);
157 }
158 }
159
160 #endif