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"
16 #include "wx/choice.h"
21 #include "wx/arrstr.h"
24 #include "wx/cocoa/string.h"
25 #include "wx/cocoa/autorelease.h"
27 #import <AppKit/NSPopUpButton.h>
28 #import <AppKit/NSMenu.h>
29 #import <Foundation/NSNotification.h>
30 #import <Foundation/NSDictionary.h>
32 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
33 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
35 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
39 m_sortedStrings = NULL;
42 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
45 const wxArrayString& choices,
47 const wxValidator& validator,
50 wxCArrayString chs(choices);
52 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
53 style, validator, name);
56 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
59 int n, const wxString choices[],
61 const wxValidator& validator,
64 wxAutoNSAutoreleasePool pool;
65 if(!CreateControl(parent,winid,pos,size,style,validator,name))
68 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
70 [m_cocoaNSView release];
72 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
73 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
77 m_sortedStrings = new wxSortedArrayString;
78 for(int i=0; i<n; i++)
80 m_sortedStrings->Add(choices[i]);
82 for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++)
84 [nsmenu addItemWithTitle:wxNSStringWithWxString(
85 m_sortedStrings->Item(i))
86 action: nil keyEquivalent:@""];
91 for(int i=0; i<n; i++)
93 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
94 action: nil keyEquivalent:@""];
97 m_itemsClientData.SetCount(n);
99 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
101 m_parent->CocoaAddChild(this);
102 SetInitialFrameRect(pos,size);
107 wxChoice::~wxChoice()
109 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
112 m_sortedStrings->Clear();
113 delete m_sortedStrings;
115 if(HasClientObjectData())
117 for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++)
118 delete (wxClientData*)m_itemsClientData.Item(i);
120 m_itemsClientData.Clear();
123 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
125 NSDictionary *userInfo = [notification userInfo];
126 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
127 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
128 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
129 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
130 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
132 event.SetEventObject(this);
133 event.SetString(GetStringSelection());
134 GetEventHandler()->ProcessEvent(event);
137 void wxChoice::Clear()
140 m_sortedStrings->Clear();
141 if(HasClientObjectData())
143 for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++)
144 delete (wxClientData*)m_itemsClientData.Item(i);
146 m_itemsClientData.Clear();
147 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
150 void wxChoice::Delete(unsigned int n)
153 m_sortedStrings->RemoveAt(n);
154 if(HasClientObjectData())
155 delete (wxClientData*)m_itemsClientData.Item(n);
156 m_itemsClientData.RemoveAt(n);
157 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
160 unsigned int wxChoice::GetCount() const
162 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
165 wxString wxChoice::GetString(unsigned int n) const
167 wxAutoNSAutoreleasePool pool;
168 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
171 void wxChoice::SetString(unsigned int n, const wxString& title)
173 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
174 [item setTitle:wxNSStringWithWxString(title)];
177 int wxChoice::FindString(const wxString& title, bool bCase) const
179 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
180 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
181 wxNSStringWithWxString(title)];
184 int wxChoice::GetSelection() const
186 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
189 int wxChoice::DoAppend(const wxString& title)
191 wxAutoNSAutoreleasePool pool;
192 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
196 int sortedIndex = m_sortedStrings->Add(title);
197 item = [nsmenu insertItemWithTitle:
198 wxNSStringWithWxString(title)
199 action: nil keyEquivalent:@"" atIndex:sortedIndex];
200 m_itemsClientData.Insert(NULL, sortedIndex);
204 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
205 action: nil keyEquivalent:@""];
206 m_itemsClientData.Add(NULL);
208 return [nsmenu indexOfItem:item];
211 int wxChoice::DoInsert(const wxString& title, unsigned int pos)
214 return DoAppend(title);
215 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
216 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
217 action: nil keyEquivalent:@"" atIndex:pos];
218 m_itemsClientData.Insert(NULL, pos);
219 return [nsmenu indexOfItem:item];
222 void wxChoice::DoSetItemClientData(unsigned int n, void *data)
224 m_itemsClientData.Item(n) = data;
227 void* wxChoice::DoGetItemClientData(unsigned int n) const
229 return m_itemsClientData.Item(n);
232 void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData *data)
234 m_itemsClientData.Item(n) = data;
237 wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
239 return (wxClientData*)m_itemsClientData.Item(n);
242 void wxChoice::SetSelection(int n)
244 wxAutoNSAutoreleasePool pool;
245 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];