1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/choice.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows 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, wxControlWithItems)
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]);
114 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
116 NSDictionary *userInfo = [notification userInfo];
117 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
118 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
119 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
120 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
121 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
123 event.SetEventObject(this);
124 event.SetString(GetStringSelection());
125 HandleWindowEvent(event);
128 void wxChoice::DoClear()
131 m_sortedStrings->Clear();
132 m_itemsClientData.Clear();
133 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
136 void wxChoice::DoDeleteOneItem(unsigned int n)
139 m_sortedStrings->RemoveAt(n);
140 m_itemsClientData.RemoveAt(n);
141 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
144 unsigned int wxChoice::GetCount() const
146 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
149 wxString wxChoice::GetString(unsigned int n) const
151 wxAutoNSAutoreleasePool pool;
152 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
155 void wxChoice::SetString(unsigned int n, const wxString& title)
157 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
158 [item setTitle:wxNSStringWithWxString(title)];
161 int wxChoice::FindString(const wxString& title, bool bCase) const
163 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
164 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
165 wxNSStringWithWxString(title)];
168 int wxChoice::GetSelection() const
170 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
173 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
175 void **clientData, wxClientDataType type)
177 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
178 NSMenuItem *item = NULL;
180 unsigned int numItems = items.GetCount();
181 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
183 const wxString& str = items[i];
184 int idx = m_sortedStrings ? m_sortedStrings->Add(str) : pos;
186 item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(str)
187 action: nil keyEquivalent:@"" atIndex:idx];
188 m_itemsClientData.Insert(NULL, idx);
189 AssignNewItemClientData(idx, clientData, i, type);
191 return [nsmenu indexOfItem:item];
194 void wxChoice::DoSetItemClientData(unsigned int n, void *data)
196 m_itemsClientData[n] = data;
199 void* wxChoice::DoGetItemClientData(unsigned int n) const
201 return m_itemsClientData[n];
204 void wxChoice::SetSelection(int n)
206 wxAutoNSAutoreleasePool pool;
207 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
210 #endif // wxUSE_CHOICE