summaryrefslogtreecommitdiff
path: root/hammer/AnchorMgr.cpp
blob: 859dfbf029ec9a9dbb60a47cd40850b702e47e11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "stdafx.h"
#include "AnchorMgr.h"


static int ProcessAnchorHorz( int originalCoord, int originalParentSize[2], EAnchorHorz eAnchor, int parentWidth, int parentHeight )
{
	if ( eAnchor == k_eAnchorLeft )
		return originalCoord;
	else
		return parentWidth - (originalParentSize[0] - originalCoord);
}

static int ProcessAnchorVert( int originalCoord, int originalParentSize[2], EAnchorVert eAnchor, int parentWidth, int parentHeight )
{
	if ( eAnchor == k_eAnchorTop )
		return originalCoord;
	else
		return parentHeight - (originalParentSize[1] - originalCoord);
}


CAnchorDef::CAnchorDef( int dlgItemID, ESimpleAnchor eSimpleAnchor )
{
	Init( NULL, dlgItemID, eSimpleAnchor );
}

CAnchorDef::CAnchorDef( int dlgItemID, EAnchorHorz eLeftSide, EAnchorVert eTopSide, EAnchorHorz eRightSide, EAnchorVert eBottomSide )
{
	Init( NULL, dlgItemID, eLeftSide, eTopSide, eRightSide, eBottomSide );
}

CAnchorDef::CAnchorDef( HWND hWnd, ESimpleAnchor eSimpleAnchor )
{
	Init( hWnd, -1, eSimpleAnchor );
}

CAnchorDef::CAnchorDef( HWND hWnd, EAnchorHorz eLeftSide, EAnchorVert eTopSide, EAnchorHorz eRightSide, EAnchorVert eBottomSide )
{
	Init( hWnd, -1, eLeftSide, eTopSide, eRightSide, eBottomSide );
}

void CAnchorDef::Init( HWND hWnd, int dlgItemID, ESimpleAnchor eSimpleAnchor )
{
	if ( eSimpleAnchor == k_eSimpleAnchorBottomRight )
	{
		Init( hWnd, dlgItemID, k_eAnchorRight, k_eAnchorBottom, k_eAnchorRight, k_eAnchorBottom );
	}
	else if ( eSimpleAnchor == k_eSimpleAnchorAllSides )
	{
		Init( hWnd, dlgItemID, k_eAnchorLeft, k_eAnchorTop, k_eAnchorRight, k_eAnchorBottom );
	}
	else if ( eSimpleAnchor == k_eSimpleAnchorStretchRight )
	{
		Init( hWnd, dlgItemID, k_eAnchorLeft, k_eAnchorTop, k_eAnchorRight, k_eAnchorTop );
	}
	else if ( eSimpleAnchor == k_eSimpleAnchorRightSide )
	{
		Init( hWnd, dlgItemID, k_eAnchorRight, k_eAnchorTop, k_eAnchorRight, k_eAnchorTop );
	}
	else if ( eSimpleAnchor == k_eSimpleAnchorBottomSide )
	{
		Init( hWnd, dlgItemID, k_eAnchorLeft, k_eAnchorBottom, k_eAnchorLeft, k_eAnchorBottom );
	}
}

void CAnchorDef::Init( HWND hWnd, int dlgItemID, EAnchorHorz eLeftSide, EAnchorVert eTopSide, EAnchorHorz eRightSide, EAnchorVert eBottomSide )
{
	m_hInputWnd = hWnd;
	m_DlgItemID = dlgItemID;
	m_AnchorLeft = eLeftSide;
	m_AnchorTop = eTopSide;
	m_AnchorRight = eRightSide;
	m_AnchorBottom = eBottomSide;
}


CAnchorMgr::CAnchorMgr()
{
}

void CAnchorMgr::Init( HWND hParentWnd, CAnchorDef *pAnchors, int nAnchors )
{
	m_Anchors.CopyArray( pAnchors, nAnchors );
	
	m_hParentWnd = hParentWnd;
	
	// Figure out the main window's size.
	RECT rcParent, rcItem;
	GetWindowRect( m_hParentWnd, &rcParent );
	m_OriginalParentSize[0] = rcParent.right - rcParent.left;
	m_OriginalParentSize[1] = rcParent.bottom - rcParent.top;

	// Get all the subitem positions.	
	for ( int i=0; i < m_Anchors.Count(); i++ )
	{
		CAnchorDef *pAnchor = &m_Anchors[i];

		if ( pAnchor->m_hInputWnd )
			pAnchor->m_hWnd = pAnchor->m_hInputWnd;
		else	
			pAnchor->m_hWnd = GetDlgItem( m_hParentWnd, pAnchor->m_DlgItemID );
			
		if ( !pAnchor->m_hWnd )
			continue;

		GetWindowRect( pAnchor->m_hWnd, &rcItem );
		POINT ptTopLeft;
		ptTopLeft.x = rcItem.left;
		ptTopLeft.y = rcItem.top;
		ScreenToClient( m_hParentWnd, &ptTopLeft );
		
		pAnchor->m_OriginalPos[0] = ptTopLeft.x;
		pAnchor->m_OriginalPos[1] = ptTopLeft.y;
		pAnchor->m_OriginalPos[2] = ptTopLeft.x + (rcItem.right - rcItem.left);
		pAnchor->m_OriginalPos[3] = ptTopLeft.y + (rcItem.bottom - rcItem.top);
	}
}

void CAnchorMgr::OnSize()
{
	// Get the new size.
	int width, height;
	RECT rcParent;
	GetWindowRect( m_hParentWnd, &rcParent );
	width = rcParent.right - rcParent.left;
	height = rcParent.bottom - rcParent.top;
	
	// Move each item.
	for ( int i=0; i < m_Anchors.Count(); i++ )
	{
		CAnchorDef *pAnchor = &m_Anchors[i];
		if ( !pAnchor->m_hWnd )
			continue;
	
		RECT rcNew;
		rcNew.left   = ProcessAnchorHorz( pAnchor->m_OriginalPos[0], m_OriginalParentSize, pAnchor->m_AnchorLeft, width, height );
		rcNew.right  = ProcessAnchorHorz( pAnchor->m_OriginalPos[2], m_OriginalParentSize, pAnchor->m_AnchorRight, width, height );
		rcNew.top    = ProcessAnchorVert( pAnchor->m_OriginalPos[1], m_OriginalParentSize, pAnchor->m_AnchorTop, width, height );
		rcNew.bottom = ProcessAnchorVert( pAnchor->m_OriginalPos[3], m_OriginalParentSize, pAnchor->m_AnchorBottom, width, height );
	
		SetWindowPos( pAnchor->m_hWnd, NULL, rcNew.left, rcNew.top, rcNew.right-rcNew.left, rcNew.bottom-rcNew.top, SWP_NOZORDER );
		InvalidateRect( pAnchor->m_hWnd, NULL, false );
	}
}