1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
17 #include "wx/arrstr.h"
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/autorelease.h"
23 #import <AppKit/NSPopUpButton.h>
24 #import <AppKit/NSMenu.h>
25 #import <Foundation/NSNotification.h>
26 #import <Foundation/NSDictionary.h>
28 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
29 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
31 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
35 m_sortedStrings = NULL;
38 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
41 const wxArrayString& choices,
43 const wxValidator& validator,
46 wxCArrayString chs(choices);
48 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
49 style, validator, name);
52 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
55 int n, const wxString choices[],
57 const wxValidator& validator,
60 wxAutoNSAutoreleasePool pool;
61 if(!CreateControl(parent,winid,pos,size,style,validator,name))
64 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
66 [m_cocoaNSView release];
68 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
69 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
73 m_sortedStrings = new wxSortedArrayString;
74 for(int i=0; i<n; i++)
76 m_sortedStrings->Add(choices[i]);
78 for(size_t i=0; i < m_sortedStrings->GetCount(); i++)
80 [nsmenu addItemWithTitle:wxNSStringWithWxString(
81 m_sortedStrings->Item(i))
82 action: nil keyEquivalent:@""];
87 for(int i=0; i<n; i++)
89 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
90 action: nil keyEquivalent:@""];
93 m_itemsClientData.SetCount(n);
95 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
97 m_parent->CocoaAddChild(this);
98 SetInitialFrameRect(pos,size);
103 wxChoice::~wxChoice()
105 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
108 m_sortedStrings->Clear();
109 delete m_sortedStrings;
111 if(HasClientObjectData())
113 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
114 delete (wxClientData*)m_itemsClientData.Item(i);
116 m_itemsClientData.Clear();
119 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
121 NSDictionary *userInfo = [notification userInfo];
122 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
123 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
124 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
125 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
126 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
128 event.SetEventObject(this);
129 event.SetString(GetStringSelection());
130 GetEventHandler()->ProcessEvent(event);
133 void wxChoice::Clear()
136 m_sortedStrings->Clear();
137 if(HasClientObjectData())
139 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
140 delete (wxClientData*)m_itemsClientData.Item(i);
142 m_itemsClientData.Clear();
143 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
146 void wxChoice::Delete(int n)
149 m_sortedStrings->RemoveAt(n);
150 if(HasClientObjectData())
151 delete (wxClientData*)m_itemsClientData.Item(n);
152 m_itemsClientData.RemoveAt(n);
153 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
156 int wxChoice::GetCount() const
158 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
161 wxString wxChoice::GetString(int n) const
163 wxAutoNSAutoreleasePool pool;
164 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
167 void wxChoice::SetString(int n, const wxString& title)
169 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
170 [item setTitle:wxNSStringWithWxString(title)];
173 int wxChoice::FindString(const wxString& title) const
175 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
176 wxNSStringWithWxString(title)];
179 int wxChoice::GetSelection() const
181 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
184 int wxChoice::DoAppend(const wxString& title)
186 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
190 int sortedIndex = m_sortedStrings->Add(title);
191 item = [nsmenu insertItemWithTitle:
192 wxNSStringWithWxString(title)
193 action: nil keyEquivalent:@"" atIndex:sortedIndex];
194 m_itemsClientData.Insert(NULL, sortedIndex);
198 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
199 action: nil keyEquivalent:@""];
200 m_itemsClientData.Add(NULL);
202 return [nsmenu indexOfItem:item];
205 int wxChoice::DoInsert(const wxString& title, int pos)
208 return DoAppend(title);
209 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
210 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
211 action: nil keyEquivalent:@"" atIndex:pos];
212 m_itemsClientData.Insert(NULL, pos);
213 return [nsmenu indexOfItem:item];
216 void wxChoice::DoSetItemClientData(int n, void *data)
218 m_itemsClientData.Item(n) = data;
221 void* wxChoice::DoGetItemClientData(int n) const
223 return m_itemsClientData.Item(n);
226 void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
228 m_itemsClientData.Item(n) = data;
231 wxClientData* wxChoice::DoGetItemClientObject(int n) const
233 return (wxClientData*)m_itemsClientData.Item(n);
236 void wxChoice::SetSelection(int n)
238 wxAutoNSAutoreleasePool pool;
239 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];