]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/choice.mm
Implement wxMimeTypesManager on mac
[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
14 #if wxUSE_CHOICE
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/app.h"
19 #include "wx/choice.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 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
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(size_t 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 if(m_sortedStrings)
111 m_sortedStrings->Clear();
112 delete m_sortedStrings;
113
114 if(HasClientObjectData())
115 {
116 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
117 delete (wxClientData*)m_itemsClientData.Item(i);
118 }
119 m_itemsClientData.Clear();
120 }
121
122 void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
123 {
124 NSDictionary *userInfo = [notification userInfo];
125 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
126 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
127 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
128 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
129 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
130 event.SetInt(index);
131 event.SetEventObject(this);
132 event.SetString(GetStringSelection());
133 GetEventHandler()->ProcessEvent(event);
134 }
135
136 void wxChoice::Clear()
137 {
138 if(m_sortedStrings)
139 m_sortedStrings->Clear();
140 if(HasClientObjectData())
141 {
142 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
143 delete (wxClientData*)m_itemsClientData.Item(i);
144 }
145 m_itemsClientData.Clear();
146 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
147 }
148
149 void wxChoice::Delete(int n)
150 {
151 if(m_sortedStrings)
152 m_sortedStrings->RemoveAt(n);
153 if(HasClientObjectData())
154 delete (wxClientData*)m_itemsClientData.Item(n);
155 m_itemsClientData.RemoveAt(n);
156 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
157 }
158
159 int wxChoice::GetCount() const
160 {
161 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
162 }
163
164 wxString wxChoice::GetString(int n) const
165 {
166 wxAutoNSAutoreleasePool pool;
167 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
168 }
169
170 void wxChoice::SetString(int n, const wxString& title)
171 {
172 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
173 [item setTitle:wxNSStringWithWxString(title)];
174 }
175
176 int wxChoice::FindString(const wxString& title) const
177 {
178 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
179 wxNSStringWithWxString(title)];
180 }
181
182 int wxChoice::GetSelection() const
183 {
184 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
185 }
186
187 int wxChoice::DoAppend(const wxString& title)
188 {
189 wxAutoNSAutoreleasePool pool;
190 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
191 NSMenuItem *item;
192 if(m_sortedStrings)
193 {
194 int sortedIndex = m_sortedStrings->Add(title);
195 item = [nsmenu insertItemWithTitle:
196 wxNSStringWithWxString(title)
197 action: nil keyEquivalent:@"" atIndex:sortedIndex];
198 m_itemsClientData.Insert(NULL, sortedIndex);
199 }
200 else
201 {
202 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
203 action: nil keyEquivalent:@""];
204 m_itemsClientData.Add(NULL);
205 }
206 return [nsmenu indexOfItem:item];
207 }
208
209 int wxChoice::DoInsert(const wxString& title, int pos)
210 {
211 if(m_sortedStrings)
212 return DoAppend(title);
213 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
214 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
215 action: nil keyEquivalent:@"" atIndex:pos];
216 m_itemsClientData.Insert(NULL, pos);
217 return [nsmenu indexOfItem:item];
218 }
219
220 void wxChoice::DoSetItemClientData(int n, void *data)
221 {
222 m_itemsClientData.Item(n) = data;
223 }
224
225 void* wxChoice::DoGetItemClientData(int n) const
226 {
227 return m_itemsClientData.Item(n);
228 }
229
230 void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
231 {
232 m_itemsClientData.Item(n) = data;
233 }
234
235 wxClientData* wxChoice::DoGetItemClientObject(int n) const
236 {
237 return (wxClientData*)m_itemsClientData.Item(n);
238 }
239
240 void wxChoice::SetSelection(int n)
241 {
242 wxAutoNSAutoreleasePool pool;
243 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
244 }
245
246 #endif