Counter Strike : Global Offensive Source Code
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.

151 lines
4.6 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "stdafx.h"
  7. #include "AnchorMgr.h"
  8. static int ProcessAnchorHorz( int originalCoord, int originalParentSize[2], EAnchorHorz eAnchor, int parentWidth, int parentHeight )
  9. {
  10. if ( eAnchor == k_eAnchorLeft )
  11. return originalCoord;
  12. else
  13. return parentWidth - (originalParentSize[0] - originalCoord);
  14. }
  15. static int ProcessAnchorVert( int originalCoord, int originalParentSize[2], EAnchorVert eAnchor, int parentWidth, int parentHeight )
  16. {
  17. if ( eAnchor == k_eAnchorTop )
  18. return originalCoord;
  19. else
  20. return parentHeight - (originalParentSize[1] - originalCoord);
  21. }
  22. CAnchorDef::CAnchorDef( int dlgItemID, ESimpleAnchor eSimpleAnchor )
  23. {
  24. Init( NULL, dlgItemID, eSimpleAnchor );
  25. }
  26. CAnchorDef::CAnchorDef( int dlgItemID, EAnchorHorz eLeftSide, EAnchorVert eTopSide, EAnchorHorz eRightSide, EAnchorVert eBottomSide )
  27. {
  28. Init( NULL, dlgItemID, eLeftSide, eTopSide, eRightSide, eBottomSide );
  29. }
  30. CAnchorDef::CAnchorDef( HWND hWnd, ESimpleAnchor eSimpleAnchor )
  31. {
  32. Init( hWnd, -1, eSimpleAnchor );
  33. }
  34. CAnchorDef::CAnchorDef( HWND hWnd, EAnchorHorz eLeftSide, EAnchorVert eTopSide, EAnchorHorz eRightSide, EAnchorVert eBottomSide )
  35. {
  36. Init( hWnd, -1, eLeftSide, eTopSide, eRightSide, eBottomSide );
  37. }
  38. void CAnchorDef::Init( HWND hWnd, int dlgItemID, ESimpleAnchor eSimpleAnchor )
  39. {
  40. if ( eSimpleAnchor == k_eSimpleAnchorBottomRight )
  41. {
  42. Init( hWnd, dlgItemID, k_eAnchorRight, k_eAnchorBottom, k_eAnchorRight, k_eAnchorBottom );
  43. }
  44. else if ( eSimpleAnchor == k_eSimpleAnchorAllSides )
  45. {
  46. Init( hWnd, dlgItemID, k_eAnchorLeft, k_eAnchorTop, k_eAnchorRight, k_eAnchorBottom );
  47. }
  48. else if ( eSimpleAnchor == k_eSimpleAnchorStretchRight )
  49. {
  50. Init( hWnd, dlgItemID, k_eAnchorLeft, k_eAnchorTop, k_eAnchorRight, k_eAnchorTop );
  51. }
  52. else if ( eSimpleAnchor == k_eSimpleAnchorRightSide )
  53. {
  54. Init( hWnd, dlgItemID, k_eAnchorRight, k_eAnchorTop, k_eAnchorRight, k_eAnchorTop );
  55. }
  56. else if ( eSimpleAnchor == k_eSimpleAnchorBottomSide )
  57. {
  58. Init( hWnd, dlgItemID, k_eAnchorLeft, k_eAnchorBottom, k_eAnchorLeft, k_eAnchorBottom );
  59. }
  60. }
  61. void CAnchorDef::Init( HWND hWnd, int dlgItemID, EAnchorHorz eLeftSide, EAnchorVert eTopSide, EAnchorHorz eRightSide, EAnchorVert eBottomSide )
  62. {
  63. m_hInputWnd = hWnd;
  64. m_DlgItemID = dlgItemID;
  65. m_AnchorLeft = eLeftSide;
  66. m_AnchorTop = eTopSide;
  67. m_AnchorRight = eRightSide;
  68. m_AnchorBottom = eBottomSide;
  69. }
  70. CAnchorMgr::CAnchorMgr()
  71. {
  72. }
  73. void CAnchorMgr::Init( HWND hParentWnd, CAnchorDef *pAnchors, int nAnchors )
  74. {
  75. m_Anchors.CopyArray( pAnchors, nAnchors );
  76. m_hParentWnd = hParentWnd;
  77. // Figure out the main window's size.
  78. RECT rcParent, rcItem;
  79. GetWindowRect( m_hParentWnd, &rcParent );
  80. m_OriginalParentSize[0] = rcParent.right - rcParent.left;
  81. m_OriginalParentSize[1] = rcParent.bottom - rcParent.top;
  82. // Get all the subitem positions.
  83. for ( int i=0; i < m_Anchors.Count(); i++ )
  84. {
  85. CAnchorDef *pAnchor = &m_Anchors[i];
  86. if ( pAnchor->m_hInputWnd )
  87. pAnchor->m_hWnd = pAnchor->m_hInputWnd;
  88. else
  89. pAnchor->m_hWnd = GetDlgItem( m_hParentWnd, pAnchor->m_DlgItemID );
  90. if ( !pAnchor->m_hWnd )
  91. continue;
  92. GetWindowRect( pAnchor->m_hWnd, &rcItem );
  93. POINT ptTopLeft;
  94. ptTopLeft.x = rcItem.left;
  95. ptTopLeft.y = rcItem.top;
  96. ScreenToClient( m_hParentWnd, &ptTopLeft );
  97. pAnchor->m_OriginalPos[0] = ptTopLeft.x;
  98. pAnchor->m_OriginalPos[1] = ptTopLeft.y;
  99. pAnchor->m_OriginalPos[2] = ptTopLeft.x + (rcItem.right - rcItem.left);
  100. pAnchor->m_OriginalPos[3] = ptTopLeft.y + (rcItem.bottom - rcItem.top);
  101. }
  102. }
  103. void CAnchorMgr::OnSize()
  104. {
  105. // Get the new size.
  106. int width, height;
  107. RECT rcParent;
  108. GetWindowRect( m_hParentWnd, &rcParent );
  109. width = rcParent.right - rcParent.left;
  110. height = rcParent.bottom - rcParent.top;
  111. // Move each item.
  112. for ( int i=0; i < m_Anchors.Count(); i++ )
  113. {
  114. CAnchorDef *pAnchor = &m_Anchors[i];
  115. if ( !pAnchor->m_hWnd )
  116. continue;
  117. RECT rcNew;
  118. rcNew.left = ProcessAnchorHorz( pAnchor->m_OriginalPos[0], m_OriginalParentSize, pAnchor->m_AnchorLeft, width, height );
  119. rcNew.right = ProcessAnchorHorz( pAnchor->m_OriginalPos[2], m_OriginalParentSize, pAnchor->m_AnchorRight, width, height );
  120. rcNew.top = ProcessAnchorVert( pAnchor->m_OriginalPos[1], m_OriginalParentSize, pAnchor->m_AnchorTop, width, height );
  121. rcNew.bottom = ProcessAnchorVert( pAnchor->m_OriginalPos[3], m_OriginalParentSize, pAnchor->m_AnchorBottom, width, height );
  122. SetWindowPos( pAnchor->m_hWnd, NULL, rcNew.left, rcNew.top, rcNew.right-rcNew.left, rcNew.bottom-rcNew.top, SWP_NOZORDER );
  123. InvalidateRect( pAnchor->m_hWnd, NULL, false );
  124. }
  125. }