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
This commit is contained in:
Vadim Zeitlin
2012-12-02 23:48:59 +00:00
parent 4d3845c073
commit 7bc740719c
6 changed files with 26 additions and 57 deletions

View File

@@ -441,6 +441,12 @@ public:
// colour // colour
virtual bool ShouldInheritColours() const { return false; } 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: protected:
// fill in the client object or data field of the event as appropriate // fill in the client object or data field of the event as appropriate
// //

View File

@@ -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 #endif // wxUSE_CONTROLS

View File

@@ -101,22 +101,6 @@ wxChoice::~wxChoice()
delete m_strings; 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 ) void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
{ {
#ifdef __WXGTK3__ #ifdef __WXGTK3__

View File

@@ -792,23 +792,10 @@ bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
// same thing anyhow) // same thing anyhow)
m_lastAcceptedSelection = wxID_NONE; m_lastAcceptedSelection = wxID_NONE;
{ SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED);
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);
}
break; break;
// don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection // don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection
// valid and the selection will be undone in CBN_CLOSEUP above // valid and the selection will be undone in CBN_CLOSEUP above

View File

@@ -311,16 +311,9 @@ bool wxComboBox::MSWCommand(WXUINT param, WXWORD id)
// could get a wrong value when it calls our GetValue() // could get a wrong value when it calls our GetValue()
::SetWindowText(GetHwnd(), value.t_str()); ::SetWindowText(GetHwnd(), value.t_str());
{ SendSelectionChangedEvent(wxEVT_COMMAND_COMBOBOX_SELECTED);
wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
event.SetInt(sel);
event.SetString(value);
InitCommandEventWithItems(event, sel);
ProcessCommand(event); // fall through: for compatibility with wxGTK, also send the text
}
// fall through: for compability with wxGTK, also send the text
// update event when the selection changes (this also seems more // update event when the selection changes (this also seems more
// logical as the text does change) // logical as the text does change)

View File

@@ -236,23 +236,7 @@ void * wxChoice::DoGetItemClientData(unsigned int n) const
bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) ) bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) )
{ {
wxCommandEvent event( wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); SendSelectionChangedEvent(wxEVT_COMMAND_CHOICE_SELECTED);
// 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 );
}
return true ; return true ;
} }