]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/choice.mm
removed assert which became incorrect after last change
[wxWidgets.git] / src / cocoa / choice.mm
... / ...
CommitLineData
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
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
18
19#include "wx/cocoa/string.h"
20
21#import <AppKit/NSPopUpButton.h>
22#import <AppKit/NSMenu.h>
23#import <Foundation/NSNotification.h>
24#import <Foundation/NSDictionary.h>
25
26IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
27BEGIN_EVENT_TABLE(wxChoice, wxChoiceBase)
28END_EVENT_TABLE()
29// WX_IMPLEMENT_COCOA_OWNER(wxChoice,NSButton,NSControl,NSView)
30
31void wxChoice::Init()
32{
33 m_sortedStrings = NULL;
34}
35
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
47 SetNSView([[NSPopUpButton alloc] initWithFrame:MakeDefaultNSRect(size)
48 pullsDown: NO]);
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];
79 if(m_parent)
80 m_parent->CocoaAddChild(this);
81 SetInitialFrameRect(pos,size);
82
83 return true;
84}
85
86wxChoice::~wxChoice()
87{
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
101 CocoaRemoveFromParent();
102}
103
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
118void wxChoice::Clear()
119{
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];
129}
130
131void wxChoice::Delete(int n)
132{
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];
139}
140
141int wxChoice::GetCount() const
142{
143 return [(NSPopUpButton*)m_cocoaNSView numberOfItems];
144}
145
146wxString wxChoice::GetString(int n) const
147{
148 return wxString([[(NSPopUpButton*)m_cocoaNSView itemTitleAtIndex:n] lossyCString]);
149}
150
151void wxChoice::SetString(int n, const wxString& title)
152{
153 NSMenuItem *item = [(NSPopUpButton*)m_cocoaNSView itemAtIndex:n];
154 [item setTitle:wxNSStringWithWxString(title)];
155}
156
157int wxChoice::FindString(const wxString& title) const
158{
159 return [(NSPopUpButton*)m_cocoaNSView indexOfItemWithTitle:
160 wxNSStringWithWxString(title)];
161}
162
163int wxChoice::GetSelection() const
164{
165 return [(NSPopUpButton*)m_cocoaNSView indexOfSelectedItem];
166}
167
168int wxChoice::DoAppend(const wxString& title)
169{
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];
187}
188
189int wxChoice::DoInsert(const wxString& title, int pos)
190{
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];
198}
199
200void wxChoice::DoSetItemClientData(int n, void *data)
201{
202 m_itemsClientData.Item(n) = data;
203}
204
205void* wxChoice::DoGetItemClientData(int n) const
206{
207 return m_itemsClientData.Item(n);
208}
209
210void wxChoice::DoSetItemClientObject(int n, wxClientData *data)
211{
212 m_itemsClientData.Item(n) = data;
213}
214
215wxClientData* wxChoice::DoGetItemClientObject(int n) const
216{
217 return (wxClientData*)m_itemsClientData.Item(n);
218}
219
220void wxChoice::SetSelection(int n)
221{
222 [(NSPopUpButton*)m_cocoaNSView selectItemAtIndex:n];
223}
224