|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 05/08/2001
// PROJECT:
// SUBSYSTEM:
// FILE: ListWnd.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of ListWnd.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "chicago.h"
#include "ListWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;#endif
CListWnd::CListWnd(){ m_ItemSize = CSize(0,0); m_Space = CSize(0,0); m_Dimensions = CSize(0,0); m_nItemVisibleFirst = 0; m_nItemVisibleLast = 0; m_nItemScrollMin = 0; m_nItemScrollMax = 0; m_nItemScrollPos = 0;}
CListWnd::~CListWnd(){ RemoveAll();}
BEGIN_MESSAGE_MAP(CListWnd, CWnd) //{{AFX_MSG_MAP(CListWnd)
ON_WM_PAINT() ON_WM_VSCROLL() ON_WM_SIZE() //}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CListWnd::Create( CWnd *pParentWnd, int x, int y, int nWidth, int nHeight ){ CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_ARROW ), NULL );
CRect rect( x, y, x + nWidth, y + nHeight );
if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_DLGFRAME | WS_CLIPSIBLINGS, rect, pParentWnd, 0 ) ) return FALSE;
return TRUE;}
BOOL CListWnd::SetItemSize( CSize size ){ if( size.cx <= 0 || size.cy <= 0 ) return FALSE;
m_ItemSize = size;
CRect rect; GetClientRect( &rect );
// increase rect if scrollbar is visible
if( GetWindowLong( GetSafeHwnd(), GWL_STYLE ) & WS_VSCROLL ) rect.right += GetSystemMetrics( SM_CXVSCROLL );
int nColumns = rect.Width() / m_ItemSize.cx; if( nColumns < 1 ) nColumns = 1;
BOOL bScrollVert = FALSE;
int nRows = rect.Height() / m_ItemSize.cy; if( nRows < 1 ) nRows = 1;
if( nRows * nColumns < m_ArrayItem.GetSize() ) { bScrollVert = TRUE; rect.right -= GetSystemMetrics( SM_CXVSCROLL ); }
int xSpace = ( rect.Width() - ( nColumns * m_ItemSize.cx ) ) / ( nColumns + 1 ); int ySpace = ( rect.Height() - ( nRows * m_ItemSize.cy ) ) / ( nRows + 1 );
m_Dimensions.cx = nColumns; m_Dimensions.cy = nRows;
m_Space.cx = xSpace; m_Space.cy = ySpace;
int nMaxVisibleItems = nRows * nColumns;
m_nItemVisibleFirst = 0; m_nItemVisibleLast = m_nItemVisibleFirst + (nMaxVisibleItems > m_ArrayItem.GetSize() ? m_ArrayItem.GetSize() : nMaxVisibleItems); m_nItemScrollMin = m_nItemVisibleFirst; m_nItemScrollMax = m_ArrayItem.GetSize() - m_nItemVisibleLast; m_nItemScrollPos = m_nItemScrollMin;
ShowScrollBar( SB_VERT, bScrollVert ); SetScrollRange( SB_VERT, m_nItemScrollMin, m_nItemScrollMax, FALSE ); SetScrollPos( SB_VERT, m_nItemScrollPos );
// InvalidateRect( NULL );
MoveItems();
return TRUE;}
void CListWnd::OnPaint() { CPaintDC dc(this); int nCount = m_nItemVisibleFirst;
CDC *pDC = &dc;
CRect rect; GetClientRect( &rect );
m_MemoryDC.FillSolidRect( &rect, RGB(0xAF,0xAF,0xAF) );
for( int i = 0; i < m_ArrayItem.GetSize(); i++ ) { CRect ItemRect; if( GetItemRect( i, &ItemRect ) ) { OnItemDraw( &m_MemoryDC, &ItemRect, m_ArrayItem[i] ); } }
pDC->BitBlt( 0, 0, rect.Width(), rect.Height(), &m_MemoryDC, 0, 0, SRCCOPY );}
void CListWnd::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar ) { int nMaxVisibleItems = m_Dimensions.cx * m_Dimensions.cy;
int nScroll = m_nItemScrollPos;
switch( nSBCode ) { case SB_BOTTOM: // Scroll to bottom.
nScroll = m_nItemScrollMax; break;
case SB_ENDSCROLL: // End scroll.
break;
case SB_LINEDOWN: // Scroll one line down.
if( nScroll < m_nItemScrollMax ) nScroll++; break;
case SB_LINEUP: // Scroll one line up.
if( nScroll > m_nItemScrollMin ) nScroll--; break;
case SB_PAGEDOWN: // Scroll one page down.
nScroll += nMaxVisibleItems; if( nScroll > m_nItemScrollMax ) nScroll = m_nItemScrollMax; break;
case SB_PAGEUP: // Scroll one page up.
nScroll -= nMaxVisibleItems; if( nScroll < m_nItemScrollMin ) nScroll = m_nItemScrollMin; break;
case SB_THUMBPOSITION: // Scroll to the absolute position. The current position is provided in nPos.
nScroll = nPos; break;
case SB_THUMBTRACK: // Drag scroll box to specified position. The current position is provided in nPos.
nScroll = nPos; break;
case SB_TOP: // Scroll to top.
nScroll = m_nItemScrollMin; break; }
if( nScroll != m_nItemScrollPos ) { m_nItemVisibleFirst = m_nItemScrollPos = nScroll; m_nItemVisibleLast = m_nItemScrollPos + (nMaxVisibleItems > m_ArrayItem.GetSize() ? m_ArrayItem.GetSize() : nMaxVisibleItems);
SetScrollPos( SB_VERT, m_nItemScrollPos, TRUE ); //InvalidateRect( NULL, FALSE );
MoveItems(); }
CWnd::OnVScroll(nSBCode, nPos, pScrollBar);}
BOOL CListWnd::GetItemRect( INT nIndex, CRect* pRect ){ if( nIndex < m_nItemVisibleFirst || nIndex >= m_nItemVisibleLast ) return FALSE;
if( !pRect ) return FALSE;
nIndex -= m_nItemVisibleFirst;
int r = nIndex / m_Dimensions.cx; int c = nIndex % m_Dimensions.cx;
pRect->SetRect( m_Space.cx * ( c + 1 ) + m_ItemSize.cx * c, m_Space.cy * ( r + 1 ) + m_ItemSize.cy * r, m_Space.cx * ( c + 1 ) + m_ItemSize.cx * (c + 1), m_Space.cy * ( r + 1 ) + m_ItemSize.cy * (r + 1) );
return TRUE;}
void CListWnd::MoveItems(){ for( int i = 0; i < m_ArrayItem.GetSize(); i++ ) { CRect rect;
BOOL bVisible = GetItemRect( i, &rect );
OnItemMove( m_ArrayItem[i], &rect, bVisible ); }}
void CListWnd::OnSize(UINT nType, int cx, int cy) { CWnd::OnSize(nType, cx, cy); m_MemoryDC.Create( cx, cy, RGB(0xAF,0xAF,0xAF) );
SetItemSize( m_ItemSize );}
BOOL CListWnd::Add( void* pItem ){ if( !pItem ) return FALSE;
try { m_ArrayItem.Add( pItem );
OnItemAdd( pItem );
return SetItemSize( m_ItemSize ); } catch( CMemoryException *e ) { e; ASSERT( NULL ); }
return FALSE;}
void CListWnd::RemoveAll(){ while( m_ArrayItem.GetSize() ) Remove( m_ArrayItem[0] );}
BOOL CListWnd::Remove( void* pItem ){ if( !pItem ) return FALSE;
for( int i = 0; i < m_ArrayItem.GetSize(); i++ ) { if( m_ArrayItem[i] == pItem ) { m_ArrayItem.RemoveAt( i );
OnItemRemove( pItem );
SetItemSize( m_ItemSize );
return TRUE; } }
return FALSE;}
BOOL CListWnd::DestroyWindow() { while( m_ArrayItem.GetSize() ) Remove( m_ArrayItem[0] ); return CWnd::DestroyWindow();}
void* CListWnd::GetItemFromPoint( CPoint *pPoint ){ if( !pPoint ) return NULL;
int xItemSpace = ( m_Space.cx + m_ItemSize.cx ); int yItemSpace = ( m_Space.cy + m_ItemSize.cy );
int xSpace = pPoint->x % xItemSpace; int ySpace = pPoint->y % yItemSpace;
if( xSpace < m_Space.cx || ySpace < m_Space.cy ) return NULL;
int xItem = pPoint->x / xItemSpace; int yItem = pPoint->y / yItemSpace;
int nIndex = xItem + yItem * m_Dimensions.cx + m_nItemVisibleFirst;
return ( nIndex < m_nItemVisibleLast ) ? m_ArrayItem[ nIndex ] : NULL;}
|