1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/choice.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/choice.h"
19 #include "wx/cocoa/string.h"
21 #import <AppKit/NSPopUpButton.h>
22 #import <AppKit/NSMenu.h>
23 #import <Foundation/NSNotification.h>
24 #import <Foundation/NSDictionary.h>
26 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
27 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
29 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
33 m_sortedStrings = NULL;
36 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
39 int n, const wxString choices[],
41 const wxValidator& validator,
44 if(!CreateControl(parent,winid,pos,size,style,validator,name))
47 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
49 [m_cocoaNSView release];
51 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
52 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
56 m_sortedStrings = new wxSortedArrayString;
57 for(int i=0; i<n; i++)
59 m_sortedStrings->Add(choices[i]);
61 for(size_t i=0; i < m_sortedStrings->GetCount(); i++)
63 [nsmenu addItemWithTitle:wxNSStringWithWxString(
64 m_sortedStrings->Item(i))
65 action: nil keyEquivalent:@""];
70 for(int i=0; i<n; i++)
72 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
73 action: nil keyEquivalent:@""];
76 m_itemsClientData.SetCount(n);
78 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
80 m_parent->CocoaAddChild(this);
81 SetInitialFrameRect(pos,size);
88 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
91 m_sortedStrings->Clear();
92 delete m_sortedStrings;
94 if(HasClientObjectData())
96 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
97 delete (wxClientData*)m_itemsClientData.Item(i);
99 m_itemsClientData.Clear();
101 CocoaRemoveFromParent();
104 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
106 NSDictionary *userInfo = [notification userInfo];
107 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
108 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
109 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
110 wxLogDebug(wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
111 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
113 event.SetEventObject(this);
114 event.SetString(GetStringSelection());
115 GetEventHandler()->ProcessEvent(event);
118 void wxChoice::Clear()
121 m_sortedStrings->Clear();
122 if(HasClientObjectData())
124 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
125 delete (wxClientData*)m_itemsClientData.Item(i);
127 m_itemsClientData.Clear();
128 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
131 void wxChoice::Delete(int n)
134 m_sortedStrings->RemoveAt(n);
135 if(HasClientObjectData())
136 delete (wxClientData*)m_itemsClientData.Item(n);
137 m_itemsClientData.RemoveAt(n);
138 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
141 int wxChoice::GetCount() const
143 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
146 wxString wxChoice::GetString(int n) const
148 return wxString([[(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n] lossyCString]);
151 void wxChoice::SetString(int n, const wxString& title)
153 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
154 [item setTitle:wxNSStringWithWxString(title)];
157 int wxChoice::FindString(const wxString& title) const
159 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
160 wxNSStringWithWxString(title)];
163 int wxChoice::GetSelection() const
165 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
168 int wxChoice::DoAppend(const wxString& title)
170 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
174 int sortedIndex = m_sortedStrings->Add(title);
175 item = [nsmenu insertItemWithTitle:
176 wxNSStringWithWxString(title)
177 action: nil keyEquivalent:@"" atIndex:sortedIndex];
178 m_itemsClientData.Insert(NULL, sortedIndex);
182 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
183 action: nil keyEquivalent:@""];
184 m_itemsClientData.Add(NULL);
186 return [nsmenu indexOfItem:item];
189 int wxChoice::DoInsert(const wxString& title, int pos)
192 return DoAppend(title);
193 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
194 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
195 action: nil keyEquivalent:@"" atIndex:pos];
196 m_itemsClientData.Insert(NULL, pos);
197 return [nsmenu indexOfItem:item];
200 void wxChoice::DoSetItemClientData(int n, void *data)
202 m_itemsClientData.Item(n) = data;
205 void* wxChoice::DoGetItemClientData(int n) const
207 return m_itemsClientData.Item(n);
210 void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
212 m_itemsClientData.Item(n) = data;
215 wxClientData* wxChoice::DoGetItemClientObject(int n) const
217 return (wxClientData*)m_itemsClientData.Item(n);
220 void wxChoice::SetSelection(int n)
222 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];