]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/choice.mm
more woodoo in DoGetBestSize() to get rid of tree borders for default-sizes controls
[wxWidgets.git] / src / cocoa / choice.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/choice.mm
3 // Purpose: wxChoice
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/16
7 // Id: $Id$
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_CHOICE
15
16 #include "wx/choice.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/log.h"
20 #include "wx/app.h"
21 #include "wx/arrstr.h"
22 #endif //WX_PRECOMP
23
24 #include "wx/cocoa/string.h"
25 #include "wx/cocoa/autorelease.h"
26
27 #import <AppKit/NSPopUpButton.h>
28 #import <AppKit/NSMenu.h>
29 #import <Foundation/NSNotification.h>
30 #import <Foundation/NSDictionary.h>
31
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
37 void wxChoice::Init()
38 {
39 m_sortedStrings = NULL;
40 }
41
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
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 {
64 wxAutoNSAutoreleasePool pool;
65 if(!CreateControl(parent,winid,pos,size,style,validator,name))
66 return false;
67
68 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
69 pullsDown: NO]);
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 }
82 for(unsigned int i=0; i < m_sortedStrings->GetCount(); i++)
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];
100 if(m_parent)
101 m_parent->CocoaAddChild(this);
102 SetInitialFrameRect(pos,size);
103
104 return true;
105 }
106
107 wxChoice::~wxChoice()
108 {
109 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
110
111 if(m_sortedStrings)
112 m_sortedStrings->Clear();
113 delete m_sortedStrings;
114
115 if(HasClientObjectData())
116 {
117 for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++)
118 delete (wxClientData*)m_itemsClientData.Item(i);
119 }
120 m_itemsClientData.Clear();
121 }
122
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];
129 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
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
137 void wxChoice::Clear()
138 {
139 if(m_sortedStrings)
140 m_sortedStrings->Clear();
141 if(HasClientObjectData())
142 {
143 for(unsigned int i=0; i < m_itemsClientData.GetCount(); i++)
144 delete (wxClientData*)m_itemsClientData.Item(i);
145 }
146 m_itemsClientData.Clear();
147 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
148 }
149
150 void wxChoice::Delete(unsigned int n)
151 {
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];
158 }
159
160 unsigned int wxChoice::GetCount() const
161 {
162 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
163 }
164
165 wxString wxChoice::GetString(unsigned int n) const
166 {
167 wxAutoNSAutoreleasePool pool;
168 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
169 }
170
171 void wxChoice::SetString(unsigned int n, const wxString& title)
172 {
173 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
174 [item setTitle:wxNSStringWithWxString(title)];
175 }
176
177 int wxChoice::FindString(const wxString& title, bool bCase) const
178 {
179 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
180 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
181 wxNSStringWithWxString(title)];
182 }
183
184 int wxChoice::GetSelection() const
185 {
186 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
187 }
188
189 int wxChoice::DoAppend(const wxString& title)
190 {
191 wxAutoNSAutoreleasePool pool;
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];
209 }
210
211 int wxChoice::DoInsert(const wxString& title, unsigned int pos)
212 {
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];
220 }
221
222 void wxChoice::DoSetItemClientData(unsigned int n, void *data)
223 {
224 m_itemsClientData.Item(n) = data;
225 }
226
227 void* wxChoice::DoGetItemClientData(unsigned int n) const
228 {
229 return m_itemsClientData.Item(n);
230 }
231
232 void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData *data)
233 {
234 m_itemsClientData.Item(n) = data;
235 }
236
237 wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
238 {
239 return (wxClientData*)m_itemsClientData.Item(n);
240 }
241
242 void wxChoice::SetSelection(int n)
243 {
244 wxAutoNSAutoreleasePool pool;
245 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
246 }
247
248 #endif