]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | WX_DEFINE_LIST(wxRadioButtonList); | |
23 | ||
da0634c1 DE |
24 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl) |
25 | // wxRadioButtonBase == wxControl | |
26 | BEGIN_EVENT_TABLE(wxRadioButton, wxControl) | |
27 | END_EVENT_TABLE() | |
28 | WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView) | |
29 | ||
30 | bool 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)]; |
da0634c1 DE |
74 | [GetNSControl() sizeToFit]; |
75 | ||
76 | if(m_parent) | |
77 | m_parent->CocoaAddChild(this); | |
8d656ea9 DE |
78 | SetInitialFrameRect(pos,size); |
79 | ||
da0634c1 DE |
80 | return true; |
81 | } | |
82 | ||
83 | wxRadioButton::~wxRadioButton() | |
84 | { | |
d0a5e34a DE |
85 | if(m_radioMaster==this) |
86 | { | |
87 | // First get rid of ourselves (we should ALWAYS be at the head) | |
88 | wxRadioButtonList::compatibility_iterator slaveNode = | |
89 | m_radioSlaves.GetFirst(); | |
90 | wxASSERT(slaveNode); | |
91 | wxASSERT(slaveNode->GetData() == this); | |
92 | m_radioSlaves.DeleteNode(slaveNode); | |
93 | ||
94 | // Now find the new master | |
95 | wxRadioButton *newMaster = NULL; | |
96 | slaveNode = m_radioSlaves.GetFirst(); | |
97 | if(slaveNode) | |
98 | newMaster = slaveNode->GetData(); | |
99 | ||
100 | // For each node (including the new master) set the master, remove | |
101 | // it from this list, and add it to the new master's list | |
102 | for(; slaveNode; slaveNode = m_radioSlaves.GetFirst()) | |
103 | { | |
104 | wxRadioButton *radioButton = slaveNode->GetData(); | |
105 | wxASSERT(radioButton->m_radioMaster == this); | |
106 | radioButton->m_radioMaster = newMaster; | |
107 | newMaster->m_radioSlaves.Append(radioButton); | |
108 | m_radioSlaves.DeleteNode(slaveNode); | |
109 | } | |
110 | } | |
111 | else if(m_radioMaster) | |
112 | { | |
113 | m_radioMaster->m_radioSlaves.DeleteObject(this); | |
114 | m_radioMaster = NULL; | |
115 | } | |
116 | // normal stuff | |
570aaadf | 117 | DisassociateNSButton(m_cocoaNSView); |
da0634c1 DE |
118 | } |
119 | ||
d0a5e34a | 120 | void wxRadioButton::SetValue(bool value) |
da0634c1 | 121 | { |
d0a5e34a DE |
122 | if(value) |
123 | [GetNSButton() setState: NSOnState]; | |
124 | else | |
125 | [GetNSButton() setState: NSOffState]; | |
da0634c1 DE |
126 | } |
127 | ||
128 | bool wxRadioButton::GetValue() const | |
129 | { | |
d0a5e34a DE |
130 | int state = [GetNSButton() state]; |
131 | wxASSERT(state!=NSMixedState); | |
132 | return state==NSOnState; | |
da0634c1 DE |
133 | } |
134 | ||
135 | void wxRadioButton::Cocoa_wxNSButtonAction(void) | |
136 | { | |
137 | wxLogDebug("wxRadioButton"); | |
d0a5e34a DE |
138 | if(m_radioMaster && ([GetNSButton() state] == NSOnState)) |
139 | { | |
140 | for(wxRadioButtonList::compatibility_iterator slaveNode = | |
141 | m_radioMaster->m_radioSlaves.GetFirst(); | |
142 | slaveNode; slaveNode = slaveNode->GetNext()) | |
143 | { | |
144 | wxRadioButton *radioButton = slaveNode->GetData(); | |
145 | if(radioButton!=this) | |
146 | radioButton->SetValue(false); | |
147 | } | |
148 | } | |
149 | wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId()); | |
150 | InitCommandEvent(event); // event.SetEventObject(this); | |
151 | event.SetInt(GetValue()); | |
152 | Command(event); | |
da0634c1 DE |
153 | } |
154 |