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"
82 #include "wx/window.h"
87 #include "wx/cocoa/ObjcPose.h"
88 #include "wx/combobox.h"
90 #import <AppKit/NSComboBox.h>
91 #import <Foundation/NSNotification.h>
92 #import <Foundation/NSString.h>
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
97 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
99 void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
103 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
105 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
106 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
107 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
108 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
112 void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
116 sm_cocoaHash.erase(cocoaNSComboBox);
117 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
118 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
119 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
120 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
124 // ============================================================================
125 // @class wxPoserNSComboBox
126 // ============================================================================
127 @interface wxPoserNSComboBox : NSComboBox
131 - (void)comboBoxSelectionDidChange:(NSNotification *)notification;
132 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
133 - (void)comboBoxWillDismiss:(NSNotification *)notification;
134 - (void)comboBoxWillPopUp:(NSNotification *)notification;
135 @end // wxPoserNSComboBox
137 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
138 @implementation wxPoserNSComboBox : NSComboBox
140 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
142 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
143 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
146 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
151 - (void)comboBoxWillDismiss:(NSNotification *)notification
156 - (void)comboBoxWillPopUp:(NSNotification *)notification
161 @end // implementation wxPoserNSComboBox
163 #include "wx/combobox.h"
165 #include "wx/cocoa/autorelease.h"
166 #include "wx/cocoa/string.h"
168 #import <AppKit/NSComboBox.h>
170 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
171 BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
173 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
175 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
176 const wxString& value,
179 const wxArrayString& choices,
181 const wxValidator& validator,
182 const wxString& name)
184 wxCArrayString chs(choices);
186 return Create(parent, winid, value, pos, size, chs.GetCount(),
187 chs.GetStrings(), style, validator, name);
190 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
191 const wxString& value,
194 int n, const wxString choices[],
196 const wxValidator& validator,
197 const wxString& name)
199 wxAutoNSAutoreleasePool pool;
200 if(!CreateControl(parent,winid,pos,size,style,validator,name))
203 m_cocoaNSView = NULL;
204 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
205 [m_cocoaNSView release];
206 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
207 [GetNSControl() sizeToFit];
209 m_parent->CocoaAddChild(this);
210 SetInitialFrameRect(pos,size);
212 for(int i = 0; i < n; ++i)
213 wxComboBox::DoAppend(choices[i]);
215 [GetNSComboBox() setCompletes:true]; //autocomplete :)
220 wxComboBox::~wxComboBox()
222 DisassociateNSComboBox(GetNSComboBox());
225 void wxComboBox::doWxEvent(int nEvent)
227 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
228 event2.SetInt(GetSelection());
229 event2.SetEventObject(this);
230 event2.SetString(GetStringSelection());
231 GetEventHandler()->ProcessEvent(event2);
233 // For consistency with MSW and GTK, also send a text updated event
234 // After all, the text is updated when a selection is made
235 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
236 TextEvent.SetString( GetStringSelection() );
237 TextEvent.SetEventObject( this );
238 GetEventHandler()->ProcessEvent( TextEvent );
242 void wxComboBox::SetSelection(int nSelection)
244 [GetNSComboBox() selectItemAtIndex:nSelection];
247 wxString wxComboBox::GetStringSelection()
249 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
252 void wxComboBox::Clear()
254 [GetNSComboBox() removeAllItems];
258 void wxComboBox::Delete(unsigned int n)
260 [GetNSComboBox() removeItemAtIndex:n];
264 unsigned int wxComboBox::GetCount() const
266 return (unsigned int)[GetNSComboBox() numberOfItems];
269 wxString wxComboBox::GetString(unsigned int nIndex) const
271 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
274 void wxComboBox::SetString(unsigned int nIndex, const wxString& szString)
276 wxAutoNSAutoreleasePool pool;
277 //FIXME: There appears to be no "set item data" method - maybe
278 //an assignment would work?
279 [GetNSComboBox() removeItemAtIndex:nIndex];
280 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
283 int wxComboBox::FindString(const wxString& szItem, bool bCase) const
285 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
286 return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)];
289 int wxComboBox::GetSelection() const
291 return [GetNSComboBox() indexOfSelectedItem];
294 int wxComboBox::DoAppend(const wxString& szItem)
297 wxAutoNSAutoreleasePool pool;
298 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
299 return [GetNSComboBox() numberOfItems];
302 int wxComboBox::DoInsert(const wxString& szItem, unsigned int nIndex)
304 m_Datas.Insert(NULL, nIndex);
305 wxAutoNSAutoreleasePool pool;
306 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
310 void wxComboBox::DoSetItemClientData(unsigned int nIndex, void* pData)
312 m_Datas[nIndex] = pData;
315 void* wxComboBox::DoGetItemClientData(unsigned int nIndex) const
317 return m_Datas[nIndex];
320 void wxComboBox::DoSetItemClientObject(unsigned int nIndex, wxClientData* pClientData)
322 m_Datas[nIndex] = (void*) pClientData;
325 wxClientData* wxComboBox::DoGetItemClientObject(unsigned int nIndex) const
327 return (wxClientData*) m_Datas[nIndex];
330 #endif //wxUSE_COMBOBOX