]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/choice.mm
updated version to match latest wxPython release
[wxWidgets.git] / src / cocoa / choice.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/choice.mm
3 // Purpose: wxChoice
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // RCS-ID: $Id:
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/choice.h"
17 #include "wx/arrstr.h"
18 #endif //WX_PRECOMP
19
20 #include "wx/cocoa/string.h"
21
22 #import <AppKit/NSPopUpButton.h>
23 #import <AppKit/NSMenu.h>
24 #import <Foundation/NSNotification.h>
25 #import <Foundation/NSDictionary.h>
26
27 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
28 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
29 END_EVENT_TABLE()
30 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
31
32 void wxChoice::Init()
33 {
34 m_sortedStrings = NULL;
35 }
36
37 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
38 const wxPoint& pos,
39 const wxSize& size,
40 const wxArrayString& choices,
41 long style,
42 const wxValidator& validator,
43 const wxString& name)
44 {
45 wxCArrayString chs(choices);
46
47 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
48 style, validator, name);
49 }
50
51 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
52 const wxPoint& pos,
53 const wxSize& size,
54 int n, const wxString choices[],
55 long style,
56 const wxValidator& validator,
57 const wxString& name)
58 {
59 if(!CreateControl(parent,winid,pos,size,style,validator,name))
60 return false;
61
62 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
63 pullsDown: NO]);
64 [m_cocoaNSView release];
65
66 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
67 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
68
69 if(style&wxCB_SORT)
70 {
71 m_sortedStrings = new wxSortedArrayString;
72 for(int i=0; i<n; i++)
73 {
74 m_sortedStrings->Add(choices[i]);
75 }
76 for(size_t i=0; i < m_sortedStrings->GetCount(); i++)
77 {
78 [nsmenu addItemWithTitle:wxNSStringWithWxString(
79 m_sortedStrings->Item(i))
80 action: nil keyEquivalent:@""];
81 }
82 }
83 else
84 {
85 for(int i=0; i<n; i++)
86 {
87 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
88 action: nil keyEquivalent:@""];
89 }
90 }
91 m_itemsClientData.SetCount(n);
92
93 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
94 if(m_parent)
95 m_parent->CocoaAddChild(this);
96 SetInitialFrameRect(pos,size);
97
98 return true;
99 }
100
101 wxChoice::~wxChoice()
102 {
103 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
104
105 if(m_sortedStrings)
106 m_sortedStrings->Clear();
107 delete m_sortedStrings;
108
109 if(HasClientObjectData())
110 {
111 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
112 delete (wxClientData*)m_itemsClientData.Item(i);
113 }
114 m_itemsClientData.Clear();
115 }
116
117 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
118 {
119 NSDictionary *userInfo = [notification userInfo];
120 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
121 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
122 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
123 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
124 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
125 event.SetInt(index);
126 event.SetEventObject(this);
127 event.SetString(GetStringSelection());
128 GetEventHandler()->ProcessEvent(event);
129 }
130
131 void wxChoice::Clear()
132 {
133 if(m_sortedStrings)
134 m_sortedStrings->Clear();
135 if(HasClientObjectData())
136 {
137 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
138 delete (wxClientData*)m_itemsClientData.Item(i);
139 }
140 m_itemsClientData.Clear();
141 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
142 }
143
144 void wxChoice::Delete(int n)
145 {
146 if(m_sortedStrings)
147 m_sortedStrings->RemoveAt(n);
148 if(HasClientObjectData())
149 delete (wxClientData*)m_itemsClientData.Item(n);
150 m_itemsClientData.RemoveAt(n);
151 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
152 }
153
154 int wxChoice::GetCount() const
155 {
156 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
157 }
158
159 wxString wxChoice::GetString(int n) const
160 {
161 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
162 }
163
164 void wxChoice::SetString(int n, const wxString& title)
165 {
166 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
167 [item setTitle:wxNSStringWithWxString(title)];
168 }
169
170 int wxChoice::FindString(const wxString& title) const
171 {
172 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
173 wxNSStringWithWxString(title)];
174 }
175
176 int wxChoice::GetSelection() const
177 {
178 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
179 }
180
181 int wxChoice::DoAppend(const wxString& title)
182 {
183 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
184 NSMenuItem *item;
185 if(m_sortedStrings)
186 {
187 int sortedIndex = m_sortedStrings->Add(title);
188 item = [nsmenu insertItemWithTitle:
189 wxNSStringWithWxString(title)
190 action: nil keyEquivalent:@"" atIndex:sortedIndex];
191 m_itemsClientData.Insert(NULL, sortedIndex);
192 }
193 else
194 {
195 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
196 action: nil keyEquivalent:@""];
197 m_itemsClientData.Add(NULL);
198 }
199 return [nsmenu indexOfItem:item];
200 }
201
202 int wxChoice::DoInsert(const wxString& title, int pos)
203 {
204 if(m_sortedStrings)
205 return DoAppend(title);
206 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
207 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
208 action: nil keyEquivalent:@"" atIndex:pos];
209 m_itemsClientData.Insert(NULL, pos);
210 return [nsmenu indexOfItem:item];
211 }
212
213 void wxChoice::DoSetItemClientData(int n, void *data)
214 {
215 m_itemsClientData.Item(n) = data;
216 }
217
218 void* wxChoice::DoGetItemClientData(int n) const
219 {
220 return m_itemsClientData.Item(n);
221 }
222
223 void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
224 {
225 m_itemsClientData.Item(n) = data;
226 }
227
228 wxClientData* wxChoice::DoGetItemClientObject(int n) const
229 {
230 return (wxClientData*)m_itemsClientData.Item(n);
231 }
232
233 void wxChoice::SetSelection(int n)
234 {
235 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
236 }
237