1 // ResizableSheet.cpp : implementation file
\r
3 /////////////////////////////////////////////////////////////////////////////
\r
5 // This file is part of ResizableLib
\r
6 // http://sourceforge.net/projects/resizablelib
\r
8 // Copyright (C) 2000-2004 by Paolo Messina
\r
9 // http://www.geocities.com/ppescher - mailto:ppescher@hotmail.com
\r
11 // The contents of this file are subject to the Artistic License (the "License").
\r
12 // You may not use this file except in compliance with the License.
\r
13 // You may obtain a copy of the License at:
\r
14 // http://www.opensource.org/licenses/artistic-license.html
\r
16 // If you find this code useful, credits would be nice!
\r
18 /////////////////////////////////////////////////////////////////////////////
\r
21 #include "ResizableSheet.h"
\r
24 #define new DEBUG_NEW
\r
26 static char THIS_FILE[] = __FILE__;
\r
29 /////////////////////////////////////////////////////////////////////////////
\r
32 IMPLEMENT_DYNAMIC(CResizableSheet, CPropertySheet)
\r
34 inline void CResizableSheet::PrivateConstruct()
\r
36 m_bEnableSaveRestore = FALSE;
\r
37 m_bSavePage = FALSE;
\r
38 m_dwGripTempState = 1;
\r
39 m_bLayoutDone = FALSE;
\r
42 inline BOOL CResizableSheet::IsWizard() const
\r
44 return (m_psh.dwFlags & PSH_WIZARD);
\r
47 CResizableSheet::CResizableSheet()
\r
52 CResizableSheet::CResizableSheet(UINT nIDCaption, CWnd *pParentWnd, UINT iSelectPage)
\r
53 : CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
\r
58 CResizableSheet::CResizableSheet(LPCTSTR pszCaption, CWnd *pParentWnd, UINT iSelectPage)
\r
59 : CPropertySheet(pszCaption, pParentWnd, iSelectPage)
\r
64 CResizableSheet::~CResizableSheet()
\r
68 BEGIN_MESSAGE_MAP(CResizableSheet, CPropertySheet)
\r
69 //{{AFX_MSG_MAP(CResizableSheet)
\r
70 ON_WM_GETMINMAXINFO()
\r
76 ON_NOTIFY_REFLECT_EX(PSN_SETACTIVE, OnPageChanging)
\r
79 /////////////////////////////////////////////////////////////////////////////
\r
80 // CResizableSheet message handlers
\r
82 BOOL CResizableSheet::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
\r
84 if (!CPropertySheet::OnNcCreate(lpCreateStruct))
\r
87 // child dialogs don't want resizable border or size grip,
\r
88 // nor they can handle the min/max size constraints
\r
89 BOOL bChild = lpCreateStruct->style & WS_CHILD;
\r
91 // create and init the size-grip
\r
92 if (!CreateSizeGrip(!bChild))
\r
95 MakeResizable(lpCreateStruct);
\r
100 BOOL CResizableSheet::OnInitDialog()
\r
102 BOOL bResult = CPropertySheet::OnInitDialog();
\r
104 // set the initial size as the min track size
\r
106 GetWindowRect(&rc);
\r
107 SetMinTrackSize(rc.Size());
\r
109 // initialize layout
\r
111 m_bLayoutDone = TRUE;
\r
116 void CResizableSheet::OnDestroy()
\r
118 if (m_bEnableSaveRestore)
\r
120 SaveWindowRect(m_sSection, m_bRectOnly);
\r
122 SavePage(m_sSection);
\r
125 RemoveAllAnchors();
\r
127 CPropertySheet::OnDestroy();
\r
130 // maps an index to a button ID and vice-versa
\r
131 static UINT _propButtons[] =
\r
133 IDOK, IDCANCEL, ID_APPLY_NOW, IDHELP,
\r
134 ID_WIZBACK, ID_WIZNEXT, ID_WIZFINISH
\r
136 const int _propButtonsCount = sizeof(_propButtons)/sizeof(UINT);
\r
138 // horizontal line in wizard mode
\r
139 #define ID_WIZLINE ID_WIZFINISH+1
\r
141 void CResizableSheet::PresetLayout()
\r
143 // set the initial size as the min track size
\r
145 GetWindowRect(&rc);
\r
146 SetMinTrackSize(rc.Size());
\r
148 if (GetStyle() & WS_CHILD)
\r
150 GetClientRect(&rc);
\r
151 GetTabControl()->MoveWindow(&rc);
\r
154 if (IsWizard()) // wizard mode
\r
156 // hide tab control
\r
157 GetTabControl()->ShowWindow(SW_HIDE);
\r
159 AddAnchor(ID_WIZLINE, BOTTOM_LEFT, BOTTOM_RIGHT);
\r
163 AddAnchor(AFX_IDC_TAB_CONTROL, TOP_LEFT, BOTTOM_RIGHT);
\r
166 // add a callback for active page (which can change at run-time)
\r
167 m_nCallbackID = AddAnchorCallback();
\r
169 // use *total* parent size to have correct margins
\r
170 CRect rectPage, rectSheet;
\r
171 GetTotalClientRect(&rectSheet);
\r
173 GetActivePage()->GetWindowRect(&rectPage);
\r
174 ::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectPage, 2);
\r
176 // pre-calculate margins
\r
177 m_sizePageTL = rectPage.TopLeft() - rectSheet.TopLeft();
\r
178 m_sizePageBR = rectPage.BottomRight() - rectSheet.BottomRight();
\r
180 // add all possible buttons, if they exist
\r
181 for (int i = 0; i < _propButtonsCount; i++)
\r
183 if (NULL != GetDlgItem(_propButtons[i]))
\r
184 AddAnchor(_propButtons[i], BOTTOM_RIGHT);
\r
187 // prevent flickering
\r
188 GetTabControl()->ModifyStyle(0, WS_CLIPSIBLINGS);
\r
191 BOOL CResizableSheet::ArrangeLayoutCallback(LAYOUTINFO &layout) const
\r
193 if (layout.nCallbackID != m_nCallbackID) // we only added 1 callback
\r
194 return CResizableLayout::ArrangeLayoutCallback(layout);
\r
196 // set layout info for active page
\r
197 layout.hWnd = (HWND)::SendMessage(m_hWnd, PSM_GETCURRENTPAGEHWND, 0, 0);
\r
198 if (!::IsWindow(layout.hWnd))
\r
202 if (IsWizard()) // wizard mode
\r
204 // use pre-calculated margins
\r
205 layout.marginTopLeft = m_sizePageTL;
\r
206 layout.marginBottomRight = m_sizePageBR;
\r
210 CTabCtrl* pTab = GetTabControl();
\r
211 ASSERT(pTab != NULL);
\r
213 // get tab position after resizing and calc page rect
\r
214 CRect rectPage, rectSheet;
\r
215 GetTotalClientRect(&rectSheet);
\r
217 if (!GetAnchorPosition(pTab->m_hWnd, rectSheet, rectPage))
\r
218 return FALSE; // no page yet
\r
220 // temporarily resize the tab control to calc page size
\r
222 pTab->GetWindowRect(rectSave);
\r
223 ::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectSave, 2);
\r
224 pTab->SetRedraw(FALSE);
\r
225 pTab->MoveWindow(rectPage, FALSE);
\r
226 pTab->AdjustRect(FALSE, &rectPage);
\r
227 pTab->MoveWindow(rectSave, FALSE);
\r
228 pTab->SetRedraw(TRUE);
\r
231 layout.marginTopLeft = rectPage.TopLeft() - rectSheet.TopLeft();
\r
232 layout.marginBottomRight = rectPage.BottomRight() - rectSheet.BottomRight();
\r
235 // set anchor types
\r
236 layout.anchorTopLeft = TOP_LEFT;
\r
237 layout.anchorBottomRight = BOTTOM_RIGHT;
\r
239 // use this layout info
\r
243 void CResizableSheet::OnSize(UINT nType, int cx, int cy)
\r
245 CWnd::OnSize(nType, cx, cy);
\r
247 if (nType == SIZE_MAXHIDE || nType == SIZE_MAXSHOW)
\r
248 return; // arrangement not needed
\r
250 if (nType == SIZE_MAXIMIZED)
\r
251 HideSizeGrip(&m_dwGripTempState);
\r
253 ShowSizeGrip(&m_dwGripTempState);
\r
255 // update grip and layout
\r
260 BOOL CResizableSheet::OnPageChanging(NMHDR* /*pNotifyStruct*/, LRESULT* /*pResult*/)
\r
262 // update new wizard page
\r
263 // active page changes after this notification
\r
264 PostMessage(WM_SIZE);
\r
266 return FALSE; // continue routing
\r
269 BOOL CResizableSheet::OnEraseBkgnd(CDC* pDC)
\r
271 ClipChildren(pDC, FALSE);
\r
273 BOOL bRet = CPropertySheet::OnEraseBkgnd(pDC);
\r
275 ClipChildren(pDC, TRUE);
\r
280 BOOL CResizableSheet::CalcSizeExtra(HWND /*hWndChild*/, CSize sizeChild, CSize &sizeExtra)
\r
282 CTabCtrl* pTab = GetTabControl();
\r
286 // get margins of tabcontrol
\r
288 if (!GetAnchorMargins(pTab->m_hWnd, sizeChild, rectMargins))
\r
291 // get margin caused by tabcontrol
\r
292 CRect rectTabMargins(0,0,0,0);
\r
294 // get tab position after resizing and calc page rect
\r
295 CRect rectPage, rectSheet;
\r
296 GetTotalClientRect(&rectSheet);
\r
298 if (!GetAnchorPosition(pTab->m_hWnd, rectSheet, rectPage))
\r
299 return FALSE; // no page yet
\r
301 // temporarily resize the tab control to calc page size
\r
303 pTab->GetWindowRect(rectSave);
\r
304 ::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectSave, 2);
\r
305 pTab->SetRedraw(FALSE);
\r
306 pTab->MoveWindow(rectPage, FALSE);
\r
307 pTab->AdjustRect(TRUE, &rectTabMargins);
\r
308 pTab->MoveWindow(rectSave, FALSE);
\r
309 pTab->SetRedraw(TRUE);
\r
311 // add non-client size
\r
312 ::AdjustWindowRectEx(&rectTabMargins, GetStyle(), !(GetStyle() & WS_CHILD) &&
\r
313 ::IsMenu(GetMenu()->GetSafeHmenu()), GetExStyle());
\r
315 // compute extra size
\r
316 sizeExtra = rectMargins.TopLeft() + rectMargins.BottomRight() +
\r
317 rectTabMargins.Size();
\r
321 void CResizableSheet::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
\r
325 CTabCtrl* pTab = GetTabControl();
\r
329 int nCount = GetPageCount();
\r
330 for (int idx = 0; idx < nCount; ++idx)
\r
332 if (IsWizard()) // wizard mode
\r
334 // use pre-calculated margins
\r
335 CRect rectExtra(-CPoint(m_sizePageTL), -CPoint(m_sizePageBR));
\r
336 // add non-client size
\r
337 ::AdjustWindowRectEx(&rectExtra, GetStyle(), !(GetStyle() & WS_CHILD) &&
\r
338 ::IsMenu(GetMenu()->GetSafeHmenu()), GetExStyle());
\r
339 ChainMinMaxInfo(lpMMI, *GetPage(idx), rectExtra.Size());
\r
343 ChainMinMaxInfoCB(lpMMI, *GetPage(idx));
\r
348 // protected members
\r
350 int CResizableSheet::GetMinWidth()
\r
353 CRect rectWnd, rectSheet;
\r
354 GetTotalClientRect(&rectSheet);
\r
356 int max = 0, min = rectSheet.Width();
\r
357 // search for leftmost and rightmost button margins
\r
358 for (int i = 0; i < 7; i++)
\r
360 pWnd = GetDlgItem(_propButtons[i]);
\r
361 // exclude not present or hidden buttons
\r
362 if (pWnd == NULL || !(pWnd->GetStyle() & WS_VISIBLE))
\r
365 // left position is relative to the right border
\r
366 // of the parent window (negative value)
\r
367 pWnd->GetWindowRect(&rectWnd);
\r
368 ::MapWindowPoints(NULL, m_hWnd, (LPPOINT)&rectWnd, 2);
\r
369 int left = rectSheet.right - rectWnd.left;
\r
370 int right = rectSheet.right - rectWnd.right;
\r
378 // sizing border width
\r
379 int border = GetSystemMetrics(SM_CXSIZEFRAME);
\r
381 // compute total width
\r
382 return max + min + 2*border;
\r
386 // NOTE: this must be called after all the other settings
\r
387 // to have the window and its controls displayed properly
\r
388 void CResizableSheet::EnableSaveRestore(LPCTSTR pszSection, BOOL bRectOnly, BOOL bWithPage)
\r
390 m_sSection = pszSection;
\r
391 m_bSavePage = bWithPage;
\r
393 m_bEnableSaveRestore = TRUE;
\r
394 m_bRectOnly = bRectOnly;
\r
396 // restore immediately
\r
397 LoadWindowRect(pszSection, bRectOnly);
\r
399 LoadPage(pszSection);
\r
400 ArrangeLayout(); // needs refresh
\r
404 void CResizableSheet::RefreshLayout()
\r
406 SendMessage(WM_SIZE);
\r
409 LRESULT CResizableSheet::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
\r
411 if (message != WM_NCCALCSIZE || wParam == 0 || !m_bLayoutDone)
\r
412 return CPropertySheet::WindowProc(message, wParam, lParam);
\r
414 // specifying valid rects needs controls already anchored
\r
415 LRESULT lResult = 0;
\r
416 HandleNcCalcSize(FALSE, (LPNCCALCSIZE_PARAMS)lParam, lResult);
\r
417 lResult = CPropertySheet::WindowProc(message, wParam, lParam);
\r
418 HandleNcCalcSize(TRUE, (LPNCCALCSIZE_PARAMS)lParam, lResult);
\r