1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/combobox.mm
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
14 // There is no custom data source because doing so unnecessarily sacrifices
15 // some native autocompletion behavior (we would have to make our own -
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
18 // only one array or be able to display numbers returned by an NSNumber
21 // One problem though is that wxCB_SORT isn't implemented...
23 // Also, not sure if it is correctly getting text field events since
24 // I'm using SetNSComboBox instead of SetNSTextField
26 // doWxEvent is really hackish... but since there's only one event...
28 // Ideas for future improvement - other notes:
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
33 // setIntercellSpacing:/setItemHeight: to autoadjust to number of inserted items?
36 //example of autocompletion from SimpleComboBox Example
37 // ==========================================================
38 // Combo box data source methods
39 // ==========================================================
41 - (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
42 return [genres count];
44 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index {
45 return [genres objectAtIndex:index];
47 - (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
48 return [genres indexOfObject: string];
51 - (NSString *) firstGenreMatchingPrefix:(NSString *)prefix {
52 NSString *string = nil;
53 NSString *lowercasePrefix = [prefix lowercaseString];
54 NSEnumerator *stringEnum = [genres objectEnumerator];
55 while ((string = [stringEnum nextObject])) {
56 if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string;
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);
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 #include "wx/wxprec.h"
81 #include "wx/combobox.h"
84 #include "wx/window.h"
89 #import <AppKit/NSComboBox.h>
90 #import <Foundation/NSNotification.h>
91 #import <Foundation/NSString.h>
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
96 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
98 void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
102 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
104 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
105 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
106 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
107 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
111 void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
115 sm_cocoaHash.erase(cocoaNSComboBox);
116 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
117 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
118 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
119 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
123 // ============================================================================
124 // @class wxPoserNSComboBox
125 // ============================================================================
126 @interface wxPoserNSComboBox : NSComboBox
130 - (void)comboBoxSelectionDidChange:(NSNotification *)notification;
131 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
132 - (void)comboBoxWillDismiss:(NSNotification *)notification;
133 - (void)comboBoxWillPopUp:(NSNotification *)notification;
134 @end // wxPoserNSComboBox
136 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
137 @implementation wxPoserNSComboBox : NSComboBox
139 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
141 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
142 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
145 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
150 - (void)comboBoxWillDismiss:(NSNotification *)notification
155 - (void)comboBoxWillPopUp:(NSNotification *)notification
160 @end // implementation wxPoserNSComboBox
162 #include "wx/cocoa/autorelease.h"
163 #include "wx/cocoa/string.h"
165 #import <AppKit/NSComboBox.h>
167 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
168 BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
170 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
172 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
173 const wxString& value,
176 const wxArrayString& choices,
178 const wxValidator& validator,
179 const wxString& name)
181 wxCArrayString chs(choices);
183 return Create(parent, winid, value, pos, size, chs.GetCount(),
184 chs.GetStrings(), style, validator, name);
187 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
188 const wxString& value,
191 int n, const wxString choices[],
193 const wxValidator& validator,
194 const wxString& name)
196 wxAutoNSAutoreleasePool pool;
197 if(!CreateControl(parent,winid,pos,size,style,validator,name))
200 m_cocoaNSView = NULL;
201 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
202 [m_cocoaNSView release];
203 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
204 [GetNSControl() sizeToFit];
206 m_parent->CocoaAddChild(this);
207 SetInitialFrameRect(pos,size);
209 for(int i = 0; i < n; ++i)
210 wxComboBox::DoAppend(choices[i]);
212 [GetNSComboBox() setCompletes:true]; //autocomplete :)
217 wxComboBox::~wxComboBox()
219 DisassociateNSComboBox(GetNSComboBox());
222 void wxComboBox::doWxEvent(int nEvent)
224 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
225 event2.SetInt(GetSelection());
226 event2.SetEventObject(this);
227 event2.SetString(GetStringSelection());
228 GetEventHandler()->ProcessEvent(event2);
230 // For consistency with MSW and GTK, also send a text updated event
231 // After all, the text is updated when a selection is made
232 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
233 TextEvent.SetString( GetStringSelection() );
234 TextEvent.SetEventObject( this );
235 GetEventHandler()->ProcessEvent( TextEvent );
239 void wxComboBox::SetSelection(int nSelection)
241 [GetNSComboBox() selectItemAtIndex:nSelection];
244 wxString wxComboBox::GetStringSelection()
246 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
249 void wxComboBox::Clear()
251 [GetNSComboBox() removeAllItems];
255 void wxComboBox::Delete(unsigned int n)
257 [GetNSComboBox() removeItemAtIndex:n];
261 unsigned int wxComboBox::GetCount() const
263 return (unsigned int)[GetNSComboBox() numberOfItems];
266 wxString wxComboBox::GetString(unsigned int nIndex) const
268 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
271 void wxComboBox::SetString(unsigned int nIndex, const wxString& szString)
273 wxAutoNSAutoreleasePool pool;
274 //FIXME: There appears to be no "set item data" method - maybe
275 //an assignment would work?
276 [GetNSComboBox() removeItemAtIndex:nIndex];
277 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
280 int wxComboBox::FindString(const wxString& szItem, bool bCase) const
282 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
283 return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)];
286 int wxComboBox::GetSelection() const
288 return [GetNSComboBox() indexOfSelectedItem];
291 int wxComboBox::DoAppend(const wxString& szItem)
294 wxAutoNSAutoreleasePool pool;
295 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
296 return [GetNSComboBox() numberOfItems];
299 int wxComboBox::DoInsert(const wxString& szItem, unsigned int nIndex)
301 m_Datas.Insert(NULL, nIndex);
302 wxAutoNSAutoreleasePool pool;
303 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
307 void wxComboBox::DoSetItemClientData(unsigned int nIndex, void* pData)
309 m_Datas[nIndex] = pData;
312 void* wxComboBox::DoGetItemClientData(unsigned int nIndex) const
314 return m_Datas[nIndex];
317 void wxComboBox::DoSetItemClientObject(unsigned int nIndex, wxClientData* pClientData)
319 m_Datas[nIndex] = (void*) pClientData;
322 wxClientData* wxComboBox::DoGetItemClientObject(unsigned int nIndex) const
324 return (wxClientData*) m_Datas[nIndex];
327 #endif //wxUSE_COMBOBOX