1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/combobox.mm
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
12 // #include "wx/wxprec.h"
16 // There is no custom data source because doing so unnecessarily sacrifices
17 // some native autocompletion behavior (we would have to make our own -
18 // the SimpleComboBox sample does so in the developer folder that
19 // comes with OSX). One reason you might want this would be to have
20 // only one array or be able to display numbers returned by an NSNumber
23 // One problem though is that wxCB_SORT isn't implemented...
25 // Also, not sure if it is correctly getting text field events since
26 // I'm using SetNSComboBox instead of SetNSTextField
28 // doWxEvent is really hackish... but since there's only one event...
30 // Ideas for future improvement - other notes:
31 // Combox w/o wxCB_DROPDOWN doesn't seem to be implementable
32 //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.
33 //wxCB_SORT is possible with data source
35 // setIntercellSpacing:/setItemHeight: to autoadjust to number of inserted items?
38 //example of autocompletion from SimpleComboBox Example
39 // ==========================================================
40 // Combo box data source methods
41 // ==========================================================
43 - (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
44 return [genres count];
46 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index {
47 return [genres objectAtIndex:index];
49 - (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
50 return [genres indexOfObject: string];
53 - (NSString *) firstGenreMatchingPrefix:(NSString *)prefix {
54 NSString *string = nil;
55 NSString *lowercasePrefix = [prefix lowercaseString];
56 NSEnumerator *stringEnum = [genres objectEnumerator];
57 while ((string = [stringEnum nextObject])) {
58 if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string;
63 - (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)inputString {
64 // This method is received after each character typed by the user, because we have checked the "completes" flag for genreComboBox in IB.
65 // 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.
66 NSString *candidate = [self firstGenreMatchingPrefix: inputString];
67 return (candidate ? candidate : inputString);
71 /////////////////////////////////////////////////////////////////////////////
72 // Name: cocoa/NSComboBox.mm
73 // Purpose: wxCocoaNSComboBox
74 // Author: Ryan Norton
76 // Created: 2005/02/16
78 // Copyright: (c) 2003 David Elliott
79 // Licence: wxWidgets licence
80 /////////////////////////////////////////////////////////////////////////////
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 #include "wx/wxprec.h"
94 #include "wx/window.h"
97 #include "wx/cocoa/ObjcPose.h"
98 #include "wx/combobox.h"
100 #import <AppKit/NSComboBox.h>
101 #import <Foundation/NSNotification.h>
102 #import <Foundation/NSString.h>
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
107 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
109 void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
113 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
115 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
116 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
117 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
118 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
122 void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
126 sm_cocoaHash.erase(cocoaNSComboBox);
127 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
128 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
130 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
134 // ============================================================================
135 // @class wxPoserNSComboBox
136 // ============================================================================
137 @interface wxPoserNSComboBox : NSComboBox
141 - (void)comboBoxSelectionDidChange:(NSNotification *)notification;
142 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
143 - (void)comboBoxWillDismiss:(NSNotification *)notification;
144 - (void)comboBoxWillPopUp:(NSNotification *)notification;
145 @end // wxPoserNSComboBox
147 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
148 @implementation wxPoserNSComboBox : NSComboBox
150 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
152 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
153 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
156 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
161 - (void)comboBoxWillDismiss:(NSNotification *)notification
166 - (void)comboBoxWillPopUp:(NSNotification *)notification
171 @end // implementation wxPoserNSComboBox
174 #include "wx/combobox.h"
177 #include "wx/cocoa/autorelease.h"
178 #include "wx/cocoa/string.h"
180 #import <AppKit/NSComboBox.h>
182 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
183 BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
185 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
187 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
188 const wxString& value,
191 const wxArrayString& choices,
193 const wxValidator& validator,
194 const wxString& name)
196 wxCArrayString chs(choices);
198 return Create(parent, winid, value, pos, size, chs.GetCount(),
199 chs.GetStrings(), style, validator, name);
202 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
203 const wxString& value,
206 int n, const wxString choices[],
208 const wxValidator& validator,
209 const wxString& name)
211 wxAutoNSAutoreleasePool pool;
212 if(!CreateControl(parent,winid,pos,size,style,validator,name))
215 m_cocoaNSView = NULL;
216 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
217 [m_cocoaNSView release];
218 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
219 [GetNSControl() sizeToFit];
221 m_parent->CocoaAddChild(this);
222 SetInitialFrameRect(pos,size);
224 for(int i = 0; i < n; ++i)
225 wxComboBox::DoAppend(choices[i]);
227 [GetNSComboBox() setCompletes:true]; //autocomplete :)
232 wxComboBox::~wxComboBox()
234 DisassociateNSComboBox(GetNSComboBox());
237 void wxComboBox::doWxEvent(int nEvent)
239 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
240 event2.SetInt(GetSelection());
241 event2.SetEventObject(this);
242 event2.SetString(GetStringSelection());
243 GetEventHandler()->ProcessEvent(event2);
245 // For consistency with MSW and GTK, also send a text updated event
246 // After all, the text is updated when a selection is made
247 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
248 TextEvent.SetString( GetStringSelection() );
249 TextEvent.SetEventObject( this );
250 GetEventHandler()->ProcessEvent( TextEvent );
254 void wxComboBox::SetSelection(int nSelection)
256 [GetNSComboBox() selectItemAtIndex:nSelection];
259 wxString wxComboBox::GetStringSelection()
261 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
264 void wxComboBox::Clear()
266 [GetNSComboBox() removeAllItems];
270 void wxComboBox::Delete(int nIndex)
272 [GetNSComboBox() removeItemAtIndex:nIndex];
273 m_Datas.RemoveAt(nIndex);
276 int wxComboBox::GetCount() const
278 return [GetNSComboBox() numberOfItems];
281 wxString wxComboBox::GetString(int nIndex) const
282 { return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); }
284 void wxComboBox::SetString(int nIndex, const wxString& szString)
286 wxAutoNSAutoreleasePool pool;
287 //FIXME: There appears to be no "set item data" method - maybe
288 //an assignment would work?
289 [GetNSComboBox() removeItemAtIndex:nIndex];
290 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
293 int wxComboBox::FindString(const wxString& szItem) const
294 { return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; }
296 int wxComboBox::GetSelection() const
297 { return [GetNSComboBox() indexOfSelectedItem]; }
299 int wxComboBox::DoAppend(const wxString& szItem)
302 wxAutoNSAutoreleasePool pool;
303 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
304 return [GetNSComboBox() numberOfItems];
307 int wxComboBox::DoInsert(const wxString& szItem, int nIndex)
309 m_Datas.Insert(NULL, nIndex);
310 wxAutoNSAutoreleasePool pool;
311 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
315 void wxComboBox::DoSetItemClientData(int nIndex, void* pData)
317 m_Datas[nIndex] = pData;
320 void* wxComboBox::DoGetItemClientData(int nIndex) const
322 return m_Datas[nIndex];
325 void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData)
327 m_Datas[nIndex] = (void*) pClientData;
330 wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const
332 return (wxClientData*) m_Datas[nIndex];
335 #endif //wxUSE_COMBOBOX