From 809020fc9791018a335b825d6a3ed870c0612925 Mon Sep 17 00:00:00 2001
From: Kevin Ollivier <kevino@theolliviers.com>
Date: Fri, 19 Feb 2010 03:00:07 +0000
Subject: [PATCH] Disable sending of events by OS X Combobox during
 programmatic changes.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63519 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 include/wx/osx/core/private.h |  7 +++++++
 src/osx/cocoa/combobox.mm     | 10 ++++++++--
 src/osx/window_osx.cpp        |  1 +
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h
index 49b2f13417..8844689e36 100644
--- a/include/wx/osx/core/private.h
+++ b/include/wx/osx/core/private.h
@@ -276,6 +276,12 @@ public :
     virtual bool        ButtonClickDidStateChange() = 0;
 
     virtual void        InstallEventHandler( WXWidget control = NULL ) = 0;
+    
+    // Mechanism used to keep track of whether a change should send an event
+    // Do SendEvents(false) when starting actions that would trigger programmatic events
+    // and SendEvents(true) at the end of the block.
+    virtual void        SendEvents(bool shouldSendEvents) { m_shouldSendEvents = shouldSendEvents; }
+    virtual bool        ShouldSendEvents() { return m_shouldSendEvents; }
 
     // static methods for associating native controls and their implementations
 
@@ -490,6 +496,7 @@ protected :
     wxWindowMac*        m_wxPeer;
     bool                m_needsFocusRect;
     bool                m_needsFrame;
+    bool                m_shouldSendEvents;
 
     DECLARE_ABSTRACT_CLASS(wxWidgetImpl)
 };
diff --git a/src/osx/cocoa/combobox.mm b/src/osx/cocoa/combobox.mm
index 3cdb19e247..a6c9e9b7c4 100644
--- a/src/osx/cocoa/combobox.mm
+++ b/src/osx/cocoa/combobox.mm
@@ -46,7 +46,7 @@
 {
     wxUnusedVar(aNotification);
     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
-    if ( impl )
+    if ( impl && impl->ShouldSendEvents() )
     {
         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
         if ( wxpeer ) {
@@ -62,7 +62,7 @@
 {
     wxUnusedVar(notification);
     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
-    if ( impl )
+    if ( impl && impl->ShouldSendEvents())
     {
         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
         if ( wxpeer ) {
@@ -95,7 +95,9 @@ int wxNSComboBoxControl::GetSelectedItem() const
 void wxNSComboBoxControl::SetSelectedItem(int item)
 {
     wxASSERT_MSG(item >= 0 && item < [m_comboBox numberOfItems], "Inavlid item index.");
+    SendEvents(false);
     [m_comboBox selectItemAtIndex: item];
+    SendEvents(true);
 }
 
 int wxNSComboBoxControl::GetNumberOfItems() const
@@ -110,12 +112,16 @@ void wxNSComboBoxControl::InsertItem(int pos, const wxString& item)
 
 void wxNSComboBoxControl::RemoveItem(int pos)
 {
+    SendEvents(false);
     [m_comboBox removeItemAtIndex:pos];
+    SendEvents(true);
 }
 
 void wxNSComboBoxControl::Clear()
 {
+    SendEvents(false);
     [m_comboBox removeAllItems];
+    SendEvents(true);
 }
 
 wxString wxNSComboBoxControl::GetStringAtIndex(int pos) const
diff --git a/src/osx/window_osx.cpp b/src/osx/window_osx.cpp
index b091cbcab5..0eeb0df526 100644
--- a/src/osx/window_osx.cpp
+++ b/src/osx/window_osx.cpp
@@ -2464,6 +2464,7 @@ wxWidgetImpl::wxWidgetImpl( wxWindowMac* peer , bool isRootControl )
     Init();
     m_isRootControl = isRootControl;
     m_wxPeer = peer;
+    m_shouldSendEvents = true;
 }
 
 wxWidgetImpl::wxWidgetImpl()
-- 
2.47.2