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.

222 lines
5.9 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: LgUIMapBackgroundThread.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgUIMapBackgroundThread.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include "LgUIMapBackgroundThread.h"
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[]=__FILE__;
  20. #define new DEBUG_NEW
  21. #endif
  22. CLgUIMapBackgroundThread::CLgUIMapBackgroundThread()
  23. {
  24. m_hMutexData = NULL;
  25. m_hMutexDraw = NULL;
  26. m_hEventDraw = NULL;
  27. m_WndRect.SetRectEmpty();
  28. m_DstRect.SetRectEmpty();
  29. m_hWnd = NULL;
  30. // log info
  31. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::CLgUIMapBackgroundThread" );
  32. }
  33. CLgUIMapBackgroundThread::~CLgUIMapBackgroundThread()
  34. {
  35. Destroy();
  36. // log info
  37. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::~CLgUIMapBackgroundThread" );
  38. }
  39. DWORD CLgUIMapBackgroundThread::ThreadProc()
  40. {
  41. HANDLE hEvents[] = { CLgThread::GetThreadStopEvent(), m_hEventDraw };
  42. CRect NewWndRect(0,0,0,0);
  43. CRect WndRect = NewWndRect;
  44. CRect DstRect = NewWndRect;
  45. while( true )
  46. {
  47. switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE ) )
  48. {
  49. default:
  50. // log info
  51. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CLgUIMapBackgroundThread::ThreadProc", "WaitForMultipleObjects returns unexpected value" );
  52. case WAIT_OBJECT_0:
  53. // nessesary to delete in the context of this thread
  54. m_MemoryDC.DeleteDC();
  55. return 0;
  56. case WAIT_OBJECT_0+1:
  57. break;
  58. }
  59. ::WaitForSingleObject( m_hMutexData, INFINITE );
  60. NewWndRect = m_WndRect;
  61. DstRect = m_DstRect;
  62. ::ReleaseMutex( m_hMutexData );
  63. ::WaitForSingleObject( m_hMutexDraw, INFINITE );
  64. if( NewWndRect.Width() != WndRect.Width() ||
  65. NewWndRect.Height() != WndRect.Height() )
  66. {
  67. WndRect = NewWndRect;
  68. m_MemoryDC.Create( WndRect.Width(), WndRect.Height() );
  69. }
  70. CLgUIMapBackground *pBackground = *this;
  71. pBackground->Draw( &m_MemoryDC, &DstRect );
  72. CRect rect[4];
  73. rect[0].SetRect( WndRect.left, WndRect.top, DstRect.left, WndRect.bottom );
  74. rect[1].SetRect( DstRect.left, WndRect.top, DstRect.right, DstRect.top );
  75. rect[2].SetRect( DstRect.right, WndRect.top, WndRect.right, WndRect.bottom );
  76. rect[3].SetRect( DstRect.left, DstRect.bottom, DstRect.right, WndRect.bottom );
  77. for( int i = 0; i < 4; i++ )
  78. if( rect[i].Width() > 0 && rect[i].Height() > 0 )
  79. m_MemoryDC.FillSolidRect( rect[i], RGB( 0,0,0 ) );
  80. ::GdiFlush();
  81. ::ReleaseMutex( m_hMutexDraw );
  82. ::PostMessage( m_hWnd, WM_BACKGROUND_DRAW_THREAD, 0, 0 );
  83. // log info
  84. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::ThreadProc", "background filled" );
  85. }
  86. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgUIMapBackgroundThread::ThreadProc"), _T("Finished") );
  87. return 0;
  88. }
  89. BOOL CLgUIMapBackgroundThread::InitDraw( LPRECT pWndRect, LPRECT pDstRect )
  90. {
  91. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapBackgroundThread::InitDraw") );
  92. if( ::WaitForSingleObject( m_hMutexData, INFINITE ) != WAIT_OBJECT_0 )
  93. return FALSE;
  94. m_WndRect = pWndRect;
  95. m_DstRect = pDstRect;
  96. ::ReleaseMutex( m_hMutexData );
  97. return ::SetEvent( m_hEventDraw );
  98. }
  99. BOOL CLgUIMapBackgroundThread::Draw( CDC *pDC )
  100. {
  101. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapBackgroundThread::Draw") );
  102. if( !pDC )
  103. return FALSE;
  104. if( ::WaitForSingleObject( m_hMutexDraw, 0UL ) != WAIT_OBJECT_0 )
  105. return FALSE;
  106. pDC->BitBlt( 0, 0, m_WndRect.right - m_WndRect.left, m_WndRect.bottom - m_WndRect.top, &m_MemoryDC, 0, 0, SRCCOPY );
  107. return ::ReleaseMutex( m_hMutexDraw );
  108. }
  109. BOOL CLgUIMapBackgroundThread::Create( CWnd *pWnd, CLgUIMapBackground *pBackground )
  110. {
  111. Destroy();
  112. if( !pBackground )
  113. {
  114. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("pBackground is NULL") );
  115. return FALSE;
  116. }
  117. Link( pBackground );
  118. if( !pWnd )
  119. {
  120. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("pWnd is NULL") );
  121. return FALSE;
  122. }
  123. m_hWnd = pWnd->GetSafeHwnd();
  124. if( !m_hWnd )
  125. {
  126. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("pWnd->GetSafeHwnd is NULL") );
  127. return FALSE;
  128. }
  129. m_hMutexData = ::CreateMutex( NULL, FALSE, NULL );
  130. if( !m_hMutexData )
  131. {
  132. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CreateMutex 1 failed") );
  133. return FALSE;
  134. }
  135. m_hMutexDraw = ::CreateMutex( NULL, FALSE, NULL );
  136. if( !m_hMutexDraw )
  137. {
  138. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CreateMutex 2 failed") );
  139. return FALSE;
  140. }
  141. m_hEventDraw = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  142. if( !m_hEventDraw )
  143. {
  144. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CreateEvent failed") );
  145. return FALSE;
  146. }
  147. if( !CLgThread::Start( FALSE ) )
  148. {
  149. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapBackgroundThread::Create"), _T("CLgThread::Start failed") );
  150. return FALSE;
  151. }
  152. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapBackgroundThread::Create", "Succeeded" );
  153. return TRUE;
  154. }
  155. void CLgUIMapBackgroundThread::Destroy()
  156. {
  157. CLgThread::Stop( TRUE );
  158. if( m_hMutexData )
  159. {
  160. ::CloseHandle( m_hMutexData );
  161. m_hMutexData = NULL;
  162. }
  163. if( m_hMutexDraw )
  164. {
  165. ::CloseHandle( m_hMutexDraw );
  166. m_hMutexDraw = NULL;
  167. }
  168. if( m_hEventDraw )
  169. {
  170. ::CloseHandle( m_hEventDraw );
  171. m_hEventDraw = NULL;
  172. }
  173. m_WndRect.SetRectEmpty();
  174. m_DstRect.SetRectEmpty();
  175. Unlink();
  176. m_hWnd = NULL;
  177. }