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.

349 lines
9.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: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgUIMapWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgUIMapWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\Version.h"
  16. #include "LgUIMapWnd.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. CLgUIMapWnd::CLgUIMapWnd()
  23. {
  24. m_dScale = 1.0;
  25. m_dpShift.x = m_dpShift.y = 0.0;
  26. m_dMeterScale = 1.0;
  27. // log info
  28. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapWnd::CLgUIMapWnd" );
  29. }
  30. CLgUIMapWnd::~CLgUIMapWnd()
  31. {
  32. m_BackgroundDrawThread.Destroy();
  33. // log info
  34. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapWnd::~CLgUIMapWnd" );
  35. }
  36. BEGIN_MESSAGE_MAP(CLgUIMapWnd, CWnd)
  37. //{{AFX_MSG_MAP(CLgUIMapWnd)
  38. ON_WM_PAINT()
  39. ON_WM_SIZE()
  40. //}}AFX_MSG_MAP
  41. ON_MESSAGE( WM_BACKGROUND_DRAW_THREAD, OnWmBackgroundDrawThread )
  42. END_MESSAGE_MAP()
  43. BOOL CLgUIMapWnd::Create( CWnd *pParentWnd, CLgUIMapBackground *pBackground, int x, int y, int nWidth, int nHeight )
  44. {
  45. if( !pBackground )
  46. {
  47. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapWnd::Create"), _T("pBackground is NULL") );
  48. return FALSE;
  49. }
  50. CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_CROSS ), NULL );
  51. CRect rect( x, y, x + nWidth, y + nHeight );
  52. if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_DLGFRAME | WS_CLIPSIBLINGS, rect, pParentWnd, 0 ) )
  53. {
  54. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapWnd::Create"), _T("CWnd::Create failed") );
  55. return FALSE;
  56. }
  57. if( !m_BackgroundDrawThread.Create( this, pBackground ) )
  58. {
  59. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgUIMapWnd::Create"), _T("m_BackgroundDrawThread.Create failed") );
  60. return FALSE;
  61. }
  62. m_dMeterScale = pBackground ? pBackground->GetMeterScale() : 1.0;
  63. OnChangeViewParams();
  64. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgUIMapWnd::Create", _T("Succeeded") );
  65. return TRUE;
  66. }
  67. void CLgUIMapWnd::OnPaint()
  68. {
  69. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnPaint"), _T("Started...") );
  70. CPaintDC dc(this);
  71. CRect rect;
  72. GetClientRect( &rect );
  73. dc.BitBlt( 0, 0, rect.Width(), rect.Height(), &m_BackgroundDC, 0, 0, SRCCOPY );
  74. OnDraw( &dc );
  75. LGLOG( LOG_LEVEL_REALTIME, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnPaint"), _T("Done") );
  76. }
  77. void CLgUIMapWnd::OnSize( UINT nType, int cx, int cy )
  78. {
  79. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnSize"), _T("Started...") );
  80. CWnd::OnSize( nType, cx, cy );
  81. // reinitialize
  82. if( cx && cy )
  83. {
  84. m_BackgroundDC.Create( cx, cy, RGB(0,0xFF,0) );
  85. OnChangeViewParams();
  86. }
  87. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnSize"), _T("Done") );
  88. }
  89. LRESULT CLgUIMapWnd::OnWmBackgroundDrawThread( WPARAM, LPARAM )
  90. {
  91. // log info
  92. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapWnd::OnWmBackgroundDrawThread", "Start..." );
  93. if( m_BackgroundDrawThread.Draw( &m_BackgroundDC ) )
  94. {
  95. OnDrawBackground( &m_BackgroundDC );
  96. InvalidateRect( NULL, FALSE );
  97. // log info
  98. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapWnd::OnWmBackgroundDrawThread", "Done successfully" );
  99. return TRUE;
  100. }
  101. // log info
  102. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgUIMapWnd::OnWmBackgroundDrawThread", "Done unsuccessfully" );
  103. return FALSE;
  104. /*
  105. CMemoryDC *pMemoryDC = m_BackgroundDrawThread.GetDC();
  106. if( !pMemoryDC )
  107. return 0;
  108. CSize size = m_BackgroundDC.GetSize();
  109. m_BackgroundDC.BitBlt( 0, 0, size.cx, size.cy, pMemoryDC, 0, 0, SRCCOPY );
  110. m_BackgroundDrawThread.ReleaseDC();
  111. InvalidateRect( NULL, FALSE );
  112. return 0;
  113. */
  114. }
  115. void CLgUIMapWnd::OnDraw( CDC* )
  116. {
  117. }
  118. void CLgUIMapWnd::OnDrawBackground( CDC* )
  119. {
  120. }
  121. CLgDPoint CLgUIMapWnd::WindowToArea( CPoint *pPoint )
  122. {
  123. CRect FitFrame = GetFitFrame();
  124. return CLgDPoint( ( (double)( pPoint->x - FitFrame.left ) ) / ( FitFrame.Width() * m_dScale ) + m_dpShift.x,
  125. ( (double)( pPoint->y - FitFrame.top ) ) / ( FitFrame.Height() * m_dScale ) + m_dpShift.y );
  126. }
  127. CLgDSize CLgUIMapWnd::WindowToArea( CSize *pSize )
  128. {
  129. CRect FitFrame = GetFitFrame();
  130. return CLgDSize( ( (double)pSize->cx ) / ( FitFrame.Width() * m_dScale ),
  131. ( (double)pSize->cy ) / ( FitFrame.Height() * m_dScale ) );
  132. }
  133. CLgDRect CLgUIMapWnd::WindowToArea( CRect *pRect )
  134. {
  135. CPoint pTopLeft( pRect->TopLeft() );
  136. CPoint pBottomRight( pRect->BottomRight() );
  137. CLgDPoint dTopLeft( WindowToArea( &pTopLeft ) );
  138. CLgDPoint dBottomRight( WindowToArea( &pBottomRight ) );
  139. return CLgDRect( dTopLeft.x, dTopLeft.y, dBottomRight.x, dBottomRight.y );
  140. }
  141. CPoint CLgUIMapWnd::AreaToWindow( CLgDPoint *pDPoint )
  142. {
  143. CRect FitFrame = GetFitFrame();
  144. return CPoint( (int)( ( pDPoint->x - m_dpShift.x ) * FitFrame.Width() * m_dScale + 0.5 ) + FitFrame.left,
  145. (int)( ( pDPoint->y - m_dpShift.y ) * FitFrame.Height() * m_dScale + 0.5 ) + FitFrame.top );
  146. }
  147. CSize CLgUIMapWnd::AreaToWindow( CLgDSize *pDSize )
  148. {
  149. CRect FitFrame = GetFitFrame();
  150. return CPoint( (int)( pDSize->cx * FitFrame.Width() * m_dScale + 0.5 ),
  151. (int)( pDSize->cy * FitFrame.Height() * m_dScale + 0.5 ) );
  152. }
  153. CRect CLgUIMapWnd::AreaToWindow( CLgDRect *pDRect )
  154. {
  155. CLgDPoint dTopLeft( pDRect->left, pDRect->top );
  156. CLgDPoint dBottomRight( pDRect->right, pDRect->bottom );
  157. CPoint pTopLeft( AreaToWindow( &dTopLeft ) );
  158. CPoint pBottomRight( AreaToWindow( &dBottomRight ) );
  159. return CRect( pTopLeft, pBottomRight );
  160. }
  161. void CLgUIMapWnd::FitAreaToWindow()
  162. {
  163. m_dpShift.x = 0.0;
  164. m_dpShift.y = 0.0;
  165. m_dScale = 1.0;
  166. OnChangeViewParams();
  167. }
  168. CRect CLgUIMapWnd::GetFitFrame()
  169. {
  170. if( !GetSafeHwnd() )
  171. return CRect(0,0,0,0);
  172. CLgUIMapBackground *pBackground = m_BackgroundDrawThread;
  173. if( !pBackground )
  174. return CRect(0,0,0,0);
  175. CRect ClientRect;
  176. GetClientRect( &ClientRect );
  177. CSize size = pBackground->GetSize();
  178. double dAspect = (((double)size.cx)/(double)size.cy);
  179. CRect FitRect;
  180. FitRectWithAspect( &ClientRect, &FitRect, dAspect );
  181. return FitRect;
  182. }
  183. void CLgUIMapWnd::OnChangeViewParams()
  184. {
  185. if( !GetSafeHwnd() )
  186. return;
  187. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnChangeViewParams"), _T("Started...") );
  188. CRect WndRect;
  189. GetClientRect( &WndRect );
  190. CRect DstRect( AreaToWindow( &CLgDPoint(0,0) ), AreaToWindow( &CLgDPoint(1.0,1.0) ) );
  191. m_BackgroundDrawThread.InitDraw( &WndRect, &DstRect );
  192. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, _T("CLgUIMapWnd::OnChangeViewParams"), _T("Done") );
  193. }
  194. void CLgUIMapWnd::SetCenter( CLgDPoint *pDPoint )
  195. {
  196. CRect FitFrame = GetFitFrame();
  197. if( !FitFrame.Width() || !FitFrame.Height() )
  198. return;
  199. CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 );
  200. m_dpShift.x = pDPoint->x - ( CenterPoint.x - FitFrame.left ) / ( FitFrame.Width() * m_dScale );
  201. m_dpShift.y = pDPoint->y - ( CenterPoint.y - FitFrame.top ) / ( FitFrame.Height() * m_dScale );
  202. OnChangeViewParams();
  203. }
  204. void CLgUIMapWnd::SetScale( double dScale, CLgDPoint *pdPoint )
  205. {
  206. if( dScale <= 0.0 )
  207. return;
  208. CLgDPoint pdCenter;
  209. if( pdPoint == NULL )
  210. {
  211. CRect FitFrame = GetFitFrame();
  212. CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 );
  213. pdCenter = WindowToArea( &CenterPoint );
  214. pdPoint = &pdCenter;
  215. }
  216. CPoint p = AreaToWindow( pdPoint );
  217. m_dScale = dScale;
  218. CLgDPoint dp = WindowToArea( &p );
  219. m_dpShift += *pdPoint - dp;
  220. OnChangeViewParams();
  221. }
  222. void CLgUIMapWnd::SetMeterScale( double dScale )
  223. {
  224. m_dMeterScale = dScale;
  225. // redraw contents
  226. if( GetSafeHwnd() )
  227. InvalidateRect( NULL );
  228. }
  229. // meter to area conversions
  230. CLgDSize CLgUIMapWnd::MeterToArea( CLgDSize* pDSize )
  231. {
  232. CRect FitFrame = GetFitFrame();
  233. return CLgDSize( pDSize->cx / m_dMeterScale, pDSize->cy * ((double)FitFrame.Width()/(double)FitFrame.Height()) / m_dMeterScale );
  234. }
  235. CLgDSize CLgUIMapWnd::AreaToMeter( CLgDSize* pDSize )
  236. {
  237. return CLgDSize( pDSize->cx * m_dMeterScale, pDSize->cy * m_dMeterScale );
  238. }
  239. CLgDPoint CLgUIMapWnd::MeterToArea( CLgDPoint* pDPoint )
  240. {
  241. return CLgDPoint( pDPoint->x / m_dMeterScale, pDPoint->y / m_dMeterScale );
  242. }
  243. CLgDPoint CLgUIMapWnd::AreaToMeter( CLgDPoint* pDPoint )
  244. {
  245. return CLgDPoint( pDPoint->x * m_dMeterScale, pDPoint->y * m_dMeterScale );
  246. }
  247. CLgDRect CLgUIMapWnd::MeterToArea( CLgDRect* pDRect )
  248. {
  249. return CLgDRect( pDRect->left / m_dMeterScale, pDRect->top / m_dMeterScale,
  250. pDRect->right / m_dMeterScale, pDRect->bottom / m_dMeterScale );
  251. }
  252. CLgDRect CLgUIMapWnd::AreaToMeter( CLgDRect* pDRect )
  253. {
  254. return CLgDRect( pDRect->left * m_dMeterScale, pDRect->top * m_dMeterScale,
  255. pDRect->right * m_dMeterScale, pDRect->bottom * m_dMeterScale );
  256. }
  257. double CLgUIMapWnd::GetMeterToPixelScale()
  258. {
  259. CSize m2p = AreaToWindow( &MeterToArea( &CLgDSize( 1000000.0, 1000000.0 ) ) );
  260. return m2p.cx / 1000000.0;
  261. }