Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "window_anchor_mgr.h"
  9. // ------------------------------------------------------------------------------------ //
  10. // CWindowAnchor.
  11. // ------------------------------------------------------------------------------------ //
  12. bool CWindowAnchor::Init( CWnd *pParentWnd, CWnd *pChildWnd, int aLeft, int aTop, int aRight, int aBottom )
  13. {
  14. m_pWnd = pChildWnd;
  15. m_aLeft = aLeft;
  16. m_aTop = aTop;
  17. m_aRight = aRight;
  18. m_aBottom = aBottom;
  19. if ( m_pWnd && pParentWnd )
  20. {
  21. pParentWnd->GetWindowRect( m_ParentRect );
  22. pChildWnd->GetWindowRect( m_Rect );
  23. return true;
  24. }
  25. else
  26. {
  27. return false;
  28. }
  29. }
  30. void AnchorElement( long *pOut, CRect &rcParent, int startElement, CRect &rcParentStart, int flags )
  31. {
  32. if ( flags == ANCHOR_LEFT )
  33. *pOut = rcParent.left + ( startElement - rcParentStart.left );
  34. else if ( flags == ANCHOR_TOP )
  35. *pOut = rcParent.top + ( startElement - rcParentStart.top );
  36. else if ( flags == ANCHOR_RIGHT )
  37. *pOut = rcParent.right - ( rcParentStart.right - startElement );
  38. else if ( flags == ANCHOR_BOTTOM )
  39. *pOut = rcParent.bottom - ( rcParentStart.bottom - startElement );
  40. else if ( flags == ANCHOR_WIDTH_PERCENT )
  41. *pOut = rcParent.left + (rcParent.Width() * ( startElement - rcParentStart.left )) / rcParentStart.Width();
  42. else if ( flags == ANCHOR_HEIGHT_PERCENT )
  43. *pOut = rcParent.top + (rcParent.Height() * ( startElement - rcParentStart.top )) / rcParentStart.Height();
  44. }
  45. void CWindowAnchor::Update( CWnd *pParentWnd )
  46. {
  47. if ( !m_pWnd )
  48. return;
  49. CRect rcParent;
  50. pParentWnd->GetWindowRect( rcParent );
  51. CRect rcNew;
  52. AnchorElement( &rcNew.left, rcParent, m_Rect.left, m_ParentRect, m_aLeft );
  53. AnchorElement( &rcNew.top, rcParent, m_Rect.top, m_ParentRect, m_aTop );
  54. AnchorElement( &rcNew.right, rcParent, m_Rect.right, m_ParentRect, m_aRight );
  55. AnchorElement( &rcNew.bottom, rcParent, m_Rect.bottom, m_ParentRect, m_aBottom );
  56. pParentWnd->ScreenToClient( rcNew );
  57. m_pWnd->SetWindowPos( NULL, rcNew.left, rcNew.top, rcNew.Width(), rcNew.Height(), SWP_NOZORDER );
  58. m_pWnd->InvalidateRect( NULL );
  59. }
  60. // ------------------------------------------------------------------------------------ //
  61. // CWindowAnchorMgr.
  62. // ------------------------------------------------------------------------------------ //
  63. bool CWindowAnchorMgr::AddAnchor( CWnd *pParentWnd, CWnd *pChildWnd, int aLeft, int aTop, int aRight, int aBottom )
  64. {
  65. int index = m_Anchors.AddToTail();
  66. return m_Anchors[index].Init( pParentWnd, pChildWnd, aLeft, aTop, aRight, aBottom );
  67. }
  68. void CWindowAnchorMgr::UpdateAnchors( CWnd *pParentWnd )
  69. {
  70. FOR_EACH_LL( m_Anchors, i )
  71. {
  72. m_Anchors[i].Update( pParentWnd );
  73. }
  74. }