1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/choice.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/choice.h"
17 #include "wx/arrstr.h"
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/autorelease.h"
23 #import <AppKit/NSPopUpButton.h>
24 #import <AppKit/NSMenu.h>
25 #import <Foundation/NSNotification.h>
26 #import <Foundation/NSDictionary.h>
28 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
29 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
31 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
35 m_sortedStrings = NULL;
38 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
41 const wxArrayString& choices,
43 const wxValidator& validator,
46 wxCArrayString chs(choices);
48 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
49 style, validator, name);
52 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
55 int n, const wxString choices[],
57 const wxValidator& validator,
60 wxAutoNSAutoreleasePool pool;
61 if(!CreateControl(parent,winid,pos,size,style,validator,name))
64 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
66 [m_cocoaNSView release];
68 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
69 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
73 m_sortedStrings = new wxSortedArrayString;
74 for(int i=0; i<n; i++)
76 m_sortedStrings->Add(choices[i]);
78 for(size_t i=0; i < m_sortedStrings->GetCount(); i++)
80 [nsmenu addItemWithTitle:wxNSStringWithWxString(
81 m_sortedStrings->Item(i))
82 action: nil keyEquivalent:@""];
87 for(int i=0; i<n; i++)
89 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
90 action: nil keyEquivalent:@""];
93 m_itemsClientData.SetCount(n);
95 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
97 m_parent->CocoaAddChild(this);
98 SetInitialFrameRect(pos,size);
103 wxChoice::~wxChoice()
105 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
108 m_sortedStrings->Clear();
109 delete m_sortedStrings;
111 if(HasClientObjectData())
113 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
114 delete (wxClientData*)m_itemsClientData.Item(i);
116 m_itemsClientData.Clear();
119 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
121 NSDictionary *userInfo = [notification userInfo];
122 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
123 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
124 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
125 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
126 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
128 event.SetEventObject(this);
129 event.SetString(GetStringSelection());
130 GetEventHandler()->ProcessEvent(event);
133 void wxChoice::Clear()
136 m_sortedStrings->Clear();
137 if(HasClientObjectData())
139 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
140 delete (wxClientData*)m_itemsClientData.Item(i);
142 m_itemsClientData.Clear();
143 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
146 void wxChoice::Delete(int n)
149 m_sortedStrings->RemoveAt(n);
150 if(HasClientObjectData())
151 delete (wxClientData*)m_itemsClientData.Item(n);
152 m_itemsClientData.RemoveAt(n);
153 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
156 int wxChoice::GetCount() const
158 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
161 wxString wxChoice::GetString(int n) const
163 wxAutoNSAutoreleasePool pool;
164 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
167 void wxChoice::SetString(int n, const wxString& title)
169 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
170 [item setTitle:wxNSStringWithWxString(title)];
173 int wxChoice::FindString(const wxString& title) const
175 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
176 wxNSStringWithWxString(title)];
179 int wxChoice::GetSelection() const
181 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
184 int wxChoice::DoAppend(const wxString& title)
186 wxAutoNSAutoreleasePool pool;
187 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
191 int sortedIndex = m_sortedStrings->Add(title);
192 item = [nsmenu insertItemWithTitle:
193 wxNSStringWithWxString(title)
194 action: nil keyEquivalent:@"" atIndex:sortedIndex];
195 m_itemsClientData.Insert(NULL, sortedIndex);
199 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
200 action: nil keyEquivalent:@""];
201 m_itemsClientData.Add(NULL);
203 return [nsmenu indexOfItem:item];
206 int wxChoice::DoInsert(const wxString& title, int pos)
209 return DoAppend(title);
210 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
211 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
212 action: nil keyEquivalent:@"" atIndex:pos];
213 m_itemsClientData.Insert(NULL, pos);
214 return [nsmenu indexOfItem:item];
217 void wxChoice::DoSetItemClientData(int n, void *data)
219 m_itemsClientData.Item(n) = data;
222 void* wxChoice::DoGetItemClientData(int n) const
224 return m_itemsClientData.Item(n);
227 void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
229 m_itemsClientData.Item(n) = data;
232 wxClientData* wxChoice::DoGetItemClientObject(int n) const
234 return (wxClientData*)m_itemsClientData.Item(n);
237 void wxChoice::SetSelection(int n)
239 wxAutoNSAutoreleasePool pool;
240 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];