]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/choice.mm
More lib and demo patches to drop the wx prefix (Jeff has been busy!)
[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
9// Licence: wxWindows license
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"
17#endif //WX_PRECOMP
da0634c1 18
f154ff60
DE
19#include "wx/cocoa/string.h"
20
d77ea20c 21#import <AppKit/NSPopUpButton.h>
f154ff60
DE
22#import <AppKit/NSMenu.h>
23#import <Foundation/NSNotification.h>
24#import <Foundation/NSDictionary.h>
d77ea20c 25
da0634c1
DE
26IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
27BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
28END_EVENT_TABLE()
29// WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
30
f154ff60
DE
31void wxChoice::Init()
32{
33 m_sortedStrings = NULL;
34}
35
da0634c1
DE
36bool wxChoice::Create(wxWindow *parent, wxWindowID winid,
37 const wxPoint& pos,
38 const wxSize& size,
39 int n, const wxString choices[],
40 long style,
41 const wxValidator& validator,
42 const wxString& name)
43{
44 if(!CreateControl(parent,winid,pos,size,style,validator,name))
45 return false;
46
d77ea20c
DE
47 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
48 pullsDown: NO]);
f154ff60
DE
49 [m_cocoaNSView release];
50
51 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
52 AssociateNSMenu(nsmenu, OBSERVE_DidSendAction);
53
54 if(style&wxCB_SORT)
55 {
56 m_sortedStrings = new wxSortedArrayString;
57 for(int i=0; i<n; i++)
58 {
59 m_sortedStrings->Add(choices[i]);
60 }
61 for(size_t i=0; i < m_sortedStrings->GetCount(); i++)
62 {
63 [nsmenu addItemWithTitle:wxNSStringWithWxString(
64 m_sortedStrings->Item(i))
65 action: nil keyEquivalent:@""];
66 }
67 }
68 else
69 {
70 for(int i=0; i<n; i++)
71 {
72 [nsmenu addItemWithTitle:wxNSStringWithWxString(choices[i])
73 action: nil keyEquivalent:@""];
74 }
75 }
76 m_itemsClientData.SetCount(n);
77
78 [(NSPopUpButton*)m_cocoaNSView sizeToFit];
da0634c1
DE
79 if(m_parent)
80 m_parent->CocoaAddChild(this);
d77ea20c
DE
81 SetInitialFrameRect(pos,size);
82
da0634c1
DE
83 return true;
84}
85
86wxChoice::~wxChoice()
87{
f154ff60
DE
88 DisassociateNSMenu([(NSPopUpButton*)m_cocoaNSView menu]);
89
90 if(m_sortedStrings)
91 m_sortedStrings->Clear();
92 delete m_sortedStrings;
93
94 if(HasClientObjectData())
95 {
96 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
97 delete (wxClientData*)m_itemsClientData.Item(i);
98 }
99 m_itemsClientData.Clear();
100
da0634c1
DE
101 CocoaRemoveFromParent();
102}
103
f154ff60
DE
104void wxChoice::CocoaNotification_menuDidSendAction(WX_NSNotification notification)
105{
106 NSDictionary *userInfo = [notification userInfo];
107 NSMenuItem *menuitem = [userInfo objectForKey:@"MenuItem"];
108 int index = [[(NSPopUpButton*)m_cocoaNSView menu] indexOfItem: menuitem];
109 int selectedItem = [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
110 wxLogDebug("menuDidSendAction, index=%d, selectedItem=%d", index, selectedItem);
111 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
112 event.SetInt(index);
113 event.SetEventObject(this);
114 event.SetString(GetStringSelection());
115 GetEventHandler()->ProcessEvent(event);
116}
117
da0634c1
DE
118void wxChoice::Clear()
119{
f154ff60
DE
120 if(m_sortedStrings)
121 m_sortedStrings->Clear();
122 if(HasClientObjectData())
123 {
124 for(size_t i=0; i < m_itemsClientData.GetCount(); i++)
125 delete (wxClientData*)m_itemsClientData.Item(i);
126 }
127 m_itemsClientData.Clear();
128 [(NSPopUpButton*)m_cocoaNSView removeAllItems];
da0634c1
DE
129}
130
f154ff60 131void wxChoice::Delete(int n)
da0634c1 132{
f154ff60
DE
133 if(m_sortedStrings)
134 m_sortedStrings->RemoveAt(n);
135 if(HasClientObjectData())
136 delete (wxClientData*)m_itemsClientData.Item(n);
137 m_itemsClientData.RemoveAt(n);
138 [(NSPopUpButton*)m_cocoaNSView removeItemAtIndex:n];
da0634c1
DE
139}
140
141int wxChoice::GetCount() const
142{
f154ff60 143 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
da0634c1
DE
144}
145
f154ff60 146wxString wxChoice::GetString(int n) const
da0634c1 147{
f154ff60 148 return wxString([[(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n] lossyCString]);
da0634c1
DE
149}
150
f154ff60 151void wxChoice::SetString(int n, const wxString& title)
da0634c1 152{
f154ff60
DE
153 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
154 [item setTitle:wxNSStringWithWxString(title)];
da0634c1
DE
155}
156
f154ff60 157int wxChoice::FindString(const wxString& title) const
da0634c1 158{
f154ff60
DE
159 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
160 wxNSStringWithWxString(title)];
da0634c1
DE
161}
162
163int wxChoice::GetSelection() const
164{
f154ff60 165 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
da0634c1
DE
166}
167
f154ff60 168int wxChoice::DoAppend(const wxString& title)
da0634c1 169{
f154ff60
DE
170 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
171 NSMenuItem *item;
172 if(m_sortedStrings)
173 {
174 int sortedIndex = m_sortedStrings->Add(title);
175 item = [nsmenu insertItemWithTitle:
176 wxNSStringWithWxString(title)
177 action: nil keyEquivalent:@"" atIndex:sortedIndex];
178 m_itemsClientData.Insert(NULL, sortedIndex);
179 }
180 else
181 {
182 item = [nsmenu addItemWithTitle:wxNSStringWithWxString(title)
183 action: nil keyEquivalent:@""];
184 m_itemsClientData.Add(NULL);
185 }
186 return [nsmenu indexOfItem:item];
da0634c1
DE
187}
188
f154ff60 189int wxChoice::DoInsert(const wxString& title, int pos)
be657756 190{
f154ff60
DE
191 if(m_sortedStrings)
192 return DoAppend(title);
193 NSMenu *nsmenu = [(NSPopUpButton*)m_cocoaNSView menu];
194 NSMenuItem *item = [nsmenu insertItemWithTitle:wxNSStringWithWxString(title)
195 action: nil keyEquivalent:@"" atIndex:pos];
196 m_itemsClientData.Insert(NULL, pos);
197 return [nsmenu indexOfItem:item];
be657756
DE
198}
199
f154ff60 200void wxChoice::DoSetItemClientData(int n, void *data)
da0634c1 201{
f154ff60 202 m_itemsClientData.Item(n) = data;
da0634c1
DE
203}
204
f154ff60 205void* wxChoice::DoGetItemClientData(int n) const
da0634c1 206{
f154ff60 207 return m_itemsClientData.Item(n);
da0634c1
DE
208}
209
f154ff60 210void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
da0634c1 211{
f154ff60 212 m_itemsClientData.Item(n) = data;
da0634c1
DE
213}
214
f154ff60 215wxClientData* wxChoice::DoGetItemClientObject(int n) const
da0634c1 216{
f154ff60 217 return (wxClientData*)m_itemsClientData.Item(n);
da0634c1
DE
218}
219
f154ff60 220void wxChoice::SetSelection(int n)
da0634c1 221{
f154ff60 222 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
da0634c1
DE
223}
224