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 #include "wx/cocoa/ObjcPose.h"
91 #import <AppKit/NSComboBox.h>
92 #import <Foundation/NSNotification.h>
93 #import <Foundation/NSString.h>
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
98 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
100 void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
104 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
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];
113 void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
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];
125 // ============================================================================
126 // @class wxPoserNSComboBox
127 // ============================================================================
128 @interface wxPoserNSComboBox : NSComboBox
132 - (void)comboBoxSelectionDidChange:(NSNotification *)notification;
133 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
134 - (void)comboBoxWillDismiss:(NSNotification *)notification;
135 - (void)comboBoxWillPopUp:(NSNotification *)notification;
136 @end // wxPoserNSComboBox
138 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
139 @implementation wxPoserNSComboBox : NSComboBox
141 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
143 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
144 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
147 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
152 - (void)comboBoxWillDismiss:(NSNotification *)notification
157 - (void)comboBoxWillPopUp:(NSNotification *)notification
162 @end // implementation wxPoserNSComboBox
164 #include "wx/cocoa/autorelease.h"
165 #include "wx/cocoa/string.h"
167 #import <AppKit/NSComboBox.h>
169 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
170 BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
172 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
174 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
175 const wxString& value,
178 const wxArrayString& choices,
180 const wxValidator& validator,
181 const wxString& name)
183 wxCArrayString chs(choices);
185 return Create(parent, winid, value, pos, size, chs.GetCount(),
186 chs.GetStrings(), style, validator, name);
189 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
190 const wxString& value,
193 int n, const wxString choices[],
195 const wxValidator& validator,
196 const wxString& name)
198 wxAutoNSAutoreleasePool pool;
199 if(!CreateControl(parent,winid,pos,size,style,validator,name))
202 m_cocoaNSView = NULL;
203 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
204 [m_cocoaNSView release];
205 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
206 [GetNSControl() sizeToFit];
208 m_parent->CocoaAddChild(this);
209 SetInitialFrameRect(pos,size);
211 for(int i = 0; i < n; ++i)
212 wxComboBox::DoAppend(choices[i]);
214 [GetNSComboBox() setCompletes:true]; //autocomplete :)
219 wxComboBox::~wxComboBox()
221 DisassociateNSComboBox(GetNSComboBox());
224 void wxComboBox::doWxEvent(int nEvent)
226 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
227 event2.SetInt(GetSelection());
228 event2.SetEventObject(this);
229 event2.SetString(GetStringSelection());
230 GetEventHandler()->ProcessEvent(event2);
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 );
241 void wxComboBox::SetSelection(int nSelection)
243 [GetNSComboBox() selectItemAtIndex:nSelection];
246 wxString wxComboBox::GetStringSelection()
248 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
251 void wxComboBox::Clear()
253 [GetNSComboBox() removeAllItems];
257 void wxComboBox::Delete(unsigned int n)
259 [GetNSComboBox() removeItemAtIndex:n];
263 unsigned int wxComboBox::GetCount() const
265 return (unsigned int)[GetNSComboBox() numberOfItems];
268 wxString wxComboBox::GetString(unsigned int nIndex) const
270 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
273 void wxComboBox::SetString(unsigned int nIndex, const wxString& szString)
275 wxAutoNSAutoreleasePool pool;
276 //FIXME: There appears to be no "set item data" method - maybe
277 //an assignment would work?
278 [GetNSComboBox() removeItemAtIndex:nIndex];
279 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
282 int wxComboBox::FindString(const wxString& szItem, bool bCase) const
284 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
285 return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)];
288 int wxComboBox::GetSelection() const
290 return [GetNSComboBox() indexOfSelectedItem];
293 int wxComboBox::DoAppend(const wxString& szItem)
296 wxAutoNSAutoreleasePool pool;
297 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
298 return [GetNSComboBox() numberOfItems];
301 int wxComboBox::DoInsert(const wxString& szItem, unsigned int nIndex)
303 m_Datas.Insert(NULL, nIndex);
304 wxAutoNSAutoreleasePool pool;
305 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
309 void wxComboBox::DoSetItemClientData(unsigned int nIndex, void* pData)
311 m_Datas[nIndex] = pData;
314 void* wxComboBox::DoGetItemClientData(unsigned int nIndex) const
316 return m_Datas[nIndex];
319 void wxComboBox::DoSetItemClientObject(unsigned int nIndex, wxClientData* pClientData)
321 m_Datas[nIndex] = (void*) pClientData;
324 wxClientData* wxComboBox::DoGetItemClientObject(unsigned int nIndex) const
326 return (wxClientData*) m_Datas[nIndex];
329 #endif //wxUSE_COMBOBOX