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