home *** CD-ROM | disk | FTP | other *** search
- // magnView.cpp : implementation of the CMagnifyView class
- //
-
- #include "stdafx.h"
- #include "magnify.h"
-
- #include "magnDoc.h"
- #include "magnView.h"
- #include "zoomdlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- extern CMagnifyApp theApp;
-
- enum { OP_NONE, OP_RESIZE_WINDOW, OP_MOVE_WINDOW };
-
- /////////////////////////////////////////////////////////////////////////////
- // CMagnifyView
-
- IMPLEMENT_DYNCREATE(CMagnifyView, CFormView)
-
- BEGIN_MESSAGE_MAP(CMagnifyView, CFormView)
- //{{AFX_MSG_MAP(CMagnifyView)
- ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
- ON_COMMAND(ID_MAGNIFY_DISABLE, OnMagnifyDisable)
- ON_UPDATE_COMMAND_UI(ID_MAGNIFY_DISABLE, OnUpdateMagnifyDisable)
- ON_COMMAND(ID_MAGNIFY_ZOOM, OnMagnifyZoom)
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMagnifyView construction/destruction
-
- CMagnifyView::CMagnifyView()
- : CFormView(CMagnifyView::IDD)
- {
- //{{AFX_DATA_INIT(CMagnifyView)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // TODO: add construction code here
-
- bMagnify = FALSE;
- fleft = ftop = 0.25f;
- fwidth = fheight = 0.5f;
- nZoomFactor = 100;
- }
-
- CMagnifyView::~CMagnifyView()
- {
- }
-
- void CMagnifyView::DoDataExchange(CDataExchange* pDX)
- {
- CFormView::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMagnifyView)
- DDX_Control(pDX, IDC_LEADCTRL1, m_Lead1);
- DDX_Control(pDX, IDC_LEADCTRL2, m_Lead2);
- //}}AFX_DATA_MAP
- }
-
- BOOL CMagnifyView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
-
- return CFormView::PreCreateWindow(cs);
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // CMagnifyView diagnostics
-
- #ifdef _DEBUG
- void CMagnifyView::AssertValid() const
- {
- CFormView::AssertValid();
- }
-
- void CMagnifyView::Dump(CDumpContext& dc) const
- {
- CFormView::Dump(dc);
- }
-
- CMagnifyDoc* CMagnifyView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMagnifyDoc)));
- return (CMagnifyDoc*)m_pDocument;
- }
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CMagnifyView message handlers
- CLead* CMagnifyView::GetDocLead()
- {
- CMagnifyDoc* pDoc = GetDocument();
- return(&pDoc->m_Lead);
- }
-
-
- void CMagnifyView::OnInitialUpdate()
- {
- CFormView::OnInitialUpdate();
-
- UNLOCKSUPPORT(m_Lead1);
-
- // Disable the use of our scroll bars. Set the control to display the whole image
- // Let CFormView handle the scrolling. If you want to use CLead's automatic scrolling,
- // make the Lead control in the form as small as possible, otherwise CFormView might create a second
- // set of scroll bars if the window becomes smaller than the size of the controls on the form.
- int xExt = (int)m_Lead1.GetBitmapWidth(),
- yExt = (int)m_Lead1.GetBitmapHeight();
- SetScrollSizes(MM_TEXT,CSize(xExt,yExt));
- // Make the window fit the image
- GetParentFrame()->RecalcLayout();
- ResizeParentToFit();
-
- // disable the view window's scroll bars
- SetScrollSizes(MM_TEXT,CSize(1,1));
- m_Lead2.ShowWindow(SW_HIDE);
- m_Lead2.SetBorderStyle(1);
- m_Lead2.SetBackErase(FALSE);
- m_Lead1.SetBackErase(FALSE);
- }
-
- LRESULT CMagnifyView::OnDoRealize(WPARAM wParam, LPARAM lParam)
- {
- if( !IsWindow(m_Lead1.m_hWnd) )
- return FALSE;
- if(lParam)
- {
- return m_Lead1.SendMessage(WM_PALETTECHANGED, wParam);
- }
- else
- {
- UINT nColorsChanged = 0;
- CDC* pdc;
-
- pdc = theApp.m_pMainWnd->GetDC();
- HPALETTE hpal = (HPALETTE)m_Lead1.GetPalette((OLE_HANDLE) pdc->m_hDC);
- if(hpal)
- {
- CPalette pal;
-
- pal.Attach(hpal);
- CPalette* oldPalette = pdc->SelectPalette(&pal, (BOOL) lParam);
- nColorsChanged = pdc->RealizePalette();
- if (nColorsChanged > 0)
- {
- m_Lead1.InvalidateRect(NULL, FALSE);
- if( bMagnify )
- m_Lead2.InvalidateRect(NULL, FALSE);
- }
- pdc->SelectPalette(oldPalette, TRUE);
- }
- theApp.m_pMainWnd->ReleaseDC(pdc);
- return ((LRESULT) (BOOL) (nColorsChanged > 0));
- }
- }
-
- void CMagnifyView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
- {
- CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
- if (bActivate)
- {
- ASSERT(pActivateView == this);
- OnDoRealize((WPARAM)theApp.m_pMainWnd->m_hWnd, (LPARAM) FALSE); // same as SendMessage(WM_DOREALIZE);
- }
- }
-
-
- void CMagnifyView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
- {
- CMagnifyDoc* pDoc = GetDocument();
- m_Lead1.SetAutoRepaint(FALSE);
- m_Lead1.SetBitmap(GetDocLead()->GetBitmap());
- m_Lead1.SetPaintPalette(PAINTPALETTE_AUTO);
- m_Lead1.SetPaintDither(PAINTDITHER_DIFFUSION);
- m_Lead1.SetBitonalScaling(BITONALSCALING_NORMAL);
- m_Lead1.SetDstRect(m_Lead1.GetDstLeft(), m_Lead1.GetDstTop(), m_Lead1.GetBitmapWidth(), m_Lead1.GetBitmapHeight());
- m_Lead1.SetDstClipRect(m_Lead1.GetDstLeft(), m_Lead1.GetDstTop(), m_Lead1.GetDstWidth(), m_Lead1.GetDstHeight());
- m_Lead1.SetAutoRepaint(TRUE);
-
- m_Lead2.SetAutoRepaint(FALSE);
- m_Lead2.SetBitmap(GetDocLead()->GetBitmap());
- m_Lead2.SetPaintPalette(PAINTPALETTE_AUTO);
- m_Lead2.SetPaintDither(PAINTDITHER_DIFFUSION);
- m_Lead2.SetBitonalScaling(BITONALSCALING_NORMAL);
- SetZoomWindow();
- m_Lead2.SetAutoRepaint(TRUE);
-
- CView::OnUpdate(pSender, lHint, pHint);
- }
-
- void CMagnifyView::OnMagnifyDisable()
- {
- if( !bMagnify ) return;
- bMagnify = FALSE;
- m_Lead2.ShowWindow(SW_HIDE);
- }
-
- void CMagnifyView::OnUpdateMagnifyDisable(CCmdUI* pCmdUI)
- {
- pCmdUI->SetCheck(!bMagnify);
- }
-
- void CMagnifyView::OnMagnifyZoom()
- {
- CZoomDlg dlg;
- dlg.nZoomFactor = nZoomFactor;
- if( dlg.DoModal() == IDOK )
- {
- nZoomFactor = dlg.nZoomFactor;
- SetZoomWindow();
- bMagnify = TRUE;
- m_Lead2.ShowWindow(SW_SHOW);
- }
- }
-
- void CMagnifyView::SetZoomWindow()
- {
- float width = m_Lead2.GetBitmapWidth(),
- height = m_Lead2.GetBitmapHeight();
-
- // the 'Dest' coordinates are relative to the client area of the zoom control (m_Lead2)
- // not of the original control (m_Lead1)!
- m_Lead2.SetAutoRepaint(FALSE);
- m_Lead2.SetDstRect(
- 0.0f,
- 0.0f,
- (float)(int)(fwidth * width),
- (float)(int)(fheight * height) );
- m_Lead2.SetDstClipRect(
- 0.0f,
- 0.0f,
- (float)int(fwidth * width),
- (float)int(fheight * height) );
- // the 'Src' coordinates are relative to the client area of the original control (m_Lead1)
- // not of the zoom control (m_Lead2)!
- float x = -m_Lead1.GetDstLeft(),
- y = -m_Lead1.GetDstTop();
- m_Lead2.SetSrcRect(
- (float)int(x + width * (fleft + (fwidth / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
- (float)int(y + height * (ftop + (fheight / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
- (float)int(fwidth * width * 100.0f / float(nZoomFactor)),
- (float)int(fheight * height * 100.0f / float(nZoomFactor)) );
- m_Lead2.SetSrcClipRect(
- (float)int(x + width * (fleft + (fwidth / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
- (float)int(y + height * (ftop + (fheight / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
- (float)int(fwidth * width * 100.0f / float(nZoomFactor)),
- (float)int(fheight * height * 100.0f / float(nZoomFactor)) );
- // as opposed to SetxxxRect() functions, which use (left, top, width, height) as parameters,
- // MoveControl uses (left, top, bottom, right) as parameters
- // The '+2' added to the width and height are for the 1 pixel border
- m_Lead2.MoveWindow(
- int(fleft * width),
- int(ftop * height),
- int(fwidth * width) + 2,
- int(fheight * height) + 2);
- m_Lead2.SetAutoRepaint(TRUE);
- m_Lead1.UpdateWindow();
- #ifdef WIN32
- GdiFlush();
- #endif
- }
-
-
- BEGIN_EVENTSINK_MAP(CMagnifyView, CFormView)
- //{{AFX_EVENTSINK_MAP(CMagnifyView)
- ON_EVENT(CMagnifyView, IDC_LEADCTRL1, -605 /* MouseDown */, OnMouseDownLeadctrl1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- ON_EVENT(CMagnifyView, IDC_LEADCTRL1, -606 /* MouseMove */, OnMouseMoveLeadctrl1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- ON_EVENT(CMagnifyView, IDC_LEADCTRL1, -607 /* MouseUp */, OnMouseUpLeadctrl1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- ON_EVENT(CMagnifyView, IDC_LEADCTRL2, -605 /* MouseDown */, OnMouseDownLeadctrl2, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- ON_EVENT(CMagnifyView, IDC_LEADCTRL2, -606 /* MouseMove */, OnMouseMoveLeadctrl2, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- ON_EVENT(CMagnifyView, IDC_LEADCTRL2, -607 /* MouseUp */, OnMouseUpLeadctrl2, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
- //}}AFX_EVENTSINK_MAP
- END_EVENTSINK_MAP()
-
- void CMagnifyView::OnMouseDownLeadctrl1(short Button, short Shift, long X, long Y)
- {
- if( bMagnify )
- {
- xStart = (int)X;
- yStart = (int)Y;
- nDraggingOp = OP_MOVE_WINDOW;
- }
- }
-
- void CMagnifyView::OnMouseMoveLeadctrl1(short Button, short Shift, long X, long Y)
- {
- if( nDraggingOp == OP_MOVE_WINDOW )
- {
- RECT rcClient;
- m_Lead1.GetClientRect(&rcClient);
- float dx = float((int)X - xStart) / m_Lead1.GetBitmapWidth(),
- dy = float((int)Y - yStart) / m_Lead1.GetBitmapHeight();
- if( fleft + dx > 0 && ftop + dy > 0 )
- {
- fleft += dx;
- ftop += dy;
- SetZoomWindow();
- xStart = (int)X;
- yStart = (int)Y;
- }
- }
- }
-
- void CMagnifyView::OnMouseUpLeadctrl1(short Button, short Shift, long X, long Y)
- {
- if( nDraggingOp == OP_MOVE_WINDOW )
- nDraggingOp = OP_NONE;
- }
-
- void CMagnifyView::OnMouseDownLeadctrl2(short Button, short Shift, long X, long Y)
- {
- xStart = (int)X;
- yStart = (int)Y;
- nDraggingOp = OP_RESIZE_WINDOW;
- }
-
- void CMagnifyView::OnMouseMoveLeadctrl2(short Button, short Shift, long X, long Y)
- {
- if( nDraggingOp == OP_RESIZE_WINDOW )
- {
- RECT rcClient;
- m_Lead1.GetClientRect(&rcClient);
- float dx = float((int)X - xStart) / m_Lead1.GetBitmapWidth(),
- dy = float((int)Y - yStart) / m_Lead1.GetBitmapHeight();
- if( fwidth + dx > 0 && fheight + dy > 0 )
- {
- fwidth += dx;
- fheight += dy;
- SetZoomWindow();
- xStart = (int)X;
- yStart = (int)Y;
- }
- }
- }
-
- void CMagnifyView::OnMouseUpLeadctrl2(short Button, short Shift, long X, long Y)
- {
- if( nDraggingOp == OP_RESIZE_WINDOW )
- nDraggingOp = OP_NONE;
- }
-
- void CMagnifyView::OnSize(UINT nType, int cx, int cy)
- {
- CFormView::OnSize(nType, cx, cy);
-
- if( IsWindow(m_Lead1.m_hWnd) )
- m_Lead1.MoveWindow(0, 0, cx, cy);
- }
-