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 unneccesaraly 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 by returned 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);
72 /////////////////////////////////////////////////////////////////////////////
73 // Name: cocoa/NSComboBox.mm
74 // Purpose: wxCocoaNSComboBox
75 // Author: Ryan Norton
77 // Created: 2005/02/16
79 // Copyright: (c) 2003 David Elliott
80 // Licence: wxWidgets licence
81 /////////////////////////////////////////////////////////////////////////////
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 #include "wx/wxprec.h"
93 #include "wx/window.h"
96 #include "wx/cocoa/ObjcPose.h"
97 #include "wx/combobox.h"
99 #import <AppKit/NSComboBox.h>
100 #import <Foundation/NSNotification.h>
101 #import <Foundation/NSString.h>
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
106 WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSComboBox)
108 void wxCocoaNSComboBox::AssociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
112 sm_cocoaHash.insert(wxCocoaNSComboBoxHash::value_type(cocoaNSComboBox,this));
114 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
115 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
116 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
117 [[NSNotificationCenter defaultCenter] addObserver:(id)cocoaNSComboBox selector:@selector(comboBoxSelectionDidChange:) name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
121 void wxCocoaNSComboBox::DisassociateNSComboBox(WX_NSComboBox cocoaNSComboBox)
125 sm_cocoaHash.erase(cocoaNSComboBox);
126 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionDidChangeNotification" object:cocoaNSComboBox];
127 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxSelectionIsChangingNotification" object:cocoaNSComboBox];
128 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillDismissNotification" object:cocoaNSComboBox];
129 [[NSNotificationCenter defaultCenter] removeObserver:(id)cocoaNSComboBox name:@"NSComboBoxWillPopUpNotification" object:cocoaNSComboBox];
133 // ============================================================================
134 // @class wxPoserNSComboBox
135 // ============================================================================
136 @interface wxPoserNSComboBox : NSComboBox
140 - (void)comboBoxSelectionDidChange:(NSNotification *)notification;
141 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification;
142 - (void)comboBoxWillDismiss:(NSNotification *)notification;
143 - (void)comboBoxWillPopUp:(NSNotification *)notification;
144 @end // wxPoserNSComboBox
146 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
147 @implementation wxPoserNSComboBox : NSComboBox
149 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
151 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
152 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
155 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
160 - (void)comboBoxWillDismiss:(NSNotification *)notification
165 - (void)comboBoxWillPopUp:(NSNotification *)notification
170 @end // implementation wxPoserNSComboBox
173 #include "wx/combobox.h"
176 #include "wx/cocoa/autorelease.h"
177 #include "wx/cocoa/string.h"
179 #import <AppKit/NSComboBox.h>
181 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxTextCtrl)
182 BEGIN_EVENT_TABLE(wxComboBox, wxTextCtrl)
184 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
186 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
187 const wxString& value,
190 const wxArrayString& choices,
192 const wxValidator& validator,
193 const wxString& name)
195 wxCArrayString chs(choices);
197 return Create(parent, winid, value, pos, size, chs.GetCount(),
198 chs.GetStrings(), style, validator, name);
201 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
202 const wxString& value,
205 int n, const wxString choices[],
207 const wxValidator& validator,
208 const wxString& name)
210 wxAutoNSAutoreleasePool pool;
211 if(!CreateControl(parent,winid,pos,size,style,validator,name))
214 m_cocoaNSView = NULL;
215 SetNSComboBox([[wxPoserNSComboBox alloc] initWithFrame:MakeDefaultNSRect(size)]);
216 [m_cocoaNSView release];
217 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
218 [GetNSControl() sizeToFit];
220 m_parent->CocoaAddChild(this);
221 SetInitialFrameRect(pos,size);
223 for(int i = 0; i < n; ++i)
224 wxComboBox::DoAppend(choices[i]);
226 [GetNSComboBox() setCompletes:true]; //autocomplete :)
231 wxComboBox::~wxComboBox()
233 DisassociateNSComboBox(GetNSComboBox());
236 void wxComboBox::doWxEvent(int nEvent)
238 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
239 event2.SetInt(GetSelection());
240 event2.SetEventObject(this);
241 event2.SetString(GetStringSelection());
242 GetEventHandler()->ProcessEvent(event2);
244 // For consistency with MSW and GTK, also send a text updated event
245 // After all, the text is updated when a selection is made
246 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
247 TextEvent.SetString( GetStringSelection() );
248 TextEvent.SetEventObject( this );
249 GetEventHandler()->ProcessEvent( TextEvent );
253 void wxComboBox::SetSelection(int nSelection)
255 [GetNSComboBox() selectItemAtIndex:nSelection];
258 wxString wxComboBox::GetStringSelection()
260 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
263 void wxComboBox::Clear()
265 [GetNSComboBox() removeAllItems];
269 void wxComboBox::Delete(int nIndex)
271 [GetNSComboBox() removeItemAtIndex:nIndex];
272 m_Datas.RemoveAt(nIndex);
275 int wxComboBox::GetCount() const
277 return [GetNSComboBox() numberOfItems];
280 wxString wxComboBox::GetString(int nIndex) const
281 { return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]); }
283 void wxComboBox::SetString(int nIndex, const wxString& szString)
285 wxAutoNSAutoreleasePool pool;
286 //FIXME: There appears to be no "set item data" method - maybe
287 //an assignment would work?
288 [GetNSComboBox() removeItemAtIndex:nIndex];
289 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
292 int wxComboBox::FindString(const wxString& szItem) const
293 { return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)]; }
295 int wxComboBox::GetSelection() const
296 { return [GetNSComboBox() indexOfSelectedItem]; }
298 int wxComboBox::DoAppend(const wxString& szItem)
301 wxAutoNSAutoreleasePool pool;
302 [GetNSComboBox() addItemWithObjectValue:wxNSStringWithWxString(szItem)];
303 return [GetNSComboBox() numberOfItems];
306 int wxComboBox::DoInsert(const wxString& szItem, int nIndex)
308 m_Datas.Insert(NULL, nIndex);
309 wxAutoNSAutoreleasePool pool;
310 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szItem) atIndex:nIndex];
314 void wxComboBox::DoSetItemClientData(int nIndex, void* pData)
316 m_Datas[nIndex] = pData;
319 void* wxComboBox::DoGetItemClientData(int nIndex) const
321 return m_Datas[nIndex];
324 void wxComboBox::DoSetItemClientObject(int nIndex, wxClientData* pClientData)
326 m_Datas[nIndex] = (void*) pClientData;
329 wxClientData* wxComboBox::DoGetItemClientObject(int nIndex) const
331 return (wxClientData*) m_Datas[nIndex];
334 #endif //wxUSE_COMBOBOX