]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/choice.mm
Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / cocoa / choice.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/choice.mm
3 // Purpose: wxChoice
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/wxprec.h"
12
13 #if wxUSE_CHOICE
14
15 #include "wx/choice.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/app.h"
20 #include "wx/arrstr.h"
21 #endif //WX_PRECOMP
22
23 #include "wx/cocoa/string.h"
24 #include "wx/cocoa/autorelease.h"
25
26 #import <AppKit/NSPopUpButton.h>
27 #import <AppKit/NSMenu.h>
28 #import <Foundation/NSNotification.h>
29 #import <Foundation/NSDictionary.h>
30
31 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
32 END_EVENT_TABLE()
33 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
34
35 void wxChoice::Init()
36 {
37 m_sortedStrings = NULL;
38 }
39
40 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
41 const wxPoint& pos,
42 const wxSize& size,
43 const wxArrayString& choices,
44 long style,
45 const wxValidator& validator,
46 const wxString& name)
47 {
48 wxCArrayString chs(choices);
49
50 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
51 style, validator, name);
52 }
53
54 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
55 const wxPoint& pos,
56 const wxSize& size,
57 int n, const wxString choices[],
58 long style,
59 const wxValidator& validator,
60 const wxString& name)
61 {
62 wxAutoNSAutoreleasePool pool;
63 if(!CreateControl(parent,winid,pos,size,style,validator,name))
64 return false;
65
66 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
67 pullsDown: NO]);
68 [m_cocoaNSView release];
69
70 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
71 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
72
73 if(style&wxCB_SORT)
74 {
75 m_sortedStrings = new wxSortedArrayString;
76 for(int i=0; i<n; i++)
77 {
78 m_sortedStrings->Add(choices[i]);
79 }
80 for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++)
81 {
82 [nsmenu addItemWithTitle:wxNSStringWithWxString(
83 m_sortedStrings->Item(i))
84 action: nil keyEquivalent:@""];
85 }
86 }
87 else
88 {
89 for(int i=0; i<n; i++)
90 {
91 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
92 action: nil keyEquivalent:@""];
93 }
94 }
95 m_itemsClientData.SetCount(n);
96
97 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
98 if(m_parent)
99 m_parent->CocoaAddChild(this);
100 SetInitialFrameRect(pos,size);
101
102 return true;
103 }
104
105 wxChoice::~wxChoice()
106 {
107 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
108
109 Clear();
110 }
111
112 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
113 {
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);
120 event.SetInt(index);
121 event.SetEventObject(this);
122 event.SetString(GetStringSelection());
123 HandleWindowEvent(event);
124 }
125
126 void wxChoice::DoClear()
127 {
128 if(m_sortedStrings)
129 m_sortedStrings->Clear();
130 m_itemsClientData.Clear();
131 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
132 }
133
134 void wxChoice::DoDeleteOneItem(unsigned int n)
135 {
136 if(m_sortedStrings)
137 m_sortedStrings->RemoveAt(n);
138 m_itemsClientData.RemoveAt(n);
139 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
140 }
141
142 unsigned int wxChoice::GetCount() const
143 {
144 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
145 }
146
147 wxString wxChoice::GetString(unsigned int n) const
148 {
149 wxAutoNSAutoreleasePool pool;
150 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
151 }
152
153 void wxChoice::SetString(unsigned int n, const wxString& title)
154 {
155 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
156 [item setTitle:wxNSStringWithWxString(title)];
157 }
158
159 int wxChoice::FindString(const wxString& title, bool bCase) const
160 {
161 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
162 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
163 wxNSStringWithWxString(title)];
164 }
165
166 int wxChoice::GetSelection() const
167 {
168 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
169 }
170
171 int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
172 unsigned int pos,
173 void **clientData, wxClientDataType type)
174 {
175 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
176 NSMenuItem *item = NULL;
177
178 unsigned int numItems = items.GetCount();
179 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
180 {
181 const wxString& str = items[i];
182 int idx = m_sortedStrings ? m_sortedStrings->Add(str) : pos;
183
184 item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(str)
185 action: nil keyEquivalent:@"" atIndex:idx];
186 m_itemsClientData.Insert(NULL, idx);
187 AssignNewItemClientData(idx, clientData, i, type);
188 }
189 return [nsmenu indexOfItem:item];
190 }
191
192 void wxChoice::DoSetItemClientData(unsigned int n, void *data)
193 {
194 m_itemsClientData[n] = data;
195 }
196
197 void* wxChoice::DoGetItemClientData(unsigned int n) const
198 {
199 return m_itemsClientData[n];
200 }
201
202 void wxChoice::SetSelection(int n)
203 {
204 wxAutoNSAutoreleasePool pool;
205 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
206 }
207
208 #endif // wxUSE_CHOICE