/*[]=====================================================================[]*/ /*[] LeadTools Run Time Library - Version 14 []*/ /*[] []*/ /*[] []*/ /*[] Copyright (c)1991-2004 LEAD Technologies, Inc. []*/ /*[] All Rights Reserved. []*/ /*[]=====================================================================[]*/ #include "stdafx.h" #include "ABC.h" #include "ABCDoc.h" #include "ABCView.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif extern CABCApp theApp; L_INT L_FAR L_EXPORT FileReadCallBack(pFILEINFO pFileInfo, pBITMAPHANDLE pBitmap, L_UCHAR L_FAR *pBuffer, L_UINT uFlags, L_INT nRow, L_INT nLines, L_VOID L_FAR * pData); ///////////////////////////////////////////////////////////////////////////// // CABCView IMPLEMENT_DYNCREATE(CABCView, CScrollView) BEGIN_MESSAGE_MAP(CABCView, CScrollView) //{{AFX_MSG_MAP(CABCView) ON_MESSAGE(WM_DOREALIZE, OnDoRealize) ON_WM_HSCROLL() ON_WM_VSCROLL() ON_COMMAND(ID_VIEW_NORMAL, OnViewNormal) ON_UPDATE_COMMAND_UI(ID_VIEW_NORMAL, OnUpdateViewNormal) ON_COMMAND(ID_VIEW_FITIMAGETOWINDOW, OnViewFitimagetowindow) ON_COMMAND(ID_VIEW_SNAPWINDOWTOIMAGE, OnViewSnapwindowtoimage) ON_UPDATE_COMMAND_UI(ID_VIEW_FITIMAGETOWINDOW, OnUpdateViewFitimagetowindow) ON_UPDATE_COMMAND_UI(ID_VIEW_SNAPWINDOWTOIMAGE, OnUpdateViewSnapwindowtoimage) ON_COMMAND(ID_VIEW_ZOOMIN2X, OnViewZoomin2x) ON_COMMAND(ID_VIEW_ZOOMOUT2X, OnViewZoomout2x) ON_WM_SIZE() ON_WM_ERASEBKGND() ON_WM_SETFOCUS() ON_WM_KILLFOCUS() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() /*--------------------------------------------------------------------------+ | Function :SetImageDataDefaults | Desc :set image data defaults | Return :bool | Notes : +--------------------------------------------------------------------------*/ L_VOID SetImageDataDefaults(LPIMAGEDATA lpImageData) { lpImageData->bFitImage = TRUE; //bFitToWindow; //if TRUE then fit image to window lpImageData->cxClient = 0; //width of client area lpImageData->cyClient = 0; //height of client area SetRect(&lpImageData->rcView, 0, 0, 0, 0); lpImageData->pBitmap = NULL; lpImageData->pointStart.x = 0; lpImageData->pointStart.y = 0; } /*--------------------------------------------------------------------------+ | Function :XClientToBitmap | Desc :convert x from window coordinates to bitmap coordinates | Return :int32 | Notes : +--------------------------------------------------------------------------*/ L_INT32 XClientToBitmap(LPIMAGEDATA pImageData, int x) { x = (L_INT)((L_INT32)(x-pImageData->rcView.left)*BITMAPWIDTH(pImageData->pBitmap)/RECTWIDTH(&pImageData->rcView)); return x; } /*--------------------------------------------------------------------------+ | Function :YClientToBitmap | Desc :convert y from window coordinates to bitmap coordinates | Return :int32 | Notes : +--------------------------------------------------------------------------*/ L_INT32 YClientToBitmap(LPIMAGEDATA pImageData, int y) { y = (L_INT)((L_INT32)(y-pImageData->rcView.top)*BITMAPHEIGHT(pImageData->pBitmap)/RECTHEIGHT(&pImageData->rcView)); return y; } /*--------------------------------------------------------------------------+ | Function :XClientToBitmapInverse | Desc :convert x from bitmap coordinates to window coordinates | Return :int32 | Notes : +--------------------------------------------------------------------------*/ static L_INT32 XClientToBitmapInverse(LPIMAGEDATA pImageData, int x) { L_INT32 xNew; xNew = x * RECTWIDTH(&pImageData->rcView)/BITMAPWIDTH(pImageData->pBitmap) + pImageData->rcView.left; return xNew; } /*--------------------------------------------------------------------------+ | Function :YClientToBitmapInverse | Desc :convert y from bitmap coordinates to window coordinates | Return :int32 | Notes : +--------------------------------------------------------------------------*/ static L_INT32 YClientToBitmapInverse(LPIMAGEDATA pImageData, int y) { L_INT32 yNew; yNew = y * RECTHEIGHT(&pImageData->rcView)/BITMAPHEIGHT(pImageData->pBitmap) + pImageData->rcView.top; return yNew; } /*--------------------------------------------------------------------------+ | Function :PixelizePoint | Desc : | Return :void | Notes : +--------------------------------------------------------------------------*/ L_VOID PixelizePoint(LPIMAGEDATA lpImageData, L_INT32 *px, L_INT32 *py, L_INT32 *pXBitmap, L_INT32 *pYBitmap) { *pXBitmap = XClientToBitmap(lpImageData, *px); *pYBitmap = YClientToBitmap(lpImageData, *py); *px = XClientToBitmapInverse(lpImageData, *pXBitmap); *py = YClientToBitmapInverse(lpImageData, *pYBitmap); } /*--------------------------------------------------------------------------+ | Function :CABCView | Desc :constructor | Return : | Notes : +--------------------------------------------------------------------------*/ CABCView::CABCView() { m_nZoom=100; SetImageDataDefaults( &m_Data ); m_bResizePlz = FALSE; Trace(TEXT("\n.........................New Image.")); } /*--------------------------------------------------------------------------+ | Function :~CABCView | Desc :Destructor | Return : | Notes : +--------------------------------------------------------------------------*/ CABCView::~CABCView() { } /*--------------------------------------------------------------------------+ | Function :PreCreateWindow | Desc :call parent PreCreateWindow | Return :bool | Notes : +--------------------------------------------------------------------------*/ BOOL CABCView::PreCreateWindow(CREATESTRUCT& cs) { return CScrollView::PreCreateWindow(cs); } /*--------------------------------------------------------------------------+ | Function :UpdateDisplayPalette | Desc :update the display palette | Return :void | Notes : +--------------------------------------------------------------------------*/ L_VOID CABCView::UpdateDisplayPalette( CDC* pDC, pBITMAPHANDLE pBitmap ) { DWORD dwFlags; HPALETTE hPal=NULL; RECT rcClip; ASSERT( pBitmap->Flags.Allocated ); ASSERT_VALID( pDC ); dwFlags = theApp.SetDisplayMode(); pDC->SaveDC(); hPal = L_CreatePaintPalette(pDC->m_hDC, pBitmap); if(hPal) { ::SelectPalette(pDC->m_hDC, hPal, FALSE); ::RealizePalette(pDC->m_hDC); } pDC->GetClipBox(&rcClip); L_PaintDC(pDC->m_hDC, pBitmap, NULL, NULL, &m_rcDst, &rcClip, SRCCOPY); pDC->RestoreDC(-1); if(hPal) DeleteObject(hPal); L_SetDisplayMode( DISPLAYMODE_RESETPOSITIONS, dwFlags ); } /*--------------------------------------------------------------------------+ | Function :OnDraw | Desc :draw bitmap | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnDraw(CDC* pDC) { CABCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pBITMAPHANDLE pBitmap = &pDoc->m_LEADBitmap; if( pBitmap->Flags.Allocated && !m_bResizePlz ) { UpdateDisplayPalette( pDC, pBitmap ); } } /*--------------------------------------------------------------------------+ | Function :OnSizeChange | Desc :on size change | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnSizeChange() { CABCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pBITMAPHANDLE pBitmap = &pDoc->m_LEADBitmap; if(pDoc->m_LEADBitmap.Flags.Allocated) { ::SetRect(&m_rcDst, 0, 0, MulDiv(pBitmap->Width, m_nZoom, 100), MulDiv(pBitmap->Height, m_nZoom, 100) ); } else { ::SetRect(&m_rcDst, 0, 0, MulDiv(pDoc->m_fi.Width, m_nZoom, 100), MulDiv(pDoc->m_fi.Height, m_nZoom, 100) ); } { BOOL bUseRGN = FALSE; BOOL bIsNewResLoaded = FALSE; RGNXFORM XForm; int nOldWidth, nOldHeight; if(L_BitmapHasRgn(m_Data.pBitmap)) { bUseRGN = TRUE; L_GetBitmapRgnHandle(m_Data.pBitmap, &XForm, &m_Data.hRgnWindows); nOldWidth = m_Data.pBitmap->Width; nOldHeight = m_Data.pBitmap->Height; } } CStatusBar *pStatusBar=NULL; CMainFrame * pMain=NULL; pMain = (CMainFrame*)AfxGetMainWnd(); if(pMain) pStatusBar = &pMain->m_wndStatusBar; if(pStatusBar) { CString cs; pStatusBar->SetPaneText(2, TEXT(""), TRUE); cs.Format(TEXT("%ux%u"), pDoc->m_LEADBitmap.Width, pDoc->m_LEADBitmap.Height); pStatusBar->SetPaneText(3, cs, TRUE); cs.Format(TEXT("%uBPP"), pDoc->m_LEADBitmap.BitsPerPixel); pStatusBar->SetPaneText(4, cs, TRUE); cs.Format(TEXT("Mem:%u"), pDoc->m_fi.SizeMem); pStatusBar->SetPaneText(5, cs, TRUE); cs.Format(TEXT("Disk:%u"), pDoc->m_fi.SizeDisk); pStatusBar->SetPaneText(6, cs, TRUE); } CSize sizeTotal(RECTWIDTH(&m_rcDst), RECTHEIGHT(&m_rcDst)); SetScrollSizes(MM_TEXT, sizeTotal); } /*--------------------------------------------------------------------------+ | Function :OnUpdate | Desc :on update | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnUpdate(CScrollView* pSender, LPARAM lHint, CObject* pHint) { UINT uFlags = (UINT) lHint; if(uFlags & CHANGED_COLOR) OnSizeChange(); CScrollView::OnUpdate(pSender, lHint, pHint); } /*--------------------------------------------------------------------------+ | Function :OnHScroll | Desc :on horizontal scroll | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnHScroll(UINT nCode, UINT nPos, CScrollBar* pScrollBar) { CScrollView::OnHScroll(nCode, nPos, pScrollBar); } /*--------------------------------------------------------------------------+ | Function :OnVScroll | Desc :on vertical scroll | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { CScrollView::OnVScroll(nSBCode, nPos, pScrollBar); } /*--------------------------------------------------------------------------+ | Function :OnInitialUpdate | Desc :on initial update | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnInitialUpdate() { RECT rcMain,rc,rcParent; CFrameWnd * pParent; HWND hWndMDIClient; CABCDoc *pDoc = (CABCDoc*)GetDocument(); CScrollView::OnInitialUpdate(); SetZoom(100); //Snap window or fit image? hWndMDIClient = ((CMDIFrameWnd *)AfxGetMainWnd())->m_hWndMDIClient; ::GetClientRect(hWndMDIClient,&rcMain); pParent = GetParentFrame(); pParent->RecalcLayout(); CopyRect(&rc,&m_rcDst); pParent->GetWindowRect(&rcParent); rcMain.right -= rcMain.left; rcMain.bottom -= rcMain.top; rc.right -= rc.left; rc.bottom -= rc.top; if( rcMain.right >= rc.right && rcMain.bottom >= rc.bottom ) { pParent->SetWindowPos(NULL,0,0,0,0,SWP_NOZORDER|SWP_NOSIZE); OnViewSnapwindowtoimage(); } else { OnViewFitimagetowindow(); pParent->SetWindowPos(NULL,0,0,rcMain.right,rcMain.bottom,SWP_NOZORDER); } OnDoRealize((WPARAM)m_hWnd, (LPARAM) FALSE); // same as SendMessage(WM_DOREALIZE); } /*--------------------------------------------------------------------------+ | Function :OnDoRealize | Desc :on do resize | Return :LRESULT | Notes : +--------------------------------------------------------------------------*/ LRESULT CABCView::OnDoRealize(WPARAM wParam, LPARAM lParam) { UINT nColorsChanged = 0; CABCDoc* pDoc = GetDocument(); CDC* pdc; DWORD dwFlags; CWnd * pWnd=NULL; HPALETTE hPal=NULL; if(lParam) { if(((HWND) wParam) == m_hWnd) return(FALSE); pdc = GetDC(); } else { pWnd =theApp.m_pMainWnd; pdc = pWnd->GetDC(); dwFlags = theApp.SetDisplayMode(); hPal = L_CreatePaintPalette(pdc->m_hDC, &pDoc->m_LEADBitmap); L_SetDisplayMode (DISPLAYMODE_RESETPOSITIONS, dwFlags); } if(hPal) { pdc->SaveDC(); ::SelectPalette(pdc->m_hDC, hPal, (BOOL) lParam); nColorsChanged = ::RealizePalette(pdc->m_hDC); if(nColorsChanged > 0) GetParent()->InvalidateRect(NULL, FALSE); pdc->RestoreDC(-1); } if(lParam) ReleaseDC(pdc); else pWnd->ReleaseDC(pdc); CRect rcC; GetClientRect(&rcC); m_Data.bFitImage = FALSE; // if TRUE then fit image to window m_Data.cxClient = rcC.Width(); // width of client area m_Data.cyClient = rcC.Height(); // height of client area m_Data.rcView = rcC; m_Data.pBitmap = &pDoc->m_LEADBitmap; return ((LRESULT) (BOOL) (nColorsChanged > 0)); } /*--------------------------------------------------------------------------+ | Function :SetZoom | Desc :set the zoom value | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::SetZoom(int nZoom) { CABCDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); m_nZoom = nZoom; OnSizeChange(); pDoc->UpdateAllViews(NULL); } /*--------------------------------------------------------------------------+ | Function :OnViewNormal | Desc :set the viewing mode to normal | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnViewNormal() { m_nViewMode = VIEW_NORMAL; SetZoom(100); } /*--------------------------------------------------------------------------+ | Function :OnUpdateViewNormal | Desc :check/uncheck the view normal control | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnUpdateViewNormal(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_nViewMode==VIEW_NORMAL); } /*--------------------------------------------------------------------------+ | Function :Fitimagetowindow | Desc :fit the image to window | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::Fitimagetowindow() { OnViewFitimagetowindow(); } /*--------------------------------------------------------------------------+ | Function :OnViewFitimagetowindow | Desc :On View Fit image to window | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnViewFitimagetowindow() { CABCDoc* pDoc = GetDocument(); int cxZoom, cyZoom; pBITMAPHANDLE pBitmap = &pDoc->m_LEADBitmap; CSize size; CSize sizeSb; GetTrueClientSize( size, sizeSb ); if( pBitmap->Flags.Allocated ) { cxZoom = (int) ((long) size.cx * 100 / pBitmap->Width); cyZoom = (int) ((long) size.cy * 100 / pBitmap->Height); } else { if( pDoc->m_fi.Width > 0 ) cxZoom = (int) ((long) size.cx * 100 / pDoc->m_fi.Width ); if( pDoc->m_fi.Height > 0 ) cyZoom = (int) ((long) size.cy * 100 / pDoc->m_fi.Height ); } SetZoom(max( min(cxZoom, cyZoom), 1)); m_nViewMode = VIEW_FIT; } /*--------------------------------------------------------------------------+ | Function :OnUpdateViewFitimagetowindow | Desc :On Update View Fit image to window | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnUpdateViewFitimagetowindow(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_nViewMode==VIEW_FIT); } /*--------------------------------------------------------------------------+ | Function :OnViewSnapwindowtoimage | Desc :On View Snap window to image | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnViewSnapwindowtoimage() { m_nViewMode = VIEW_SNAP; WINDOWPLACEMENT wndpl; wndpl.length = sizeof(WINDOWPLACEMENT); wndpl.flags = 0; GetParent()->GetWindowPlacement(&wndpl); wndpl.flags = 0; wndpl.showCmd = SW_RESTORE; GetParent()->SetWindowPlacement(&wndpl); ResizeParentToFit(FALSE); } /*--------------------------------------------------------------------------+ | Function :OnUpdateViewSnapwindowtoimage | Desc :On Update View Snap window to image | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnUpdateViewSnapwindowtoimage(CCmdUI* pCmdUI) { pCmdUI->SetCheck(m_nViewMode==VIEW_SNAP); pCmdUI->Enable(m_nViewMode!=VIEW_FIT); } /*--------------------------------------------------------------------------+ | Function :OnViewZoomin2x | Desc :reduce the bitmap size twice | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnViewZoomin2x() { if( m_nZoom <= 800 ) m_nZoom = min( 800, m_nZoom * 2 ); if( m_nZoom == 100 ) m_nViewMode = VIEW_NORMAL; else m_nViewMode = VIEW_ZOOM; SetZoom(m_nZoom); } /*--------------------------------------------------------------------------+ | Function :OnViewZoomout2x | Desc :enlarge the bitmap size twice | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnViewZoomout2x() { if( m_nZoom >= 6 ) m_nZoom = max( 6, m_nZoom / 2); if( m_nZoom == 100 ) m_nViewMode = VIEW_NORMAL; else m_nViewMode = VIEW_ZOOM; SetZoom( m_nZoom ); } /*--------------------------------------------------------------------------+ | Function :OnSize | Desc :on size | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnSize(UINT nType, int cx, int cy) { CScrollView::OnSize(nType, cx, cy); if( m_nViewMode == VIEW_FIT ) OnViewFitimagetowindow(); } /*--------------------------------------------------------------------------+ | Function :OnEraseBkgnd | Desc :On Erase Backkground | Return :bool | Notes : +--------------------------------------------------------------------------*/ BOOL CABCView::OnEraseBkgnd(CDC* pDC) { CBrush br; br.CreateStockObject(GRAY_BRUSH); FillOutsideRect(pDC, &br); return(TRUE); } /*--------------------------------------------------------------------------+ | Function :OnSetFocus | Desc :set focus | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnSetFocus(CWnd* pOldWnd) { CABCDoc* pDoc = GetDocument(); CScrollView::OnSetFocus(pOldWnd); CStatusBar *pStatusBar=NULL; CMainFrame * pMain=NULL; pMain = (CMainFrame*)AfxGetMainWnd(); if(pMain) pStatusBar = &pMain->m_wndStatusBar; if(pStatusBar) { CString cs; cs.Format(TEXT("%ux%u"), pDoc->m_LEADBitmap.Width, pDoc->m_LEADBitmap.Height); pStatusBar->SetPaneText(3, cs, TRUE); cs.Format(TEXT("%uBPP"), pDoc->m_LEADBitmap.BitsPerPixel); pStatusBar->SetPaneText(4, cs, TRUE); cs.Format(TEXT("Mem:%u"), pDoc->m_fi.SizeMem); pStatusBar->SetPaneText(5, cs, TRUE); cs.Format(TEXT("Disk:%u"), pDoc->m_fi.SizeDisk); pStatusBar->SetPaneText(6, cs, TRUE); } } /*--------------------------------------------------------------------------+ | Function :OnKillFocus | Desc :On Kill Focus | Return :void | Notes : +--------------------------------------------------------------------------*/ void CABCView::OnKillFocus(CWnd* pNewWnd) { CScrollView::OnKillFocus(pNewWnd); CMainFrame* pMain=NULL; CStatusBar *pStatusBar=NULL; pMain = (CMainFrame*)AfxGetMainWnd(); if(pMain) pStatusBar = &pMain->m_wndStatusBar; if(pStatusBar) { pStatusBar->SetPaneText(3, TEXT(" "), TRUE); pStatusBar->SetPaneText(4, TEXT(" "), TRUE); pStatusBar->SetPaneText(5, TEXT(" "), TRUE); pStatusBar->SetPaneText(6, TEXT(" "), TRUE); } } /*--------------------------------------------------------------------------+ | Function :GetDocument | Desc :inline function | Return :CABCDoc* | Notes : +--------------------------------------------------------------------------*/ inline CABCDoc* CABCView::GetDocument() { return (CABCDoc*)m_pDocument; }