Refactor listbox event sending code to avoid duplication.
wxMSW wxListBox implementation contained the same code as the private LBSendEvent() function in lboxcmn.cpp, so make this function a (protected) member of wxListBoxBase and reuse it instead. Also change its and CalcAndSendEvent() return type to bool to be able to return whether the event was processed or not. As the result of this refactoring, the "is selected" flag is now set correctly for the selection events under MSW (it was always off before). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64496 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -96,10 +96,13 @@ public:
|
|||||||
int HitTest(int x, int y) const { return DoListHitTest(wxPoint(x, y)); }
|
int HitTest(int x, int y) const { return DoListHitTest(wxPoint(x, y)); }
|
||||||
|
|
||||||
|
|
||||||
// For generating events in multiple and extended mode
|
// For generating events in multiple and extended mode: compare the current
|
||||||
|
// selections with the previously recorded ones (in m_oldSelections) and
|
||||||
|
// send the appropriate event if they differ, otherwise just return false.
|
||||||
|
bool CalcAndSendEvent();
|
||||||
|
|
||||||
wxArrayInt m_oldSelections;
|
wxArrayInt m_oldSelections;
|
||||||
void UpdateOldSelections();
|
void UpdateOldSelections();
|
||||||
void CalcAndSendEvent();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void DoSetFirstItem(int n) = 0;
|
virtual void DoSetFirstItem(int n) = 0;
|
||||||
@@ -110,6 +113,11 @@ protected:
|
|||||||
virtual int DoListHitTest(const wxPoint& WXUNUSED(point)) const
|
virtual int DoListHitTest(const wxPoint& WXUNUSED(point)) const
|
||||||
{ return wxNOT_FOUND; }
|
{ return wxNOT_FOUND; }
|
||||||
|
|
||||||
|
// Send a listbox (de)selection or double click event.
|
||||||
|
//
|
||||||
|
// Returns true if the event was processed.
|
||||||
|
bool SendEvent(wxEventType evtType, int item, bool selected);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxDECLARE_NO_COPY_CLASS(wxListBoxBase);
|
wxDECLARE_NO_COPY_CLASS(wxListBoxBase);
|
||||||
};
|
};
|
||||||
|
@@ -90,29 +90,33 @@ void wxListBoxBase::UpdateOldSelections()
|
|||||||
GetSelections( m_oldSelections );
|
GetSelections( m_oldSelections );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LBSendEvent( wxCommandEvent &event, wxListBoxBase *listbox, int item )
|
bool wxListBoxBase::SendEvent(wxEventType evtType, int item, bool selected)
|
||||||
{
|
{
|
||||||
event.SetInt( item );
|
wxCommandEvent event(evtType, GetId());
|
||||||
event.SetString( listbox->GetString( item ) );
|
event.SetEventObject(this);
|
||||||
if ( listbox->HasClientObjectData() )
|
|
||||||
event.SetClientObject( listbox->GetClientObject(item) );
|
event.SetInt(item);
|
||||||
else if ( listbox->HasClientUntypedData() )
|
event.SetString(GetString(item));
|
||||||
event.SetClientData( listbox->GetClientData(item) );
|
event.SetExtraLong(selected);
|
||||||
listbox->HandleWindowEvent( event );
|
|
||||||
|
if ( HasClientObjectData() )
|
||||||
|
event.SetClientObject(GetClientObject(item));
|
||||||
|
else if ( HasClientUntypedData() )
|
||||||
|
event.SetClientData(GetClientData(item));
|
||||||
|
|
||||||
|
return HandleWindowEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListBoxBase::CalcAndSendEvent()
|
bool wxListBoxBase::CalcAndSendEvent()
|
||||||
{
|
{
|
||||||
wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, GetId());
|
|
||||||
event.SetEventObject( this );
|
|
||||||
|
|
||||||
wxArrayInt selections;
|
wxArrayInt selections;
|
||||||
GetSelections(selections);
|
GetSelections(selections);
|
||||||
|
bool selected = true;
|
||||||
|
|
||||||
if ( selections.empty() && m_oldSelections.empty() )
|
if ( selections.empty() && m_oldSelections.empty() )
|
||||||
{
|
{
|
||||||
// nothing changed, just leave
|
// nothing changed, just leave
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const size_t countSel = selections.size(),
|
const size_t countSel = selections.size(),
|
||||||
@@ -131,14 +135,13 @@ void wxListBoxBase::CalcAndSendEvent()
|
|||||||
|
|
||||||
// nothing changed, just leave
|
// nothing changed, just leave
|
||||||
if ( !changed )
|
if ( !changed )
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int item = wxNOT_FOUND;
|
int item = wxNOT_FOUND;
|
||||||
if ( selections.empty() )
|
if ( selections.empty() )
|
||||||
{
|
{
|
||||||
// indicate that this is a deselection
|
selected = false;
|
||||||
event.SetExtraLong(0);
|
|
||||||
item = m_oldSelections[0];
|
item = m_oldSelections[0];
|
||||||
}
|
}
|
||||||
else // we [still] have some selections
|
else // we [still] have some selections
|
||||||
@@ -155,14 +158,9 @@ void wxListBoxBase::CalcAndSendEvent()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( any_new_selected )
|
if ( !any_new_selected )
|
||||||
{
|
{
|
||||||
// indicate that this is a selection
|
// No new items selected, now test if any new item is deselected
|
||||||
event.SetExtraLong(1);
|
|
||||||
}
|
|
||||||
else // no new items selected
|
|
||||||
{
|
|
||||||
// Now test if any new item is deselected
|
|
||||||
bool any_new_deselected = false;
|
bool any_new_deselected = false;
|
||||||
for ( size_t idx = 0; idx < countSelOld; idx++ )
|
for ( size_t idx = 0; idx < countSelOld; idx++ )
|
||||||
{
|
{
|
||||||
@@ -177,7 +175,7 @@ void wxListBoxBase::CalcAndSendEvent()
|
|||||||
if ( any_new_deselected )
|
if ( any_new_deselected )
|
||||||
{
|
{
|
||||||
// indicate that this is a selection
|
// indicate that this is a selection
|
||||||
event.SetExtraLong(0);
|
selected = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -191,7 +189,7 @@ void wxListBoxBase::CalcAndSendEvent()
|
|||||||
|
|
||||||
m_oldSelections = selections;
|
m_oldSelections = selections;
|
||||||
|
|
||||||
LBSendEvent(event, this, item);
|
return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@@ -647,16 +647,13 @@ wxSize wxListBox::DoGetBestClientSize() const
|
|||||||
|
|
||||||
bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
|
bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
|
||||||
{
|
{
|
||||||
if ((param == LBN_SELCHANGE) && HasMultipleSelection())
|
|
||||||
{
|
|
||||||
CalcAndSendEvent();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
wxEventType evtType;
|
wxEventType evtType;
|
||||||
int n;
|
int n;
|
||||||
if ( param == LBN_SELCHANGE )
|
if ( param == LBN_SELCHANGE )
|
||||||
{
|
{
|
||||||
|
if ( HasMultipleSelection() )
|
||||||
|
return CalcAndSendEvent();
|
||||||
|
|
||||||
evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
|
evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
|
||||||
n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
|
n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
|
||||||
|
|
||||||
@@ -673,22 +670,8 @@ bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// retrieve the affected item
|
// only send an event if we have a valid item
|
||||||
if ( n == wxNOT_FOUND )
|
return n != wxNOT_FOUND && SendEvent(evtType, n, true /* selection */);
|
||||||
return false;
|
|
||||||
|
|
||||||
wxCommandEvent event(evtType, m_windowId);
|
|
||||||
event.SetEventObject(this);
|
|
||||||
|
|
||||||
if ( HasClientObjectData() )
|
|
||||||
event.SetClientObject( GetClientObject(n) );
|
|
||||||
else if ( HasClientUntypedData() )
|
|
||||||
event.SetClientData( GetClientData(n) );
|
|
||||||
|
|
||||||
event.SetString(GetString(n));
|
|
||||||
event.SetInt(n);
|
|
||||||
|
|
||||||
return HandleWindowEvent(event);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user