1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/combobox.mm
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows 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"
83 #include "wx/cocoa/objc/objc_uniquifying.h"
86 #include "wx/window.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
137 WX_DECLARE_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
139 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
140 @implementation wxPoserNSComboBox : NSComboBox
142 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
144 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
145 win->doWxEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
148 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
153 - (void)comboBoxWillDismiss:(NSNotification *)notification
158 - (void)comboBoxWillPopUp:(NSNotification *)notification
163 @end // implementation wxPoserNSComboBox
164 WX_IMPLEMENT_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
166 #include "wx/cocoa/autorelease.h"
167 #include "wx/cocoa/string.h"
169 #import <AppKit/NSComboBox.h>
171 IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
172 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
174 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
175 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSTextField,NSControl,NSView)
177 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
178 const wxString& value,
181 const wxArrayString& choices,
183 const wxValidator& validator,
184 const wxString& name)
186 wxCArrayString chs(choices);
188 return Create(parent, winid, value, pos, size, chs.GetCount(),
189 chs.GetStrings(), style, validator, name);
192 bool wxComboBox::Create(wxWindow *parent, wxWindowID winid,
193 const wxString& value,
196 int n, const wxString choices[],
198 const wxValidator& validator,
199 const wxString& name)
201 wxAutoNSAutoreleasePool pool;
202 if(!CreateControl(parent,winid,pos,size,style,validator,name))
205 m_cocoaNSView = NULL;
206 SetNSComboBox([[WX_GET_OBJC_CLASS(wxPoserNSComboBox) alloc] initWithFrame:MakeDefaultNSRect(size)]);
207 [m_cocoaNSView release];
208 [GetNSTextField() setStringValue:wxNSStringWithWxString(value.c_str())];
209 [GetNSControl() sizeToFit];
211 m_parent->CocoaAddChild(this);
212 SetInitialFrameRect(pos,size);
214 wxComboBox::Append(n, choices);
216 [GetNSComboBox() setCompletes:true]; //autocomplete :)
221 wxComboBox::~wxComboBox()
223 DisassociateNSComboBox(GetNSComboBox());
226 void wxComboBox::doWxEvent(int nEvent)
228 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId() );
229 event2.SetInt(GetSelection());
230 event2.SetEventObject(this);
231 event2.SetString(GetStringSelection());
232 HandleWindowEvent(event2);
234 // For consistency with MSW and GTK, also send a text updated event
235 // After all, the text is updated when a selection is made
236 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
237 TextEvent.SetString( GetStringSelection() );
238 TextEvent.SetEventObject( this );
239 HandleWindowEvent( TextEvent );
243 void wxComboBox::SetSelection(int nSelection)
245 [GetNSComboBox() selectItemAtIndex:nSelection];
248 wxString wxComboBox::GetStringSelection()
250 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
253 void wxComboBox::DoClear()
255 [GetNSComboBox() removeAllItems];
259 void wxComboBox::DoDeleteOneItem(unsigned int n)
261 [GetNSComboBox() removeItemAtIndex:n];
265 unsigned int wxComboBox::GetCount() const
267 return (unsigned int)[GetNSComboBox() numberOfItems];
270 wxString wxComboBox::GetString(unsigned int nIndex) const
272 return wxStringWithNSString([GetNSComboBox() itemObjectValueAtIndex:nIndex]);
275 void wxComboBox::SetString(unsigned int nIndex, const wxString& szString)
277 wxAutoNSAutoreleasePool pool;
278 //FIXME: There appears to be no "set item data" method - maybe
279 //an assignment would work?
280 [GetNSComboBox() removeItemAtIndex:nIndex];
281 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(szString) atIndex:nIndex];
284 int wxComboBox::FindString(const wxString& szItem, bool bCase) const
286 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
287 return [GetNSComboBox() indexOfItemWithObjectValue:wxNSStringWithWxString(szItem)];
290 int wxComboBox::GetSelection() const
292 return [GetNSComboBox() indexOfSelectedItem];
295 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
298 wxClientDataType type)
300 wxAutoNSAutoreleasePool pool;
301 const unsigned int numITems = items.GetCount();
302 for ( unsigned int i = 0; i < numITems; ++i, ++pos )
304 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(items[i]) atIndex:(pos)];
305 m_Datas.Insert(NULL, pos);
306 AssignNewItemClientData(pos, clientData, i, type);
311 void wxComboBox::DoSetItemClientData(unsigned int nIndex, void* pData)
313 m_Datas[nIndex] = pData;
316 void* wxComboBox::DoGetItemClientData(unsigned int nIndex) const
318 return m_Datas[nIndex];
321 /////////////////////////////////////////////////////////////////////////////
322 // wxTextEntry virtual implementations:
324 void wxComboBox::WriteText(wxString const&)
328 wxString wxComboBox::GetValue() const
330 wxAutoNSAutoreleasePool pool;
331 return wxStringWithNSString([GetNSTextField() stringValue]);
334 void wxComboBox::Remove(long, long)
338 void wxComboBox::Cut()
342 void wxComboBox::Copy()
346 void wxComboBox::Paste()
350 void wxComboBox::Undo()
354 void wxComboBox::Redo()
358 bool wxComboBox::CanUndo() const
363 bool wxComboBox::CanRedo() const
368 void wxComboBox::SetInsertionPoint(long)
372 long wxComboBox::GetInsertionPoint() const
377 wxTextPos wxComboBox::GetLastPosition() const
379 // working - returns the size of the wxString
380 return (long)(GetValue().Len());
383 void wxComboBox::SetSelection(long, long)
387 void wxComboBox::GetSelection(long*, long*) const
391 bool wxComboBox::IsEditable() const
393 return [GetNSTextField() isEditable];
396 void wxComboBox::SetEditable(bool editable)
398 // first ensure that the current value is stored (in case the user had not finished editing
399 // before SetEditable(FALSE) was called)
400 DoSetValue(GetValue(),1);
402 [GetNSTextField() setEditable: editable];
404 // forces the focus on the textctrl to be lost - while focus is still maintained
405 // after SetEditable(FALSE) the user may still edit the control
406 // (might not the best way to do this..)
407 [GetNSTextField() abortEditing];
410 #endif // wxUSE_COMBOBOX