-
Notifications
You must be signed in to change notification settings - Fork 1
/
GroupControl.cpp
204 lines (177 loc) · 5.65 KB
/
GroupControl.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
////////////////////////////////////////////////////////////////////////////
// File: GroupControl.cpp
// Version: 1
// Created: 24-Jul-2002
//
// Author: Paul S. Vickery
// E-mail: [email protected]
//
// CButton-derived class to make using groups easier. CGroupControl will
// automatically show/hide or enable/disable controls within the group when
// the group is shown/hidden or enabled/disabled. Also, if the group box is
// moved then its controls are moved with it.
//
// You are free to use or modify this code, with no restrictions, other than
// you continue to acknowledge me as the original author in this source code,
// or any code derived from it.
//
// If you use this code, or use it as a base for your own code, it would be
// nice to hear from you simply so I know it's not been a waste of time!
//
// Copyright (c) 2002 Paul S. Vickery
//
////////////////////////////////////////////////////////////////////////////
// Version History:
//
// Version 1 - 24-Jul-2002
// =======================
// Initial version
//
////////////////////////////////////////////////////////////////////////////
// PLEASE LEAVE THIS HEADER INTACT
////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GroupControl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CGroupControl
CGroupControl::CGroupControl()
{
m_bAllowOverlap = FALSE;
m_bUseTabOrder = FALSE;
}
CGroupControl::~CGroupControl()
{
}
BEGIN_MESSAGE_MAP(CGroupControl, CButton)
//{{AFX_MSG_MAP(CGroupControl)
ON_WM_ENABLE()
ON_WM_WINDOWPOSCHANGING()
ON_WM_SHOWWINDOW()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGroupControl message handlers
BOOL CGroupControl::Create(LPCTSTR lpszCaption, DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID)
{
dwStyle &= ~(0xF); // remove styles with conflict with group box
dwStyle |= BS_GROUPBOX; // add group box style
return CButton::Create(lpszCaption, dwStyle, rect, pParentWnd, nID);
}
void CGroupControl::PreSubclassWindow()
{
CButton::PreSubclassWindow();
// if this 'button' is not a group then
// it's a bit meaningless to use this control at all
ASSERT((GetStyle() & 0xF) & BS_GROUPBOX);
}
BOOL CGroupControl::IsInGroup(CWnd *pCtrl, BOOL &bOverlapped) const
{
BOOL bIsInGroup = FALSE;
if (pCtrl == NULL)
return FALSE;
CRect rcGroup, rc;
GetWindowRect(&rcGroup);
pCtrl->GetWindowRect(&rc);
if (rcGroup.PtInRect(rc.BottomRight()) && rcGroup.PtInRect(rc.TopLeft()))
bIsInGroup = TRUE;
if (! bIsInGroup && (rcGroup.PtInRect(rc.BottomRight()) || rcGroup.PtInRect(rc.TopLeft())))
bOverlapped = bIsInGroup = TRUE;
return bIsInGroup;
}
BOOL CGroupControl::DoGroupControlAction(GROUPCONTROLACTIONFUNC pfnGCAF, LPARAM lParam/*=0*/) const
{
if (pfnGCAF == NULL)
return FALSE;
// don't do anything if we're not a groupbox
if (!((GetStyle() & 0xF) & BS_GROUPBOX))
return FALSE;
// go through all controls that lie inside the group and enable/disable
// those controls also
CRect rcGroup;
GetWindowRect(&rcGroup);
// go through siblings, and see if they lie within the boundary
// of this group control
CWnd* pCtrl = NULL;
if (m_bUseTabOrder)
pCtrl = GetNextWindow();
else
pCtrl = GetParent()->GetWindow(GW_CHILD);
while (pCtrl != NULL)
{
if (pCtrl->GetSafeHwnd() != GetSafeHwnd())
{
BOOL bOverlapped = FALSE;
BOOL bIsInGroup = IsInGroup(pCtrl, bOverlapped);
if (bIsInGroup && (m_bAllowOverlap || ! bOverlapped))
{
if (! pfnGCAF(pCtrl, lParam))
return FALSE;
}
else if (! bOverlapped && m_bUseTabOrder) // found out side of group, so ditch out
break;
}
pCtrl = pCtrl->GetNextWindow();
}
return TRUE;
}
static BOOL GroupControlActionFunc_Enable(CWnd* pCtrl, LPARAM lParam)
{
if (pCtrl == NULL)
return TRUE;
BOOL bEnable = (BOOL)lParam;
pCtrl->EnableWindow(bEnable);
return TRUE;
}
void CGroupControl::OnEnable(BOOL bEnable)
{
CButton::OnEnable(bEnable);
DoGroupControlAction(GroupControlActionFunc_Enable, bEnable);
}
static BOOL GroupControlActionFunc_Move(CWnd* pCtrl, LPARAM lParam)
{
if (pCtrl == NULL)
return TRUE;
short nDeltaX = LOWORD(lParam);
short nDeltaY = HIWORD(lParam);
CRect rc;
pCtrl->GetWindowRect(&rc);
rc.OffsetRect(nDeltaX, nDeltaY);
pCtrl->GetParent()->ScreenToClient(&rc);
pCtrl->MoveWindow(rc);
return TRUE;
}
void CGroupControl::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
CButton::OnWindowPosChanging(lpwndpos);
if (lpwndpos->flags & SWP_NOMOVE)
return;
// see if we've moved x or y, and
// move group controls with group box
CRect rcGroup;
GetWindowRect(&rcGroup);
GetParent()->ScreenToClient(&rcGroup);
int nDeltaX = lpwndpos->x - rcGroup.left;
int nDeltaY = lpwndpos->y - rcGroup.top;
if (nDeltaX == 0 && nDeltaY == 0)
return;
DoGroupControlAction(GroupControlActionFunc_Move, MAKELPARAM(nDeltaX, nDeltaY));
Invalidate();
}
static BOOL GroupControlActionFunc_Show(CWnd* pCtrl, LPARAM lParam)
{
if (pCtrl == NULL)
return TRUE;
BOOL bShow = (BOOL)lParam;
pCtrl->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
return TRUE;
}
void CGroupControl::OnShowWindow(BOOL bShow, UINT nStatus)
{
CButton::OnShowWindow(bShow, nStatus);
DoGroupControlAction(GroupControlActionFunc_Show, bShow);
}