]>
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 | ||
449c5673 DE |
12 | #include "wx/wxprec.h" |
13 | #ifndef WX_PRECOMP | |
14 | #include "wx/log.h" | |
15 | #include "wx/app.h" | |
16 | #include "wx/radiobut.h" | |
17 | #endif //WX_PRECOMP | |
da0634c1 DE |
18 | |
19 | #import <AppKit/NSButton.h> | |
c8cae94c | 20 | #include "wx/cocoa/string.h" |
e51c36de | 21 | #include "wx/cocoa/autorelease.h" |
da0634c1 | 22 | |
d0a5e34a DE |
23 | #include "wx/listimpl.cpp" |
24 | ||
25 | WX_DEFINE_LIST(wxRadioButtonList); | |
26 | ||
da0634c1 DE |
27 | IMPLEMENT_DYNAMIC_CLASS(wxRadioButton, wxControl) |
28 | // wxRadioButtonBase == wxControl | |
29 | BEGIN_EVENT_TABLE(wxRadioButton, wxControl) | |
30 | END_EVENT_TABLE() | |
31 | WX_IMPLEMENT_COCOA_OWNER(wxRadioButton,NSButton,NSControl,NSView) | |
32 | ||
33 | bool wxRadioButton::Create(wxWindow *parent, wxWindowID winid, | |
34 | const wxString& label, | |
35 | const wxPoint& pos, | |
36 | const wxSize& size, | |
37 | long style, | |
38 | const wxValidator& validator, | |
39 | const wxString& name) | |
40 | { | |
e51c36de | 41 | wxAutoNSAutoreleasePool pool; |
d0a5e34a | 42 | m_radioMaster = NULL; |
da0634c1 DE |
43 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
44 | return false; | |
d0a5e34a DE |
45 | if(style&wxRB_GROUP) |
46 | { | |
47 | m_radioMaster = this; | |
48 | m_radioSlaves.Append(this); | |
49 | } | |
50 | else if(style&wxRB_SINGLE) | |
51 | m_radioMaster = NULL; | |
52 | else | |
53 | { | |
54 | for(wxWindowList::compatibility_iterator siblingNode= GetParent()->GetChildren().GetLast(); | |
55 | siblingNode; | |
56 | siblingNode = siblingNode->GetPrevious()) | |
57 | { | |
58 | wxRadioButton *radioButton = wxDynamicCast(siblingNode->GetData(),wxRadioButton); | |
59 | if(radioButton && radioButton!=this) | |
60 | { | |
61 | m_radioMaster = radioButton->m_radioMaster; | |
62 | wxASSERT_MSG(m_radioMaster, | |
2b030203 | 63 | wxT("Previous radio button should be part of a group")); |
d0a5e34a DE |
64 | // Don't crash, assume user meant wxRB_SINGLE |
65 | if(m_radioMaster) | |
66 | m_radioMaster->m_radioSlaves.Append(this); | |
67 | break; | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
da0634c1 | 72 | m_cocoaNSView = NULL; |
8d656ea9 | 73 | SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]); |
da0634c1 DE |
74 | [m_cocoaNSView release]; |
75 | [GetNSButton() setButtonType: NSRadioButton]; | |
c8cae94c | 76 | [GetNSButton() setTitle:wxNSStringWithWxString(label)]; |
bd8e6e37 DE |
77 | // If it's the first in a group, it should be selected |
78 | if(style&wxRB_GROUP) | |
79 | [GetNSButton() setState: NSOnState]; | |
da0634c1 DE |
80 | [GetNSControl() sizeToFit]; |
81 | ||
82 | if(m_parent) | |
83 | m_parent->CocoaAddChild(this); | |
8d656ea9 DE |
84 | SetInitialFrameRect(pos,size); |
85 | ||
da0634c1 DE |
86 | return true; |
87 | } | |
88 | ||
89 | wxRadioButton::~wxRadioButton() | |
90 | { | |
d0a5e34a DE |
91 | if(m_radioMaster==this) |
92 | { | |
93 | // First get rid of ourselves (we should ALWAYS be at the head) | |
94 | wxRadioButtonList::compatibility_iterator slaveNode = | |
95 | m_radioSlaves.GetFirst(); | |
96 | wxASSERT(slaveNode); | |
97 | wxASSERT(slaveNode->GetData() == this); | |
98 | m_radioSlaves.DeleteNode(slaveNode); | |
99 | ||
100 | // Now find the new master | |
101 | wxRadioButton *newMaster = NULL; | |
102 | slaveNode = m_radioSlaves.GetFirst(); | |
103 | if(slaveNode) | |
104 | newMaster = slaveNode->GetData(); | |
105 | ||
106 | // For each node (including the new master) set the master, remove | |
107 | // it from this list, and add it to the new master's list | |
108 | for(; slaveNode; slaveNode = m_radioSlaves.GetFirst()) | |
109 | { | |
110 | wxRadioButton *radioButton = slaveNode->GetData(); | |
111 | wxASSERT(radioButton->m_radioMaster == this); | |
112 | radioButton->m_radioMaster = newMaster; | |
113 | newMaster->m_radioSlaves.Append(radioButton); | |
114 | m_radioSlaves.DeleteNode(slaveNode); | |
115 | } | |
116 | } | |
117 | else if(m_radioMaster) | |
118 | { | |
119 | m_radioMaster->m_radioSlaves.DeleteObject(this); | |
120 | m_radioMaster = NULL; | |
121 | } | |
122 | // normal stuff | |
911e17c6 | 123 | DisassociateNSButton(GetNSButton()); |
da0634c1 DE |
124 | } |
125 | ||
d0a5e34a | 126 | void wxRadioButton::SetValue(bool value) |
da0634c1 | 127 | { |
d0a5e34a DE |
128 | if(value) |
129 | [GetNSButton() setState: NSOnState]; | |
130 | else | |
131 | [GetNSButton() setState: NSOffState]; | |
da0634c1 DE |
132 | } |
133 | ||
134 | bool wxRadioButton::GetValue() const | |
135 | { | |
d0a5e34a DE |
136 | int state = [GetNSButton() state]; |
137 | wxASSERT(state!=NSMixedState); | |
138 | return state==NSOnState; | |
da0634c1 DE |
139 | } |
140 | ||
141 | void wxRadioButton::Cocoa_wxNSButtonAction(void) | |
142 | { | |
2b030203 | 143 | wxLogDebug(wxT("wxRadioButton")); |
d0a5e34a DE |
144 | if(m_radioMaster && ([GetNSButton() state] == NSOnState)) |
145 | { | |
146 | for(wxRadioButtonList::compatibility_iterator slaveNode = | |
147 | m_radioMaster->m_radioSlaves.GetFirst(); | |
148 | slaveNode; slaveNode = slaveNode->GetNext()) | |
149 | { | |
150 | wxRadioButton *radioButton = slaveNode->GetData(); | |
151 | if(radioButton!=this) | |
152 | radioButton->SetValue(false); | |
153 | } | |
154 | } | |
155 | wxCommandEvent event(wxEVT_COMMAND_RADIOBUTTON_SELECTED, GetId()); | |
156 | InitCommandEvent(event); // event.SetEventObject(this); | |
157 | event.SetInt(GetValue()); | |
158 | Command(event); | |
da0634c1 DE |
159 | } |
160 |