diff --git a/include/wx/ctrlsub.h b/include/wx/ctrlsub.h index 76f8353220..a55e124148 100644 --- a/include/wx/ctrlsub.h +++ b/include/wx/ctrlsub.h @@ -441,6 +441,12 @@ public: // colour virtual bool ShouldInheritColours() const { return false; } + + // Implementation only from now on. + + // Generate an event of the given type for the selection change. + void SendSelectionChangedEvent(wxEventType eventType); + protected: // fill in the client object or data field of the event as appropriate // diff --git a/src/common/ctrlsub.cpp b/src/common/ctrlsub.cpp index 388af3e6ff..7f35cfa947 100644 --- a/src/common/ctrlsub.cpp +++ b/src/common/ctrlsub.cpp @@ -292,4 +292,19 @@ wxControlWithItemsBase::InitCommandEventWithItems(wxCommandEvent& event, int n) } } +void wxControlWithItemsBase::SendSelectionChangedEvent(wxEventType eventType) +{ + const int n = GetSelection(); + if ( n == wxNOT_FOUND ) + return; + + wxCommandEvent event(eventType, m_windowId); + event.SetInt(n); + event.SetEventObject(this); + event.SetString(GetStringSelection()); + InitCommandEventWithItems(event, n); + + HandleWindowEvent(event); +} + #endif // wxUSE_CONTROLS diff --git a/src/gtk/choice.cpp b/src/gtk/choice.cpp index 7a6be36377..ff73d14474 100644 --- a/src/gtk/choice.cpp +++ b/src/gtk/choice.cpp @@ -101,22 +101,6 @@ wxChoice::~wxChoice() delete m_strings; } -void wxChoice::SendSelectionChangedEvent(wxEventType evt_type) -{ - if (GetSelection() == -1) - return; - - wxCommandEvent event( evt_type, GetId() ); - - int n = GetSelection(); - event.SetInt( n ); - event.SetString( GetStringSelection() ); - event.SetEventObject( this ); - InitCommandEventWithItems( event, n ); - - HandleWindowEvent( event ); -} - void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text ) { #ifdef __WXGTK3__ diff --git a/src/msw/choice.cpp b/src/msw/choice.cpp index e7c17023c1..b6670350f3 100644 --- a/src/msw/choice.cpp +++ b/src/msw/choice.cpp @@ -792,23 +792,10 @@ bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) // same thing anyhow) m_lastAcceptedSelection = wxID_NONE; - { - const int n = GetSelection(); - - wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); - event.SetInt(n); - event.SetEventObject(this); - - if ( n > -1 ) - { - event.SetString(GetStringSelection()); - InitCommandEventWithItems(event, n); - } - - ProcessCommand(event); - } + SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED); break; + // don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection // valid and the selection will be undone in CBN_CLOSEUP above diff --git a/src/msw/combobox.cpp b/src/msw/combobox.cpp index dc91cfb252..d0b9836baf 100644 --- a/src/msw/combobox.cpp +++ b/src/msw/combobox.cpp @@ -311,16 +311,9 @@ bool wxComboBox::MSWCommand(WXUINT param, WXWORD id) // could get a wrong value when it calls our GetValue() ::SetWindowText(GetHwnd(), value.t_str()); - { - wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId()); - event.SetInt(sel); - event.SetString(value); - InitCommandEventWithItems(event, sel); + SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED); - ProcessCommand(event); - } - - // fall through: for compability with wxGTK, also send the text + // fall through: for compatibility with wxGTK, also send the text // update event when the selection changes (this also seems more // logical as the text does change) diff --git a/src/osx/choice_osx.cpp b/src/osx/choice_osx.cpp index 2bf2c10253..d7ea4197fa 100644 --- a/src/osx/choice_osx.cpp +++ b/src/osx/choice_osx.cpp @@ -236,23 +236,7 @@ void * wxChoice::DoGetItemClientData(unsigned int n) const bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) ) { - wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); - - // actually n should be made sure by the os to be a valid selection, but ... - int n = GetSelection(); - if ( n > -1 ) - { - event.SetInt( n ); - event.SetString( GetStringSelection() ); - event.SetEventObject( this ); - - if ( HasClientObjectData() ) - event.SetClientObject( GetClientObject( n ) ); - else if ( HasClientUntypedData() ) - event.SetClientData( GetClientData( n ) ); - - ProcessCommand( event ); - } + SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED); return true ; }