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.

229 lines
5.4 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: 26/11/2001
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: AreaWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of AreaWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "chicago.h"
  16. #include "AreaWnd.h"
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. CAreaWnd::CAreaWnd()
  23. {
  24. m_dScale = 1.0;
  25. m_dpShift.x = m_dpShift.y = 0.0;
  26. m_pBackground = NULL;
  27. }
  28. CAreaWnd::~CAreaWnd()
  29. {
  30. }
  31. BEGIN_MESSAGE_MAP(CAreaWnd, CWnd)
  32. //{{AFX_MSG_MAP(CAreaWnd)
  33. ON_WM_PAINT()
  34. ON_WM_SIZE()
  35. //}}AFX_MSG_MAP
  36. ON_MESSAGE( WM_BACKGROUND_DRAW_THREAD, OnBackgroundDraw )
  37. END_MESSAGE_MAP()
  38. BOOL CAreaWnd::Create( CWnd *pParentWnd, CBackground *pBackground, int x, int y, int nWidth, int nHeight )
  39. {
  40. m_pBackground = pBackground;
  41. if( !m_pBackground )
  42. return FALSE;
  43. CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_CROSS ), NULL );
  44. CRect rect( x, y, x + nWidth, y + nHeight );
  45. if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_DLGFRAME | WS_CLIPSIBLINGS, rect, pParentWnd, 0 ) )
  46. return FALSE;
  47. if( !m_BackgroundDrawThread.Create( this, pBackground ) )
  48. return FALSE;
  49. return TRUE;
  50. }
  51. void CAreaWnd::OnPaint()
  52. {
  53. CPaintDC dc(this);
  54. CRect rect;
  55. GetClientRect( &rect );
  56. dc.BitBlt( 0, 0, rect.Width(), rect.Height(), &m_BackgroundDC, 0, 0, SRCCOPY );
  57. OnDraw( &dc );
  58. }
  59. void CAreaWnd::OnSize( UINT nType, int cx, int cy )
  60. {
  61. CWnd::OnSize( nType, cx, cy );
  62. // reinitialize
  63. if( cx && cy )
  64. {
  65. m_BackgroundDC.Create( cx, cy, RGB(0,0xFF,0) );
  66. OnChangeViewParams();
  67. }
  68. }
  69. LRESULT CAreaWnd::OnBackgroundDraw( WPARAM, LPARAM )
  70. {
  71. if( m_BackgroundDrawThread.Draw( &m_BackgroundDC ) )
  72. {
  73. InvalidateRect( NULL, FALSE );
  74. return TRUE;
  75. }
  76. return FALSE;
  77. /*
  78. CMemoryDC *pMemoryDC = m_BackgroundDrawThread.GetDC();
  79. if( !pMemoryDC )
  80. return 0;
  81. CSize size = m_BackgroundDC.GetSize();
  82. m_BackgroundDC.BitBlt( 0, 0, size.cx, size.cy, pMemoryDC, 0, 0, SRCCOPY );
  83. m_BackgroundDrawThread.ReleaseDC();
  84. InvalidateRect( NULL, FALSE );
  85. return 0;
  86. */
  87. }
  88. void CAreaWnd::OnDraw( CDC *pDC )
  89. {
  90. }
  91. CLgDPoint CAreaWnd::WindowToArea( CPoint *pPoint )
  92. {
  93. CRect FitFrame = GetFitFrame();
  94. return CLgDPoint( ( (double)( pPoint->x - FitFrame.left ) ) / ( FitFrame.Width() * m_dScale ) + m_dpShift.x,
  95. ( (double)( pPoint->y - FitFrame.top ) ) / ( FitFrame.Height() * m_dScale ) + m_dpShift.y );
  96. }
  97. CLgDRect CAreaWnd::WindowToArea( CRect *pRect )
  98. {
  99. CPoint pTopLeft( pRect->TopLeft() );
  100. CPoint pBottomRight( pRect->BottomRight() );
  101. CLgDPoint dTopLeft( WindowToArea( &pTopLeft ) );
  102. CLgDPoint dBottomRight( WindowToArea( &pBottomRight ) );
  103. return CLgDRect( dTopLeft.x, dTopLeft.y, dBottomRight.x, dBottomRight.y );
  104. }
  105. CPoint CAreaWnd::AreaToWindow( CLgDPoint *pDPoint )
  106. {
  107. CRect FitFrame = GetFitFrame();
  108. return CPoint( (int)( ( pDPoint->x - m_dpShift.x ) * FitFrame.Width() * m_dScale + 0.5 ) + FitFrame.left,
  109. (int)( ( pDPoint->y - m_dpShift.y ) * FitFrame.Height() * m_dScale + 0.5 ) + FitFrame.top );
  110. }
  111. CRect CAreaWnd::AreaToWindow( CLgDRect *pDRect )
  112. {
  113. CLgDPoint dTopLeft( pDRect->left, pDRect->top );
  114. CLgDPoint dBottomRight( pDRect->right, pDRect->bottom );
  115. CPoint pTopLeft( AreaToWindow( &dTopLeft ) );
  116. CPoint pBottomRight( AreaToWindow( &dBottomRight ) );
  117. return CRect( pTopLeft, pBottomRight );
  118. }
  119. void CAreaWnd::FitAreaToWindow()
  120. {
  121. m_dpShift.x = 0.0;
  122. m_dpShift.y = 0.0;
  123. m_dScale = 1.0;
  124. OnChangeViewParams();
  125. }
  126. CRect CAreaWnd::GetFitFrame()
  127. {
  128. if( !GetSafeHwnd() )
  129. return CRect(0,0,0,0);
  130. CRect ClientRect;
  131. GetClientRect( &ClientRect );
  132. CSize size = m_pBackground->GetSize();
  133. double dAspect = (((double)size.cx)/(double)size.cy);
  134. CRect FitRect;
  135. FitRectWithAspect( &ClientRect, &FitRect, dAspect );
  136. return FitRect;
  137. }
  138. void CAreaWnd::OnChangeViewParams()
  139. {
  140. if( !GetSafeHwnd() )
  141. return;
  142. CRect WndRect;
  143. GetClientRect( &WndRect );
  144. CRect DstRect( AreaToWindow( &CLgDPoint(0,0) ), AreaToWindow( &CLgDPoint(1.0,1.0) ) );
  145. m_BackgroundDrawThread.InitDraw( &WndRect, &DstRect );
  146. }
  147. void CAreaWnd::SetCenter( CLgDPoint *pDPoint )
  148. {
  149. CRect FitFrame = GetFitFrame();
  150. if( !FitFrame.Width() || !FitFrame.Height() )
  151. return;
  152. CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 );
  153. m_dpShift.x = pDPoint->x - ( CenterPoint.x - FitFrame.left ) / ( FitFrame.Width() * m_dScale );
  154. m_dpShift.y = pDPoint->y - ( CenterPoint.y - FitFrame.top ) / ( FitFrame.Height() * m_dScale );
  155. OnChangeViewParams();
  156. }
  157. void CAreaWnd::SetScale( double dScale, CLgDPoint *pdPoint )
  158. {
  159. if( dScale <= 0.0 )
  160. return;
  161. CLgDPoint pdCenter;
  162. if( pdPoint == NULL )
  163. {
  164. CRect FitFrame = GetFitFrame();
  165. CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 );
  166. pdCenter = WindowToArea( &CenterPoint );
  167. pdPoint = &pdCenter;
  168. }
  169. CPoint p = AreaToWindow( pdPoint );
  170. m_dScale = dScale;
  171. CLgDPoint dp = WindowToArea( &p );
  172. m_dpShift += *pdPoint - dp;
  173. OnChangeViewParams();
  174. }