]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/combobox.mm
fix evtloop.h header dependency
[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
e7e1ad7d
DE
83#include "wx/cocoa/objc/objc_uniquifying.h"
84
8f248607
RN
85#ifndef WX_PRECOMP
86 #include "wx/window.h"
e4db172a 87 #include "wx/log.h"
670f9935 88 #include "wx/app.h"
8f248607
RN
89#endif // WX_PRECOMP
90
8f248607
RN
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
e7e1ad7d 137WX_DECLARE_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
8f248607
RN
138
139//WX_IMPLEMENT_POSER(wxPoserNSComboBox);
140@implementation wxPoserNSComboBox : NSComboBox
141
142- (void)comboBoxSelectionDidChange:(NSNotification *)notification
143{
144 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
145 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
146}
147
148- (void)comboBoxSelectionIsChanging:(NSNotification *)notification
149{
150 //...
151}
152
153- (void)comboBoxWillDismiss:(NSNotification *)notification
154{
155 //...
156}
157
158- (void)comboBoxWillPopUp:(NSNotification *)notification
159{
160 //...
161}
162
163@end // implementation wxPoserNSComboBox
e7e1ad7d 164WX_IMPLEMENT_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
8f248607 165
421a8431
DE
166#include "wx/cocoa/autorelease.h"
167#include "wx/cocoa/string.h"
168
169#import <AppKit/NSComboBox.h>
170
e960ba59
DE
171IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
172BEGIN_EVENT_TABLE(wxComboBox, wxControl)
421a8431 173END_EVENT_TABLE()
8f248607 174WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
e960ba59 175WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSTextField,NSControl,NSView)
421a8431 176
584ad2a3
MB
177bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
178 const wxString& value,
179 const wxPoint& pos,
180 const wxSize& size,
181 const wxArrayString& choices,
182 long style,
183 const wxValidator& validator,
184 const wxString& name)
185{
186 wxCArrayString chs(choices);
187
188 return Create(parent, winid, value, pos, size, chs.GetCount(),
189 chs.GetStrings(), style, validator, name);
190}
191
421a8431
DE
192bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
193 const wxString& value,
194 const wxPoint& pos,
195 const wxSize& size,
196 int n, const wxString choices[],
197 long style,
198 const wxValidator& validator,
199 const wxString& name)
200{
201 wxAutoNSAutoreleasePool pool;
202 if(!CreateControl(parent,winid,pos,size,style,validator,name))
203 return false;
11e62fe6 204
421a8431 205 m_cocoaNSView = NULL;
e7e1ad7d 206 SetNSComboBox([[WX_GET_OBJC_CLASS(wxPoserNSComboBox) alloc] initWithFrame:MakeDefaultNSRect(size)]);
421a8431
DE
207 [m_cocoaNSView release];
208 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
209 [GetNSControl() sizeToFit];
210 if(m_parent)
211 m_parent->CocoaAddChild(this);
8d656ea9
DE
212 SetInitialFrameRect(pos,size);
213
a236aa20 214 wxComboBox::Append(n, choices);
11e62fe6 215
8f248607 216 [GetNSComboBox() setCompletes:true]; //autocomplete :)
11e62fe6 217
421a8431
DE
218 return true;
219}
220
221wxComboBox::~wxComboBox()
222{
8f248607 223 DisassociateNSComboBox(GetNSComboBox());
421a8431
DE
224}
225
8f248607 226void wxComboBox::doWxEvent(int nEvent)
421a8431 227{
8f248607
RN
228 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
229 event2.SetInt(GetSelection());
230 event2.SetEventObject(this);
231 event2.SetString(GetStringSelection());
937013e0 232 HandleWindowEvent(event2);
8f248607
RN
233
234 // For consistency with MSW and GTK, also send a text updated event
235 // After all, the text is updated when a selection is made
236 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
237 TextEvent.SetString( GetStringSelection() );
238 TextEvent.SetEventObject( this );
937013e0 239 HandleWindowEvent( TextEvent );
8f248607
RN
240}
241
242
243void wxComboBox::SetSelection(int nSelection)
244{
245 [GetNSComboBox() selectItemAtIndex:nSelection];
421a8431
DE
246}
247
248wxString wxComboBox::GetStringSelection()
249{
8f248607 250 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
421a8431
DE
251}
252
a236aa20 253void wxComboBox::DoClear()
421a8431 254{
8f248607
RN
255 [GetNSComboBox() removeAllItems];
256 m_Datas.Clear();
421a8431
DE
257}
258
a236aa20 259void wxComboBox::DoDeleteOneItem(unsigned int n)
421a8431 260{
aa61d352
VZ
261 [GetNSComboBox() removeItemAtIndex:n];
262 m_Datas.RemoveAt(n);
421a8431
DE
263}
264
aa61d352 265unsigned int wxComboBox::GetCount() const
421a8431 266{
aa61d352 267 return (unsigned int)[GetNSComboBox() numberOfItems];
421a8431
DE
268}
269
aa61d352 270wxString wxComboBox::GetString(unsigned int nIndex) const
11e62fe6
WS
271{
272 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
273}
421a8431 274
aa61d352 275void wxComboBox::SetString(unsigned int nIndex, const wxString& szString)
11e62fe6 276{
8f248607
RN
277 wxAutoNSAutoreleasePool pool;
278 //FIXME: There appears to be no "set item data" method - maybe
279 //an assignment would work?
280 [GetNSComboBox() removeItemAtIndex:nIndex];
11e62fe6 281 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
421a8431
DE
282}
283
11e62fe6
WS
284int wxComboBox::FindString(const wxString& szItem, bool bCase) const
285{
286 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
287 return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)];
288}
421a8431
DE
289
290int wxComboBox::GetSelection() const
11e62fe6
WS
291{
292 return [GetNSComboBox() indexOfSelectedItem];
293}
421a8431 294
a236aa20
VZ
295int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
296 unsigned int pos,
297 void **clientData,
298 wxClientDataType type)
421a8431 299{
8f248607 300 wxAutoNSAutoreleasePool pool;
a236aa20
VZ
301 const unsigned int numITems = items.GetCount();
302 for ( unsigned int i = 0; i < numITems; ++i, ++pos )
303 {
304 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(items[i]) atIndex:(pos)];
305 m_Datas.Insert(NULL, pos);
306 AssignNewItemClientData(pos, clientData, i, type);
307 }
308 return pos - 1;
421a8431
DE
309}
310
aa61d352 311void wxComboBox::DoSetItemClientData(unsigned int nIndex, void* pData)
421a8431 312{
8f248607 313 m_Datas[nIndex] = pData;
421a8431
DE
314}
315
aa61d352 316void* wxComboBox::DoGetItemClientData(unsigned int nIndex) const
421a8431 317{
8f248607 318 return m_Datas[nIndex];
421a8431
DE
319}
320
e960ba59
DE
321/////////////////////////////////////////////////////////////////////////////
322// wxTextEntry virtual implementations:
323
324void wxComboBox::WriteText(wxString const&)
325{
326}
327
328wxString wxComboBox::GetValue() const
329{
330 wxAutoNSAutoreleasePool pool;
331 return wxStringWithNSString([GetNSTextField() stringValue]);
332}
333
334void wxComboBox::Remove(long, long)
335{
336}
337
338void wxComboBox::Cut()
339{
340}
341
342void wxComboBox::Copy()
343{
344}
345
346void wxComboBox::Paste()
347{
348}
349
350void wxComboBox::Undo()
351{
352}
353
354void wxComboBox::Redo()
355{
356}
357
358bool wxComboBox::CanUndo() const
359{
360 return false;
361}
362
363bool wxComboBox::CanRedo() const
364{
365 return false;
366}
367
368void wxComboBox::SetInsertionPoint(long)
369{
370}
371
372long wxComboBox::GetInsertionPoint() const
373{
374 return 0;
375}
376
377wxTextPos wxComboBox::GetLastPosition() const
378{
379 // working - returns the size of the wxString
380 return (long)(GetValue().Len());
381}
382
383void wxComboBox::SetSelection(long, long)
384{
385}
386
387void wxComboBox::GetSelection(long*, long*) const
388{
389}
390
391bool wxComboBox::IsEditable() const
392{
393 return [GetNSTextField() isEditable];
394}
395
396void wxComboBox::SetEditable(bool editable)
397{
398 // first ensure that the current value is stored (in case the user had not finished editing
399 // before SetEditable(FALSE) was called)
400 DoSetValue(GetValue(),1);
401
402 [GetNSTextField() setEditable: editable];
403
404 // forces the focus on the textctrl to be lost - while focus is still maintained
405 // after SetEditable(FALSE) the user may still edit the control
406 // (might not the best way to do this..)
407 [GetNSTextField() abortEditing];
408}
409
a236aa20 410#endif // wxUSE_COMBOBOX