]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/choice.mm
no changes, just a typo fix
[wxWidgets.git] / src / cocoa / choice.mm
... / ...
CommitLineData
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
32IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
33BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
34END_EVENT_TABLE()
35// WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
36
37void wxChoice::Init()
38{
39 m_sortedStrings = NULL;
40}
41
42bool 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
56bool 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
107wxChoice::~wxChoice()
108{
109 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
110
111 Clear();
112}
113
114void 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];
120 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
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
128void wxChoice::DoClear()
129{
130 if(m_sortedStrings)
131 m_sortedStrings->Clear();
132 m_itemsClientData.Clear();
133 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
134}
135
136void wxChoice::DoDeleteOneItem(unsigned int n)
137{
138 if(m_sortedStrings)
139 m_sortedStrings->RemoveAt(n);
140 m_itemsClientData.RemoveAt(n);
141 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
142}
143
144unsigned int wxChoice::GetCount() const
145{
146 return (unsigned int)[(NSPopUpButton*)m_cocoaNSView numberOfItems];
147}
148
149wxString wxChoice::GetString(unsigned int n) const
150{
151 wxAutoNSAutoreleasePool pool;
152 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
153}
154
155void wxChoice::SetString(unsigned int n, const wxString& title)
156{
157 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
158 [item setTitle:wxNSStringWithWxString(title)];
159}
160
161int wxChoice::FindString(const wxString& title, bool bCase) const
162{
163 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
164 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
165 wxNSStringWithWxString(title)];
166}
167
168int wxChoice::GetSelection() const
169{
170 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
171}
172
173int wxChoice::DoInsertItems(const wxArrayStringsAdapter & items,
174 unsigned int pos,
175 void **clientData, wxClientDataType type)
176{
177 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
178 NSMenuItem *item = NULL;
179
180 unsigned int numItems = items.GetCount();
181 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
182 {
183 const wxString& str = items[i];
184 int idx = m_sortedStrings ? m_sortedStrings->Add(str) : pos;
185
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 }
191 return [nsmenu indexOfItem:item];
192}
193
194void wxChoice::DoSetItemClientData(unsigned int n, void *data)
195{
196 m_itemsClientData[n] = data;
197}
198
199void* wxChoice::DoGetItemClientData(unsigned int n) const
200{
201 return m_itemsClientData[n];
202}
203
204void wxChoice::SetSelection(int n)
205{
206 wxAutoNSAutoreleasePool pool;
207 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
208}
209
210#endif // wxUSE_CHOICE