]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/choice.mm
compilation fix after last patch
[wxWidgets.git] / src / cocoa / choice.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/choice.mm
3 // Purpose: wxChoice
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // RCS-ID: $Id:
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/choice.h"
17 #include "wx/arrstr.h"
18 #endif //WX_PRECOMP
19
20 #include "wx/cocoa/string.h"
21 #include "wx/cocoa/autorelease.h"
22
23 #import <AppKit/NSPopUpButton.h>
24 #import <AppKit/NSMenu.h>
25 #import <Foundation/NSNotification.h>
26 #import <Foundation/NSDictionary.h>
27
28 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
29 BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
30 END_EVENT_TABLE()
31 // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
32
33 void wxChoice::Init()
34 {
35 m_sortedStrings = NULL;
36 }
37
38 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
39 const wxPoint& pos,
40 const wxSize& size,
41 const wxArrayString& choices,
42 long style,
43 const wxValidator& validator,
44 const wxString& name)
45 {
46 wxCArrayString chs(choices);
47
48 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
49 style, validator, name);
50 }
51
52 bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
53 const wxPoint& pos,
54 const wxSize& size,
55 int n, const wxString choices[],
56 long style,
57 const wxValidator& validator,
58 const wxString& name)
59 {
60 wxAutoNSAutoreleasePool pool;
61 if(!CreateControl(parent,winid,pos,size,style,validator,name))
62 return false;
63
64 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
65 pullsDown: NO]);
66 [m_cocoaNSView release];
67
68 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
69 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
70
71 if(style&wxCB_SORT)
72 {
73 m_sortedStrings = new wxSortedArrayString;
74 for(int i=0; i<n; i++)
75 {
76 m_sortedStrings->Add(choices[i]);
77 }
78 for(size_t i=0; i < m_sortedStrings->GetCount(); i++)
79 {
80 [nsmenu addItemWithTitle:wxNSStringWithWxString(
81 m_sortedStrings->Item(i))
82 action: nil keyEquivalent:@""];
83 }
84 }
85 else
86 {
87 for(int i=0; i<n; i++)
88 {
89 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
90 action: nil keyEquivalent:@""];
91 }
92 }
93 m_itemsClientData.SetCount(n);
94
95 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
96 if(m_parent)
97 m_parent->CocoaAddChild(this);
98 SetInitialFrameRect(pos,size);
99
100 return true;
101 }
102
103 wxChoice::~wxChoice()
104 {
105 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
106
107 if(m_sortedStrings)
108 m_sortedStrings->Clear();
109 delete m_sortedStrings;
110
111 if(HasClientObjectData())
112 {
113 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
114 delete (wxClientData*)m_itemsClientData.Item(i);
115 }
116 m_itemsClientData.Clear();
117 }
118
119 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
120 {
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);
127 event.SetInt(index);
128 event.SetEventObject(this);
129 event.SetString(GetStringSelection());
130 GetEventHandler()->ProcessEvent(event);
131 }
132
133 void wxChoice::Clear()
134 {
135 if(m_sortedStrings)
136 m_sortedStrings->Clear();
137 if(HasClientObjectData())
138 {
139 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
140 delete (wxClientData*)m_itemsClientData.Item(i);
141 }
142 m_itemsClientData.Clear();
143 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
144 }
145
146 void wxChoice::Delete(int n)
147 {
148 if(m_sortedStrings)
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];
154 }
155
156 int wxChoice::GetCount() const
157 {
158 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
159 }
160
161 wxString wxChoice::GetString(int n) const
162 {
163 wxAutoNSAutoreleasePool pool;
164 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
165 }
166
167 void wxChoice::SetString(int n, const wxString& title)
168 {
169 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
170 [item setTitle:wxNSStringWithWxString(title)];
171 }
172
173 int wxChoice::FindString(const wxString& title) const
174 {
175 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
176 wxNSStringWithWxString(title)];
177 }
178
179 int wxChoice::GetSelection() const
180 {
181 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
182 }
183
184 int wxChoice::DoAppend(const wxString& title)
185 {
186 wxAutoNSAutoreleasePool pool;
187 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
188 NSMenuItem *item;
189 if(m_sortedStrings)
190 {
191 int sortedIndex = m_sortedStrings->Add(title);
192 item = [nsmenu insertItemWithTitle:
193 wxNSStringWithWxString(title)
194 action: nil keyEquivalent:@"" atIndex:sortedIndex];
195 m_itemsClientData.Insert(NULL, sortedIndex);
196 }
197 else
198 {
199 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
200 action: nil keyEquivalent:@""];
201 m_itemsClientData.Add(NULL);
202 }
203 return [nsmenu indexOfItem:item];
204 }
205
206 int wxChoice::DoInsert(const wxString& title, int pos)
207 {
208 if(m_sortedStrings)
209 return DoAppend(title);
210 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
211 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
212 action: nil keyEquivalent:@"" atIndex:pos];
213 m_itemsClientData.Insert(NULL, pos);
214 return [nsmenu indexOfItem:item];
215 }
216
217 void wxChoice::DoSetItemClientData(int n, void *data)
218 {
219 m_itemsClientData.Item(n) = data;
220 }
221
222 void* wxChoice::DoGetItemClientData(int n) const
223 {
224 return m_itemsClientData.Item(n);
225 }
226
227 void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
228 {
229 m_itemsClientData.Item(n) = data;
230 }
231
232 wxClientData* wxChoice::DoGetItemClientObject(int n) const
233 {
234 return (wxClientData*)m_itemsClientData.Item(n);
235 }
236
237 void wxChoice::SetSelection(int n)
238 {
239 wxAutoNSAutoreleasePool pool;
240 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
241 }
242