home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / olebar.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  187 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_OLE4_SEG
  14. #pragma code_seg(AFX_OLE4_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // COleResizeBar
  26.  
  27. COleResizeBar::COleResizeBar()
  28. {
  29.     // setup the state flags to do resize handles outside with hatched border
  30.     m_tracker.m_nStyle =
  31.         CRectTracker::hatchedBorder|CRectTracker::resizeOutside|
  32.         CRectTracker::solidLine;
  33.  
  34.     // the actual rectangle is updated in COleResizeBar::OnSize
  35. }
  36.  
  37. COleResizeBar::~COleResizeBar()
  38. {
  39. }
  40.  
  41. BOOL COleResizeBar::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
  42. {
  43.     ASSERT_VALID(this);
  44.  
  45.     if (pParentWnd != NULL)
  46.         ASSERT_VALID(pParentWnd);   // must have a parent
  47.  
  48.     // force WS_CLIPSIBLINGS (avoids SetWindowPos bugs)
  49.     dwStyle |= WS_CLIPSIBLINGS;
  50.  
  51.     VERIFY(AfxDeferRegisterClass(AFX_WNDCONTROLBAR_REG));
  52.  
  53.     // create the HWND
  54.     CRect rect;
  55.     rect.SetRectEmpty();
  56.     // Note: Parent must resize itself for control bar to be resized
  57.     return CWnd::Create(AFX_WNDCONTROLBAR, NULL, dwStyle, rect, pParentWnd, nID);
  58. }
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // COleResizeBar message handling
  62.  
  63. BEGIN_MESSAGE_MAP(COleResizeBar, CControlBar)
  64.     //{{AFX_MSG_MAP(COleResizeBar)
  65.     ON_WM_ERASEBKGND()
  66.     ON_WM_PAINT()
  67.     ON_WM_SIZE()
  68.     ON_WM_SETCURSOR()
  69.     ON_WM_LBUTTONDOWN()
  70.     ON_MESSAGE(WM_SIZEPARENT, OnSizeParent)
  71.     //}}AFX_MSG_MAP
  72. END_MESSAGE_MAP()
  73.  
  74. BOOL COleResizeBar::OnEraseBkgnd(CDC*)
  75. {
  76.     return TRUE;    // no erasing necessary
  77. }
  78.  
  79. void COleResizeBar::OnPaint()
  80. {
  81.     CPaintDC dc(this);
  82.  
  83.     // always use the same brush origin
  84.     CRect rect;
  85.     GetWindowRect(&rect);
  86.     dc.SetBrushOrg(rect.left & 7, rect.top & 7);
  87.  
  88.     // draw it
  89.     m_tracker.Draw(&dc);
  90. }
  91.  
  92. void COleResizeBar::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
  93. {
  94.     GetClientRect(&m_tracker.m_rect);
  95.     int nHandleSize = m_tracker.m_nHandleSize;
  96.     m_tracker.m_rect.InflateRect(-nHandleSize, -nHandleSize);
  97. }
  98.  
  99. BOOL COleResizeBar::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
  100. {
  101.     // hit-test the tracker -- we only care about hits on handles
  102.     CPoint point;
  103.     ::GetCursorPos(&point);
  104.     ScreenToClient(&point);
  105.     int hit = m_tracker.HitTest(point);
  106.     if (hit < 0)
  107.         return CControlBar::OnSetCursor(pWnd, nHitTest, message);
  108.  
  109.     // let the tracker handle setting the cursor
  110.     return m_tracker.SetCursor(pWnd, nHitTest);
  111. }
  112.  
  113. void COleResizeBar::OnLButtonDown(UINT /*nFlags*/, CPoint point)
  114. {
  115.     // track to parent of the parent
  116.     CWnd* pFrameWnd = GetParentFrame();
  117.     ASSERT_VALID(pFrameWnd);
  118.     CWnd* pParent = pFrameWnd->GetParent();
  119.  
  120.     pFrameWnd->UpdateWindow();  // update ourselves
  121.  
  122.     // limit tracking to parent client rectangle
  123.     if (pParent != NULL)
  124.     {
  125.         pParent->UpdateWindow();    // always update before tracking
  126.  
  127.         // clip cursor to parent window
  128.         CRect rect;
  129.         pParent->GetClientRect(&rect);
  130.         pParent->ClientToScreen(&rect);
  131.         ::ClipCursor(&rect);
  132.     }
  133.  
  134.     // save the rect, track, then restore
  135.     CRect rectSave = m_tracker.m_rect;
  136.     BOOL bNotify = m_tracker.Track(this, point, FALSE, pParent);
  137.     CRect rectNew = m_tracker.m_rect;
  138.     m_tracker.m_rect = rectSave;
  139.  
  140.     // allow full mouse movement again
  141.     ::ClipCursor(NULL);
  142.  
  143.     // notify owner window if tracker changed
  144.     if (bNotify)
  145.     {
  146.         CWnd* pOwner = GetOwner();
  147.         ASSERT_VALID(pOwner);
  148.  
  149.         // convert relative to parent coordinates
  150.         ClientToScreen(&rectNew);
  151.         pOwner->ScreenToClient(&rectNew);
  152.  
  153.         // send notification to owner
  154.         pOwner->SendMessage(WM_SIZECHILD, (WPARAM)_AfxGetDlgCtrlID(m_hWnd),
  155.             (LPARAM)(LPCRECT)&rectNew);
  156.     }
  157. }
  158.  
  159. LRESULT COleResizeBar::OnSizeParent(WPARAM, LPARAM lParam)
  160. {
  161.     AFX_SIZEPARENTPARAMS* lpLayout = (AFX_SIZEPARENTPARAMS*)lParam;
  162.  
  163.     // only resize the window if doing layout and not just rect query
  164.     if (lpLayout->hDWP != NULL)
  165.         AfxRepositionWindow(lpLayout, m_hWnd, &lpLayout->rect);
  166.  
  167.     // always adjust the rectangle after the resize
  168.     int nHandleSize = m_tracker.m_nHandleSize;
  169.     ::InflateRect(&lpLayout->rect, -nHandleSize, -nHandleSize);
  170.  
  171.     return 0;
  172. }
  173.  
  174. void COleResizeBar::OnUpdateCmdUI(CFrameWnd* /*pTarget*/,
  175.     BOOL /*bDisableIfNoHndler*/)
  176. {
  177.     // just do nothing
  178. }
  179.  
  180. #ifdef AFX_INIT_SEG
  181. #pragma code_seg(AFX_INIT_SEG)
  182. #endif
  183.  
  184. IMPLEMENT_DYNAMIC(COleResizeBar, CControlBar)
  185.  
  186. /////////////////////////////////////////////////////////////////////////////
  187.