//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: ListWnd.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of ListWnd.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "..\Version.h" #include "ListWnd.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CLgListWnd::CLgListWnd() { 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; m_nItemActive = -1; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgListWnd::CLgListWnd") ); } CLgListWnd::~CLgListWnd() { RemoveAll(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgListWnd::~CLgListWnd") ); } BEGIN_MESSAGE_MAP(CLgListWnd, CWnd) //{{AFX_MSG_MAP(CLgListWnd) ON_WM_PAINT() ON_WM_VSCROLL() ON_WM_SIZE() //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CLgListWnd::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_CLIPSIBLINGS, rect, pParentWnd, 0 ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgListWnd::Create"), _T("CWnd::Create failed") ); return FALSE; } LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgListWnd::Create"), _T("Succeeded") ); return TRUE; } BOOL CLgListWnd::SetItemSize( CSize size ) { if( size.cx <= 0 || size.cy <= 0 || !GetSafeHwnd() ) 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 CLgListWnd::OnPaint() { LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgListWnd::OnPaint"), _T("Started...") ); CPaintDC dc(this); int nCount = m_nItemVisibleFirst; CDC *pDC = &dc; CRect rect; GetClientRect( &rect ); m_MemoryDC.FillSolidRect( &rect, GetSysColor(COLOR_BTNFACE)/*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], m_nItemActive == i ); } } pDC->BitBlt( 0, 0, rect.Width(), rect.Height(), &m_MemoryDC, 0, 0, SRCCOPY ); LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgListWnd::OnPaint"), _T("Done") ); } void CLgListWnd::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 CLgListWnd::GetItemRect( INT nIndex, CRect* pRect ) { if( pRect ) { int i = nIndex - m_nItemVisibleFirst; int r = i / m_Dimensions.cx; int c = i % 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) ); } if( nIndex < m_nItemVisibleFirst || nIndex >= m_nItemVisibleLast ) return FALSE; return TRUE; } void CLgListWnd::MoveItems() { for( int i = 0; i < m_ArrayItem.GetSize(); i++ ) { CRect rect; BOOL bVisible = GetItemRect( i, &rect ); OnItemMove( m_ArrayItem[i], &rect, bVisible, m_nItemActive == i ); } } BOOL CLgListWnd::Activate( void* pItem ) { for( INT i = 0; i < m_ArrayItem.GetSize(); i++ ) { if( m_ArrayItem[i] == pItem ) { if( m_nItemActive != i ) { if( m_nItemActive >=0 && m_nItemActive < m_ArrayItem.GetSize() ) OnItemActivate( m_ArrayItem[m_nItemActive], FALSE ); m_nItemActive = i; OnItemActivate( m_ArrayItem[m_nItemActive], TRUE ); } return TRUE; } } return FALSE; } void CLgListWnd::OnSize(UINT nType, int cx, int cy) { LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgListWnd::OnSize"), _T("Started...") ); CWnd::OnSize(nType, cx, cy); m_MemoryDC.Create( cx, cy, RGB(0xAF,0xAF,0xAF) ); SetItemSize( m_ItemSize ); LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgListWnd::OnSize"), _T("Done") ); } BOOL CLgListWnd::IsExist( void* pItem ) { if( !pItem ) return FALSE; for( INT i = 0; i < m_ArrayItem.GetSize(); i++ ) { if( m_ArrayItem[i] == pItem ) return TRUE; } return FALSE; } BOOL CLgListWnd::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 CLgListWnd::RemoveAll() { while( m_ArrayItem.GetSize() ) Remove( m_ArrayItem[0] ); } BOOL CLgListWnd::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 CLgListWnd::DestroyWindow() { while( m_ArrayItem.GetSize() ) Remove( m_ArrayItem[0] ); return CWnd::DestroyWindow(); } void* CLgListWnd::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; } void* CLgListWnd::GetItemActive() { return ( m_nItemActive >=0 && m_nItemActive < m_ArrayItem.GetSize() ) ? m_ArrayItem[ m_nItemActive ] : NULL; } void* CLgListWnd::GetItem( INT nIndex ) { return ( nIndex >=0 && nIndex < m_ArrayItem.GetSize() ) ? m_ArrayItem[ nIndex ] : NULL; }