Translated using Weblate (Chinese (Simplified))
[cygwin-setup.git] / propsheet.cc
bloba2dca4581818bd8be1e9a4b5f6d1846facbdb6dd
1 /*
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
10 * http://www.gnu.org/
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"
22 #include "proppage.h"
23 #include "resource.h"
24 #include "RECTWrapper.h"
25 #include "ControlAdjuster.h"
26 #include "choose.h"
27 #include "msg.h"
29 // Sort of a "hidden" Windows structure. Used in the PropSheetCallback.
30 #include <pshpack1.h>
31 typedef struct DLGTEMPLATEEX
33 WORD dlgVer;
34 WORD signature;
35 DWORD helpID;
36 DWORD exStyle;
37 DWORD style;
38 WORD cDlgItems;
39 short x;
40 short y;
41 short cx;
42 short cy;
44 DLGTEMPLATEEX, *LPDLGTEMPLATEEX;
45 #include <poppack.h>
47 PropSheet::PropSheet ()
51 PropSheet::~PropSheet ()
55 HPROPSHEETPAGE *
56 PropSheet::CreatePages ()
58 HPROPSHEETPAGE *retarray;
60 // Create the return array
61 retarray = new HPROPSHEETPAGE[PropertyPages.size()];
63 // Create the pages with CreatePropertySheetPage().
64 // We do it here rather than in the PropertyPages themselves
65 // because, for reasons known only to Microsoft, these handles will be
66 // destroyed by the property sheet before the PropertySheet() call returns,
67 // at least if it's modal (don't know about modeless).
68 unsigned int i;
69 for (i = 0; i < PropertyPages.size(); i++)
71 retarray[i] =
72 CreatePropertySheetPageW (PropertyPages[i]->GetPROPSHEETPAGEPtr ());
74 // Set position info
75 if (i == 0)
77 PropertyPages[i]->YouAreFirst ();
79 else if (i == PropertyPages.size() - 1)
81 PropertyPages[i]->YouAreLast ();
83 else
85 PropertyPages[i]->YouAreMiddle ();
89 return retarray;
92 // Stuff needed by the PropSheet wndproc hook
93 struct PropSheetData
95 WNDPROC oldWndProc;
96 bool clientRectValid;
97 RECTWrapper lastClientRect;
98 bool gotPage;
99 RECTWrapper pageRect;
100 bool hasMinRect;
101 RECTWrapper minRect;
103 PropSheetData ()
105 oldWndProc = 0;
106 clientRectValid = false;
107 gotPage = false;
108 hasMinRect = false;
111 // @@@ Ugly. Really only works because only one PS is used now.
112 static PropSheetData& Instance()
114 static PropSheetData TheInstance;
115 return TheInstance;
119 static ControlAdjuster::ControlInfo PropSheetControlsInfo[] = {
120 {0x3023, CP_RIGHT, CP_BOTTOM}, // Back
121 {0x3024, CP_RIGHT, CP_BOTTOM}, // Next
122 {0x3025, CP_RIGHT, CP_BOTTOM}, // Finish
123 {0x3026, CP_STRETCH, CP_BOTTOM}, // Line above buttons
124 { 2, CP_RIGHT, CP_BOTTOM}, // Cancel
125 {0, CP_LEFT, CP_TOP}
128 static bool IsDialog (HWND hwnd)
130 char className[7];
131 GetClassName (hwnd, className, sizeof (className));
133 return (strcmp (className, "#32770") == 0);
136 BOOL CALLBACK EnumPages (HWND hwnd, LPARAM lParam)
138 // Is it really a dialog?
139 if (IsDialog (hwnd))
141 PropSheetData& psd = PropSheetData::Instance();
142 SetWindowPos (hwnd, 0, psd.pageRect.left, psd.pageRect.top,
143 psd.pageRect.width (), psd.pageRect.height (),
144 SWP_NOACTIVATE | SWP_NOZORDER);
147 return TRUE;
150 static LRESULT CALLBACK PropSheetWndProc (HWND hwnd, UINT uMsg,
151 WPARAM wParam, LPARAM lParam)
153 PropSheetData& psd = PropSheetData::Instance();
154 switch (uMsg)
156 case WM_SYSCOMMAND:
157 if ((wParam & 0xfff0) == SC_CLOSE)
158 goto areyousure;
159 break;
160 case WM_COMMAND:
161 if (wParam != 2)
162 break;
163 areyousure:
164 if (unattended_mode == unattended)
165 return 0;
166 if (mbox(hwnd, IDS_CONFIRM_EXIT, MB_YESNO) == IDNO)
167 return 0;
168 break;
169 case WM_SIZE:
171 RECTWrapper clientRect;
172 GetClientRect (hwnd, &clientRect);
175 When the window is minimized, the client rect is reduced to
176 (0,0-0,0), which causes child adjusting to screw slightly up. Work
177 around by not adjusting child upon minimization - it isn't really
178 needed then, anyway.
180 if (wParam != SIZE_MINIMIZED)
183 The first time we get a WM_SIZE, the client rect will be all zeros.
185 if (psd.clientRectValid)
187 const int dX =
188 clientRect.width () - psd.lastClientRect.width ();
189 const int dY =
190 clientRect.height () - psd.lastClientRect.height ();
192 ControlAdjuster::AdjustControls (hwnd, PropSheetControlsInfo,
193 dX, dY);
195 psd.pageRect.right += dX;
196 psd.pageRect.bottom += dY;
199 The pages are child windows, but don't have IDs.
200 So change them by enumerating all childs and adjust all
201 dialogs among them.
203 if (psd.gotPage)
204 EnumChildWindows (hwnd, &EnumPages, 0);
206 else
208 psd.clientRectValid = true;
211 Store away the current size and use it as the minmal window size.
213 if (!psd.hasMinRect)
215 GetWindowRect (hwnd, &psd.minRect);
216 psd.hasMinRect = true;
219 psd.lastClientRect = clientRect;
222 break;
223 case WM_GETMINMAXINFO:
225 if (psd.hasMinRect)
227 LPMINMAXINFO mmi = (LPMINMAXINFO)lParam;
228 mmi->ptMinTrackSize.x = psd.minRect.width ();
229 mmi->ptMinTrackSize.y = psd.minRect.height ();
232 break;
235 return CallWindowProc (psd.oldWndProc,
236 hwnd, uMsg, wParam, lParam);
239 static int CALLBACK
240 PropSheetProc (HWND hwndDlg, UINT uMsg, LPARAM lParam)
242 switch (uMsg)
244 case PSCB_PRECREATE:
246 LONG additionalStyle =
247 (WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME);
248 // Add a minimize box to the sheet/wizard.
249 if (((LPDLGTEMPLATEEX) lParam)->signature == 0xFFFF)
251 ((LPDLGTEMPLATEEX) lParam)->style |= additionalStyle;
253 else
255 ((LPDLGTEMPLATE) lParam)->style |= additionalStyle;
258 return TRUE;
259 case PSCB_INITIALIZED:
262 PropSheet() with PSH_USEICONID only sets the small icon,
263 so we must set the big icon ourselves
265 SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM)LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_CYGWIN)));
267 Hook into the window proc.
268 We need to catch some messages for resizing.
270 PropSheetData::Instance().oldWndProc = (WNDPROC)GetWindowLongPtrW (hwndDlg, GWLP_WNDPROC);
271 SetWindowLongPtrW (hwndDlg, GWLP_WNDPROC, (LONG_PTR)&PropSheetWndProc);
273 Store the PropSheet HWND in the Chooser, for use in resizing
275 (XXX: this is just silly: The PropSheet HWND is the parent of the
276 PropPage HWND, so chooser has access to that HWND as GetParent()
277 anyhow... )
279 ChooserPage::SetHwndDialog (hwndDlg);
281 return TRUE;
283 return TRUE;
286 bool
287 PropSheet::Create (const Window * Parent, DWORD Style)
289 PROPSHEETHEADERW p;
291 PageHandles = CreatePages ();
293 p.dwSize = sizeof (PROPSHEETHEADERW);
294 p.dwFlags = PSH_NOAPPLYNOW | PSH_WIZARD | PSH_USECALLBACK
295 /*| PSH_MODELESS */ | PSH_USEICONID;
296 if (Parent != NULL)
298 p.hwndParent = Parent->GetHWND ();
300 else
302 p.hwndParent = NULL;
304 p.hInstance = GetInstance ();
305 p.nPages = PropertyPages.size();
306 p.pszIcon = MAKEINTRESOURCEW(IDI_CYGWIN);
307 p.nStartPage = 0;
308 p.phpage = PageHandles;
309 p.pfnCallback = PropSheetProc;
311 // The winmain event loop actually resides in here.
312 PropertySheetW (&p);
314 SetHWND (NULL);
317 return true;
320 void
321 PropSheet::SetHWNDFromPage (HWND h)
323 // If we're a modal dialog, there's no way for us to know our window handle unless
324 // one of our pages tells us through this function.
325 SetHWND (h);
329 Adjust the size of a page so that it fits nicely into the window.
331 void
332 PropSheet::AdjustPageSize (HWND page)
334 PropSheetData& psd = PropSheetData::Instance();
335 if (!psd.clientRectValid) return;
338 It's probably not obvious what's done here:
339 When this method is called the first time, the first page is already
340 created and sized, but at the coordinates (0,0). The sheet, however,
341 isn't in it's final size. My guess is that the sheet first creates the
342 page, and then resizes itself to have the right metrics to contain the
343 page and moves it to it's position. For our purposes, however, we need
344 the final metrics of the page. So, the first time this method is called,
345 we basically grab the size of the page, but calculate the top/left coords
346 ourselves.
349 if (!psd.gotPage)
351 psd.gotPage = true;
353 RECTWrapper& pageRect = psd.pageRect;
354 ::GetWindowRect (page, &pageRect);
355 // We want client coords.
356 ::ScreenToClient (page, (LPPOINT)&pageRect.left);
357 ::ScreenToClient (page, (LPPOINT)&pageRect.right);
359 LONG dialogBaseUnits = ::GetDialogBaseUnits ();
360 // The margins in DUs are a result of "educated guesses" and T&E.
361 int marginX = MulDiv (5, LOWORD(dialogBaseUnits), 4);
362 int marginY = MulDiv (5, HIWORD(dialogBaseUnits), 8);
364 pageRect.move (marginX, marginY);
367 SetWindowPos (page, 0, psd.pageRect.left, psd.pageRect.top,
368 psd.pageRect.width (), psd.pageRect.height (),
369 SWP_NOACTIVATE | SWP_NOZORDER);
372 void
373 PropSheet::AddPage (PropertyPage * p)
375 // Add a page to the property sheet.
376 p->YouAreBeingAddedToASheet (this);
377 PropertyPages.push_back(p);
380 bool
381 PropSheet::SetActivePage (int i)
383 // Posts a message to the message queue, so this won't block
384 return static_cast < bool > (PropSheet_SetCurSel (GetHWND (), NULL, i));
387 bool
388 PropSheet::SetActivePageByID (int resource_id)
390 // Posts a message to the message queue, so this won't block
391 return static_cast < bool >
392 (PropSheet_SetCurSelByID (GetHWND (), resource_id));
395 void
396 PropSheet::SetButtons (DWORD flags)
398 // Posts a message to the message queue, so this won't block
399 PropSheet_SetWizButtons (GetHWND (), flags);
402 void
403 PropSheet::PressButton (int button)
405 PropSheet_PressButton (GetHWND (), button);