]>
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 | |
da0634c1 | 7 | // Copyright: (c) 2003 David Elliott |
526954c5 | 8 | // Licence: wxWindows licence |
da0634c1 DE |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
449c5673 | 11 | #include "wx/wxprec.h" |
16c81607 RN |
12 | |
13 | #if wxUSE_CHOICE | |
14 | ||
b36e08d0 WS |
15 | #include "wx/choice.h" |
16 | ||
449c5673 DE |
17 | #ifndef WX_PRECOMP |
18 | #include "wx/log.h" | |
19 | #include "wx/app.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 | BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase) |
32 | END_EVENT_TABLE() | |
33 | // WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView) | |
34 | ||
f154ff60 DE |
35 | void wxChoice::Init() |
36 | { | |
37 | m_sortedStrings = NULL; | |
38 | } | |
39 | ||
584ad2a3 MB |
40 | bool wxChoice::Create(wxWindow *parent, wxWindowID winid, |
41 | const wxPoint& pos, | |
42 | const wxSize& size, | |
43 | const wxArrayString& choices, | |
44 | long style, | |
45 | const wxValidator& validator, | |
46 | const wxString& name) | |
47 | { | |
48 | wxCArrayString chs(choices); | |
49 | ||
50 | return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(), | |
51 | style, validator, name); | |
52 | } | |
53 | ||
da0634c1 DE |
54 | bool wxChoice::Create(wxWindow *parent, wxWindowID winid, |
55 | const wxPoint& pos, | |
56 | const wxSize& size, | |
57 | int n, const wxString choices[], | |
58 | long style, | |
59 | const wxValidator& validator, | |
60 | const wxString& name) | |
61 | { | |
d1adc257 | 62 | wxAutoNSAutoreleasePool pool; |
da0634c1 DE |
63 | if(!CreateControl(parent,winid,pos,size,style,validator,name)) |
64 | return false; | |
65 | ||
d77ea20c DE |
66 | SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size) |
67 | pullsDown: NO]); | |
f154ff60 DE |
68 | [m_cocoaNSView release]; |
69 | ||
70 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; | |
71 | AssociateNSMenu(nsmenu, OBSERVE_DidSendAction); | |
72 | ||
73 | if(style&wxCB_SORT) | |
74 | { | |
75 | m_sortedStrings = new wxSortedArrayString; | |
76 | for(int i=0; i<n; i++) | |
77 | { | |
78 | m_sortedStrings->Add(choices[i]); | |
79 | } | |
aa61d352 | 80 | for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++) |
f154ff60 DE |
81 | { |
82 | [nsmenu addItemWithTitle:wxNSStringWithWxString( | |
83 | m_sortedStrings->Item(i)) | |
84 | action: nil keyEquivalent:@""]; | |
85 | } | |
86 | } | |
87 | else | |
88 | { | |
89 | for(int i=0; i<n; i++) | |
90 | { | |
91 | [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i]) | |
92 | action: nil keyEquivalent:@""]; | |
93 | } | |
94 | } | |
95 | m_itemsClientData.SetCount(n); | |
96 | ||
97 | [(NSPopUpButton*)m_cocoaNSView sizeToFit]; | |
da0634c1 DE |
98 | if(m_parent) |
99 | m_parent->CocoaAddChild(this); | |
d77ea20c DE |
100 | SetInitialFrameRect(pos,size); |
101 | ||
da0634c1 DE |
102 | return true; |
103 | } | |
104 | ||
105 | wxChoice::~wxChoice() | |
106 | { | |
f154ff60 DE |
107 | DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]); |
108 | ||
a236aa20 | 109 | Clear(); |
da0634c1 DE |
110 | } |
111 | ||
f154ff60 DE |
112 | void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification) |
113 | { | |
114 | NSDictionary *userInfo = [notification userInfo]; | |
115 | NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"]; | |
116 | int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem]; | |
117 | int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem]; | |
48580976 | 118 | wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem); |
ce7fe42e | 119 | wxCommandEvent event(wxEVT_CHOICE, m_windowId); |
f154ff60 DE |
120 | event.SetInt(index); |
121 | event.SetEventObject(this); | |
122 | event.SetString(GetStringSelection()); | |
937013e0 | 123 | HandleWindowEvent(event); |
f154ff60 DE |
124 | } |
125 | ||
a236aa20 | 126 | void wxChoice::DoClear() |
da0634c1 | 127 | { |
f154ff60 DE |
128 | if(m_sortedStrings) |
129 | m_sortedStrings->Clear(); | |
f154ff60 DE |
130 | m_itemsClientData.Clear(); |
131 | [(NSPopUpButton*)m_cocoaNSView removeAllItems]; | |
da0634c1 DE |
132 | } |
133 | ||
a236aa20 | 134 | void wxChoice::DoDeleteOneItem(unsigned int n) |
da0634c1 | 135 | { |
f154ff60 DE |
136 | if(m_sortedStrings) |
137 | m_sortedStrings->RemoveAt(n); | |
f154ff60 DE |
138 | m_itemsClientData.RemoveAt(n); |
139 | [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n]; | |
da0634c1 DE |
140 | } |
141 | ||
aa61d352 | 142 | unsigned int wxChoice::GetCount() const |
da0634c1 | 143 | { |
aa61d352 | 144 | return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems]; |
da0634c1 DE |
145 | } |
146 | ||
aa61d352 | 147 | wxString wxChoice::GetString(unsigned int n) const |
da0634c1 | 148 | { |
d1adc257 | 149 | wxAutoNSAutoreleasePool pool; |
b0c0a393 | 150 | return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]); |
da0634c1 DE |
151 | } |
152 | ||
aa61d352 | 153 | void wxChoice::SetString(unsigned int n, const wxString& title) |
da0634c1 | 154 | { |
f154ff60 DE |
155 | NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n]; |
156 | [item setTitle:wxNSStringWithWxString(title)]; | |
da0634c1 DE |
157 | } |
158 | ||
11e62fe6 | 159 | int wxChoice::FindString(const wxString& title, bool bCase) const |
da0634c1 | 160 | { |
11e62fe6 | 161 | // FIXME: use wxItemContainerImmutable::FindString for bCase parameter |
f154ff60 DE |
162 | return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle: |
163 | wxNSStringWithWxString(title)]; | |
da0634c1 DE |
164 | } |
165 | ||
166 | int wxChoice::GetSelection() const | |
167 | { | |
f154ff60 | 168 | return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem]; |
da0634c1 DE |
169 | } |
170 | ||
a236aa20 VZ |
171 | int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items, |
172 | unsigned int pos, | |
173 | void **clientData, wxClientDataType type) | |
da0634c1 | 174 | { |
f154ff60 | 175 | NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu]; |
a236aa20 VZ |
176 | NSMenuItem *item = NULL; |
177 | ||
178 | unsigned int numItems = items.GetCount(); | |
179 | for ( unsigned int i = 0; i < numItems; ++i, ++pos ) | |
f154ff60 | 180 | { |
a236aa20 VZ |
181 | const wxString& str = items[i]; |
182 | int idx = m_sortedStrings ? m_sortedStrings->Add(str) : pos; | |
da0634c1 | 183 | |
a236aa20 VZ |
184 | item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(str) |
185 | action: nil keyEquivalent:@"" atIndex:idx]; | |
186 | m_itemsClientData.Insert(NULL, idx); | |
187 | AssignNewItemClientData(idx, clientData, i, type); | |
188 | } | |
f154ff60 | 189 | return [nsmenu indexOfItem:item]; |
be657756 DE |
190 | } |
191 | ||
aa61d352 | 192 | void wxChoice::DoSetItemClientData(unsigned int n, void *data) |
da0634c1 | 193 | { |
a236aa20 | 194 | m_itemsClientData[n] = data; |
da0634c1 DE |
195 | } |
196 | ||
aa61d352 | 197 | void* wxChoice::DoGetItemClientData(unsigned int n) const |
da0634c1 | 198 | { |
a236aa20 | 199 | return m_itemsClientData[n]; |
da0634c1 DE |
200 | } |
201 | ||
f154ff60 | 202 | void wxChoice::SetSelection(int n) |
da0634c1 | 203 | { |
d1adc257 | 204 | wxAutoNSAutoreleasePool pool; |
f154ff60 | 205 | [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n]; |
da0634c1 DE |
206 | } |
207 | ||
a236aa20 | 208 | #endif // wxUSE_CHOICE |