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