From: Vadim Zeitlin Date: Sun, 2 Dec 2012 23:48:59 +0000 (+0000) Subject: Add wxControlWithItems::SendSelectionChangedEvent() helper. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/7bc740719c93355e447db5c9bcb24cf7cdc29b61 Add wxControlWithItems::SendSelectionChangedEvent() helper. Reuse the same event generation code for wxChoice in wxMSW, wxGTK and wxOSX and also wxComboBox in wxMSW and wxGTK instead of duplicating it (incompletely and so partially incorrectly in wxOSX case). This is just a refactoring so no changes in behaviour. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73102 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- 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); - - ProcessCommand(event); - } + SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED); - // 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 ; }