]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/choice.mm
wxCocoa: Add native file dialog from Ryan Norton. Modified patch #1039368
[wxWidgets.git] / src / cocoa / choice.mm
CommitLineData
da0634c1
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/choice.mm
3// Purpose: wxChoice
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
7// RCS-ID: $Id:
8// Copyright: (c) 2003 David Elliott
065e208e 9// Licence: wxWidgets licence
da0634c1
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/choice.h"
584ad2a3 17 #include "wx/arrstr.h"
449c5673 18#endif //WX_PRECOMP
da0634c1 19
f154ff60 20#include "wx/cocoa/string.h"
d1adc257 21#include "wx/cocoa/autorelease.h"
f154ff60 22
d77ea20c 23#import <AppKit/NSPopUpButton.h>
f154ff60
DE
24#import <AppKit/NSMenu.h>
25#import <Foundation/NSNotification.h>
26#import <Foundation/NSDictionary.h>
d77ea20c 27
da0634c1
DE
28IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
29BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
30END_EVENT_TABLE()
31// WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
32
f154ff60
DE
33void wxChoice::Init()
34{
35 m_sortedStrings = NULL;
36}
37
584ad2a3
MB
38bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
39 const wxPoint& pos,
40 const wxSize& size,
41 const wxArrayString& choices,
42 long style,
43 const wxValidator& validator,
44 const wxString& name)
45{
46 wxCArrayString chs(choices);
47
48 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
49 style, validator, name);
50}
51
da0634c1
DE
52bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
53 const wxPoint& pos,
54 const wxSize& size,
55 int n, const wxString choices[],
56 long style,
57 const wxValidator& validator,
58 const wxString& name)
59{
d1adc257 60 wxAutoNSAutoreleasePool pool;
da0634c1
DE
61 if(!CreateControl(parent,winid,pos,size,style,validator,name))
62 return false;
63
d77ea20c
DE
64 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
65 pullsDown: NO]);
f154ff60
DE
66 [m_cocoaNSView release];
67
68 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
69 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
70
71 if(style&wxCB_SORT)
72 {
73 m_sortedStrings = new wxSortedArrayString;
74 for(int i=0; i<n; i++)
75 {
76 m_sortedStrings->Add(choices[i]);
77 }
78 for(size_t i=0; i < m_sortedStrings->GetCount(); i++)
79 {
80 [nsmenu addItemWithTitle:wxNSStringWithWxString(
81 m_sortedStrings->Item(i))
82 action: nil keyEquivalent:@""];
83 }
84 }
85 else
86 {
87 for(int i=0; i<n; i++)
88 {
89 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
90 action: nil keyEquivalent:@""];
91 }
92 }
93 m_itemsClientData.SetCount(n);
94
95 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
da0634c1
DE
96 if(m_parent)
97 m_parent->CocoaAddChild(this);
d77ea20c
DE
98 SetInitialFrameRect(pos,size);
99
da0634c1
DE
100 return true;
101}
102
103wxChoice::~wxChoice()
104{
f154ff60
DE
105 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
106
107 if(m_sortedStrings)
108 m_sortedStrings->Clear();
109 delete m_sortedStrings;
110
111 if(HasClientObjectData())
112 {
113 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
114 delete (wxClientData*)m_itemsClientData.Item(i);
115 }
116 m_itemsClientData.Clear();
da0634c1
DE
117}
118
f154ff60
DE
119void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
120{
121 NSDictionary *userInfo = [notification userInfo];
122 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
123 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
124 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
48580976 125 wxLogTrace(wxTRACE_COCOA,wxT("menuDidSendAction, index=%d, selectedItem=%d"), index, selectedItem);
f154ff60
DE
126 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
127 event.SetInt(index);
128 event.SetEventObject(this);
129 event.SetString(GetStringSelection());
130 GetEventHandler()->ProcessEvent(event);
131}
132
da0634c1
DE
133void wxChoice::Clear()
134{
f154ff60
DE
135 if(m_sortedStrings)
136 m_sortedStrings->Clear();
137 if(HasClientObjectData())
138 {
139 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
140 delete (wxClientData*)m_itemsClientData.Item(i);
141 }
142 m_itemsClientData.Clear();
143 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
da0634c1
DE
144}
145
f154ff60 146void wxChoice::Delete(int n)
da0634c1 147{
f154ff60
DE
148 if(m_sortedStrings)
149 m_sortedStrings->RemoveAt(n);
150 if(HasClientObjectData())
151 delete (wxClientData*)m_itemsClientData.Item(n);
152 m_itemsClientData.RemoveAt(n);
153 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
da0634c1
DE
154}
155
156int wxChoice::GetCount() const
157{
f154ff60 158 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
da0634c1
DE
159}
160
f154ff60 161wxString wxChoice::GetString(int n) const
da0634c1 162{
d1adc257 163 wxAutoNSAutoreleasePool pool;
b0c0a393 164 return wxStringWithNSString([(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n]);
da0634c1
DE
165}
166
f154ff60 167void wxChoice::SetString(int n, const wxString& title)
da0634c1 168{
f154ff60
DE
169 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
170 [item setTitle:wxNSStringWithWxString(title)];
da0634c1
DE
171}
172
f154ff60 173int wxChoice::FindString(const wxString& title) const
da0634c1 174{
f154ff60
DE
175 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
176 wxNSStringWithWxString(title)];
da0634c1
DE
177}
178
179int wxChoice::GetSelection() const
180{
f154ff60 181 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
da0634c1
DE
182}
183
f154ff60 184int wxChoice::DoAppend(const wxString& title)
da0634c1 185{
f154ff60
DE
186 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
187 NSMenuItem *item;
188 if(m_sortedStrings)
189 {
190 int sortedIndex = m_sortedStrings->Add(title);
191 item = [nsmenu insertItemWithTitle:
192 wxNSStringWithWxString(title)
193 action: nil keyEquivalent:@"" atIndex:sortedIndex];
194 m_itemsClientData.Insert(NULL, sortedIndex);
195 }
196 else
197 {
198 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
199 action: nil keyEquivalent:@""];
200 m_itemsClientData.Add(NULL);
201 }
202 return [nsmenu indexOfItem:item];
da0634c1
DE
203}
204
f154ff60 205int wxChoice::DoInsert(const wxString& title, int pos)
be657756 206{
f154ff60
DE
207 if(m_sortedStrings)
208 return DoAppend(title);
209 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
210 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
211 action: nil keyEquivalent:@"" atIndex:pos];
212 m_itemsClientData.Insert(NULL, pos);
213 return [nsmenu indexOfItem:item];
be657756
DE
214}
215
f154ff60 216void wxChoice::DoSetItemClientData(int n, void *data)
da0634c1 217{
f154ff60 218 m_itemsClientData.Item(n) = data;
da0634c1
DE
219}
220
f154ff60 221void* wxChoice::DoGetItemClientData(int n) const
da0634c1 222{
f154ff60 223 return m_itemsClientData.Item(n);
da0634c1
DE
224}
225
f154ff60 226void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
da0634c1 227{
f154ff60 228 m_itemsClientData.Item(n) = data;
da0634c1
DE
229}
230
f154ff60 231wxClientData* wxChoice::DoGetItemClientObject(int n) const
da0634c1 232{
f154ff60 233 return (wxClientData*)m_itemsClientData.Item(n);
da0634c1
DE
234}
235
f154ff60 236void wxChoice::SetSelection(int n)
da0634c1 237{
d1adc257 238 wxAutoNSAutoreleasePool pool;
f154ff60 239 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
da0634c1
DE
240}
241