2 * Copyright (c) 2001, 2002, 2003 Gary R. Van Sickle.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by Gary R. Van Sickle <g.r.vansickle@worldnet.att.net>
16 // This is the implementation of the PropSheet class. This class encapsulates
17 // a Windows property sheet / wizard and interfaces with the PropertyPage class.
18 // It's named PropSheet instead of PropertySheet because the latter conflicts with
19 // the Windows function of the same name.
21 #include "propsheet.h"
24 #include "RECTWrapper.h"
25 #include "ControlAdjuster.h"
28 // Sort of a "hidden" Windows structure. Used in the PropSheetCallback.
30 typedef struct DLGTEMPLATEEX
43 DLGTEMPLATEEX
, *LPDLGTEMPLATEEX
;
46 PropSheet::PropSheet ()
50 PropSheet::~PropSheet ()
55 PropSheet::CreatePages ()
57 HPROPSHEETPAGE
*retarray
;
59 // Create the return array
60 retarray
= new HPROPSHEETPAGE
[PropertyPages
.size()];
62 // Create the pages with CreatePropertySheetPage().
63 // We do it here rather than in the PropertyPages themselves
64 // because, for reasons known only to Microsoft, these handles will be
65 // destroyed by the property sheet before the PropertySheet() call returns,
66 // at least if it's modal (don't know about modeless).
68 for (i
= 0; i
< PropertyPages
.size(); i
++)
71 CreatePropertySheetPage (PropertyPages
[i
]->GetPROPSHEETPAGEPtr ());
76 PropertyPages
[i
]->YouAreFirst ();
78 else if (i
== PropertyPages
.size() - 1)
80 PropertyPages
[i
]->YouAreLast ();
84 PropertyPages
[i
]->YouAreMiddle ();
91 // Stuff needed by the PropSheet wndproc hook
96 RECTWrapper lastClientRect
;
105 clientRectValid
= false;
110 // @@@ Ugly. Really only works because only one PS is used now.
111 static PropSheetData
& Instance()
113 static PropSheetData TheInstance
;
118 static ControlAdjuster::ControlInfo PropSheetControlsInfo
[] = {
119 {0x3023, CP_RIGHT
, CP_BOTTOM
}, // Back
120 {0x3024, CP_RIGHT
, CP_BOTTOM
}, // Next
121 {0x3025, CP_RIGHT
, CP_BOTTOM
}, // Finish
122 {0x3026, CP_STRETCH
, CP_BOTTOM
}, // Line above buttons
123 { 2, CP_RIGHT
, CP_BOTTOM
}, // Cancel
127 static bool IsDialog (HWND hwnd
)
130 GetClassName (hwnd
, className
, sizeof (className
));
132 return (strcmp (className
, "#32770") == 0);
135 BOOL CALLBACK
EnumPages (HWND hwnd
, LPARAM lParam
)
137 // Is it really a dialog?
140 PropSheetData
& psd
= PropSheetData::Instance();
141 SetWindowPos (hwnd
, 0, psd
.pageRect
.left
, psd
.pageRect
.top
,
142 psd
.pageRect
.width (), psd
.pageRect
.height (),
143 SWP_NOACTIVATE
| SWP_NOZORDER
);
149 static LRESULT CALLBACK
PropSheetWndProc (HWND hwnd
, UINT uMsg
,
150 WPARAM wParam
, LPARAM lParam
)
152 PropSheetData
& psd
= PropSheetData::Instance();
156 if ((wParam
& 0xfff0) == SC_CLOSE
)
164 "Are you sure you want to exit setup? Any current download or installation will be aborted.",
165 "Exit Cygwin Setup?", MB_YESNO
) == IDNO
)
170 RECTWrapper clientRect
;
171 GetClientRect (hwnd
, &clientRect
);
174 When the window is minimized, the client rect is reduced to
175 (0,0-0,0), which causes child adjusting to screw slightly up. Work
176 around by not adjusting child upon minimization - it isn't really
179 if (wParam
!= SIZE_MINIMIZED
)
182 The first time we get a WM_SIZE, the client rect will be all zeros.
184 if (psd
.clientRectValid
)
187 clientRect
.width () - psd
.lastClientRect
.width ();
189 clientRect
.height () - psd
.lastClientRect
.height ();
191 ControlAdjuster::AdjustControls (hwnd
, PropSheetControlsInfo
,
194 psd
.pageRect
.right
+= dX
;
195 psd
.pageRect
.bottom
+= dY
;
198 The pages are child windows, but don't have IDs.
199 So change them by enumerating all childs and adjust all
203 EnumChildWindows (hwnd
, &EnumPages
, 0);
207 psd
.clientRectValid
= true;
210 Store away the current size and use it as the minmal window size.
214 GetWindowRect (hwnd
, &psd
.minRect
);
215 psd
.hasMinRect
= true;
218 psd
.lastClientRect
= clientRect
;
222 case WM_GETMINMAXINFO
:
226 LPMINMAXINFO mmi
= (LPMINMAXINFO
)lParam
;
227 mmi
->ptMinTrackSize
.x
= psd
.minRect
.width ();
228 mmi
->ptMinTrackSize
.y
= psd
.minRect
.height ();
234 return CallWindowProc (psd
.oldWndProc
,
235 hwnd
, uMsg
, wParam
, lParam
);
239 PropSheetProc (HWND hwndDlg
, UINT uMsg
, LPARAM lParam
)
245 LONG additionalStyle
=
246 (WS_MINIMIZEBOX
| WS_MAXIMIZEBOX
| WS_THICKFRAME
);
247 // Add a minimize box to the sheet/wizard.
248 if (((LPDLGTEMPLATEEX
) lParam
)->signature
== 0xFFFF)
250 ((LPDLGTEMPLATEEX
) lParam
)->style
|= additionalStyle
;
254 ((LPDLGTEMPLATE
) lParam
)->style
|= additionalStyle
;
258 case PSCB_INITIALIZED
:
261 PropSheet() with PSH_USEICONID only sets the small icon,
262 so we must set the big icon ourselves
264 SendMessage(hwndDlg
, WM_SETICON
, ICON_BIG
, (LPARAM
)LoadIcon(GetModuleHandle(NULL
), MAKEINTRESOURCE(IDI_CYGWIN
)));
266 Hook into the window proc.
267 We need to catch some messages for resizing.
269 PropSheetData::Instance().oldWndProc
=
270 (WNDPROC
)GetWindowLongPtr (hwndDlg
, GWLP_WNDPROC
);
271 SetWindowLongPtr (hwndDlg
, GWLP_WNDPROC
,
272 (LONG_PTR
)&PropSheetWndProc
);
273 ChooserPage::SetHwndDialog (hwndDlg
);
281 PropSheet::Create (const Window
* Parent
, DWORD Style
)
285 PageHandles
= CreatePages ();
287 p
.dwSize
= sizeof (PROPSHEETHEADER
);
288 p
.dwFlags
= PSH_NOAPPLYNOW
| PSH_WIZARD
| PSH_USECALLBACK
289 /*| PSH_MODELESS */ | PSH_USEICONID
;
292 p
.hwndParent
= Parent
->GetHWND ();
298 p
.hInstance
= GetInstance ();
299 p
.nPages
= PropertyPages
.size();
300 p
.pszIcon
= MAKEINTRESOURCE(IDI_CYGWIN
);
302 p
.phpage
= PageHandles
;
303 p
.pfnCallback
= PropSheetProc
;
306 // The winmain event loop actually resides in here.
309 // Do a modeless property sheet...
310 //SetHWND((HWND)PropertySheet(&p));
311 /*Show(SW_SHOWNORMAL);
313 // ...but pretend it's modal
315 MessageBox(NULL, "DONE", NULL, MB_OK);
317 // FIXME: Enable the parent before destroying this window to prevent another window
318 // from becoming the foreground window
319 // ala: EnableWindow(<parent_hwnd>, TRUE);
320 //DestroyWindow(WindowHandle);
329 PropSheet::SetHWNDFromPage (HWND h
)
331 // If we're a modal dialog, there's no way for us to know our window handle unless
332 // one of our pages tells us through this function.
337 Adjust the size of a page so that it fits nicely into the window.
340 PropSheet::AdjustPageSize (HWND page
)
342 PropSheetData
& psd
= PropSheetData::Instance();
343 if (!psd
.clientRectValid
) return;
346 It's probably not obvious what's done here:
347 When this method is called the first time, the first page is already
348 created and sized, but at the coordinates (0,0). The sheet, however,
349 isn't in it's final size. My guess is that the sheet first creates the
350 page, and then resizes itself to have the right metrics to contain the
351 page and moves it to it's position. For our purposes, however, we need
352 the final metrics of the page. So, the first time this method is called,
353 we basically grab the size of the page, but calculate the top/left coords
361 RECTWrapper
& pageRect
= psd
.pageRect
;
362 ::GetWindowRect (page
, &pageRect
);
363 // We want client coords.
364 ::ScreenToClient (page
, (LPPOINT
)&pageRect
.left
);
365 ::ScreenToClient (page
, (LPPOINT
)&pageRect
.right
);
367 LONG dialogBaseUnits
= ::GetDialogBaseUnits ();
368 // The margins in DUs are a result of "educated guesses" and T&E.
369 int marginX
= MulDiv (5, LOWORD(dialogBaseUnits
), 4);
370 int marginY
= MulDiv (5, HIWORD(dialogBaseUnits
), 8);
372 pageRect
.move (marginX
, marginY
);
375 SetWindowPos (page
, 0, psd
.pageRect
.left
, psd
.pageRect
.top
,
376 psd
.pageRect
.width (), psd
.pageRect
.height (),
377 SWP_NOACTIVATE
| SWP_NOZORDER
);
381 PropSheet::AddPage (PropertyPage
* p
)
383 // Add a page to the property sheet.
384 p
->YouAreBeingAddedToASheet (this);
385 PropertyPages
.push_back(p
);
389 PropSheet::SetActivePage (int i
)
391 // Posts a message to the message queue, so this won't block
392 return static_cast < bool > (PropSheet_SetCurSel (GetHWND (), NULL
, i
));
396 PropSheet::SetActivePageByID (int resource_id
)
398 // Posts a message to the message queue, so this won't block
399 return static_cast < bool >
400 (PropSheet_SetCurSelByID (GetHWND (), resource_id
));
404 PropSheet::SetButtons (DWORD flags
)
406 // Posts a message to the message queue, so this won't block
407 PropSheet_SetWizButtons (GetHWND (), flags
);
411 PropSheet::PressButton (int button
)
413 PropSheet_PressButton (GetHWND (), button
);