1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/radiobut.mm
3 // Purpose: wxRadioButton
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/radiobut.h"
16 #import <AppKit/NSButton.h>
17 #include "wx/cocoa/string.h"
18 #include "wx/cocoa/autorelease.h"
20 #include "wx/listimpl.cpp"
22 WX_DEFINE_LIST(wxRadioButtonList);
24 IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
25 // wxRadioButtonBase == wxControl
26 BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
28 WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
30 bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid,
31 const wxString& label,
35 const wxValidator& validator,
38 wxAutoNSAutoreleasePool pool;
40 if(!CreateControl(parent,winid,pos,size,style,validator,name))
45 m_radioSlaves.Append(this);
47 else if(style&wxRB_SINGLE)
51 for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
53 siblingNode = siblingNode->GetPrevious())
55 wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
56 if(radioButton && radioButton!=this)
58 m_radioMaster = radioButton->m_radioMaster;
59 wxASSERT_MSG(m_radioMaster,
60 "Previous radio button should be part of a group");
61 // Don't crash, assume user meant wxRB_SINGLE
63 m_radioMaster->m_radioSlaves.Append(this);
70 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
71 [m_cocoaNSView release];
72 [GetNSButton() setButtonType: NSRadioButton];
73 [GetNSButton() setTitle:wxNSStringWithWxString(label)];
74 // If it's the first in a group, it should be selected
76 [GetNSButton() setState: NSOnState];
77 [GetNSControl() sizeToFit];
80 m_parent->CocoaAddChild(this);
81 SetInitialFrameRect(pos,size);
86 wxRadioButton::~wxRadioButton()
88 if(m_radioMaster==this)
90 // First get rid of ourselves (we should ALWAYS be at the head)
91 wxRadioButtonList::compatibility_iterator slaveNode =
92 m_radioSlaves.GetFirst();
94 wxASSERT(slaveNode->GetData() == this);
95 m_radioSlaves.DeleteNode(slaveNode);
97 // Now find the new master
98 wxRadioButton *newMaster = NULL;
99 slaveNode = m_radioSlaves.GetFirst();
101 newMaster = slaveNode->GetData();
103 // For each node (including the new master) set the master, remove
104 // it from this list, and add it to the new master's list
105 for(; slaveNode; slaveNode = m_radioSlaves.GetFirst())
107 wxRadioButton *radioButton = slaveNode->GetData();
108 wxASSERT(radioButton->m_radioMaster == this);
109 radioButton->m_radioMaster = newMaster;
110 newMaster->m_radioSlaves.Append(radioButton);
111 m_radioSlaves.DeleteNode(slaveNode);
114 else if(m_radioMaster)
116 m_radioMaster->m_radioSlaves.DeleteObject(this);
117 m_radioMaster = NULL;
120 DisassociateNSButton(m_cocoaNSView);
123 void wxRadioButton::SetValue(bool value)
126 [GetNSButton() setState: NSOnState];
128 [GetNSButton() setState: NSOffState];
131 bool wxRadioButton::GetValue() const
133 int state = [GetNSButton() state];
134 wxASSERT(state!=NSMixedState);
135 return state==NSOnState;
138 void wxRadioButton::Cocoa_wxNSButtonAction(void)
140 wxLogDebug("wxRadioButton");
141 if(m_radioMaster && ([GetNSButton() state] == NSOnState))
143 for(wxRadioButtonList::compatibility_iterator slaveNode =
144 m_radioMaster->m_radioSlaves.GetFirst();
145 slaveNode; slaveNode = slaveNode->GetNext())
147 wxRadioButton *radioButton = slaveNode->GetData();
148 if(radioButton!=this)
149 radioButton->SetValue(false);
152 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
153 InitCommandEvent(event); // event.SetEventObject(this);
154 event.SetInt(GetValue());