Fix problems in DPA_Merge exposed by previous code cleanup.

diff --git a/dlls/comctl32/comctl32undoc.c b/dlls/comctl32/comctl32undoc.c
index b93d3d1..f426b3c 100644
--- a/dlls/comctl32/comctl32undoc.c
+++ b/dlls/comctl32/comctl32undoc.c
@@ -198,7 +198,7 @@
 {
     INT nCount;
     LPVOID *pWork1, *pWork2;
-    INT nResult;
+    INT nResult, i;
     INT nIndex;
 
     TRACE("%p %p %08lx %p %p %08lx)\n",
@@ -216,7 +216,7 @@
     if (IsBadCodePtr ((FARPROC)pfnMerge))
 	return FALSE;
 
-    if (dwFlags & DPAM_SORT) {
+    if (!(dwFlags & DPAM_NOSORT)) {
 	TRACE("sorting dpa's!\n");
 	if (hdpa1->nItemCount > 0)
 	DPA_Sort (hdpa1, pfnCompare, lParam);
@@ -243,7 +243,23 @@
 
     do
     {
-        if (nIndex < 0) break;
+        if (nIndex < 0) {
+	    if ((nCount >= 0) && (dwFlags & DPAM_INSERT)) {
+		/* Now insert the remaining new items into DPA 1 */
+		TRACE("%d items to be inserted at start of DPA 1\n",
+		      nCount+1);
+		for (i=nCount; i>=0; i--) {
+		    PVOID ptr;
+
+		    ptr = (pfnMerge)(3, *pWork2, NULL, lParam);
+		    if (!ptr)
+			return FALSE;
+		    DPA_InsertPtr (hdpa1, 0, ptr);
+		    pWork2--;
+		}
+	    }
+	    break;
+	}
 	nResult = (pfnCompare)(*pWork1, *pWork2, lParam);
 	TRACE("compare result=%d, dpa1.cnt=%d, dpa2.cnt=%d\n", 
 	      nResult, nIndex, nCount);
@@ -262,10 +278,12 @@
 	    nIndex--;
 	    pWork1--;
 	}
-	else if (nResult < 0)
+	else if (nResult > 0)
 	{
-	    if (!(dwFlags & 8))
+	    /* item in DPA 1 missing from DPA 2 */
+	    if (dwFlags & DPAM_DELETE)
 	    {
+		/* Now delete the extra item in DPA1 */
 		PVOID ptr;
 
 		ptr = DPA_DeletePtr (hdpa1, hdpa1->nItemCount - 1);
@@ -277,14 +295,16 @@
 	}
 	else
 	{
-	    if (!(dwFlags & 4))
+	    /* new item in DPA 2 */
+	    if (dwFlags & DPAM_INSERT)
 	    {
+		/* Now insert the new item in DPA 1 */
 		PVOID ptr;
 
 		ptr = (pfnMerge)(3, *pWork2, NULL, lParam);
 		if (!ptr)
 		    return FALSE;
-		DPA_InsertPtr (hdpa1, nIndex, ptr);
+		DPA_InsertPtr (hdpa1, nIndex+1, ptr);
 	    }
 	    nCount--;
 	    pWork2--;