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 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]);
113 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
115 NSDictionary *userInfo = [notification userInfo];
116 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
117 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
118 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
119 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
120 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
122 event.SetEventObject(this);
123 event.SetString(GetStringSelection());
124 HandleWindowEvent(event);
127 void wxChoice::DoClear()
130 m_sortedStrings->Clear();
131 m_itemsClientData.Clear();
132 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
135 void wxChoice::DoDeleteOneItem(unsigned int n)
138 m_sortedStrings->RemoveAt(n);
139 m_itemsClientData.RemoveAt(n);
140 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
143 unsigned int wxChoice::GetCount() const
145 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
148 wxString wxChoice::GetString(unsigned int n) const
150 wxAutoNSAutoreleasePool pool;
151 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
154 void wxChoice::SetString(unsigned int n, const wxString& title)
156 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
157 [item setTitle:wxNSStringWithWxString(title)];
160 int wxChoice::FindString(const wxString& title, bool bCase) const
162 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
163 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
164 wxNSStringWithWxString(title)];
167 int wxChoice::GetSelection() const
169 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
172 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
174 void **clientData, wxClientDataType type)
176 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
177 NSMenuItem *item = NULL;
179 unsigned int numItems = items.GetCount();
180 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
182 const wxString& str = items[i];
183 int idx = m_sortedStrings ? m_sortedStrings->Add(str) : pos;
185 item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(str)
186 action: nil keyEquivalent:@"" atIndex:idx];
187 m_itemsClientData.Insert(NULL, idx);
188 AssignNewItemClientData(idx, clientData, i, type);
190 return [nsmenu indexOfItem:item];
193 void wxChoice::DoSetItemClientData(unsigned int n, void *data)
195 m_itemsClientData[n] = data;
198 void* wxChoice::DoGetItemClientData(unsigned int n) const
200 return m_itemsClientData[n];
203 void wxChoice::SetSelection(int n)
205 wxAutoNSAutoreleasePool pool;
206 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
209 #endif // wxUSE_CHOICE