Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

328 lines
7.2 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 05/08/2001
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: ListWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of ListWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "chicago.h"
  16. #include "ListWnd.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. CListWnd::CListWnd()
  23. {
  24. m_ItemSize = CSize(0,0);
  25. m_Space = CSize(0,0);
  26. m_Dimensions = CSize(0,0);
  27. m_nItemVisibleFirst = 0;
  28. m_nItemVisibleLast = 0;
  29. m_nItemScrollMin = 0;
  30. m_nItemScrollMax = 0;
  31. m_nItemScrollPos = 0;
  32. }
  33. CListWnd::~CListWnd()
  34. {
  35. RemoveAll();
  36. }
  37. BEGIN_MESSAGE_MAP(CListWnd, CWnd)
  38. //{{AFX_MSG_MAP(CListWnd)
  39. ON_WM_PAINT()
  40. ON_WM_VSCROLL()
  41. ON_WM_SIZE()
  42. //}}AFX_MSG_MAP
  43. END_MESSAGE_MAP()
  44. BOOL CListWnd::Create( CWnd *pParentWnd, int x, int y, int nWidth, int nHeight )
  45. {
  46. CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_ARROW ), NULL );
  47. CRect rect( x, y, x + nWidth, y + nHeight );
  48. if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_DLGFRAME | WS_CLIPSIBLINGS, rect, pParentWnd, 0 ) )
  49. return FALSE;
  50. return TRUE;
  51. }
  52. BOOL CListWnd::SetItemSize( CSize size )
  53. {
  54. if( size.cx <= 0 || size.cy <= 0 )
  55. return FALSE;
  56. m_ItemSize = size;
  57. CRect rect;
  58. GetClientRect( &rect );
  59. // increase rect if scrollbar is visible
  60. if( GetWindowLong( GetSafeHwnd(), GWL_STYLE ) & WS_VSCROLL )
  61. rect.right += GetSystemMetrics( SM_CXVSCROLL );
  62. int nColumns = rect.Width() / m_ItemSize.cx;
  63. if( nColumns < 1 )
  64. nColumns = 1;
  65. BOOL bScrollVert = FALSE;
  66. int nRows = rect.Height() / m_ItemSize.cy;
  67. if( nRows < 1 )
  68. nRows = 1;
  69. if( nRows * nColumns < m_ArrayItem.GetSize() )
  70. {
  71. bScrollVert = TRUE;
  72. rect.right -= GetSystemMetrics( SM_CXVSCROLL );
  73. }
  74. int xSpace = ( rect.Width() - ( nColumns * m_ItemSize.cx ) ) / ( nColumns + 1 );
  75. int ySpace = ( rect.Height() - ( nRows * m_ItemSize.cy ) ) / ( nRows + 1 );
  76. m_Dimensions.cx = nColumns;
  77. m_Dimensions.cy = nRows;
  78. m_Space.cx = xSpace;
  79. m_Space.cy = ySpace;
  80. int nMaxVisibleItems = nRows * nColumns;
  81. m_nItemVisibleFirst = 0;
  82. m_nItemVisibleLast = m_nItemVisibleFirst + (nMaxVisibleItems > m_ArrayItem.GetSize() ? m_ArrayItem.GetSize() : nMaxVisibleItems);
  83. m_nItemScrollMin = m_nItemVisibleFirst;
  84. m_nItemScrollMax = m_ArrayItem.GetSize() - m_nItemVisibleLast;
  85. m_nItemScrollPos = m_nItemScrollMin;
  86. ShowScrollBar( SB_VERT, bScrollVert );
  87. SetScrollRange( SB_VERT, m_nItemScrollMin, m_nItemScrollMax, FALSE );
  88. SetScrollPos( SB_VERT, m_nItemScrollPos );
  89. // InvalidateRect( NULL );
  90. MoveItems();
  91. return TRUE;
  92. }
  93. void CListWnd::OnPaint()
  94. {
  95. CPaintDC dc(this);
  96. int nCount = m_nItemVisibleFirst;
  97. CDC *pDC = &dc;
  98. CRect rect;
  99. GetClientRect( &rect );
  100. m_MemoryDC.FillSolidRect( &rect, RGB(0xAF,0xAF,0xAF) );
  101. for( int i = 0; i < m_ArrayItem.GetSize(); i++ )
  102. {
  103. CRect ItemRect;
  104. if( GetItemRect( i, &ItemRect ) )
  105. {
  106. OnItemDraw( &m_MemoryDC, &ItemRect, m_ArrayItem[i] );
  107. }
  108. }
  109. pDC->BitBlt( 0, 0, rect.Width(), rect.Height(), &m_MemoryDC, 0, 0, SRCCOPY );
  110. }
  111. void CListWnd::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar )
  112. {
  113. int nMaxVisibleItems = m_Dimensions.cx * m_Dimensions.cy;
  114. int nScroll = m_nItemScrollPos;
  115. switch( nSBCode )
  116. {
  117. case SB_BOTTOM: // Scroll to bottom.
  118. nScroll = m_nItemScrollMax;
  119. break;
  120. case SB_ENDSCROLL: // End scroll.
  121. break;
  122. case SB_LINEDOWN: // Scroll one line down.
  123. if( nScroll < m_nItemScrollMax )
  124. nScroll++;
  125. break;
  126. case SB_LINEUP: // Scroll one line up.
  127. if( nScroll > m_nItemScrollMin )
  128. nScroll--;
  129. break;
  130. case SB_PAGEDOWN: // Scroll one page down.
  131. nScroll += nMaxVisibleItems;
  132. if( nScroll > m_nItemScrollMax )
  133. nScroll = m_nItemScrollMax;
  134. break;
  135. case SB_PAGEUP: // Scroll one page up.
  136. nScroll -= nMaxVisibleItems;
  137. if( nScroll < m_nItemScrollMin )
  138. nScroll = m_nItemScrollMin;
  139. break;
  140. case SB_THUMBPOSITION: // Scroll to the absolute position. The current position is provided in nPos.
  141. nScroll = nPos;
  142. break;
  143. case SB_THUMBTRACK: // Drag scroll box to specified position. The current position is provided in nPos.
  144. nScroll = nPos;
  145. break;
  146. case SB_TOP: // Scroll to top.
  147. nScroll = m_nItemScrollMin;
  148. break;
  149. }
  150. if( nScroll != m_nItemScrollPos )
  151. {
  152. m_nItemVisibleFirst = m_nItemScrollPos = nScroll;
  153. m_nItemVisibleLast = m_nItemScrollPos + (nMaxVisibleItems > m_ArrayItem.GetSize() ? m_ArrayItem.GetSize() : nMaxVisibleItems);
  154. SetScrollPos( SB_VERT, m_nItemScrollPos, TRUE );
  155. //InvalidateRect( NULL, FALSE );
  156. MoveItems();
  157. }
  158. CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
  159. }
  160. BOOL CListWnd::GetItemRect( INT nIndex, CRect* pRect )
  161. {
  162. if( nIndex < m_nItemVisibleFirst || nIndex >= m_nItemVisibleLast )
  163. return FALSE;
  164. if( !pRect )
  165. return FALSE;
  166. nIndex -= m_nItemVisibleFirst;
  167. int r = nIndex / m_Dimensions.cx;
  168. int c = nIndex % m_Dimensions.cx;
  169. pRect->SetRect( m_Space.cx * ( c + 1 ) + m_ItemSize.cx * c,
  170. m_Space.cy * ( r + 1 ) + m_ItemSize.cy * r,
  171. m_Space.cx * ( c + 1 ) + m_ItemSize.cx * (c + 1),
  172. m_Space.cy * ( r + 1 ) + m_ItemSize.cy * (r + 1) );
  173. return TRUE;
  174. }
  175. void CListWnd::MoveItems()
  176. {
  177. for( int i = 0; i < m_ArrayItem.GetSize(); i++ )
  178. {
  179. CRect rect;
  180. BOOL bVisible = GetItemRect( i, &rect );
  181. OnItemMove( m_ArrayItem[i], &rect, bVisible );
  182. }
  183. }
  184. void CListWnd::OnSize(UINT nType, int cx, int cy)
  185. {
  186. CWnd::OnSize(nType, cx, cy);
  187. m_MemoryDC.Create( cx, cy, RGB(0xAF,0xAF,0xAF) );
  188. SetItemSize( m_ItemSize );
  189. }
  190. BOOL CListWnd::Add( void* pItem )
  191. {
  192. if( !pItem )
  193. return FALSE;
  194. try
  195. {
  196. m_ArrayItem.Add( pItem );
  197. OnItemAdd( pItem );
  198. return SetItemSize( m_ItemSize );
  199. }
  200. catch( CMemoryException *e )
  201. {
  202. e;
  203. ASSERT( NULL );
  204. }
  205. return FALSE;
  206. }
  207. void CListWnd::RemoveAll()
  208. {
  209. while( m_ArrayItem.GetSize() )
  210. Remove( m_ArrayItem[0] );
  211. }
  212. BOOL CListWnd::Remove( void* pItem )
  213. {
  214. if( !pItem )
  215. return FALSE;
  216. for( int i = 0; i < m_ArrayItem.GetSize(); i++ )
  217. {
  218. if( m_ArrayItem[i] == pItem )
  219. {
  220. m_ArrayItem.RemoveAt( i );
  221. OnItemRemove( pItem );
  222. SetItemSize( m_ItemSize );
  223. return TRUE;
  224. }
  225. }
  226. return FALSE;
  227. }
  228. BOOL CListWnd::DestroyWindow()
  229. {
  230. while( m_ArrayItem.GetSize() )
  231. Remove( m_ArrayItem[0] );
  232. return CWnd::DestroyWindow();
  233. }
  234. void* CListWnd::GetItemFromPoint( CPoint *pPoint )
  235. {
  236. if( !pPoint )
  237. return NULL;
  238. int xItemSpace = ( m_Space.cx + m_ItemSize.cx );
  239. int yItemSpace = ( m_Space.cy + m_ItemSize.cy );
  240. int xSpace = pPoint->x % xItemSpace;
  241. int ySpace = pPoint->y % yItemSpace;
  242. if( xSpace < m_Space.cx || ySpace < m_Space.cy )
  243. return NULL;
  244. int xItem = pPoint->x / xItemSpace;
  245. int yItem = pPoint->y / yItemSpace;
  246. int nIndex = xItem + yItem * m_Dimensions.cx + m_nItemVisibleFirst;
  247. return ( nIndex < m_nItemVisibleLast ) ? m_ArrayItem[ nIndex ] : NULL;
  248. }