home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / leadtools / ocx32.lt / MAGNVIEW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-15  |  10.9 KB  |  356 lines

  1. // magnView.cpp : implementation of the CMagnifyView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "magnify.h"
  6.  
  7. #include "magnDoc.h"
  8. #include "magnView.h"
  9. #include "zoomdlg.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. extern CMagnifyApp theApp;
  18.  
  19. enum { OP_NONE, OP_RESIZE_WINDOW, OP_MOVE_WINDOW };
  20.  
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CMagnifyView
  23.  
  24. IMPLEMENT_DYNCREATE(CMagnifyView, CFormView)
  25.  
  26. BEGIN_MESSAGE_MAP(CMagnifyView, CFormView)
  27.     //{{AFX_MSG_MAP(CMagnifyView)
  28.     ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
  29.     ON_COMMAND(ID_MAGNIFY_DISABLE, OnMagnifyDisable)
  30.     ON_UPDATE_COMMAND_UI(ID_MAGNIFY_DISABLE, OnUpdateMagnifyDisable)
  31.     ON_COMMAND(ID_MAGNIFY_ZOOM, OnMagnifyZoom)
  32.     ON_WM_SIZE()
  33.     //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CMagnifyView construction/destruction
  38.  
  39. CMagnifyView::CMagnifyView()
  40.     : CFormView(CMagnifyView::IDD)
  41. {
  42.     //{{AFX_DATA_INIT(CMagnifyView)
  43.         // NOTE: the ClassWizard will add member initialization here
  44.     //}}AFX_DATA_INIT
  45.     // TODO: add construction code here
  46.  
  47.     bMagnify = FALSE;
  48.     fleft = ftop = 0.25f;
  49.     fwidth = fheight = 0.5f;
  50.     nZoomFactor = 100;
  51. }
  52.  
  53. CMagnifyView::~CMagnifyView()
  54. {
  55. }
  56.  
  57. void CMagnifyView::DoDataExchange(CDataExchange* pDX)
  58. {
  59.     CFormView::DoDataExchange(pDX);
  60.     //{{AFX_DATA_MAP(CMagnifyView)
  61.     DDX_Control(pDX, IDC_LEADCTRL1, m_Lead1);
  62.     DDX_Control(pDX, IDC_LEADCTRL2, m_Lead2);
  63.     //}}AFX_DATA_MAP
  64. }
  65.  
  66. BOOL CMagnifyView::PreCreateWindow(CREATESTRUCT& cs)
  67. {
  68.     // TODO: Modify the Window class or styles here by modifying
  69.     //  the CREATESTRUCT cs
  70.  
  71.     return CFormView::PreCreateWindow(cs);
  72. }
  73.  
  74. /////////////////////////////////////////////////////////////////////////////
  75. // CMagnifyView diagnostics
  76.  
  77. #ifdef _DEBUG
  78. void CMagnifyView::AssertValid() const
  79. {
  80.     CFormView::AssertValid();
  81. }
  82.  
  83. void CMagnifyView::Dump(CDumpContext& dc) const
  84. {
  85.     CFormView::Dump(dc);
  86. }
  87.  
  88. CMagnifyDoc* CMagnifyView::GetDocument() // non-debug version is inline
  89. {
  90.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMagnifyDoc)));
  91.     return (CMagnifyDoc*)m_pDocument;
  92. }
  93. #endif //_DEBUG
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CMagnifyView message handlers
  97. CLead* CMagnifyView::GetDocLead() 
  98. {
  99.     CMagnifyDoc* pDoc = GetDocument();
  100.     return(&pDoc->m_Lead);
  101. }
  102.  
  103.  
  104. void CMagnifyView::OnInitialUpdate() 
  105. {
  106.     CFormView::OnInitialUpdate();
  107.  
  108.    UNLOCKSUPPORT(m_Lead1);
  109.  
  110.     // Disable the use of our scroll bars. Set the control to display the whole image
  111.     // Let CFormView handle the scrolling. If you want to use CLead's automatic scrolling, 
  112.     // make the Lead control in the form as small as possible, otherwise CFormView might create a second
  113.     // set of scroll bars if the window becomes smaller than the size of the controls on the form.
  114.     int xExt = (int)m_Lead1.GetBitmapWidth(),
  115.         yExt = (int)m_Lead1.GetBitmapHeight();
  116.     SetScrollSizes(MM_TEXT,CSize(xExt,yExt));
  117.     // Make the window fit the image
  118.     GetParentFrame()->RecalcLayout();
  119.     ResizeParentToFit();
  120.  
  121.     // disable the view window's scroll bars
  122.     SetScrollSizes(MM_TEXT,CSize(1,1));
  123.     m_Lead2.ShowWindow(SW_HIDE);
  124.     m_Lead2.SetBorderStyle(1);
  125.     m_Lead2.SetBackErase(FALSE);
  126.     m_Lead1.SetBackErase(FALSE);
  127. }
  128.  
  129. LRESULT CMagnifyView::OnDoRealize(WPARAM wParam, LPARAM lParam)
  130. {
  131.     if( !IsWindow(m_Lead1.m_hWnd) )
  132.         return FALSE;
  133.     if(lParam)
  134.     {
  135.         return m_Lead1.SendMessage(WM_PALETTECHANGED, wParam);
  136.     }
  137.     else
  138.     {
  139.         UINT nColorsChanged = 0;
  140.         CDC* pdc;
  141.  
  142.         pdc = theApp.m_pMainWnd->GetDC();
  143.         HPALETTE hpal = (HPALETTE)m_Lead1.GetPalette((OLE_HANDLE) pdc->m_hDC);
  144.         if(hpal)
  145.         {
  146.             CPalette pal;
  147.  
  148.             pal.Attach(hpal);
  149.             CPalette* oldPalette = pdc->SelectPalette(&pal, (BOOL) lParam);
  150.             nColorsChanged = pdc->RealizePalette();
  151.             if (nColorsChanged > 0)
  152.             {
  153.                 m_Lead1.InvalidateRect(NULL, FALSE);
  154.                 if( bMagnify )
  155.                     m_Lead2.InvalidateRect(NULL, FALSE);
  156.             }
  157.             pdc->SelectPalette(oldPalette, TRUE);
  158.         }
  159.         theApp.m_pMainWnd->ReleaseDC(pdc);
  160.         return ((LRESULT) (BOOL) (nColorsChanged > 0));
  161.     }
  162. }
  163.  
  164. void CMagnifyView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
  165. {
  166.     CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  167.     if (bActivate)
  168.     {
  169.         ASSERT(pActivateView == this);
  170.         OnDoRealize((WPARAM)theApp.m_pMainWnd->m_hWnd, (LPARAM) FALSE);   // same as SendMessage(WM_DOREALIZE);
  171.     }
  172. }
  173.  
  174.  
  175. void CMagnifyView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
  176. {
  177.     CMagnifyDoc* pDoc = GetDocument();
  178.     m_Lead1.SetAutoRepaint(FALSE);
  179.     m_Lead1.SetBitmap(GetDocLead()->GetBitmap());
  180.     m_Lead1.SetPaintPalette(PAINTPALETTE_AUTO);
  181.     m_Lead1.SetPaintDither(PAINTDITHER_DIFFUSION);
  182.     m_Lead1.SetBitonalScaling(BITONALSCALING_NORMAL);
  183.     m_Lead1.SetDstRect(m_Lead1.GetDstLeft(), m_Lead1.GetDstTop(), m_Lead1.GetBitmapWidth(), m_Lead1.GetBitmapHeight());
  184.     m_Lead1.SetDstClipRect(m_Lead1.GetDstLeft(), m_Lead1.GetDstTop(), m_Lead1.GetDstWidth(), m_Lead1.GetDstHeight());
  185.     m_Lead1.SetAutoRepaint(TRUE);
  186.  
  187.     m_Lead2.SetAutoRepaint(FALSE);
  188.     m_Lead2.SetBitmap(GetDocLead()->GetBitmap());
  189.     m_Lead2.SetPaintPalette(PAINTPALETTE_AUTO);
  190.     m_Lead2.SetPaintDither(PAINTDITHER_DIFFUSION);
  191.     m_Lead2.SetBitonalScaling(BITONALSCALING_NORMAL);
  192.     SetZoomWindow();
  193.     m_Lead2.SetAutoRepaint(TRUE);
  194.  
  195.     CView::OnUpdate(pSender, lHint, pHint);
  196. }
  197.  
  198. void CMagnifyView::OnMagnifyDisable() 
  199. {
  200.    if( !bMagnify ) return;
  201.    bMagnify = FALSE;
  202.     m_Lead2.ShowWindow(SW_HIDE);
  203. }
  204.  
  205. void CMagnifyView::OnUpdateMagnifyDisable(CCmdUI* pCmdUI) 
  206. {
  207.    pCmdUI->SetCheck(!bMagnify);
  208. }
  209.  
  210. void CMagnifyView::OnMagnifyZoom() 
  211. {
  212.     CZoomDlg dlg;
  213.     dlg.nZoomFactor = nZoomFactor;
  214.     if( dlg.DoModal() == IDOK )
  215.     {
  216.         nZoomFactor = dlg.nZoomFactor;
  217.         SetZoomWindow();
  218.         bMagnify = TRUE;
  219.         m_Lead2.ShowWindow(SW_SHOW);
  220.     }
  221. }
  222.  
  223. void CMagnifyView::SetZoomWindow()
  224. {
  225.     float width = m_Lead2.GetBitmapWidth(),
  226.         height = m_Lead2.GetBitmapHeight();
  227.  
  228.     // the 'Dest' coordinates are relative to the client area of the zoom control (m_Lead2)
  229.     // not of the original control (m_Lead1)!
  230.     m_Lead2.SetAutoRepaint(FALSE);
  231.     m_Lead2.SetDstRect( 
  232.         0.0f,
  233.         0.0f,
  234.         (float)(int)(fwidth * width),
  235.         (float)(int)(fheight * height) );
  236.     m_Lead2.SetDstClipRect( 
  237.         0.0f,
  238.         0.0f,
  239.         (float)int(fwidth * width),
  240.         (float)int(fheight * height) );
  241.     // the 'Src' coordinates are relative to the client area of the original control (m_Lead1)
  242.     // not of the zoom control (m_Lead2)!
  243.     float x = -m_Lead1.GetDstLeft(),
  244.           y = -m_Lead1.GetDstTop();
  245.     m_Lead2.SetSrcRect( 
  246.         (float)int(x + width * (fleft + (fwidth / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
  247.         (float)int(y + height * (ftop + (fheight / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
  248.         (float)int(fwidth * width * 100.0f / float(nZoomFactor)),
  249.         (float)int(fheight * height * 100.0f / float(nZoomFactor)) );
  250.     m_Lead2.SetSrcClipRect( 
  251.         (float)int(x + width * (fleft + (fwidth / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
  252.         (float)int(y + height * (ftop + (fheight / 2.0f) * (1.0f - 100.0f / float(nZoomFactor)))),
  253.         (float)int(fwidth * width * 100.0f / float(nZoomFactor)),
  254.         (float)int(fheight * height * 100.0f / float(nZoomFactor)) );
  255.     // as opposed to SetxxxRect() functions, which use (left, top, width, height) as parameters,
  256.     // MoveControl uses (left, top, bottom, right) as parameters
  257.     // The '+2' added to the width and height are for the 1 pixel border
  258.     m_Lead2.MoveWindow( 
  259.         int(fleft * width),
  260.         int(ftop * height),
  261.         int(fwidth * width) + 2,
  262.         int(fheight * height) + 2);
  263.     m_Lead2.SetAutoRepaint(TRUE);
  264.     m_Lead1.UpdateWindow();
  265. #ifdef WIN32
  266.     GdiFlush();
  267. #endif
  268. }
  269.  
  270.  
  271. BEGIN_EVENTSINK_MAP(CMagnifyView, CFormView)
  272.     //{{AFX_EVENTSINK_MAP(CMagnifyView)
  273.     ON_EVENT(CMagnifyView, IDC_LEADCTRL1, -605 /* MouseDown */, OnMouseDownLeadctrl1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
  274.     ON_EVENT(CMagnifyView, IDC_LEADCTRL1, -606 /* MouseMove */, OnMouseMoveLeadctrl1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
  275.     ON_EVENT(CMagnifyView, IDC_LEADCTRL1, -607 /* MouseUp */, OnMouseUpLeadctrl1, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
  276.     ON_EVENT(CMagnifyView, IDC_LEADCTRL2, -605 /* MouseDown */, OnMouseDownLeadctrl2, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
  277.     ON_EVENT(CMagnifyView, IDC_LEADCTRL2, -606 /* MouseMove */, OnMouseMoveLeadctrl2, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
  278.     ON_EVENT(CMagnifyView, IDC_LEADCTRL2, -607 /* MouseUp */, OnMouseUpLeadctrl2, VTS_I2 VTS_I2 VTS_I4 VTS_I4)
  279.     //}}AFX_EVENTSINK_MAP
  280. END_EVENTSINK_MAP()
  281.  
  282. void CMagnifyView::OnMouseDownLeadctrl1(short Button, short Shift, long X, long Y) 
  283. {
  284.     if( bMagnify )
  285.     {
  286.         xStart = (int)X;
  287.         yStart = (int)Y;
  288.         nDraggingOp = OP_MOVE_WINDOW;
  289.     }
  290. }
  291.  
  292. void CMagnifyView::OnMouseMoveLeadctrl1(short Button, short Shift, long X, long Y) 
  293. {
  294.     if( nDraggingOp == OP_MOVE_WINDOW )
  295.     {
  296.         RECT rcClient;
  297.         m_Lead1.GetClientRect(&rcClient);
  298.         float dx = float((int)X - xStart) / m_Lead1.GetBitmapWidth(),
  299.               dy = float((int)Y - yStart) / m_Lead1.GetBitmapHeight();
  300.         if( fleft + dx > 0 && ftop + dy > 0 )
  301.         {
  302.             fleft += dx;
  303.             ftop += dy;
  304.             SetZoomWindow();
  305.             xStart = (int)X;
  306.             yStart = (int)Y;
  307.         }
  308.     }
  309. }
  310.  
  311. void CMagnifyView::OnMouseUpLeadctrl1(short Button, short Shift, long X, long Y) 
  312. {
  313.     if( nDraggingOp == OP_MOVE_WINDOW )
  314.         nDraggingOp = OP_NONE;
  315. }
  316.  
  317. void CMagnifyView::OnMouseDownLeadctrl2(short Button, short Shift, long X, long Y) 
  318. {
  319.     xStart = (int)X;
  320.     yStart = (int)Y;
  321.     nDraggingOp = OP_RESIZE_WINDOW;
  322. }
  323.  
  324. void CMagnifyView::OnMouseMoveLeadctrl2(short Button, short Shift, long X, long Y) 
  325. {
  326.     if( nDraggingOp == OP_RESIZE_WINDOW )
  327.     {
  328.         RECT rcClient;
  329.         m_Lead1.GetClientRect(&rcClient);
  330.         float dx = float((int)X - xStart) / m_Lead1.GetBitmapWidth(),
  331.               dy = float((int)Y - yStart) / m_Lead1.GetBitmapHeight();
  332.         if( fwidth + dx > 0 && fheight + dy > 0 )
  333.         {
  334.             fwidth += dx;
  335.             fheight += dy;
  336.             SetZoomWindow();
  337.             xStart = (int)X;
  338.             yStart = (int)Y;
  339.         }
  340.     }
  341. }
  342.  
  343. void CMagnifyView::OnMouseUpLeadctrl2(short Button, short Shift, long X, long Y) 
  344. {
  345.     if( nDraggingOp == OP_RESIZE_WINDOW )
  346.         nDraggingOp = OP_NONE;
  347. }
  348.  
  349. void CMagnifyView::OnSize(UINT nType, int cx, int cy) 
  350. {
  351.     CFormView::OnSize(nType, cx, cy);
  352.     
  353.     if( IsWindow(m_Lead1.m_hWnd) )
  354.         m_Lead1.MoveWindow(0, 0, cx, cy);
  355. }
  356.