1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/choice.mm
4 // Author: David Elliott
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #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 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
33 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
37 m_sortedStrings = NULL;
40 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
43 const wxArrayString& choices,
45 const wxValidator& validator,
48 wxCArrayString chs(choices);
50 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
51 style, validator, name);
54 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
57 int n, const wxString choices[],
59 const wxValidator& validator,
62 wxAutoNSAutoreleasePool pool;
63 if(!CreateControl(parent,winid,pos,size,style,validator,name))
66 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
68 [m_cocoaNSView release];
70 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
71 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
75 m_sortedStrings = new wxSortedArrayString;
76 for(int i=0; i<n; i++)
78 m_sortedStrings->Add(choices[i]);
80 for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++)
82 [nsmenu addItemWithTitle:wxNSStringWithWxString(
83 m_sortedStrings->Item(i))
84 action: nil keyEquivalent:@""];
89 for(int i=0; i<n; i++)
91 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
92 action: nil keyEquivalent:@""];
95 m_itemsClientData.SetCount(n);
97 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
99 m_parent->CocoaAddChild(this);
100 SetInitialFrameRect(pos,size);
105 wxChoice::~wxChoice()
107 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
112 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
114 NSDictionary *userInfo = [notification userInfo];
115 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
116 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
117 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
118 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
119 wxCommandEvent event(wxEVT_CHOICE, m_windowId);
121 event.SetEventObject(this);
122 event.SetString(GetStringSelection());
123 HandleWindowEvent(event);
126 void wxChoice::DoClear()
129 m_sortedStrings->Clear();
130 m_itemsClientData.Clear();
131 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
134 void wxChoice::DoDeleteOneItem(unsigned int n)
137 m_sortedStrings->RemoveAt(n);
138 m_itemsClientData.RemoveAt(n);
139 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
142 unsigned int wxChoice::GetCount() const
144 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
147 wxString wxChoice::GetString(unsigned int n) const
149 wxAutoNSAutoreleasePool pool;
150 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
153 void wxChoice::SetString(unsigned int n, const wxString& title)
155 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
156 [item setTitle:wxNSStringWithWxString(title)];
159 int wxChoice::FindString(const wxString& title, bool bCase) const
161 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
162 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
163 wxNSStringWithWxString(title)];
166 int wxChoice::GetSelection() const
168 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
171 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
173 void **clientData, wxClientDataType type)
175 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
176 NSMenuItem *item = NULL;
178 unsigned int numItems = items.GetCount();
179 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
181 const wxString& str = items[i];
182 int idx = m_sortedStrings ? m_sortedStrings->Add(str) : pos;
184 item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(str)
185 action: nil keyEquivalent:@"" atIndex:idx];
186 m_itemsClientData.Insert(NULL, idx);
187 AssignNewItemClientData(idx, clientData, i, type);
189 return [nsmenu indexOfItem:item];
192 void wxChoice::DoSetItemClientData(unsigned int n, void *data)
194 m_itemsClientData[n] = data;
197 void* wxChoice::DoGetItemClientData(unsigned int n) const
199 return m_itemsClientData[n];
202 void wxChoice::SetSelection(int n)
204 wxAutoNSAutoreleasePool pool;
205 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
208 #endif // wxUSE_CHOICE