List View Sorting and Callback

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

List View Sorting and Callback

 
0
  #1
Sep 6th, 2004
I have a list view report style. Im trying to sort by click on a header. For some reason it does nothing.

This Peice of code is invoked when the column is clicked.
  1. if (pnm->hdr.code == LVN_COLUMNCLICK)
  2. {
  3. //DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_SORT),
  4. // NULL, (DLGPROC)SortDlgProc, 0);
  5. if(nSortDir[pnm->iSubItem])
  6. nSortDir[pnm->iSubItem] = false;
  7. else
  8. nSortDir[pnm->iSubItem] = true;
  9.  
  10. ListView_SortItems (hList, ListViewCompareProc, pnm->iSubItem);
  11.  
  12.  
  13. nItem1=0;
  14. return 0;
  15. }

Now Here is my Call back function
  1. int CALLBACK ListViewCompareProc (LPARAM lParam1, LPARAM lParam2, LPARAM
  2. lParamSort)
  3. {
  4. static LV_FINDINFO fi;
  5. static int nItem1, nItem2;
  6. static char szBuf1[30], szBuf2[30];
  7.  
  8. // Determine the items that we are comparing.
  9. //...........................................
  10. fi.flags = LVFI_PARAM;
  11. fi.lParam = lParam1;
  12. nItem1 = ListView_FindItem(hList, -1, &fi);
  13.  
  14. fi.lParam = lParam2;
  15. nItem2 = ListView_FindItem(hList, -1, &fi);
  16.  
  17. // Retrieve the item text so we can compare it.
  18. //.............................................
  19. ListView_GetItemText(hList, nItem1, lParamSort, szBuf1,
  20. sizeof(szBuf1));
  21. ListView_GetItemText(hList, nItem2, lParamSort, szBuf2,
  22. sizeof(szBuf2));
  23.  
  24. // Return the comparison results.
  25. //...............................
  26. if (nSortDir[lParamSort] ) // ACENDING ORDER
  27. return(strcmp(szBuf1, szBuf2));
  28. else
  29. return(strcmp(szBuf1, szBuf2) * -1);
  30. }

According to MSDN, the Sort Macro should handle the results of the callback just like strcmp, and switch the items accordingly. Once again, the callback function is called and works, yet it does not sort properly, what could be wrong?
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: List View Sorting and Callback

 
0
  #2
Sep 6th, 2004
It looks kosher. Can you debug it and make sure the two strings are being fetched?

thoughts: What happens if the strings are > 30 bytes? Why are the strings static? Just to save stack space? Do you need to invalidate the list view to get it to redraw?
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: List View Sorting and Callback

 
0
  #3
Sep 7th, 2004
Since the callback function pretty much enumerates the list, I made the buffers static to save space on the stack.

I did find something funny after playing with it. The two strings that are being compared are always the same. From this I have concluded that the LPARAM value returned from the Find Item macro is pointing to the same item.

It dosn't really make sense since the two LPRAM items being compared are passed in as different values.

I'm so lost :cry:
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 219
Reputation: BountyX is an unknown quantity at this point 
Solved Threads: 8
BountyX's Avatar
BountyX BountyX is offline Offline
Code Guru

Re: List View Sorting and Callback

 
0
  #4
Sep 8th, 2004
Hey I got it working!!!!!!! After digging a bit more through MSDN I came up with a solution. The problem was that finding the item index, for the purpose of getting the text is unstable within the call back procedure, the index returned by the lparam found, will always be the same. The solution was to use ListView_SortItemsEx macro instead of ListView_SortItems. The reason being is that ListView_SortItemsEx passes the intem index as the first two prarmeters of the callback procedure, where as ListView_SortItems simple passes the lparam. IT ALL MAKES SO MUCH SENSE NOW. Think about it why would you ever need to use ListView_SortItems? For owner drawn lists where sometimes, text is not avaible! So for anything text oriented ListView_SortItemsEx would be the right macro to use.

Here is the working code for listview sorting and callback.

if (pnm->hdr.code == LVN_COLUMNCLICK)
 
{
 
//DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_SORT), 
 
// NULL, (DLGPROC)SortDlgProc, 0); 
 
if(nSortDir[pnm->iSubItem])
 
nSortDir[pnm->iSubItem] = false;
 
else
 
nSortDir[pnm->iSubItem] = true;
 
ListView_SortItemsEx(hList, ListViewCompareProc, pnm->iSubItem);
 
nItem1=0;
 
return 0;
 
}


and for the callback....
int CALLBACK ListViewCompareProc (LPARAM lParam1, LPARAM lParam2, LPARAM
 
lParamSort)
 
{
 
staticchar szBuf1[30], szBuf2[30];
 
// Determine the items that we are comparing.
 
//...........................................
 
 
 
// Retrieve the item text so we can compare it.
 
//.............................................
 
ListView_GetItemText(hList, lParam1, lParamSort, szBuf1,
 
sizeof(szBuf1));
 
ListView_GetItemText(hList, lParam2, lParamSort, szBuf2,
 
sizeof(szBuf2));
 
// Return the comparison results.
 
//...............................
 
if (nSortDir[lParamSort] ) // ACENDING ORDER
 
return(strcmp(szBuf1, szBuf2) * -1);
 
else
 
return(strcmp(szBuf1, szBuf2));
 
}


As simple as that! Thnx for the help.
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC