//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 26/11/2001 // PROJECT: // SUBSYSTEM: // FILE: AreaWnd.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of AreaWnd.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "chicago.h" #include "AreaWnd.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CAreaWnd::CAreaWnd() { m_dScale = 1.0; m_dpShift.x = m_dpShift.y = 0.0; m_pBackground = NULL; } CAreaWnd::~CAreaWnd() { } BEGIN_MESSAGE_MAP(CAreaWnd, CWnd) //{{AFX_MSG_MAP(CAreaWnd) ON_WM_PAINT() ON_WM_SIZE() //}}AFX_MSG_MAP ON_MESSAGE( WM_BACKGROUND_DRAW_THREAD, OnBackgroundDraw ) END_MESSAGE_MAP() BOOL CAreaWnd::Create( CWnd *pParentWnd, CBackground *pBackground, int x, int y, int nWidth, int nHeight ) { m_pBackground = pBackground; if( !m_pBackground ) return FALSE; CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_CROSS ), NULL ); CRect rect( x, y, x + nWidth, y + nHeight ); if( !CWnd::Create( strClassName, NULL, WS_CHILD | WS_DLGFRAME | WS_CLIPSIBLINGS, rect, pParentWnd, 0 ) ) return FALSE; if( !m_BackgroundDrawThread.Create( this, pBackground ) ) return FALSE; return TRUE; } void CAreaWnd::OnPaint() { CPaintDC dc(this); CRect rect; GetClientRect( &rect ); dc.BitBlt( 0, 0, rect.Width(), rect.Height(), &m_BackgroundDC, 0, 0, SRCCOPY ); OnDraw( &dc ); } void CAreaWnd::OnSize( UINT nType, int cx, int cy ) { CWnd::OnSize( nType, cx, cy ); // reinitialize if( cx && cy ) { m_BackgroundDC.Create( cx, cy, RGB(0,0xFF,0) ); OnChangeViewParams(); } } LRESULT CAreaWnd::OnBackgroundDraw( WPARAM, LPARAM ) { if( m_BackgroundDrawThread.Draw( &m_BackgroundDC ) ) { InvalidateRect( NULL, FALSE ); return TRUE; } return FALSE; /* CMemoryDC *pMemoryDC = m_BackgroundDrawThread.GetDC(); if( !pMemoryDC ) return 0; CSize size = m_BackgroundDC.GetSize(); m_BackgroundDC.BitBlt( 0, 0, size.cx, size.cy, pMemoryDC, 0, 0, SRCCOPY ); m_BackgroundDrawThread.ReleaseDC(); InvalidateRect( NULL, FALSE ); return 0; */ } void CAreaWnd::OnDraw( CDC *pDC ) { } CLgDPoint CAreaWnd::WindowToArea( CPoint *pPoint ) { CRect FitFrame = GetFitFrame(); return CLgDPoint( ( (double)( pPoint->x - FitFrame.left ) ) / ( FitFrame.Width() * m_dScale ) + m_dpShift.x, ( (double)( pPoint->y - FitFrame.top ) ) / ( FitFrame.Height() * m_dScale ) + m_dpShift.y ); } CLgDRect CAreaWnd::WindowToArea( CRect *pRect ) { CPoint pTopLeft( pRect->TopLeft() ); CPoint pBottomRight( pRect->BottomRight() ); CLgDPoint dTopLeft( WindowToArea( &pTopLeft ) ); CLgDPoint dBottomRight( WindowToArea( &pBottomRight ) ); return CLgDRect( dTopLeft.x, dTopLeft.y, dBottomRight.x, dBottomRight.y ); } CPoint CAreaWnd::AreaToWindow( CLgDPoint *pDPoint ) { CRect FitFrame = GetFitFrame(); return CPoint( (int)( ( pDPoint->x - m_dpShift.x ) * FitFrame.Width() * m_dScale + 0.5 ) + FitFrame.left, (int)( ( pDPoint->y - m_dpShift.y ) * FitFrame.Height() * m_dScale + 0.5 ) + FitFrame.top ); } CRect CAreaWnd::AreaToWindow( CLgDRect *pDRect ) { CLgDPoint dTopLeft( pDRect->left, pDRect->top ); CLgDPoint dBottomRight( pDRect->right, pDRect->bottom ); CPoint pTopLeft( AreaToWindow( &dTopLeft ) ); CPoint pBottomRight( AreaToWindow( &dBottomRight ) ); return CRect( pTopLeft, pBottomRight ); } void CAreaWnd::FitAreaToWindow() { m_dpShift.x = 0.0; m_dpShift.y = 0.0; m_dScale = 1.0; OnChangeViewParams(); } CRect CAreaWnd::GetFitFrame() { if( !GetSafeHwnd() ) return CRect(0,0,0,0); CRect ClientRect; GetClientRect( &ClientRect ); CSize size = m_pBackground->GetSize(); double dAspect = (((double)size.cx)/(double)size.cy); CRect FitRect; FitRectWithAspect( &ClientRect, &FitRect, dAspect ); return FitRect; } void CAreaWnd::OnChangeViewParams() { if( !GetSafeHwnd() ) return; CRect WndRect; GetClientRect( &WndRect ); CRect DstRect( AreaToWindow( &CLgDPoint(0,0) ), AreaToWindow( &CLgDPoint(1.0,1.0) ) ); m_BackgroundDrawThread.InitDraw( &WndRect, &DstRect ); } void CAreaWnd::SetCenter( CLgDPoint *pDPoint ) { CRect FitFrame = GetFitFrame(); if( !FitFrame.Width() || !FitFrame.Height() ) return; CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 ); m_dpShift.x = pDPoint->x - ( CenterPoint.x - FitFrame.left ) / ( FitFrame.Width() * m_dScale ); m_dpShift.y = pDPoint->y - ( CenterPoint.y - FitFrame.top ) / ( FitFrame.Height() * m_dScale ); OnChangeViewParams(); } void CAreaWnd::SetScale( double dScale, CLgDPoint *pdPoint ) { if( dScale <= 0.0 ) return; CLgDPoint pdCenter; if( pdPoint == NULL ) { CRect FitFrame = GetFitFrame(); CPoint CenterPoint( FitFrame.left + FitFrame.Width() / 2, FitFrame.top + FitFrame.Height() / 2 ); pdCenter = WindowToArea( &CenterPoint ); pdPoint = &pdCenter; } CPoint p = AreaToWindow( pdPoint ); m_dScale = dScale; CLgDPoint dp = WindowToArea( &p ); m_dpShift += *pdPoint - dp; OnChangeViewParams(); }