From ae845a045e7eeebf529af86939bb8f6f0d5f1a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83t=C4=83lin=20R=C4=83ceanu?= Date: Mon, 16 May 2016 17:19:45 +0300 Subject: [PATCH] Send events when deleting selected item in wxMSW wxTreeCtrl The changes of c749d9e6384a411e710fa8114d57cc7c11dcd212 broke sending of wxEVT_TREE_SEL_CHANGING and wxEVT_TREE_SEL_CHANGED events for the controls without wxTR_MULTIPLE style, restore sending them now. Closes #16926. Closes https://github.com/wxWidgets/wxWidgets/pull/285 --- src/msw/treectrl.cpp | 64 +++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/src/msw/treectrl.cpp b/src/msw/treectrl.cpp index 9c06328607..c6c908fa02 100644 --- a/src/msw/treectrl.cpp +++ b/src/msw/treectrl.cpp @@ -1608,54 +1608,40 @@ void wxTreeCtrl::Delete(const wxTreeItemId& item) // tree ctrl will eventually crash after item deletion TreeItemUnlocker unlock_all; + const bool selected = IsSelected(item); + + // attempt to delete the item, and continue only if it succeeds + if ( !MSWDeleteItem(item) ) + return; + + // if the item was not selected we don't need to do anything about the selection + if ( !selected ) + return; + if ( HasFlag(wxTR_MULTIPLE) ) { - bool selected = IsSelected(item); - wxTreeItemId next; - - if ( selected ) - { - next = TreeView_GetNextVisible(GetHwnd(), HITEM(item)); - - if ( !next.IsOk() ) - { - next = TreeView_GetPrevVisible(GetHwnd(), HITEM(item)); - } - } - - if ( !MSWDeleteItem(item) ) - return; - - if ( !selected ) - { - return; - } - if ( item == m_htSelStart ) m_htSelStart.Unset(); if ( item == m_htClickedItem ) m_htClickedItem.Unset(); - - if ( next.IsOk() ) - { - wxTreeEvent changingEvent(wxEVT_TREE_SEL_CHANGING, this, next); - - if ( IsTreeEventAllowed(changingEvent) ) - { - wxTreeEvent changedEvent(wxEVT_TREE_SEL_CHANGED, this, next); - (void)HandleTreeEvent(changedEvent); - } - else - { - DoUnselectItem(next); - ClearFocusedItem(); - } - } } - else + + // if a selected item was deleted announce that selection changed, no matter what + const wxTreeItemId next = GetFocusedItem(); + + wxTreeEvent changingEvent(wxEVT_TREE_SEL_CHANGING, this, next); + + // if "selection changing" event is allowed, send "selection changed" too + if ( IsTreeEventAllowed(changingEvent) ) { - MSWDeleteItem(item); + wxTreeEvent changedEvent(wxEVT_TREE_SEL_CHANGED, this, next); + HandleTreeEvent(changedEvent); + } + else if ( next.IsOk() ) + { + DoUnselectItem(next); + ClearFocusedItem(); } }