#include "stdafx.h"
#include "imp_view.h"
#include "FrmWnd.h"

CImpView::CImpView(CFrmWnd *pParent)
{
	m_scrollHelper.AttachWnd(this);
	m_pParent = pParent;
	m_pStoredBitmap = NULL;
	m_dwStoredDataLen = 0;
}

CImpView::~CImpView()
{
	m_scrollHelper.DetachWnd();
	if ( m_pStoredBitmap )
		free(m_pStoredBitmap);
}

BEGIN_MESSAGE_MAP(CImpView, CWnd)
ON_WM_PAINT()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_MOUSEWHEEL()
ON_WM_SIZE()
END_MESSAGE_MAP()

void CImpView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	OnPrepareDC(&dc);
	OnDraw(&dc);
}

void CImpView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	m_scrollHelper.OnHScroll(nSBCode, nPos, pScrollBar);
	CWnd::OnHScroll(nSBCode, nPos, pScrollBar);
	//Invalidate();
	UpdateWindow();
}

void CImpView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	// TODO: Add your message handler code here and/or call default
	m_scrollHelper.OnVScroll(nSBCode, nPos, pScrollBar);
	CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
	//Invalidate();
	UpdateWindow();
}

BOOL CImpView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
	// TODO: Add your message handler code here and/or call default
	return m_scrollHelper.OnMouseWheel(nFlags, zDelta, pt);
	//return CWnd::OnMouseWheel(nFlags, zDelta, pt);
}

void CImpView::OnPrepareDC(CDC *pDC)
{
	CSize p = m_scrollHelper.GetScrollPos();
	pDC->SetViewportOrg(-p.cx, -p.cy);
}

void CImpView::OnDraw(CDC *pDC)
{
	DWORD dwLayer = m_pParent->m_dwLayer;
	LPVIEWERDATA pData = m_pParent->m_pData;

	if ( m_pStoredBitmap )
	{
		if ( m_dwStoredDataLen >= sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) )
		{
			BYTE* pBits = (BYTE*)m_pStoredBitmap;//(BYTE*)pData->ppData[dwLayer];
			BITMAPINFO* bi = (BITMAPINFO*)&pBits[14];
			BITMAPFILEHEADER* bfh = (BITMAPFILEHEADER*)pBits;
			DWORD dwWidth = bi->bmiHeader.biWidth;
			DWORD dwHeight = bi->bmiHeader.biHeight;
			RECT rc;
			DWORD dwX = 0, dwY = 0;
			GetClientRect(&rc);
			if ( dwWidth < (DWORD)rc.right )
			{
				dwX = rc.right / 2 - dwWidth / 2;
			}
			if ( dwHeight < (DWORD)rc.bottom )
			{
				dwY = rc.bottom / 2 - dwHeight / 2;
			}
			SetDIBitsToDevice(pDC->m_hDC, dwX, dwY, dwWidth, dwHeight, 0, 0, 0, bi->bmiHeader.biHeight, &pBits[bfh->bfOffBits], bi, DIB_RGB_COLORS);

			HGDIOBJ oldpen = pDC->SelectObject(GetStockObject(WHITE_PEN));
			if ( dwY )
			{
				pDC->Rectangle(0, 0, rc.right, dwY);
				pDC->Rectangle(0, dwHeight+dwY, rc.right, rc.bottom);
			}
			if ( dwX )
			{
				pDC->Rectangle(0, dwY, dwX, dwHeight+dwY);
				pDC->Rectangle(rc.right-dwX, dwY, rc.right, dwHeight+dwY);
			}
			pDC->SelectObject(oldpen);
		}
	}
}

void CImpView::UpdateScrollBars()
{
	DWORD dwLayer = m_pParent->m_dwLayer;
	LPVIEWERDATA pData = m_pParent->m_pData;

	if ( pData && dwLayer < pData->dwLayers && pData->ppData[dwLayer] )
	{
		if ( pData->dwDataLenArray[dwLayer] >= sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) )
		{
			BYTE* pBits = (BYTE*)pData->ppData[dwLayer];
			BITMAPINFOHEADER *bi = (BITMAPINFOHEADER*)&pBits[14];
			m_scrollHelper.SetDisplaySize(bi->biWidth, bi->biHeight);
		}
	}
}
void CImpView::OnSize(UINT nType, int cx, int cy)
{
	UpdateScrollBars();
	
	CWnd::OnSize(nType, cx, cy);
	Invalidate(FALSE);
	// TODO: Add your message handler code here
}

BOOL CImpView::StoreBitmap()
{
	DWORD dwLayer = m_pParent->m_dwLayer;
	LPVIEWERDATA pData = m_pParent->m_pData;
	if ( m_pStoredBitmap )
		free(m_pStoredBitmap);
	if ( pData && dwLayer < pData->dwLayers )
	{
		m_dwStoredDataLen = pData->dwDataLenArray[dwLayer];
		m_pStoredBitmap = malloc(pData->dwDataLenArray[dwLayer]);
		memcpy(m_pStoredBitmap, pData->ppData[dwLayer], pData->dwDataLenArray[dwLayer]);
	}
	else
	{
		m_pStoredBitmap = NULL;
		m_dwStoredDataLen = 0;
	}
	return TRUE;
}