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