-
Notifications
You must be signed in to change notification settings - Fork 1
/
ResizableSheet.cpp
420 lines (340 loc) · 10.2 KB
/
ResizableSheet.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// ResizableSheet.cpp : implementation file
//
/////////////////////////////////////////////////////////////////////////////
//
// This file is part of ResizableLib
// http://sourceforge.net/projects/resizablelib
//
// Copyright (C) 2000-2004 by Paolo Messina
// http://www.geocities.com/ppescher - mailto:ppescher@hotmail.com
//
// The contents of this file are subject to the Artistic License (the "License").
// You may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.opensource.org/licenses/artistic-license.html
//
// If you find this code useful, credits would be nice!
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ResizableSheet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CResizableSheet
IMPLEMENT_DYNAMIC(CResizableSheet, CPropertySheet)
inline void CResizableSheet::PrivateConstruct()
{
m_bEnableSaveRestore = FALSE;
m_bSavePage = FALSE;
m_dwGripTempState = 1;
m_bLayoutDone = FALSE;
}
inline BOOL CResizableSheet::IsWizard() const
{
return (m_psh.dwFlags & PSH_WIZARD);
}
CResizableSheet::CResizableSheet()
{
PrivateConstruct();
}
CResizableSheet::CResizableSheet(UINT nIDCaption, CWnd *pParentWnd, UINT iSelectPage)
: CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
{
PrivateConstruct();
}
CResizableSheet::CResizableSheet(LPCTSTR pszCaption, CWnd *pParentWnd, UINT iSelectPage)
: CPropertySheet(pszCaption, pParentWnd, iSelectPage)
{
PrivateConstruct();
}
CResizableSheet::~CResizableSheet()
{
}
BEGIN_MESSAGE_MAP(CResizableSheet, CPropertySheet)
//{{AFX_MSG_MAP(CResizableSheet)
ON_WM_GETMINMAXINFO()
ON_WM_SIZE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_NCCREATE()
//}}AFX_MSG_MAP
ON_NOTIFY_REFLECT_EX(PSN_SETACTIVE, OnPageChanging)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CResizableSheet message handlers
BOOL CResizableSheet::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
{
if (!CPropertySheet::OnNcCreate(lpCreateStruct))
return FALSE;
// child dialogs don't want resizable border or size grip,
// nor they can handle the min/max size constraints
BOOL bChild = lpCreateStruct->style & WS_CHILD;
// create and init the size-grip
if (!CreateSizeGrip(!bChild))
return FALSE;
MakeResizable(lpCreateStruct);
return TRUE;
}
BOOL CResizableSheet::OnInitDialog()
{
BOOL bResult = CPropertySheet::OnInitDialog();
// set the initial size as the min track size
CRect rc;
GetWindowRect(&rc);
SetMinTrackSize(rc.Size());
// initialize layout
PresetLayout();
m_bLayoutDone = TRUE;
return bResult;
}
void CResizableSheet::OnDestroy()
{
if (m_bEnableSaveRestore)
{
SaveWindowRect(m_sSection, m_bRectOnly);
if (m_bSavePage)
SavePage(m_sSection);
}
RemoveAllAnchors();
CPropertySheet::OnDestroy();
}
// maps an index to a button ID and vice-versa
static UINT _propButtons[] =
{
IDOK, IDCANCEL, ID_APPLY_NOW, IDHELP,
ID_WIZBACK, ID_WIZNEXT, ID_WIZFINISH
};
const int _propButtonsCount = sizeof(_propButtons)/sizeof(UINT);
// horizontal line in wizard mode
#define ID_WIZLINE ID_WIZFINISH+1
void CResizableSheet::PresetLayout()
{
// set the initial size as the min track size
CRect rc;
GetWindowRect(&rc);
SetMinTrackSize(rc.Size());
if (GetStyle() & WS_CHILD)
{
GetClientRect(&rc);
GetTabControl()->MoveWindow(&rc);
}
if (IsWizard()) // wizard mode
{
// hide tab control
GetTabControl()->ShowWindow(SW_HIDE);
AddAnchor(ID_WIZLINE, BOTTOM_LEFT, BOTTOM_RIGHT);
}
else // tab mode
{
AddAnchor(AFX_IDC_TAB_CONTROL, TOP_LEFT, BOTTOM_RIGHT);
}
// add a callback for active page (which can change at run-time)
m_nCallbackID = AddAnchorCallback();
// use *total* parent size to have correct margins
CRect rectPage, rectSheet;
GetTotalClientRect(&rectSheet);
GetActivePage()->GetWindowRect(&rectPage);
::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectPage, 2);
// pre-calculate margins
m_sizePageTL = rectPage.TopLeft() - rectSheet.TopLeft();
m_sizePageBR = rectPage.BottomRight() - rectSheet.BottomRight();
// add all possible buttons, if they exist
for (int i = 0; i < _propButtonsCount; i++)
{
if (NULL != GetDlgItem(_propButtons[i]))
AddAnchor(_propButtons[i], BOTTOM_RIGHT);
}
// prevent flickering
GetTabControl()->ModifyStyle(0, WS_CLIPSIBLINGS);
}
BOOL CResizableSheet::ArrangeLayoutCallback(LAYOUTINFO &layout) const
{
if (layout.nCallbackID != m_nCallbackID) // we only added 1 callback
return CResizableLayout::ArrangeLayoutCallback(layout);
// set layout info for active page
layout.hWnd = (HWND)::SendMessage(m_hWnd, PSM_GETCURRENTPAGEHWND, 0, 0);
if (!::IsWindow(layout.hWnd))
return FALSE;
// set margins
if (IsWizard()) // wizard mode
{
// use pre-calculated margins
layout.marginTopLeft = m_sizePageTL;
layout.marginBottomRight = m_sizePageBR;
}
else // tab mode
{
CTabCtrl* pTab = GetTabControl();
ASSERT(pTab != NULL);
// get tab position after resizing and calc page rect
CRect rectPage, rectSheet;
GetTotalClientRect(&rectSheet);
if (!GetAnchorPosition(pTab->m_hWnd, rectSheet, rectPage))
return FALSE; // no page yet
// temporarily resize the tab control to calc page size
CRect rectSave;
pTab->GetWindowRect(rectSave);
::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectSave, 2);
pTab->SetRedraw(FALSE);
pTab->MoveWindow(rectPage, FALSE);
pTab->AdjustRect(FALSE, &rectPage);
pTab->MoveWindow(rectSave, FALSE);
pTab->SetRedraw(TRUE);
// set margins
layout.marginTopLeft = rectPage.TopLeft() - rectSheet.TopLeft();
layout.marginBottomRight = rectPage.BottomRight() - rectSheet.BottomRight();
}
// set anchor types
layout.anchorTopLeft = TOP_LEFT;
layout.anchorBottomRight = BOTTOM_RIGHT;
// use this layout info
return TRUE;
}
void CResizableSheet::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);
if (nType == SIZE_MAXHIDE || nType == SIZE_MAXSHOW)
return; // arrangement not needed
if (nType == SIZE_MAXIMIZED)
HideSizeGrip(&m_dwGripTempState);
else
ShowSizeGrip(&m_dwGripTempState);
// update grip and layout
UpdateSizeGrip();
ArrangeLayout();
}
BOOL CResizableSheet::OnPageChanging(NMHDR* /*pNotifyStruct*/, LRESULT* /*pResult*/)
{
// update new wizard page
// active page changes after this notification
PostMessage(WM_SIZE);
return FALSE; // continue routing
}
BOOL CResizableSheet::OnEraseBkgnd(CDC* pDC)
{
ClipChildren(pDC, FALSE);
BOOL bRet = CPropertySheet::OnEraseBkgnd(pDC);
ClipChildren(pDC, TRUE);
return bRet;
}
BOOL CResizableSheet::CalcSizeExtra(HWND /*hWndChild*/, CSize sizeChild, CSize &sizeExtra)
{
CTabCtrl* pTab = GetTabControl();
if (!pTab)
return FALSE;
// get margins of tabcontrol
CRect rectMargins;
if (!GetAnchorMargins(pTab->m_hWnd, sizeChild, rectMargins))
return FALSE;
// get margin caused by tabcontrol
CRect rectTabMargins(0,0,0,0);
// get tab position after resizing and calc page rect
CRect rectPage, rectSheet;
GetTotalClientRect(&rectSheet);
if (!GetAnchorPosition(pTab->m_hWnd, rectSheet, rectPage))
return FALSE; // no page yet
// temporarily resize the tab control to calc page size
CRect rectSave;
pTab->GetWindowRect(rectSave);
::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectSave, 2);
pTab->SetRedraw(FALSE);
pTab->MoveWindow(rectPage, FALSE);
pTab->AdjustRect(TRUE, &rectTabMargins);
pTab->MoveWindow(rectSave, FALSE);
pTab->SetRedraw(TRUE);
// add non-client size
::AdjustWindowRectEx(&rectTabMargins, GetStyle(), !(GetStyle() & WS_CHILD) &&
::IsMenu(GetMenu()->GetSafeHmenu()), GetExStyle());
// compute extra size
sizeExtra = rectMargins.TopLeft() + rectMargins.BottomRight() +
rectTabMargins.Size();
return TRUE;
}
void CResizableSheet::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
MinMaxInfo(lpMMI);
CTabCtrl* pTab = GetTabControl();
if (!pTab)
return;
int nCount = GetPageCount();
for (int idx = 0; idx < nCount; ++idx)
{
if (IsWizard()) // wizard mode
{
// use pre-calculated margins
CRect rectExtra(-CPoint(m_sizePageTL), -CPoint(m_sizePageBR));
// add non-client size
::AdjustWindowRectEx(&rectExtra, GetStyle(), !(GetStyle() & WS_CHILD) &&
::IsMenu(GetMenu()->GetSafeHmenu()), GetExStyle());
ChainMinMaxInfo(lpMMI, *GetPage(idx), rectExtra.Size());
}
else // tab mode
{
ChainMinMaxInfoCB(lpMMI, *GetPage(idx));
}
}
}
// protected members
int CResizableSheet::GetMinWidth()
{
CWnd* pWnd = NULL;
CRect rectWnd, rectSheet;
GetTotalClientRect(&rectSheet);
int max = 0, min = rectSheet.Width();
// search for leftmost and rightmost button margins
for (int i = 0; i < 7; i++)
{
pWnd = GetDlgItem(_propButtons[i]);
// exclude not present or hidden buttons
if (pWnd == NULL || !(pWnd->GetStyle() & WS_VISIBLE))
continue;
// left position is relative to the right border
// of the parent window (negative value)
pWnd->GetWindowRect(&rectWnd);
::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectWnd, 2);
int left = rectSheet.right - rectWnd.left;
int right = rectSheet.right - rectWnd.right;
if (left > max)
max = left;
if (right < min)
min = right;
}
// sizing border width
int border = GetSystemMetrics(SM_CXSIZEFRAME);
// compute total width
return max + min + 2*border;
}
// NOTE: this must be called after all the other settings
// to have the window and its controls displayed properly
void CResizableSheet::EnableSaveRestore(LPCTSTR pszSection, BOOL bRectOnly, BOOL bWithPage)
{
m_sSection = pszSection;
m_bSavePage = bWithPage;
m_bEnableSaveRestore = TRUE;
m_bRectOnly = bRectOnly;
// restore immediately
LoadWindowRect(pszSection, bRectOnly);
{
LoadPage(pszSection);
ArrangeLayout(); // needs refresh
}
}
void CResizableSheet::RefreshLayout()
{
SendMessage(WM_SIZE);
}
LRESULT CResizableSheet::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message != WM_NCCALCSIZE || wParam == 0 || !m_bLayoutDone)
return CPropertySheet::WindowProc(message, wParam, lParam);
// specifying valid rects needs controls already anchored
LRESULT lResult = 0;
HandleNcCalcSize(FALSE, (LPNCCALCSIZE_PARAMS)lParam, lResult);
lResult = CPropertySheet::WindowProc(message, wParam, lParam);
HandleNcCalcSize(TRUE, (LPNCCALCSIZE_PARAMS)lParam, lResult);
return lResult;
}