1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/radiobut.mm
3 // Purpose: wxRadioButton
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/radiobut.h"
22 #import <AppKit/NSButton.h>
23 #include "wx/cocoa/string.h"
24 #include "wx/cocoa/autorelease.h"
26 #include "wx/listimpl.cpp"
28 WX_DEFINE_LIST(wxRadioButtonList);
30 // wxRadioButtonBase == wxControl
31 BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
33 WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
35 bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid,
36 const wxString& label,
40 const wxValidator& validator,
43 wxAutoNSAutoreleasePool pool;
45 if(!CreateControl(parent,winid,pos,size,style,validator,name))
50 m_radioSlaves.Append(this);
52 else if(style&wxRB_SINGLE)
56 for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
58 siblingNode = siblingNode->GetPrevious())
60 wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
61 if(radioButton && radioButton!=this)
63 m_radioMaster = radioButton->m_radioMaster;
64 wxASSERT_MSG(m_radioMaster,
65 wxT("Previous radio button should be part of a group"));
66 // Don't crash, assume user meant wxRB_SINGLE
68 m_radioMaster->m_radioSlaves.Append(this);
75 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
76 [m_cocoaNSView release];
77 [GetNSButton() setButtonType: NSRadioButton];
78 CocoaSetLabelForObject(label, GetNSButton());
79 // If it's the first in a group, it should be selected
81 [GetNSButton() setState: NSOnState];
82 [GetNSControl() sizeToFit];
85 m_parent->CocoaAddChild(this);
86 SetInitialFrameRect(pos,size);
91 wxRadioButton::~wxRadioButton()
93 if(m_radioMaster==this)
95 // First get rid of ourselves (we should ALWAYS be at the head)
96 wxRadioButtonList::compatibility_iterator slaveNode =
97 m_radioSlaves.GetFirst();
99 wxASSERT(slaveNode->GetData() == this);
100 m_radioSlaves.Erase(slaveNode);
102 // Now find the new master
103 wxRadioButton *newMaster = NULL;
104 slaveNode = m_radioSlaves.GetFirst();
106 newMaster = slaveNode->GetData();
108 // For each node (including the new master) set the master, remove
109 // it from this list, and add it to the new master's list
110 for(; slaveNode; slaveNode = m_radioSlaves.GetFirst())
112 wxRadioButton *radioButton = slaveNode->GetData();
113 wxASSERT(radioButton->m_radioMaster == this);
114 radioButton->m_radioMaster = newMaster;
115 newMaster->m_radioSlaves.Append(radioButton);
116 m_radioSlaves.Erase(slaveNode);
119 else if(m_radioMaster)
121 m_radioMaster->m_radioSlaves.DeleteObject(this);
122 m_radioMaster = NULL;
125 DisassociateNSButton(GetNSButton());
128 void wxRadioButton::SetValue(bool value)
132 [GetNSButton() setState: NSOnState];
133 Cocoa_DeselectOtherButtonsInTheGroup();
136 [GetNSButton() setState: NSOffState];
139 bool wxRadioButton::GetValue() const
141 int state = [GetNSButton() state];
142 wxASSERT(state!=NSMixedState);
143 return state==NSOnState;
146 void wxRadioButton::SetLabel(const wxString& label)
148 wxAutoNSAutoreleasePool pool;
149 CocoaSetLabelForObject(label, GetNSButton());
152 wxString wxRadioButton::GetLabel() const
154 return wxStringWithNSString([GetNSButton() title]);
158 * If this radio button is part of a group, this method turns off every other
159 * button in the group. If this radio button is not part of a group, this
160 * method does absolutely nothing.
162 void wxRadioButton::Cocoa_DeselectOtherButtonsInTheGroup(void)
166 for(wxRadioButtonList::compatibility_iterator slaveNode =
167 m_radioMaster->m_radioSlaves.GetFirst();
168 slaveNode; slaveNode = slaveNode->GetNext())
170 wxRadioButton *radioButton = slaveNode->GetData();
171 if(radioButton!=this)
172 radioButton->SetValue(false);
177 void wxRadioButton::Cocoa_wxNSButtonAction(void)
179 wxLogTrace(wxTRACE_COCOA,wxT("wxRadioButton"));
180 if([GetNSButton() state] == NSOnState)
182 Cocoa_DeselectOtherButtonsInTheGroup();
184 wxCommandEvent event(wxEVT_RADIOBUTTON, GetId());
185 InitCommandEvent(event); // event.SetEventObject(this);
186 event.SetInt(GetValue());