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
// colour
virtual bool ShouldInheritColours() const { return false; }
// 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
//
protected:
// fill in the client object or data field of the event as appropriate
//
+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);
+}
+
-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__
void wxChoice::GTKInsertComboBoxTextItem( unsigned int n, const wxString& text )
{
#ifdef __WXGTK3__
// same thing anyhow)
m_lastAcceptedSelection = wxID_NONE;
// 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);
// don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection
// valid and the selection will be undone in CBN_CLOSEUP above
// don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection
// valid and the selection will be undone in CBN_CLOSEUP above
// could get a wrong value when it calls our GetValue()
::SetWindowText(GetHwnd(), value.t_str());
// 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)
// update event when the selection changes (this also seems more
// logical as the text does change)
bool wxChoice::OSXHandleClicked( double WXUNUSED(timestampsec) )
{
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);