]>
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 | |
b1294ada | 32 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControlWithItems) |
da0634c1 DE |
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 | ||
a236aa20 | 111 | Clear(); |
da0634c1 DE |
112 | } |
113 | ||
f154ff60 DE |
114 | void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification) |
115 | { | |
116 | NSDictionary *userInfo = [notification userInfo]; | |
117 | NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"]; | |
118 | int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem]; | |
119 | int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem]; | |
48580976 | 120 | wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem); |
f154ff60 DE |
121 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); |
122 | event.SetInt(index); | |
123 | event.SetEventObject(this); | |
124 | event.SetString(GetStringSelection()); | |
125 | GetEventHandler()->ProcessEvent(event); | |
126 | } | |
127 | ||
a236aa20 | 128 | void wxChoice::DoClear() |
da0634c1 | 129 | { |
f154ff60 DE |
130 | if(m_sortedStrings) |
131 | m_sortedStrings->Clear(); | |
f154ff60 DE |
132 | m_itemsClientData.Clear(); |
133 | [(NSPopUpButton*)m_cocoaNSView removeAllItems]; | |
da0634c1 DE |
134 | } |
135 | ||
a236aa20 | 136 | void wxChoice::DoDeleteOneItem(unsigned int n) |
da0634c1 | 137 | { |
f154ff60 DE |
138 | if(m_sortedStrings) |
139 | m_sortedStrings->RemoveAt(n); | |
f154ff60 DE |
140 | m_itemsClientData.RemoveAt(n); |
141 | [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n]; | |
da0634c1 DE |
142 | } |
143 | ||
aa61d352 | 144 | unsigned int wxChoice::GetCount() const |
da0634c1 | 145 | { |
aa61d352 | 146 | return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems]; |
da0634c1 DE |
147 | } |
148 | ||
aa61d352 | 149 | wxString wxChoice::GetString(unsigned int n) const |
da0634c1 | 150 | { |
d1adc257 | 151 | wxAutoNSAutoreleasePool pool; |
b0c0a393 | 152 | return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]); |
da0634c1 DE |
153 | } |
154 | ||
aa61d352 | 155 | void wxChoice::SetString(unsigned int n, const wxString& title) |
da0634c1 | 156 | { |
f154ff60 DE |
157 | NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n]; |
158 | [item setTitle:wxNSStringWithWxString(title)]; | |
da0634c1 DE |
159 | } |
160 | ||
11e62fe6 | 161 | int wxChoice::FindString(const wxString& title, bool bCase) const |
da0634c1 | 162 | { |
11e62fe6 | 163 | // FIXME: use wxItemContainerImmutable::FindString for bCase parameter |
f154ff60 DE |
164 | return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle: |
165 | wxNSStringWithWxString(title)]; | |
da0634c1 DE |
166 | } |
167 | ||
168 | int wxChoice::GetSelection() const | |
169 | { | |
f154ff60 | 170 | return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem]; |
da0634c1 DE |
171 | } |
172 | ||
a236aa20 VZ |
173 | int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, |
174 | unsigned int pos, | |
175 | void **clientData, wxClientDataType type) | |
da0634c1 | 176 | { |
f154ff60 | 177 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; |
a236aa20 VZ |
178 | NSMenuItem *item = NULL; |
179 | ||
180 | unsigned int numItems = items.GetCount(); | |
181 | for ( unsigned int i = 0; i < numItems; ++i, ++pos ) | |
f154ff60 | 182 | { |
a236aa20 VZ |
183 | const wxString& str = items[i]; |
184 | int idx = m_sortedStrings ? m_sortedStrings->Add(str) : pos; | |
da0634c1 | 185 | |
a236aa20 VZ |
186 | item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(str) |
187 | action: nil keyEquivalent:@"" atIndex:idx]; | |
188 | m_itemsClientData.Insert(NULL, idx); | |
189 | AssignNewItemClientData(idx, clientData, i, type); | |
190 | } | |
f154ff60 | 191 | return [nsmenu indexOfItem:item]; |
be657756 DE |
192 | } |
193 | ||
aa61d352 | 194 | void wxChoice::DoSetItemClientData(unsigned int n, void *data) |
da0634c1 | 195 | { |
a236aa20 | 196 | m_itemsClientData[n] = data; |
da0634c1 DE |
197 | } |
198 | ||
aa61d352 | 199 | void* wxChoice::DoGetItemClientData(unsigned int n) const |
da0634c1 | 200 | { |
a236aa20 | 201 | return m_itemsClientData[n]; |
da0634c1 DE |
202 | } |
203 | ||
f154ff60 | 204 | void wxChoice::SetSelection(int n) |
da0634c1 | 205 | { |
d1adc257 | 206 | wxAutoNSAutoreleasePool pool; |
f154ff60 | 207 | [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n]; |
da0634c1 DE |
208 | } |
209 | ||
a236aa20 | 210 | #endif // wxUSE_CHOICE |