]>
Commit | Line | Data |
---|---|---|
da0634c1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
11e62fe6 | 2 | // Name: src/cocoa/choice.mm |
da0634c1 DE |
3 | // Purpose: wxChoice |
4 | // Author: David Elliott | |
5 | // Modified by: | |
6 | // Created: 2003/03/16 | |
11e62fe6 | 7 | // Id: $Id$ |
da0634c1 | 8 | // Copyright: (c) 2003 David Elliott |
11e62fe6 | 9 | // Licence: wxWidgets licence |
da0634c1 DE |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
449c5673 | 12 | #include "wx/wxprec.h" |
16c81607 RN |
13 | |
14 | #if wxUSE_CHOICE | |
15 | ||
449c5673 DE |
16 | #ifndef WX_PRECOMP |
17 | #include "wx/log.h" | |
18 | #include "wx/app.h" | |
19 | #include "wx/choice.h" | |
584ad2a3 | 20 | #include "wx/arrstr.h" |
449c5673 | 21 | #endif //WX_PRECOMP |
da0634c1 | 22 | |
f154ff60 | 23 | #include "wx/cocoa/string.h" |
d1adc257 | 24 | #include "wx/cocoa/autorelease.h" |
f154ff60 | 25 | |
d77ea20c | 26 | #import <AppKit/NSPopUpButton.h> |
f154ff60 DE |
27 | #import <AppKit/NSMenu.h> |
28 | #import <Foundation/NSNotification.h> | |
29 | #import <Foundation/NSDictionary.h> | |
d77ea20c | 30 | |
da0634c1 DE |
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 | ||
f154ff60 DE |
36 | void wxChoice::Init() |
37 | { | |
38 | m_sortedStrings = NULL; | |
39 | } | |
40 | ||
584ad2a3 MB |
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 | ||
da0634c1 DE |
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 | { | |
d1adc257 | 63 | wxAutoNSAutoreleasePool pool; |
da0634c1 DE |
64 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
65 | return false; | |
66 | ||
d77ea20c DE |
67 | SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size) |
68 | pullsDown: NO]); | |
f154ff60 DE |
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 | } | |
aa61d352 | 81 | for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++) |
f154ff60 DE |
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]; | |
da0634c1 DE |
99 | if(m_parent) |
100 | m_parent->CocoaAddChild(this); | |
d77ea20c DE |
101 | SetInitialFrameRect(pos,size); |
102 | ||
da0634c1 DE |
103 | return true; |
104 | } | |
105 | ||
106 | wxChoice::~wxChoice() | |
107 | { | |
f154ff60 DE |
108 | DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]); |
109 | ||
110 | if(m_sortedStrings) | |
111 | m_sortedStrings->Clear(); | |
112 | delete m_sortedStrings; | |
113 | ||
114 | if(HasClientObjectData()) | |
115 | { | |
aa61d352 | 116 | for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++) |
f154ff60 DE |
117 | delete (wxClientData*)m_itemsClientData.Item(i); |
118 | } | |
119 | m_itemsClientData.Clear(); | |
da0634c1 DE |
120 | } |
121 | ||
f154ff60 DE |
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]; | |
48580976 | 128 | wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem); |
f154ff60 DE |
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 | ||
da0634c1 DE |
136 | void wxChoice::Clear() |
137 | { | |
f154ff60 DE |
138 | if(m_sortedStrings) |
139 | m_sortedStrings->Clear(); | |
140 | if(HasClientObjectData()) | |
141 | { | |
aa61d352 | 142 | for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++) |
f154ff60 DE |
143 | delete (wxClientData*)m_itemsClientData.Item(i); |
144 | } | |
145 | m_itemsClientData.Clear(); | |
146 | [(NSPopUpButton*)m_cocoaNSView removeAllItems]; | |
da0634c1 DE |
147 | } |
148 | ||
aa61d352 | 149 | void wxChoice::Delete(unsigned int n) |
da0634c1 | 150 | { |
f154ff60 DE |
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]; | |
da0634c1 DE |
157 | } |
158 | ||
aa61d352 | 159 | unsigned int wxChoice::GetCount() const |
da0634c1 | 160 | { |
aa61d352 | 161 | return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems]; |
da0634c1 DE |
162 | } |
163 | ||
aa61d352 | 164 | wxString wxChoice::GetString(unsigned int n) const |
da0634c1 | 165 | { |
d1adc257 | 166 | wxAutoNSAutoreleasePool pool; |
b0c0a393 | 167 | return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]); |
da0634c1 DE |
168 | } |
169 | ||
aa61d352 | 170 | void wxChoice::SetString(unsigned int n, const wxString& title) |
da0634c1 | 171 | { |
f154ff60 DE |
172 | NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n]; |
173 | [item setTitle:wxNSStringWithWxString(title)]; | |
da0634c1 DE |
174 | } |
175 | ||
11e62fe6 | 176 | int wxChoice::FindString(const wxString& title, bool bCase) const |
da0634c1 | 177 | { |
11e62fe6 | 178 | // FIXME: use wxItemContainerImmutable::FindString for bCase parameter |
f154ff60 DE |
179 | return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle: |
180 | wxNSStringWithWxString(title)]; | |
da0634c1 DE |
181 | } |
182 | ||
183 | int wxChoice::GetSelection() const | |
184 | { | |
f154ff60 | 185 | return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem]; |
da0634c1 DE |
186 | } |
187 | ||
f154ff60 | 188 | int wxChoice::DoAppend(const wxString& title) |
da0634c1 | 189 | { |
11e62fe6 | 190 | wxAutoNSAutoreleasePool pool; |
f154ff60 DE |
191 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; |
192 | NSMenuItem *item; | |
193 | if(m_sortedStrings) | |
194 | { | |
195 | int sortedIndex = m_sortedStrings->Add(title); | |
196 | item = [nsmenu insertItemWithTitle: | |
197 | wxNSStringWithWxString(title) | |
198 | action: nil keyEquivalent:@"" atIndex:sortedIndex]; | |
199 | m_itemsClientData.Insert(NULL, sortedIndex); | |
200 | } | |
201 | else | |
202 | { | |
203 | item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title) | |
204 | action: nil keyEquivalent:@""]; | |
205 | m_itemsClientData.Add(NULL); | |
206 | } | |
207 | return [nsmenu indexOfItem:item]; | |
da0634c1 DE |
208 | } |
209 | ||
aa61d352 | 210 | int wxChoice::DoInsert(const wxString& title, unsigned int pos) |
be657756 | 211 | { |
f154ff60 DE |
212 | if(m_sortedStrings) |
213 | return DoAppend(title); | |
214 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; | |
215 | NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title) | |
216 | action: nil keyEquivalent:@"" atIndex:pos]; | |
217 | m_itemsClientData.Insert(NULL, pos); | |
218 | return [nsmenu indexOfItem:item]; | |
be657756 DE |
219 | } |
220 | ||
aa61d352 | 221 | void wxChoice::DoSetItemClientData(unsigned int n, void *data) |
da0634c1 | 222 | { |
f154ff60 | 223 | m_itemsClientData.Item(n) = data; |
da0634c1 DE |
224 | } |
225 | ||
aa61d352 | 226 | void* wxChoice::DoGetItemClientData(unsigned int n) const |
da0634c1 | 227 | { |
f154ff60 | 228 | return m_itemsClientData.Item(n); |
da0634c1 DE |
229 | } |
230 | ||
aa61d352 | 231 | void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData *data) |
da0634c1 | 232 | { |
f154ff60 | 233 | m_itemsClientData.Item(n) = data; |
da0634c1 DE |
234 | } |
235 | ||
aa61d352 | 236 | wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const |
da0634c1 | 237 | { |
f154ff60 | 238 | return (wxClientData*)m_itemsClientData.Item(n); |
da0634c1 DE |
239 | } |
240 | ||
f154ff60 | 241 | void wxChoice::SetSelection(int n) |
da0634c1 | 242 | { |
d1adc257 | 243 | wxAutoNSAutoreleasePool pool; |
f154ff60 | 244 | [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n]; |
da0634c1 DE |
245 | } |
246 | ||
16c81607 | 247 | #endif |