]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/radiobut.mm
* Use wxTopLevelWindow::Show() for modeless dialogs
[wxWidgets.git] / src / cocoa / radiobut.mm
CommitLineData
da0634c1
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/radiobut.mm
3// Purpose: wxRadioButton
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
7// RCS-ID: $Id:
8// Copyright: (c) 2003 David Elliott
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/app.h"
13#include "wx/radiobut.h"
14#include "wx/log.h"
15
16#import <AppKit/NSButton.h>
c8cae94c 17#include "wx/cocoa/string.h"
e51c36de 18#include "wx/cocoa/autorelease.h"
da0634c1 19
d0a5e34a
DE
20#include "wx/listimpl.cpp"
21
22WX_DEFINE_LIST(wxRadioButtonList);
23
da0634c1
DE
24IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl)
25// wxRadioButtonBase == wxControl
26BEGIN_EVENT_TABLE(wxRadioButton, wxControl)
27END_EVENT_TABLE()
28WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView)
29
30bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid,
31 const wxString& label,
32 const wxPoint& pos,
33 const wxSize& size,
34 long style,
35 const wxValidator& validator,
36 const wxString& name)
37{
e51c36de 38 wxAutoNSAutoreleasePool pool;
d0a5e34a 39 m_radioMaster = NULL;
da0634c1
DE
40 if(!CreateControl(parent,winid,pos,size,style,validator,name))
41 return false;
d0a5e34a
DE
42 if(style&wxRB_GROUP)
43 {
44 m_radioMaster = this;
45 m_radioSlaves.Append(this);
46 }
47 else if(style&wxRB_SINGLE)
48 m_radioMaster = NULL;
49 else
50 {
51 for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast();
52 siblingNode;
53 siblingNode = siblingNode->GetPrevious())
54 {
55 wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton);
56 if(radioButton && radioButton!=this)
57 {
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
62 if(m_radioMaster)
63 m_radioMaster->m_radioSlaves.Append(this);
64 break;
65 }
66 }
67 }
68
da0634c1 69 m_cocoaNSView = NULL;
8d656ea9 70 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
da0634c1
DE
71 [m_cocoaNSView release];
72 [GetNSButton() setButtonType: NSRadioButton];
c8cae94c 73 [GetNSButton() setTitle:wxNSStringWithWxString(label)];
bd8e6e37
DE
74 // If it's the first in a group, it should be selected
75 if(style&wxRB_GROUP)
76 [GetNSButton() setState: NSOnState];
da0634c1
DE
77 [GetNSControl() sizeToFit];
78
79 if(m_parent)
80 m_parent->CocoaAddChild(this);
8d656ea9
DE
81 SetInitialFrameRect(pos,size);
82
da0634c1
DE
83 return true;
84}
85
86wxRadioButton::~wxRadioButton()
87{
d0a5e34a
DE
88 if(m_radioMaster==this)
89 {
90 // First get rid of ourselves (we should ALWAYS be at the head)
91 wxRadioButtonList::compatibility_iterator slaveNode =
92 m_radioSlaves.GetFirst();
93 wxASSERT(slaveNode);
94 wxASSERT(slaveNode->GetData() == this);
95 m_radioSlaves.DeleteNode(slaveNode);
96
97 // Now find the new master
98 wxRadioButton *newMaster = NULL;
99 slaveNode = m_radioSlaves.GetFirst();
100 if(slaveNode)
101 newMaster = slaveNode->GetData();
102
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())
106 {
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);
112 }
113 }
114 else if(m_radioMaster)
115 {
116 m_radioMaster->m_radioSlaves.DeleteObject(this);
117 m_radioMaster = NULL;
118 }
119 // normal stuff
570aaadf 120 DisassociateNSButton(m_cocoaNSView);
da0634c1
DE
121}
122
d0a5e34a 123void wxRadioButton::SetValue(bool value)
da0634c1 124{
d0a5e34a
DE
125 if(value)
126 [GetNSButton() setState: NSOnState];
127 else
128 [GetNSButton() setState: NSOffState];
da0634c1
DE
129}
130
131bool wxRadioButton::GetValue() const
132{
d0a5e34a
DE
133 int state = [GetNSButton() state];
134 wxASSERT(state!=NSMixedState);
135 return state==NSOnState;
da0634c1
DE
136}
137
138void wxRadioButton::Cocoa_wxNSButtonAction(void)
139{
140 wxLogDebug("wxRadioButton");
d0a5e34a
DE
141 if(m_radioMaster && ([GetNSButton() state] == NSOnState))
142 {
143 for(wxRadioButtonList::compatibility_iterator slaveNode =
144 m_radioMaster->m_radioSlaves.GetFirst();
145 slaveNode; slaveNode = slaveNode->GetNext())
146 {
147 wxRadioButton *radioButton = slaveNode->GetData();
148 if(radioButton!=this)
149 radioButton->SetValue(false);
150 }
151 }
152 wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId());
153 InitCommandEvent(event); // event.SetEventObject(this);
154 event.SetInt(GetValue());
155 Command(event);
da0634c1
DE
156}
157