1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/choice.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
19 #include "wx/choice.h"
20 #include "wx/arrstr.h"
23 #include "wx/cocoa/string.h"
24 #include "wx/cocoa/autorelease.h"
26 #import <AppKit/NSPopUpButton.h>
27 #import <AppKit/NSMenu.h>
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSDictionary.h>
31 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
32 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
34 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
38 m_sortedStrings = NULL;
41 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
44 const wxArrayString& choices,
46 const wxValidator& validator,
49 wxCArrayString chs(choices);
51 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
52 style, validator, name);
55 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
58 int n, const wxString choices[],
60 const wxValidator& validator,
63 wxAutoNSAutoreleasePool pool;
64 if(!CreateControl(parent,winid,pos,size,style,validator,name))
67 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
69 [m_cocoaNSView release];
71 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
72 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
76 m_sortedStrings = new wxSortedArrayString;
77 for(int i=0; i<n; i++)
79 m_sortedStrings->Add(choices[i]);
81 for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++)
83 [nsmenu addItemWithTitle:wxNSStringWithWxString(
84 m_sortedStrings->Item(i))
85 action: nil keyEquivalent:@""];
90 for(int i=0; i<n; i++)
92 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
93 action: nil keyEquivalent:@""];
96 m_itemsClientData.SetCount(n);
98 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
100 m_parent->CocoaAddChild(this);
101 SetInitialFrameRect(pos,size);
106 wxChoice::~wxChoice()
108 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
111 m_sortedStrings->Clear();
112 delete m_sortedStrings;
114 if(HasClientObjectData())
116 for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++)
117 delete (wxClientData*)m_itemsClientData.Item(i);
119 m_itemsClientData.Clear();
122 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
124 NSDictionary *userInfo = [notification userInfo];
125 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
126 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
127 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
128 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
129 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
131 event.SetEventObject(this);
132 event.SetString(GetStringSelection());
133 GetEventHandler()->ProcessEvent(event);
136 void wxChoice::Clear()
139 m_sortedStrings->Clear();
140 if(HasClientObjectData())
142 for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++)
143 delete (wxClientData*)m_itemsClientData.Item(i);
145 m_itemsClientData.Clear();
146 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
149 void wxChoice::Delete(unsigned int n)
152 m_sortedStrings->RemoveAt(n);
153 if(HasClientObjectData())
154 delete (wxClientData*)m_itemsClientData.Item(n);
155 m_itemsClientData.RemoveAt(n);
156 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
159 unsigned int wxChoice::GetCount() const
161 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
164 wxString wxChoice::GetString(unsigned int n) const
166 wxAutoNSAutoreleasePool pool;
167 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
170 void wxChoice::SetString(unsigned int n, const wxString& title)
172 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
173 [item setTitle:wxNSStringWithWxString(title)];
176 int wxChoice::FindString(const wxString& title, bool bCase) const
178 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
179 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
180 wxNSStringWithWxString(title)];
183 int wxChoice::GetSelection() const
185 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
188 int wxChoice::DoAppend(const wxString& title)
190 wxAutoNSAutoreleasePool pool;
191 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
195 int sortedIndex = m_sortedStrings->Add(title);
196 item = [nsmenu insertItemWithTitle:
197 wxNSStringWithWxString(title)
198 action: nil keyEquivalent:@"" atIndex:sortedIndex];
199 m_itemsClientData.Insert(NULL, sortedIndex);
203 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
204 action: nil keyEquivalent:@""];
205 m_itemsClientData.Add(NULL);
207 return [nsmenu indexOfItem:item];
210 int wxChoice::DoInsert(const wxString& title, unsigned int pos)
213 return DoAppend(title);
214 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
215 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
216 action: nil keyEquivalent:@"" atIndex:pos];
217 m_itemsClientData.Insert(NULL, pos);
218 return [nsmenu indexOfItem:item];
221 void wxChoice::DoSetItemClientData(unsigned int n, void *data)
223 m_itemsClientData.Item(n) = data;
226 void* wxChoice::DoGetItemClientData(unsigned int n) const
228 return m_itemsClientData.Item(n);
231 void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData *data)
233 m_itemsClientData.Item(n) = data;
236 wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
238 return (wxClientData*)m_itemsClientData.Item(n);
241 void wxChoice::SetSelection(int n)
243 wxAutoNSAutoreleasePool pool;
244 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];