From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sun, 22 Aug 2010 22:15:17 +0000 (+0000)
Subject: Don't send event from wxMSW::wxListCtrl::DeleteAllItems() if it did nothing.
X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4b97af90bf2f010e61ed785cec8029e8e7903bc9

Don't send event from wxMSW::wxListCtrl::DeleteAllItems() if it did nothing.

wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS should only be sent if the control hadn't
been empty before. Document this behaviour and adjust wxMSW to match the
other platforms.

Also document the return value better.

Closes #12336.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65381 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---

diff --git a/interface/wx/listctrl.h b/interface/wx/listctrl.h
index bb6865f7e2..fa380eeb01 100644
--- a/interface/wx/listctrl.h
+++ b/interface/wx/listctrl.h
@@ -214,9 +214,15 @@ public:
     /**
         Deletes all items in the list control.
 
-        @note This function does @e not send the @c wxEVT_COMMAND_LIST_DELETE_ITEM
-              event because deleting many items from the control would be too slow
-              then (unlike wxListCtrl::DeleteItem).
+        This function does @e not send the @c wxEVT_COMMAND_LIST_DELETE_ITEM
+        event because deleting many items from the control would be too slow
+        then (unlike wxListCtrl::DeleteItem) but it does send the special @c
+        wxEVT_COMMAND_LIST_DELETE_ALL_ITEMS event if the control was not empty.
+        If it was already empty, nothing is done and no event is sent.
+
+        @return @true if the items were successfully deleted or if the control
+            was already empty, @false if an error occurred while deleting the
+            items.
     */
     bool DeleteAllItems();
 
diff --git a/src/msw/listctrl.cpp b/src/msw/listctrl.cpp
index 484443466f..997c3ca9bc 100644
--- a/src/msw/listctrl.cpp
+++ b/src/msw/listctrl.cpp
@@ -1464,7 +1464,9 @@ bool wxListCtrl::DeleteItem(long item)
 // Deletes all items
 bool wxListCtrl::DeleteAllItems()
 {
-    return ListView_DeleteAllItems(GetHwnd()) != 0;
+    // Calling ListView_DeleteAllItems() will always generate an event but we
+    // shouldn't do it if the control is empty 
+    return !GetItemCount() || ListView_DeleteAllItems(GetHwnd()) != 0;
 }
 
 // Deletes all items