1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/combobox.mm
7 // Copyright: (c) 2003 David Elliott
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 // There is no custom data source because doing so unnecessarily sacrifices
14 // some native autocompletion behaviour (we would have to make our own -
15 // the SimpleComboBox sample does so in the developer folder that
16 // comes with OSX). One reason you might want this would be to have
17 // only one array or be able to display numbers returned by an NSNumber
20 // One problem though is that wxCB_SORT isn't implemented...
22 // Also, not sure if it is correctly getting text field events since
23 // I'm using SetNSComboBox instead of SetNSTextField
25 // doWxEvent is really hackish... but since there's only one event...
27 // Ideas for future improvement - other notes:
28 // Combox w/o wxCB_DROPDOWN doesn't seem to be implementable
29 //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.
30 //wxCB_SORT is possible with data source
32 // setIntercellSpacing:/setItemHeight: to autoadjust to number of inserted items?
35 //example of autocompletion from SimpleComboBox Example
36 // ==========================================================
37 // Combo box data source methods
38 // ==========================================================
40 - (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
41 return [genres count];
43 - (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index {
44 return [genres objectAtIndex:index];
46 - (unsigned int)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)string {
47 return [genres indexOfObject: string];
50 - (NSString *) firstGenreMatchingPrefix:(NSString *)prefix {
51 NSString *string = nil;
52 NSString *lowercasePrefix = [prefix lowercaseString];
53 NSEnumerator *stringEnum = [genres objectEnumerator];
54 while ((string = [stringEnum nextObject])) {
55 if ([[string lowercaseString] hasPrefix: lowercasePrefix]) return string;
60 - (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)inputString {
61 // This method is received after each character typed by the user, because we have checked the "completes" flag for genreComboBox in IB.
62 // 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.
63 NSString *candidate = [self firstGenreMatchingPrefix: inputString];
64 return (candidate ? candidate : inputString);
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 #include "wx/wxprec.h"
80 #include "wx/combobox.h"
82 #include "wx/cocoa/objc/objc_uniquifying.h"
85 #include "wx/window.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
136 WX_DECLARE_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
138 //WX_IMPLEMENT_POSER(wxPoserNSComboBox);
139 @implementation wxPoserNSComboBox : NSComboBox
141 - (void)comboBoxSelectionDidChange:(NSNotification *)notification
143 wxCocoaNSComboBox *win = wxCocoaNSComboBox::GetFromCocoa(self);
144 win->doWxEvent(wxEVT_COMBOBOX);
147 - (void)comboBoxSelectionIsChanging:(NSNotification *)notification
152 - (void)comboBoxWillDismiss:(NSNotification *)notification
157 - (void)comboBoxWillPopUp:(NSNotification *)notification
162 @end // implementation wxPoserNSComboBox
163 WX_IMPLEMENT_GET_OBJC_CLASS(wxPoserNSComboBox,NSComboBox)
165 #include "wx/cocoa/autorelease.h"
166 #include "wx/cocoa/string.h"
168 #import <AppKit/NSComboBox.h>
170 BEGIN_EVENT_TABLE(wxComboBox, wxControl)
172 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSComboBox,NSTextField,NSView)
173 WX_IMPLEMENT_COCOA_OWNER(wxComboBox,NSTextField,NSControl,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([[WX_GET_OBJC_CLASS(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 wxComboBox::Append(n, choices);
214 [GetNSComboBox() setCompletes:true]; //autocomplete :)
219 wxComboBox::~wxComboBox()
221 DisassociateNSComboBox(GetNSComboBox());
224 void wxComboBox::doWxEvent(int nEvent)
226 wxCommandEvent event2(wxEVT_COMBOBOX, GetId() );
227 event2.SetInt(GetSelection());
228 event2.SetEventObject(this);
229 event2.SetString(GetStringSelection());
230 HandleWindowEvent(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_TEXT, GetId() );
235 TextEvent.SetString( GetStringSelection() );
236 TextEvent.SetEventObject( this );
237 HandleWindowEvent( TextEvent );
241 void wxComboBox::SetSelection(int nSelection)
243 [GetNSComboBox() selectItemAtIndex:nSelection];
246 wxString wxComboBox::GetStringSelection()
248 return wxStringWithNSString([GetNSComboBox() objectValueOfSelectedItem]);
251 void wxComboBox::DoClear()
253 [GetNSComboBox() removeAllItems];
257 void wxComboBox::DoDeleteOneItem(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::DoInsertItems(const wxArrayStringsAdapter& items,
296 wxClientDataType type)
298 wxAutoNSAutoreleasePool pool;
299 const unsigned int numITems = items.GetCount();
300 for ( unsigned int i = 0; i < numITems; ++i, ++pos )
302 [GetNSComboBox() insertItemWithObjectValue:wxNSStringWithWxString(items[i]) atIndex:(pos)];
303 m_Datas.Insert(NULL, pos);
304 AssignNewItemClientData(pos, clientData, i, type);
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 /////////////////////////////////////////////////////////////////////////////
320 // wxTextEntry virtual implementations:
322 void wxComboBox::WriteText(wxString const&)
326 wxString wxComboBox::GetValue() const
328 wxAutoNSAutoreleasePool pool;
329 return wxStringWithNSString([GetNSTextField() stringValue]);
332 void wxComboBox::Remove(long, long)
336 void wxComboBox::Cut()
340 void wxComboBox::Copy()
344 void wxComboBox::Paste()
348 void wxComboBox::Undo()
352 void wxComboBox::Redo()
356 bool wxComboBox::CanUndo() const
361 bool wxComboBox::CanRedo() const
366 void wxComboBox::SetInsertionPoint(long)
370 long wxComboBox::GetInsertionPoint() const
375 wxTextPos wxComboBox::GetLastPosition() const
377 // working - returns the size of the wxString
378 return (long)(GetValue().Len());
381 void wxComboBox::SetSelection(long, long)
385 void wxComboBox::GetSelection(long*, long*) const
389 bool wxComboBox::IsEditable() const
391 return [GetNSTextField() isEditable];
394 void wxComboBox::SetEditable(bool editable)
396 // first ensure that the current value is stored (in case the user had not finished editing
397 // before SetEditable(FALSE) was called)
398 DoSetValue(GetValue(),1);
400 [GetNSTextField() setEditable: editable];
402 // forces the focus on the textctrl to be lost - while focus is still maintained
403 // after SetEditable(FALSE) the user may still edit the control
404 // (might not the best way to do this..)
405 [GetNSTextField() abortEditing];
408 #endif // wxUSE_COMBOBOX