From 73247c72e12b8047f1541d6525c4ab151b505b43 Mon Sep 17 00:00:00 2001
From: George Hazan <ghazan@miranda.im>
Date: Tue, 28 Mar 2023 14:15:31 +0300
Subject: fixes #3467 (MyHandle must always be first)

---
 src/mir_app/src/contacts.cpp | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)

(limited to 'src/mir_app')

diff --git a/src/mir_app/src/contacts.cpp b/src/mir_app/src/contacts.cpp
index 55f55b5bff..7fc760e550 100644
--- a/src/mir_app/src/contacts.cpp
+++ b/src/mir_app/src/contacts.cpp
@@ -335,6 +335,7 @@ public:
 	{
 		m_nameOrder.SetFlags(MTREE_DND);
 		m_nameOrder.OnBeginDrag = Callback(this, &CContactOptsDlg::OnBeginDrag);
+		m_nameOrder.OnEndDrag = Callback(this, &CContactOptsDlg::OnEndDrag);
 	}
 
 	bool OnInitDialog() override
@@ -367,11 +368,29 @@ public:
 		return true;
 	}
 
-	void OnBeginDrag(CCtrlTreeView::TEventInfo *evt)
+	bool OnBeginDrag(CCtrlTreeView::TEventInfo *evt)
 	{
 		LPNMTREEVIEW pNotify = evt->nmtv;
-		if (pNotify->itemNew.lParam == 0 || pNotify->itemNew.lParam == _countof(nameOrderDescr) - 1)
-			pNotify->hdr.code = 0; // deny dragging
+		return (pNotify->itemNew.lParam != 0 && pNotify->itemNew.lParam != _countof(nameOrderDescr)-1);
+	}
+
+	bool OnEndDrag(CCtrlTreeView::TEventInfo *evt)
+	{
+		auto &hti = *evt->ntvhi;
+
+		// do not allow to move selection over the first item
+		if (hti.flags & TVHT_ABOVE)
+			return false;
+
+		// do not allow to move selection below the last item either
+		TVITEMEX tvi;
+		tvi.mask = TVIF_HANDLE | TVIF_PARAM;
+		tvi.hItem = hti.hItem;
+		m_nameOrder.GetItem(&tvi);
+		if (tvi.lParam == _countof(nameOrderDescr) - 1)
+			return false;
+
+		return true;
 	}
 };
 
-- 
cgit v1.2.3