]>
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 | ||
b36e08d0 WS |
16 | #include "wx/choice.h" |
17 | ||
449c5673 DE |
18 | #ifndef WX_PRECOMP |
19 | #include "wx/log.h" | |
20 | #include "wx/app.h" | |
584ad2a3 | 21 | #include "wx/arrstr.h" |
449c5673 | 22 | #endif //WX_PRECOMP |
da0634c1 | 23 | |
f154ff60 | 24 | #include "wx/cocoa/string.h" |
d1adc257 | 25 | #include "wx/cocoa/autorelease.h" |
f154ff60 | 26 | |
d77ea20c | 27 | #import <AppKit/NSPopUpButton.h> |
f154ff60 DE |
28 | #import <AppKit/NSMenu.h> |
29 | #import <Foundation/NSNotification.h> | |
30 | #import <Foundation/NSDictionary.h> | |
d77ea20c | 31 | |
da0634c1 DE |
32 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) |
33 | BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase) | |
34 | END_EVENT_TABLE() | |
35 | // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView) | |
36 | ||
f154ff60 DE |
37 | void wxChoice::Init() |
38 | { | |
39 | m_sortedStrings = NULL; | |
40 | } | |
41 | ||
584ad2a3 MB |
42 | bool wxChoice::Create(wxWindow *parent, wxWindowID winid, |
43 | const wxPoint& pos, | |
44 | const wxSize& size, | |
45 | const wxArrayString& choices, | |
46 | long style, | |
47 | const wxValidator& validator, | |
48 | const wxString& name) | |
49 | { | |
50 | wxCArrayString chs(choices); | |
51 | ||
52 | return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(), | |
53 | style, validator, name); | |
54 | } | |
55 | ||
da0634c1 DE |
56 | bool wxChoice::Create(wxWindow *parent, wxWindowID winid, |
57 | const wxPoint& pos, | |
58 | const wxSize& size, | |
59 | int n, const wxString choices[], | |
60 | long style, | |
61 | const wxValidator& validator, | |
62 | const wxString& name) | |
63 | { | |
d1adc257 | 64 | wxAutoNSAutoreleasePool pool; |
da0634c1 DE |
65 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
66 | return false; | |
67 | ||
d77ea20c DE |
68 | SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size) |
69 | pullsDown: NO]); | |
f154ff60 DE |
70 | [m_cocoaNSView release]; |
71 | ||
72 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; | |
73 | AssociateNSMenu(nsmenu, OBSERVE_DidSendAction); | |
74 | ||
75 | if(style&wxCB_SORT) | |
76 | { | |
77 | m_sortedStrings = new wxSortedArrayString; | |
78 | for(int i=0; i<n; i++) | |
79 | { | |
80 | m_sortedStrings->Add(choices[i]); | |
81 | } | |
aa61d352 | 82 | for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++) |
f154ff60 DE |
83 | { |
84 | [nsmenu addItemWithTitle:wxNSStringWithWxString( | |
85 | m_sortedStrings->Item(i)) | |
86 | action: nil keyEquivalent:@""]; | |
87 | } | |
88 | } | |
89 | else | |
90 | { | |
91 | for(int i=0; i<n; i++) | |
92 | { | |
93 | [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i]) | |
94 | action: nil keyEquivalent:@""]; | |
95 | } | |
96 | } | |
97 | m_itemsClientData.SetCount(n); | |
98 | ||
99 | [(NSPopUpButton*)m_cocoaNSView sizeToFit]; | |
da0634c1 DE |
100 | if(m_parent) |
101 | m_parent->CocoaAddChild(this); | |
d77ea20c DE |
102 | SetInitialFrameRect(pos,size); |
103 | ||
da0634c1 DE |
104 | return true; |
105 | } | |
106 | ||
107 | wxChoice::~wxChoice() | |
108 | { | |
f154ff60 DE |
109 | DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]); |
110 | ||
111 | if(m_sortedStrings) | |
112 | m_sortedStrings->Clear(); | |
113 | delete m_sortedStrings; | |
114 | ||
115 | if(HasClientObjectData()) | |
116 | { | |
aa61d352 | 117 | for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++) |
f154ff60 DE |
118 | delete (wxClientData*)m_itemsClientData.Item(i); |
119 | } | |
120 | m_itemsClientData.Clear(); | |
da0634c1 DE |
121 | } |
122 | ||
f154ff60 DE |
123 | void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification) |
124 | { | |
125 | NSDictionary *userInfo = [notification userInfo]; | |
126 | NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"]; | |
127 | int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem]; | |
128 | int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem]; | |
48580976 | 129 | wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem); |
f154ff60 DE |
130 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); |
131 | event.SetInt(index); | |
132 | event.SetEventObject(this); | |
133 | event.SetString(GetStringSelection()); | |
134 | GetEventHandler()->ProcessEvent(event); | |
135 | } | |
136 | ||
da0634c1 DE |
137 | void wxChoice::Clear() |
138 | { | |
f154ff60 DE |
139 | if(m_sortedStrings) |
140 | m_sortedStrings->Clear(); | |
141 | if(HasClientObjectData()) | |
142 | { | |
aa61d352 | 143 | for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++) |
f154ff60 DE |
144 | delete (wxClientData*)m_itemsClientData.Item(i); |
145 | } | |
146 | m_itemsClientData.Clear(); | |
147 | [(NSPopUpButton*)m_cocoaNSView removeAllItems]; | |
da0634c1 DE |
148 | } |
149 | ||
aa61d352 | 150 | void wxChoice::Delete(unsigned int n) |
da0634c1 | 151 | { |
f154ff60 DE |
152 | if(m_sortedStrings) |
153 | m_sortedStrings->RemoveAt(n); | |
154 | if(HasClientObjectData()) | |
155 | delete (wxClientData*)m_itemsClientData.Item(n); | |
156 | m_itemsClientData.RemoveAt(n); | |
157 | [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n]; | |
da0634c1 DE |
158 | } |
159 | ||
aa61d352 | 160 | unsigned int wxChoice::GetCount() const |
da0634c1 | 161 | { |
aa61d352 | 162 | return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems]; |
da0634c1 DE |
163 | } |
164 | ||
aa61d352 | 165 | wxString wxChoice::GetString(unsigned int n) const |
da0634c1 | 166 | { |
d1adc257 | 167 | wxAutoNSAutoreleasePool pool; |
b0c0a393 | 168 | return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]); |
da0634c1 DE |
169 | } |
170 | ||
aa61d352 | 171 | void wxChoice::SetString(unsigned int n, const wxString& title) |
da0634c1 | 172 | { |
f154ff60 DE |
173 | NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n]; |
174 | [item setTitle:wxNSStringWithWxString(title)]; | |
da0634c1 DE |
175 | } |
176 | ||
11e62fe6 | 177 | int wxChoice::FindString(const wxString& title, bool bCase) const |
da0634c1 | 178 | { |
11e62fe6 | 179 | // FIXME: use wxItemContainerImmutable::FindString for bCase parameter |
f154ff60 DE |
180 | return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle: |
181 | wxNSStringWithWxString(title)]; | |
da0634c1 DE |
182 | } |
183 | ||
184 | int wxChoice::GetSelection() const | |
185 | { | |
f154ff60 | 186 | return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem]; |
da0634c1 DE |
187 | } |
188 | ||
f154ff60 | 189 | int wxChoice::DoAppend(const wxString& title) |
da0634c1 | 190 | { |
11e62fe6 | 191 | wxAutoNSAutoreleasePool pool; |
f154ff60 DE |
192 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; |
193 | NSMenuItem *item; | |
194 | if(m_sortedStrings) | |
195 | { | |
196 | int sortedIndex = m_sortedStrings->Add(title); | |
197 | item = [nsmenu insertItemWithTitle: | |
198 | wxNSStringWithWxString(title) | |
199 | action: nil keyEquivalent:@"" atIndex:sortedIndex]; | |
200 | m_itemsClientData.Insert(NULL, sortedIndex); | |
201 | } | |
202 | else | |
203 | { | |
204 | item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title) | |
205 | action: nil keyEquivalent:@""]; | |
206 | m_itemsClientData.Add(NULL); | |
207 | } | |
208 | return [nsmenu indexOfItem:item]; | |
da0634c1 DE |
209 | } |
210 | ||
aa61d352 | 211 | int wxChoice::DoInsert(const wxString& title, unsigned int pos) |
be657756 | 212 | { |
f154ff60 DE |
213 | if(m_sortedStrings) |
214 | return DoAppend(title); | |
215 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; | |
216 | NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title) | |
217 | action: nil keyEquivalent:@"" atIndex:pos]; | |
218 | m_itemsClientData.Insert(NULL, pos); | |
219 | return [nsmenu indexOfItem:item]; | |
be657756 DE |
220 | } |
221 | ||
aa61d352 | 222 | void wxChoice::DoSetItemClientData(unsigned int n, void *data) |
da0634c1 | 223 | { |
f154ff60 | 224 | m_itemsClientData.Item(n) = data; |
da0634c1 DE |
225 | } |
226 | ||
aa61d352 | 227 | void* wxChoice::DoGetItemClientData(unsigned int n) const |
da0634c1 | 228 | { |
f154ff60 | 229 | return m_itemsClientData.Item(n); |
da0634c1 DE |
230 | } |
231 | ||
aa61d352 | 232 | void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData *data) |
da0634c1 | 233 | { |
f154ff60 | 234 | m_itemsClientData.Item(n) = data; |
da0634c1 DE |
235 | } |
236 | ||
aa61d352 | 237 | wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const |
da0634c1 | 238 | { |
f154ff60 | 239 | return (wxClientData*)m_itemsClientData.Item(n); |
da0634c1 DE |
240 | } |
241 | ||
f154ff60 | 242 | void wxChoice::SetSelection(int n) |
da0634c1 | 243 | { |
d1adc257 | 244 | wxAutoNSAutoreleasePool pool; |
f154ff60 | 245 | [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n]; |
da0634c1 DE |
246 | } |
247 | ||
16c81607 | 248 | #endif |