]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/combobox.mm
fixed a signed/unsigned comparison warning introudced by last warning fix...
[wxWidgets.git] / src / cocoa / combobox.mm
CommitLineData
421a8431
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/combobox.mm
3// Purpose: wxComboBox
8f248607 4// Author: Ryan Norton
421a8431 5// Modified by:
8f248607 6// Created: 2005/02/16
a6660cbb 7// RCS-ID: $Id$
421a8431 8// Copyright: (c) 2003 David Elliott
065e208e 9// Licence: wxWidgets licence
421a8431
DE
10/////////////////////////////////////////////////////////////////////////////
11
acd3dca8 12// #include "wx/wxprec.h"
2ee09b55 13
8f248607
RN
14//
15// Impl notes:
3103e8a9 16// There is no custom data source because doing so unnecessarily sacrifices
8f248607
RN
17// some native autocompletion behavior (we would have to make our own -
18// the SimpleComboBox sample does so in the developer folder that
19// comes with OSX). One reason you might want this would be to have
3103e8a9 20// only one array or be able to display numbers returned by an NSNumber
8f248607
RN
21// from the methods.
22//
23// One problem though is that wxCB_SORT isn't implemented...
24//
25// Also, not sure if it is correctly getting text field events since
26// I'm using SetNSComboBox instead of SetNSTextField
27//
28// doWxEvent is really hackish... but since there's only one event...
29//
30// Ideas for future improvement - other notes:
31// Combox w/o wxCB_DROPDOWN doesn't seem to be implementable
32//wxCB_READONLY Same as wxCB_DROPDOWN but only the strings specified as the combobox choices can be selected, it is impossible to select (even from a program) a string which is not in the choices list.
33//wxCB_SORT is possible with data source
34//
35// setIntercellSpacing:/setItemHeight: to autoadjust to number of inserted items?
36//
37/*
38 //example of autocompletion from SimpleComboBox Example
39 // ==========================================================
40// Combo box data source methods
41// ==========================================================
42
43- (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
44 return [genres count];
45}
46- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index {
47 return [genres objectAtIndex:index];
48}
49- (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
50 return [genres indexOfObject: string];
51}
52
53- (NSString *) firstGenreMatchingPrefix:(NSString *)prefix {
54 NSString *string = nil;
55 NSString *lowercasePrefix = [prefix lowercaseString];
56 NSEnumerator *stringEnum = [genres objectEnumerator];
57 while ((string = [stringEnum nextObject])) {
58 if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string;
59 }
60 return nil;
61}
62
63- (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)inputString {
64 // This method is received after each character typed by the user, because we have checked the "completes" flag for genreComboBox in IB.
65 // Given the inputString the user has typed, see if we can find a genre with the prefix, and return it as the suggested complete string.
66 NSString *candidate = [self firstGenreMatchingPrefix: inputString];
67 return (candidate ? candidate : inputString);
68}
69*/
2ee09b55 70
8f248607
RN
71/////////////////////////////////////////////////////////////////////////////
72// Name: cocoa/NSComboBox.mm
73// Purpose: wxCocoaNSComboBox
74// Author: Ryan Norton
75// Modified by:
76// Created: 2005/02/16
77// RCS-ID: $Id:
78// Copyright: (c) 2003 David Elliott
79// Licence: wxWidgets licence
80/////////////////////////////////////////////////////////////////////////////
81
82// ============================================================================
83// declarations
84// ============================================================================
85
86// ----------------------------------------------------------------------------
87// headers
88// ----------------------------------------------------------------------------
89
90#include "wx/wxprec.h"
061896d1
DE
91#if wxUSE_COMBOBOX
92
8f248607
RN
93#ifndef WX_PRECOMP
94 #include "wx/window.h"
95#endif // WX_PRECOMP
96
97#include "wx/cocoa/ObjcPose.h"
98#include "wx/combobox.h"
99
100#import <AppKit/NSComboBox.h>
101#import <Foundation/NSNotification.h>
102#import <Foundation/NSString.h>
103
104// ----------------------------------------------------------------------------
105// globals
106// ----------------------------------------------------------------------------
107WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
108
109void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
110{
111 if(cocoaNSComboBox)
112 {
113 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
114
115 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
116 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
117 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
118 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
119 }
120}
121
122void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
123{
124 if(cocoaNSComboBox)
125 {
126 sm_cocoaHash.erase(cocoaNSComboBox);
127 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
128 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
131 }
132}
133
134// ============================================================================
135// @class wxPoserNSComboBox
136// ============================================================================
137@interface wxPoserNSComboBox : NSComboBox
138{
139}
140
141- (void)comboBoxSelectionDidChange:(NSNotification *)notification;
142- (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
143- (void)comboBoxWillDismiss:(NSNotification *)notification;
144- (void)comboBoxWillPopUp:(NSNotification *)notification;
145@end // wxPoserNSComboBox
146
147//WX_IMPLEMENT_POSER(wxPoserNSComboBox);
148@implementation wxPoserNSComboBox : NSComboBox
149
150- (void)comboBoxSelectionDidChange:(NSNotification *)notification
151{
152 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
153 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
154}
155
156- (void)comboBoxSelectionIsChanging:(NSNotification *)notification
157{
158 //...
159}
160
161- (void)comboBoxWillDismiss:(NSNotification *)notification
162{
163 //...
164}
165
166- (void)comboBoxWillPopUp:(NSNotification *)notification
167{
168 //...
169}
170
171@end // implementation wxPoserNSComboBox
172
421a8431
DE
173#include "wx/app.h"
174#include "wx/combobox.h"
175#include "wx/log.h"
176
177#include "wx/cocoa/autorelease.h"
178#include "wx/cocoa/string.h"
179
180#import <AppKit/NSComboBox.h>
181
182IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
183BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
184END_EVENT_TABLE()
8f248607 185WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
421a8431 186
584ad2a3
MB
187bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
188 const wxString& value,
189 const wxPoint& pos,
190 const wxSize& size,
191 const wxArrayString& choices,
192 long style,
193 const wxValidator& validator,
194 const wxString& name)
195{
196 wxCArrayString chs(choices);
197
198 return Create(parent, winid, value, pos, size, chs.GetCount(),
199 chs.GetStrings(), style, validator, name);
200}
201
421a8431
DE
202bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
203 const wxString& value,
204 const wxPoint& pos,
205 const wxSize& size,
206 int n, const wxString choices[],
207 long style,
208 const wxValidator& validator,
209 const wxString& name)
210{
211 wxAutoNSAutoreleasePool pool;
212 if(!CreateControl(parent,winid,pos,size,style,validator,name))
213 return false;
8f248607 214
421a8431 215 m_cocoaNSView = NULL;
8f248607 216 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
421a8431
DE
217 [m_cocoaNSView release];
218 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
219 [GetNSControl() sizeToFit];
220 if(m_parent)
221 m_parent->CocoaAddChild(this);
8d656ea9
DE
222 SetInitialFrameRect(pos,size);
223
8f248607
RN
224 for(int i = 0; i < n; ++i)
225 wxComboBox::DoAppend(choices[i]);
226
227 [GetNSComboBox() setCompletes:true]; //autocomplete :)
228
421a8431
DE
229 return true;
230}
231
232wxComboBox::~wxComboBox()
233{
8f248607 234 DisassociateNSComboBox(GetNSComboBox());
421a8431
DE
235}
236
8f248607 237void wxComboBox::doWxEvent(int nEvent)
421a8431 238{
8f248607
RN
239 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
240 event2.SetInt(GetSelection());
241 event2.SetEventObject(this);
242 event2.SetString(GetStringSelection());
243 GetEventHandler()->ProcessEvent(event2);
244
245 // For consistency with MSW and GTK, also send a text updated event
246 // After all, the text is updated when a selection is made
247 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
248 TextEvent.SetString( GetStringSelection() );
249 TextEvent.SetEventObject( this );
250 GetEventHandler()->ProcessEvent( TextEvent );
251}
252
253
254void wxComboBox::SetSelection(int nSelection)
255{
256 [GetNSComboBox() selectItemAtIndex:nSelection];
421a8431
DE
257}
258
259wxString wxComboBox::GetStringSelection()
260{
8f248607 261 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
421a8431
DE
262}
263
421a8431
DE
264void wxComboBox::Clear()
265{
8f248607
RN
266 [GetNSComboBox() removeAllItems];
267 m_Datas.Clear();
421a8431
DE
268}
269
8f248607 270void wxComboBox::Delete(int nIndex)
421a8431 271{
8f248607
RN
272 [GetNSComboBox() removeItemAtIndex:nIndex];
273 m_Datas.RemoveAt(nIndex);
421a8431
DE
274}
275
276int wxComboBox::GetCount() const
277{
8f248607 278 return [GetNSComboBox() numberOfItems];
421a8431
DE
279}
280
8f248607
RN
281wxString wxComboBox::GetString(int nIndex) const
282{ return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); }
421a8431 283
8f248607
RN
284void wxComboBox::SetString(int nIndex, const wxString& szString)
285{
286 wxAutoNSAutoreleasePool pool;
287 //FIXME: There appears to be no "set item data" method - maybe
288 //an assignment would work?
289 [GetNSComboBox() removeItemAtIndex:nIndex];
290 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
421a8431
DE
291}
292
8f248607
RN
293int wxComboBox::FindString(const wxString& szItem) const
294{ return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; }
421a8431
DE
295
296int wxComboBox::GetSelection() const
8f248607 297{ return [GetNSComboBox() indexOfSelectedItem]; }
421a8431 298
8f248607 299int wxComboBox::DoAppend(const wxString& szItem)
421a8431 300{
8f248607
RN
301 m_Datas.Add(NULL);
302 wxAutoNSAutoreleasePool pool;
303 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
304 return [GetNSComboBox() numberOfItems];
421a8431
DE
305}
306
8f248607 307int wxComboBox::DoInsert(const wxString& szItem, int nIndex)
421a8431 308{
8f248607
RN
309 m_Datas.Insert(NULL, nIndex);
310 wxAutoNSAutoreleasePool pool;
311 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
312 return nIndex;
421a8431
DE
313}
314
8f248607 315void wxComboBox::DoSetItemClientData(int nIndex, void* pData)
421a8431 316{
8f248607 317 m_Datas[nIndex] = pData;
421a8431
DE
318}
319
8f248607 320void* wxComboBox::DoGetItemClientData(int nIndex) const
421a8431 321{
8f248607 322 return m_Datas[nIndex];
421a8431
DE
323}
324
8f248607 325void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData)
421a8431 326{
8f248607 327 m_Datas[nIndex] = (void*) pClientData;
421a8431
DE
328}
329
8f248607 330wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const
421a8431 331{
8f248607 332 return (wxClientData*) m_Datas[nIndex];
421a8431
DE
333}
334
2ee09b55 335#endif //wxUSE_COMBOBOX