/*----------------------------------------------------------------------
Copyright (C)2001 MJSoft. All Rights Reserved.
This source may be used freely as long as it is not sold for
profit and this copyright information is not altered or removed.
Visit the web-site at www.mjsoft.co.uk
e-mail comments to info@mjsoft.co.uk
File: SortListCtrl.cpp
Purpose: Provides a sortable list control, it will sort text, numbers
and dates, ascending or descending, and will even draw the
arrows just like windows explorer!
----------------------------------------------------------------------*/
#include "stdafx.h"
#include "SortListCtrl.h"
#include <afxtempl.h>
#include "resource.h"
#include "appsetting.h" // for convert to char
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
LPCTSTR g_pszSection = _T("ListCtrls");
int watoi(const CString& str)
{
char temp[100] = {'\0'};
ConvertToChar(str, temp);
return atoi(temp);
}
struct ItemData
{
public:
ItemData() : arrpsz( NULL ), dwData( NULL ) {}
LPTSTR* arrpsz;
DWORD dwData;
private:
// ban copying.
ItemData( const ItemData& );
ItemData& operator=( const ItemData& );
};
CSortListCtrl::CSortListCtrl()
: m_iNumColumns( 0 ),m_bIsAdding(FALSE)
, m_iSortColumn( -1 )
, m_bSortAscending( TRUE )
{
m_colors[0] = RGB(0,0,255);
m_colors[1] = RGB(100,100,100);
m_colors[2] = RGB(200,0,85);
m_colors[3] = RGB(200,85,0);
m_colors[4] = RGB(255,0,0);
}
CSortListCtrl::~CSortListCtrl()
{
}
BEGIN_MESSAGE_MAP(CSortListCtrl, CListCtrl)
//{{AFX_MSG_MAP(CSortListCtrl)
ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnClick)
ON_WM_DESTROY()
ON_NOTIFY_REFLECT ( NM_CUSTOMDRAW, OnCustomdrawMyList )
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSortListCtrl message handlers
void CSortListCtrl::PreSubclassWindow()
{
srand( (unsigned)time( NULL ) );
// the list control must have the report style.
ASSERT( GetStyle() & LVS_REPORT );
CListCtrl::PreSubclassWindow();
VERIFY( m_ctlHeader.SubclassWindow( GetHeaderCtrl()->GetSafeHwnd() ) );
HIMAGELIST hList = ImageList_Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 1);
m_cImageListSmall.Attach(hList);
// Load the small icons
CBitmap cBmp;
cBmp.LoadBitmap(IDB_IMAGES_SMALL);
m_cImageListSmall.Add(&cBmp, RGB(255,0, 255));
SetImageList(&m_cImageListSmall, LVSIL_SMALL);
}
BOOL CSortListCtrl::SetHeadings( UINT uiStringID )
{
CString strHeadings;
VERIFY( strHeadings.LoadString( uiStringID ) );
return SetHeadings( strHeadings );
}
// the heading text is in the format column 1 text,column 1 width;column 2 text,column 3 width;etc.
BOOL CSortListCtrl::SetHeadings( const CString& strHeadings )
{
int iStart = 0;
for( ;; )
{
const int iComma = strHeadings.Find( _T(','), iStart );
if( iComma == -1 )
break;
const CString strHeading = strHeadings.Mid( iStart, iComma - iStart );
iStart = iComma + 1;
int iSemiColon = strHeadings.Find( _T(';'), iStart );
if( iSemiColon == -1 )
iSemiColon = strHeadings.GetLength();
const int iWidth = watoi( strHeadings.Mid( iStart, iSemiColon - iStart ) );
iStart = iSemiColon + 1;
if( InsertColumn( m_iNumColumns++, strHeading, LVCFMT_LEFT, iWidth ) == -1 )
return FALSE;
}
return TRUE;
}
int CSortListCtrl::AddItem( LPCTSTR pszText, ... )
{
m_bIsAdding = TRUE;
const int iIndex = InsertItem( GetItemCount(), pszText );
//int iImage = watoi(pszText);
CListCtrl::SetItem(iIndex,0,LVIF_TEXT|LVIF_IMAGE,pszText,/*iImage*/0,0,0,0);
LPTSTR* arrpsz = new LPTSTR[ m_iNumColumns ];
arrpsz[ 0 ] = new TCHAR[ lstrlen( pszText ) + 1 ];
(void)lstrcpy( arrpsz[ 0 ], pszText );
va_list list;
va_start( list, pszText );
for( int iColumn = 1; iColumn < m_iNumColumns; iColumn++ )
{
pszText = va_arg( list, LPCTSTR );
ASSERT_VALID_STRING( pszText );
VERIFY( CListCtrl::SetItem( iIndex, iColumn, LVIF_TEXT, pszText, 0, 0, 0, 0 ) );
arrpsz[ iColumn ] = new TCHAR[ lstrlen( pszText ) + 1 ];
(void)lstrcpy( arrpsz[ iColumn ], pszText );
}
va_end( list );
VERIFY( SetTextArray( iIndex, arrpsz ) );
m_bIsAdding = FALSE;
return iIndex;
}
void CSortListCtrl::FreeItemMemory( const int iItem )
{
ItemData* pid = reinterpret_cast<ItemData*>( CListCtrl::GetItemData( iItem ) );
LPTSTR* arrpsz = pid->arrpsz;
for( int i = 0; i < m_iNumColumns; i++ )
delete[] arrpsz[ i ];
delete[] arrpsz;
delete pid;
VERIFY( CListCtrl::SetItemData( iItem, NULL ) );
}
BOOL CSortListCtrl::DeleteItem( int iItem )
{
if ( m_bIsAdding ) return FALSE;
FreeItemMemory( iItem );
return CListCtrl::DeleteItem( iItem );
}
BOOL CSortListCtrl::DeleteAllItems()
{
if ( m_bIsAdding ) return FALSE;
for( int iItem = 0; iItem < GetItemCount(); iItem ++ )
FreeItemMemory( iItem );
return CListCtrl::DeleteAllItems();
}
bool IsNumber( LPCTSTR pszText )
{
ASSERT_VALID_STRING( pszText );
for( int i = 0; i < lstrlen( pszText ); i++ )
if( !_istdigit( pszText[ i ] ) )
return false;
return true;
}
int NumberCompare( LPCTSTR pszNumber1, LPCTSTR pszNumber2 )
{
ASSERT_VALID_STRING( pszNumber1 );
ASSERT_VALID_STRING( pszNumber2 );
const int iNumber1 = watoi( pszNumber1 );
const int iNumber2 = watoi( pszNumber2 );
if( iNumber1 < iNumber2 )
return -1;
if( iNumber1 > iNumber2 )
return 1;
return 0;
}
bool IsDate( LPCTSTR pszText )
{
ASSERT_VALID_STRING( pszText );
// format should be 99/99/9999.
if( lstrlen( pszText ) != 10 )
return false;
return _istdigit( pszText[ 0 ] )
&& _istdigit( pszText[ 1 ] )
&& pszText[ 2 ] == _T('/')
&& _istdigit( pszText[ 3 ] )
&& _istdigit( pszText[ 4 ] )
&& pszText[ 5 ] == _T('/')
&& _istdigit( pszText[ 6 ] )
&& _istdigit( pszText[ 7 ] )
&& _istdigit( pszText[ 8 ] )
&& _istdigit( pszText[ 9 ] );
}
int DateCompare( const CString& strDate1, const CString& strDate2 )
{
const int iYear1 = watoi( strDate1.Mid( 6, 4 ) );
const int iYear2 = watoi( strDate2.Mid( 6, 4 ) );
if( iYear1 < iYear2 )
return -1;
if( iYear1 > iYear2 )
return 1;
const int iMonth1 = watoi( strDate1.Mid( 3, 2 ) );
const int iMonth2 = watoi( strDate2.Mid( 3, 2 ) );
if( iMonth1 < iMonth2 )
return -1;
if( iMonth1 > iMonth2 )
return 1;
const int iDay1 = watoi( strDate1.Mid( 0, 2 ) );
const int iDay2 = watoi( strDate2.Mid( 0, 2 ) );
if( iDay1 < iDay2 )
return -1;
if( iDay1 > iDay2 )
return 1;
return 0;
}
int CALLBACK CSortListCtrl::CompareFunction( LPARAM lParam1, LPARAM lParam2, LPARAM lParamData )
{
CSortListCtrl* pListCtrl = reinterpret_cast<CSortListCtrl*>( lParamData );
ASSERT( pListCtrl->IsKindOf( RUNTIME_CLASS( CListCtrl ) ) );
ItemData* pid1 = reinterpret_cast<ItemData*>( lParam1 );
ItemData* pid2 = reinterpret_cast<ItemData*>( lParam2 );
ASSERT( pid1 );
ASSERT( pid2 );
LPCTSTR pszText1 = pid1->arrpsz[ pListCtrl->m_iSortColumn ];
LPCTSTR pszText2 = pid2->arrpsz[ pListCtrl->m_iSortColumn ];
ASSERT_VALID_STRING( pszText1 );
ASSERT_VALID_STRING( pszText2 );
if( IsNumber( pszText1 ) )
return pListCtrl->m_bSortAscending ? NumberCompare( pszText1, pszText2 ) : NumberCompare( pszText2, pszText1 );
else if( IsDate( pszText1 ) )
return pListCtrl->m_bSortAscending ? DateCompare( pszText1, pszText2 ) : DateCompare( pszText2, pszText1 );
else
// text.
return pListCtrl->m_bSortAscending ? lstrcmp( pszText1, pszText2 ) : lstrcmp( pszText2, pszText1 );
}
void CSortListCtrl::OnColumnClick( NMHDR* pNMHDR, LRESULT* pResult )
{
if ( m_bIsAdding )
{
*pResult = 0;
return;
}
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
const int iColumn = pNMListView->iSubItem;
// if it's a second click on the same column then reverse the sort order,
// otherwise sort the new column in ascending order.
Sort( iColumn, iColumn == m_iSortColumn ? !m_bSortAscending : TRUE );
*pResult = 0;
}
void CSortListCtrl::Sort( int iColumn, BOOL bAscending )
{
m_iSortColumn = iColumn;
m_bSortAscending = bAscending;
// show the appropriate arrow in the header control.
m_ctlHeader.SetSortArrow( m_iSortColumn, m_bSortAscending );
VERIFY( SortItems( CompareFunction, reinterpret_cast<DWORD>( this ) ) );
}
void CSortListCtrl::LoadColumnInfo()
{
// you must call this after setting the column headings.
ASSERT( m_iNumColumns > 0 );
CString strKey;
strKey.Format( _T("%d"), GetDlgCtrlID() );
UINT nBytes = 0;
BYTE* buf = NULL;
if( AfxGetApp()->GetProfileBinary( g_pszSection, strKey, &buf, &nBytes ) )
{
if( nBytes > 0 )
{
CMemFile memFile( buf, nBytes );
CArchive ar( &memFile, CArchive::load );
m_ctlHeader.Serialize( ar );
ar.Close();
m_ctlHeader.Invalidate();
}
delete[] buf;
}
}
void CSortListCtrl::SaveColumnInfo()
{
ASSERT( m_iNumColumns > 0 );
CString strKey;
strKey.Format( _T("%d"), GetDlgCtrlID() );
CMemFile memFile;
CArchive ar( &memFile, CArchive::store );
m_ctlHeader.Serialize( ar );
ar.Close();
DWORD dwLen = (DWORD)memFile.GetLength();
BYTE* buf = memFile.Detach();
VERIFY( AfxGetApp()->WriteProfileBinary( g_pszSection, strKey, buf, dwLen ) );
free( buf );
}
void CSortListCtrl::OnDestroy()
{
for( int iItem = 0; iItem < GetItemCount(); iItem ++ )
FreeItemMemory( iItem );
CListCtrl::OnDestroy();
}
BOOL CSortListCtrl::SetItemText( int nItem, int nSubItem, LPCTSTR lpszText )
{
if( !CListCtrl::SetItemText( nItem, nSubItem, lpszText ) )
return FALSE;
LPTSTR* arrpsz = GetTextArray( nItem );
LPTSTR pszText = arrpsz[ nSubItem ];
delete[] pszText;
pszText = new TCHAR[ lstrlen( lpszText ) + 1 ];
(void)lstrcpy( pszText, lpszText );
arrpsz[ nSubItem ] = pszText;
return TRUE;
}
BOOL CSortListCtrl::SetItemData( int nItem, DWORD dwData )
{
if( nItem >= GetItemCount() )
return FALSE;
ItemData* pid = reinterpret_cast<ItemData*>( CListCtrl::GetItemData( nItem ) );
ASSERT( pid );
pid->dwData = dwData;
return TRUE;
}
DWORD CSortListCtrl::GetItemData( int nItem ) const
{
ASSERT( nItem < GetItemCount() );
ItemData* pid = reinterpret_cast<ItemData*>( CListCtrl::GetItemData( nItem ) );
ASSERT( pid );
return pid->dwData;
}
BOOL CSortListCtrl::SetTextArray( int iItem, LPTSTR* arrpsz )
{
ASSERT( CListCtrl::GetItemData( iItem ) == NULL );
ItemData* pid = new ItemData;
pid->arrpsz = arrpsz;
return CListCtrl::SetItemData( iItem, reinterpret_cast<DWORD>( pid ) );
}
LPTSTR* CSortListCtrl::GetTextArray( int iItem ) const
{
ASSERT( iItem < GetItemCount() );
ItemData* pid = reinterpret_cast<ItemData*>( CListCtrl::GetItemData( iItem ) );
return pid->arrpsz;
}
void CSortListCtrl::OnCustomdrawMyList ( NMHDR* pNMHDR, LRESULT* pResult )
{
NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>( pNMHDR );
// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;
// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
// This is the prepaint stage for an item. Here's where we set the
// item's text color. Our return value will tell Windows to draw the
// item itself, but it will use the new color we set here.
// We'll cycle the colors through red, green, and light blue.
COLORREF crText = RGB(160,0,64);
int i = watoi(GetItemText(pLVCD->nmcd.dwItemSpec,0));
// odd lines blue, even red
if (i%2 == 1)
{
crText = RGB(0,32,128);
}
/*switch(i)
{
case 0: crText = RGB(0,0,255);break;
case 1: crText = RGB(100,100,100);break;
case 2: crText = RGB(200,0,85);break;
case 3: crText = RGB(200,85,0);break;
case 4: crText = RGB(255,0,0);break;
default:crText = RGB(0,255,0);
}*/
// Store the color back in the NMLVCUSTOMDRAW struct.
pLVCD->clrText = crText;
// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}
BOOL CSortListCtrl::PreTranslateMessage(MSG* pMsg)
{
//if( pMsg->message == WM_KEYDOWN )
// {
// switch ( pMsg->wParam )
// {
// case 0x11: m_CtrlButtonDown = TRUE;break; // CTRL down
// case 0x41: if ( m_CtrlButtonDown ) // A down
// SelectAll();
// break;
// case 0x43: if ( m_CtrlButtonDown ) // C down
// Copy();
// break;
// case 0x2E: Delete();break; // DEL down
// }
// }
// if( pMsg->message == WM_KEYUP )
// m_CtrlButtonDown = FALSE;
return CListCtrl::PreTranslateMessage(pMsg);
}
void CSortListCtrl::Copy()
{
if ( m_bIsAdding ) return;
CString str;
POSITION pos = GetFirstSelectedItemPosition();
if (pos == NULL)
return;
int col = GetHeaderCtrl()->GetItemCount();
while (pos)
{
int nItem = GetNextSelectedItem(pos);
int j(0);
for(;j<col-1;j++)
{
str+=GetItemText(nItem,j);
str+=" - ";
}
str+=GetItemText(nItem,j);
str+="\r\n";
}
// Copy text into clipboard
if (OpenClipboard())
{
EmptyClipboard();
HANDLE hMem = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE ,
str.GetLength()+1);
if (!hMem) return;
LPSTR lpStr = (LPSTR)GlobalLock(hMem);
char temp[100] = {'\0'};
ConvertToChar(str, temp);
strcpy_s(lpStr, str.GetLength()+1, temp);
GlobalUnlock(hMem);
VERIFY(SetClipboardData(CF_TEXT, hMem));
CloseClipboard();
}
}
void CSortListCtrl::SelectAll()
{
if ( m_bIsAdding ) return;
LV_ITEM theItem;
theItem.mask = LVIF_IMAGE | LVIF_STATE;
theItem.iItem = -1;
theItem.iSubItem = 0;
theItem.state = LVIS_SELECTED;
theItem.stateMask = 2;
SetItemState(-1, &theItem);
SetFocus();
}
void CSortListCtrl::Delete()
{
//CList<int,int> itemList;
POSITION pos;
while ( (pos = GetFirstSelectedItemPosition()) != NULL )
{
int nItem = GetNextSelectedItem(pos);
DeleteItem(nItem);
}
}
void CSortListCtrl::SetPriorityColor(int nPriority,COLORREF nColor)
{
m_colors[nPriority] = nColor;
}