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