]>
Commit | Line | Data |
---|---|---|
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 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; | |
187 | NSMenuItem *item; | |
188 | if(m_sortedStrings) | |
189 | { | |
190 | int sortedIndex = m_sortedStrings->Add(title); | |
191 | item = [nsmenu insertItemWithTitle: | |
192 | wxNSStringWithWxString(title) | |
193 | action: nil keyEquivalent:@"" atIndex:sortedIndex]; | |
194 | m_itemsClientData.Insert(NULL, sortedIndex); | |
195 | } | |
196 | else | |
197 | { | |
198 | item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title) | |
199 | action: nil keyEquivalent:@""]; | |
200 | m_itemsClientData.Add(NULL); | |
201 | } | |
202 | return [nsmenu indexOfItem:item]; | |
203 | } | |
204 | ||
205 | int wxChoice::DoInsert(const wxString& title, int pos) | |
206 | { | |
207 | if(m_sortedStrings) | |
208 | return DoAppend(title); | |
209 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; | |
210 | NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title) | |
211 | action: nil keyEquivalent:@"" atIndex:pos]; | |
212 | m_itemsClientData.Insert(NULL, pos); | |
213 | return [nsmenu indexOfItem:item]; | |
214 | } | |
215 | ||
216 | void wxChoice::DoSetItemClientData(int n, void *data) | |
217 | { | |
218 | m_itemsClientData.Item(n) = data; | |
219 | } | |
220 | ||
221 | void* wxChoice::DoGetItemClientData(int n) const | |
222 | { | |
223 | return m_itemsClientData.Item(n); | |
224 | } | |
225 | ||
226 | void wxChoice::DoSetItemClientObject(int n, wxClientData *data) | |
227 | { | |
228 | m_itemsClientData.Item(n) = data; | |
229 | } | |
230 | ||
231 | wxClientData* wxChoice::DoGetItemClientObject(int n) const | |
232 | { | |
233 | return (wxClientData*)m_itemsClientData.Item(n); | |
234 | } | |
235 | ||
236 | void wxChoice::SetSelection(int n) | |
237 | { | |
238 | wxAutoNSAutoreleasePool pool; | |
239 | [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n]; | |
240 | } | |
241 |