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.

387 lines
9.0 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: 08/10/2003
  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 "..\Version.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. CLgListWnd::CLgListWnd()
  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. m_nItemActive = -1;
  33. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgListWnd::CLgListWnd") );
  34. }
  35. CLgListWnd::~CLgListWnd()
  36. {
  37. RemoveAll();
  38. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgListWnd::~CLgListWnd") );
  39. }
  40. BEGIN_MESSAGE_MAP(CLgListWnd, CWnd)
  41. //{{AFX_MSG_MAP(CLgListWnd)
  42. ON_WM_PAINT()
  43. ON_WM_VSCROLL()
  44. ON_WM_SIZE()
  45. //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47. BOOL CLgListWnd::Create( CWnd *pParentWnd, int x, int y, int nWidth, int nHeight )
  48. {
  49. CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_ARROW ), NULL );
  50. CRect rect( x, y, x + nWidth, y + nHeight );
  51. if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_CLIPSIBLINGS, rect, pParentWnd, 0 ) )
  52. {
  53. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgListWnd::Create"), _T("CWnd::Create failed") );
  54. return FALSE;
  55. }
  56. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgListWnd::Create"), _T("Succeeded") );
  57. return TRUE;
  58. }
  59. BOOL CLgListWnd::SetItemSize( CSize size )
  60. {
  61. if( size.cx <= 0 || size.cy <= 0 || !GetSafeHwnd() )
  62. return FALSE;
  63. m_ItemSize = size;
  64. CRect rect;
  65. GetClientRect( &rect );
  66. // increase rect if scrollbar is visible
  67. if( GetWindowLong( GetSafeHwnd(), GWL_STYLE ) & WS_VSCROLL )
  68. rect.right += GetSystemMetrics( SM_CXVSCROLL );
  69. int nColumns = rect.Width() / m_ItemSize.cx;
  70. if( nColumns < 1 )
  71. nColumns = 1;
  72. BOOL bScrollVert = FALSE;
  73. int nRows = rect.Height() / m_ItemSize.cy;
  74. if( nRows < 1 )
  75. nRows = 1;
  76. if( nRows * nColumns < m_ArrayItem.GetSize() )
  77. {
  78. bScrollVert = TRUE;
  79. rect.right -= GetSystemMetrics( SM_CXVSCROLL );
  80. }
  81. int xSpace = ( rect.Width() - ( nColumns * m_ItemSize.cx ) ) / ( nColumns + 1 );
  82. int ySpace = ( rect.Height() - ( nRows * m_ItemSize.cy ) ) / ( nRows + 1 );
  83. m_Dimensions.cx = nColumns;
  84. m_Dimensions.cy = nRows;
  85. m_Space.cx = xSpace;
  86. m_Space.cy = ySpace;
  87. int nMaxVisibleItems = nRows * nColumns;
  88. m_nItemVisibleFirst = 0;
  89. m_nItemVisibleLast = m_nItemVisibleFirst + (nMaxVisibleItems > m_ArrayItem.GetSize() ? m_ArrayItem.GetSize() : nMaxVisibleItems);
  90. m_nItemScrollMin = m_nItemVisibleFirst;
  91. m_nItemScrollMax = m_ArrayItem.GetSize() - m_nItemVisibleLast;
  92. m_nItemScrollPos = m_nItemScrollMin;
  93. ShowScrollBar( SB_VERT, bScrollVert );
  94. SetScrollRange( SB_VERT, m_nItemScrollMin, m_nItemScrollMax, FALSE );
  95. SetScrollPos( SB_VERT, m_nItemScrollPos );
  96. // InvalidateRect( NULL );
  97. MoveItems();
  98. return TRUE;
  99. }
  100. void CLgListWnd::OnPaint()
  101. {
  102. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgListWnd::OnPaint"), _T("Started...") );
  103. CPaintDC dc(this);
  104. int nCount = m_nItemVisibleFirst;
  105. CDC *pDC = &dc;
  106. CRect rect;
  107. GetClientRect( &rect );
  108. m_MemoryDC.FillSolidRect( &rect, GetSysColor(COLOR_BTNFACE)/*RGB(0xAF,0xAF,0xAF)*/ );
  109. for( int i = 0; i < m_ArrayItem.GetSize(); i++ )
  110. {
  111. CRect ItemRect;
  112. if( GetItemRect( i, &ItemRect ) )
  113. {
  114. OnItemDraw( &m_MemoryDC, &ItemRect, m_ArrayItem[i], m_nItemActive == i );
  115. }
  116. }
  117. pDC->BitBlt( 0, 0, rect.Width(), rect.Height(), &m_MemoryDC, 0, 0, SRCCOPY );
  118. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgListWnd::OnPaint"), _T("Done") );
  119. }
  120. void CLgListWnd::OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar )
  121. {
  122. int nMaxVisibleItems = m_Dimensions.cx * m_Dimensions.cy;
  123. int nScroll = m_nItemScrollPos;
  124. switch( nSBCode )
  125. {
  126. case SB_BOTTOM: // Scroll to bottom.
  127. nScroll = m_nItemScrollMax;
  128. break;
  129. case SB_ENDSCROLL: // End scroll.
  130. break;
  131. case SB_LINEDOWN: // Scroll one line down.
  132. if( nScroll < m_nItemScrollMax )
  133. nScroll++;
  134. break;
  135. case SB_LINEUP: // Scroll one line up.
  136. if( nScroll > m_nItemScrollMin )
  137. nScroll--;
  138. break;
  139. case SB_PAGEDOWN: // Scroll one page down.
  140. nScroll += nMaxVisibleItems;
  141. if( nScroll > m_nItemScrollMax )
  142. nScroll = m_nItemScrollMax;
  143. break;
  144. case SB_PAGEUP: // Scroll one page up.
  145. nScroll -= nMaxVisibleItems;
  146. if( nScroll < m_nItemScrollMin )
  147. nScroll = m_nItemScrollMin;
  148. break;
  149. case SB_THUMBPOSITION: // Scroll to the absolute position. The current position is provided in nPos.
  150. nScroll = nPos;
  151. break;
  152. case SB_THUMBTRACK: // Drag scroll box to specified position. The current position is provided in nPos.
  153. nScroll = nPos;
  154. break;
  155. case SB_TOP: // Scroll to top.
  156. nScroll = m_nItemScrollMin;
  157. break;
  158. }
  159. if( nScroll != m_nItemScrollPos )
  160. {
  161. m_nItemVisibleFirst = m_nItemScrollPos = nScroll;
  162. m_nItemVisibleLast = m_nItemScrollPos + (nMaxVisibleItems > m_ArrayItem.GetSize() ? m_ArrayItem.GetSize() : nMaxVisibleItems);
  163. SetScrollPos( SB_VERT, m_nItemScrollPos, TRUE );
  164. //InvalidateRect( NULL, FALSE );
  165. MoveItems();
  166. }
  167. CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
  168. }
  169. BOOL CLgListWnd::GetItemRect( INT nIndex, CRect* pRect )
  170. {
  171. if( pRect )
  172. {
  173. int i = nIndex - m_nItemVisibleFirst;
  174. int r = i / m_Dimensions.cx;
  175. int c = i % m_Dimensions.cx;
  176. pRect->SetRect( m_Space.cx * ( c + 1 ) + m_ItemSize.cx * c,
  177. m_Space.cy * ( r + 1 ) + m_ItemSize.cy * r,
  178. m_Space.cx * ( c + 1 ) + m_ItemSize.cx * (c + 1),
  179. m_Space.cy * ( r + 1 ) + m_ItemSize.cy * (r + 1) );
  180. }
  181. if( nIndex < m_nItemVisibleFirst || nIndex >= m_nItemVisibleLast )
  182. return FALSE;
  183. return TRUE;
  184. }
  185. void CLgListWnd::MoveItems()
  186. {
  187. for( int i = 0; i < m_ArrayItem.GetSize(); i++ )
  188. {
  189. CRect rect;
  190. BOOL bVisible = GetItemRect( i, &rect );
  191. OnItemMove( m_ArrayItem[i], &rect, bVisible, m_nItemActive == i );
  192. }
  193. }
  194. BOOL CLgListWnd::Activate( void* pItem )
  195. {
  196. for( INT i = 0; i < m_ArrayItem.GetSize(); i++ )
  197. {
  198. if( m_ArrayItem[i] == pItem )
  199. {
  200. if( m_nItemActive != i )
  201. {
  202. if( m_nItemActive >=0 && m_nItemActive < m_ArrayItem.GetSize() )
  203. OnItemActivate( m_ArrayItem[m_nItemActive], FALSE );
  204. m_nItemActive = i;
  205. OnItemActivate( m_ArrayItem[m_nItemActive], TRUE );
  206. }
  207. return TRUE;
  208. }
  209. }
  210. return FALSE;
  211. }
  212. void CLgListWnd::OnSize(UINT nType, int cx, int cy)
  213. {
  214. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgListWnd::OnSize"), _T("Started...") );
  215. CWnd::OnSize(nType, cx, cy);
  216. m_MemoryDC.Create( cx, cy, RGB(0xAF,0xAF,0xAF) );
  217. SetItemSize( m_ItemSize );
  218. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgListWnd::OnSize"), _T("Done") );
  219. }
  220. BOOL CLgListWnd::IsExist( void* pItem )
  221. {
  222. if( !pItem )
  223. return FALSE;
  224. for( INT i = 0; i < m_ArrayItem.GetSize(); i++ )
  225. {
  226. if( m_ArrayItem[i] == pItem )
  227. return TRUE;
  228. }
  229. return FALSE;
  230. }
  231. BOOL CLgListWnd::Add( void* pItem )
  232. {
  233. if( !pItem )
  234. return FALSE;
  235. try
  236. {
  237. m_ArrayItem.Add( pItem );
  238. OnItemAdd( pItem );
  239. return SetItemSize( m_ItemSize );
  240. }
  241. catch( CMemoryException *e )
  242. {
  243. e;
  244. ASSERT( NULL );
  245. }
  246. return FALSE;
  247. }
  248. void CLgListWnd::RemoveAll()
  249. {
  250. while( m_ArrayItem.GetSize() )
  251. Remove( m_ArrayItem[0] );
  252. }
  253. BOOL CLgListWnd::Remove( void* pItem )
  254. {
  255. if( !pItem )
  256. return FALSE;
  257. for( int i = 0; i < m_ArrayItem.GetSize(); i++ )
  258. {
  259. if( m_ArrayItem[i] == pItem )
  260. {
  261. m_ArrayItem.RemoveAt( i );
  262. OnItemRemove( pItem );
  263. SetItemSize( m_ItemSize );
  264. return TRUE;
  265. }
  266. }
  267. return FALSE;
  268. }
  269. BOOL CLgListWnd::DestroyWindow()
  270. {
  271. while( m_ArrayItem.GetSize() )
  272. Remove( m_ArrayItem[0] );
  273. return CWnd::DestroyWindow();
  274. }
  275. void* CLgListWnd::GetItemFromPoint( CPoint *pPoint )
  276. {
  277. if( !pPoint )
  278. return NULL;
  279. int xItemSpace = ( m_Space.cx + m_ItemSize.cx );
  280. int yItemSpace = ( m_Space.cy + m_ItemSize.cy );
  281. int xSpace = pPoint->x % xItemSpace;
  282. int ySpace = pPoint->y % yItemSpace;
  283. if( xSpace < m_Space.cx || ySpace < m_Space.cy )
  284. return NULL;
  285. int xItem = pPoint->x / xItemSpace;
  286. int yItem = pPoint->y / yItemSpace;
  287. int nIndex = xItem + yItem * m_Dimensions.cx + m_nItemVisibleFirst;
  288. return ( nIndex < m_nItemVisibleLast ) ? m_ArrayItem[ nIndex ] : NULL;
  289. }
  290. void* CLgListWnd::GetItemActive()
  291. {
  292. return ( m_nItemActive >=0 && m_nItemActive < m_ArrayItem.GetSize() ) ? m_ArrayItem[ m_nItemActive ] : NULL;
  293. }
  294. void* CLgListWnd::GetItem( INT nIndex )
  295. {
  296. return ( nIndex >=0 && nIndex < m_ArrayItem.GetSize() ) ? m_ArrayItem[ nIndex ] : NULL;
  297. }