From ab7ba98dbca12eb61a93ab1393121cfb7ef22ec5 Mon Sep 17 00:00:00 2001 From: oneeyeman1 Date: Tue, 23 Jul 2019 00:10:43 -0500 Subject: [PATCH] Implement support for wxCB_SORT in wxOSX wxComboBox Insert the item at the correct location when this style is specified. Closes https://github.com/wxWidgets/wxWidgets/pull/1438 --- interface/wx/combobox.h | 3 +-- src/osx/cocoa/combobox.mm | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/interface/wx/combobox.h b/interface/wx/combobox.h index 7c67126551..7c2d4314d6 100644 --- a/interface/wx/combobox.h +++ b/interface/wx/combobox.h @@ -38,8 +38,7 @@ allows the user to choose from the list of options but doesn't allow to enter a value not present in the list. @style{wxCB_SORT} - Sorts the entries in the list alphabetically. Notice that this style - is not currently implemented in wxOSX. + Sorts the entries in the list alphabetically. @style{wxTE_PROCESS_ENTER} The control will generate the event @c wxEVT_TEXT_ENTER that can be handled by the program. Otherwise, i.e. either if this style not diff --git a/src/osx/cocoa/combobox.mm b/src/osx/cocoa/combobox.mm index 84fd62cb2a..71aa165d44 100644 --- a/src/osx/cocoa/combobox.mm +++ b/src/osx/cocoa/combobox.mm @@ -264,7 +264,23 @@ int wxNSComboBoxControl::GetNumberOfItems() const void wxNSComboBoxControl::InsertItem(int pos, const wxString& item) { - [m_comboBox insertItemWithObjectValue:wxCFStringRef( item , m_wxPeer->GetFont().GetEncoding() ).AsNSString() atIndex:pos]; + wxCFStringRef itemLabel( item, m_wxPeer->GetFont().GetEncoding() ); + NSString* const cocoaStr = itemLabel.AsNSString(); + + if ( m_wxPeer->HasFlag(wxCB_SORT) ) + { + NSArray* const objectValues = m_comboBox.objectValues; + + pos = [objectValues indexOfObject: cocoaStr + inSortedRange: NSMakeRange(0, objectValues.count) + options: NSBinarySearchingInsertionIndex + usingComparator: ^(id obj1, id obj2) + { + return [obj1 caseInsensitiveCompare: obj2]; + }]; + } + + [m_comboBox insertItemWithObjectValue:cocoaStr atIndex:pos]; } void wxNSComboBoxControl::RemoveItem(int pos)