Remove unneeded headers to reduce unneeded rebuilds.
[wine] / dlls / comctl32 / propsheet.c
1 /*
2  * Property Sheets
3  *
4  * Copyright 1998 Francis Beaudet
5  * Copyright 1999 Thuy Nguyen
6  * Copyright 2004 Maxime Bellenge
7  * Copyright 2004 Filip Navara
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  * This code was audited for completeness against the documented features
24  * of Comctl32.dll version 6.0 on Sep. 12, 2004, by Filip Navara.
25  * 
26  * Unless otherwise noted, we believe this code to be complete, as per
27  * the specification mentioned above.
28  * If you discover missing features, or bugs, please note them below.
29  *
30  * TODO:
31  *   - Tab order
32  *   - Wizard 97 header resizing
33  *   - Enforcing of minimal wizard size
34  *   - Messages:
35  *     o PSM_GETRESULT
36  *     o PSM_IDTOINDEX
37  *     o PSM_INSERTPAGE
38  *     o PSM_RECALCPAGESIZES
39  *     o PSM_SETHEADERSUBTITLE
40  *     o PSM_SETHEADERTITLE
41  *     o WM_HELP
42  *     o WM_CONTEXTMENU
43  *   - Notifications:
44  *     o PSN_GETOBJECT
45  *     o PSN_QUERYINITIALFOCUS
46  *     o PSN_TRANSLATEACCELERATOR
47  *   - Styles:
48  *     o PSH_WIZARDHASFINISH
49  *     o PSH_RTLREADING
50  *     o PSH_STRETCHWATERMARK
51  *     o PSH_USEPAGELANG
52  *     o PSH_USEPSTARTPAGE
53  *   - Page styles:
54  *     o PSP_USEFUSIONCONTEXT
55  *     o PSP_USEREFPARENT
56  */
57
58 #include <stdarg.h>
59 #include <string.h>
60
61 #define NONAMELESSUNION
62 #define NONAMELESSSTRUCT
63 #include "windef.h"
64 #include "winbase.h"
65 #include "wingdi.h"
66 #include "winuser.h"
67 #include "winnls.h"
68 #include "commctrl.h"
69 #include "prsht.h"
70 #include "comctl32.h"
71
72 #include "wine/debug.h"
73 #include "wine/unicode.h"
74
75 /******************************************************************************
76  * Data structures
77  */
78 #include "pshpack2.h"
79
80 typedef struct
81 {
82   WORD dlgVer;
83   WORD signature;
84   DWORD helpID;
85   DWORD exStyle;
86   DWORD style;
87 } MyDLGTEMPLATEEX;
88
89 typedef struct
90 {
91   DWORD helpid;
92   DWORD exStyle;
93   DWORD style;
94   short x;
95   short y;
96   short cx;
97   short cy;
98   DWORD id;
99 } MyDLGITEMTEMPLATEEX;
100 #include "poppack.h"
101
102 typedef struct tagPropPageInfo
103 {
104   HPROPSHEETPAGE hpage; /* to keep track of pages not passed to PropertySheet */
105   HWND hwndPage;
106   BOOL isDirty;
107   LPCWSTR pszText;
108   BOOL hasHelp;
109   BOOL useCallback;
110   BOOL hasIcon;
111 } PropPageInfo;
112
113 typedef struct tagPropSheetInfo
114 {
115   HWND hwnd;
116   PROPSHEETHEADERW ppshheader;
117   BOOL unicode;
118   LPWSTR strPropertiesFor;
119   int nPages;
120   int active_page;
121   BOOL isModeless;
122   BOOL hasHelp;
123   BOOL hasApply;
124   BOOL useCallback;
125   BOOL restartWindows;
126   BOOL rebootSystem;
127   BOOL activeValid;
128   PropPageInfo* proppage;
129   HFONT hFont;
130   HFONT hFontBold;
131   int width;
132   int height;
133   HIMAGELIST hImageList;
134 } PropSheetInfo;
135
136 typedef struct
137 {
138   int x;
139   int y;
140 } PADDING_INFO;
141
142 /******************************************************************************
143  * Defines and global variables
144  */
145
146 const WCHAR PropSheetInfoStr[] =
147     {'P','r','o','p','e','r','t','y','S','h','e','e','t','I','n','f','o',0 };
148
149 #define PSP_INTERNAL_UNICODE 0x80000000
150
151 #define MAX_CAPTION_LENGTH 255
152 #define MAX_TABTEXT_LENGTH 255
153 #define MAX_BUTTONTEXT_LENGTH 64
154
155 #define INTRNL_ANY_WIZARD (PSH_WIZARD | PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)
156
157 /* Wizard metrics specified in DLUs */
158 #define WIZARD_PADDING 7
159 #define WIZARD_HEADER_HEIGHT 36
160                                 
161 /******************************************************************************
162  * Prototypes
163  */
164 static int PROPSHEET_CreateDialog(PropSheetInfo* psInfo);
165 static BOOL PROPSHEET_SizeMismatch(HWND hwndDlg, PropSheetInfo* psInfo);
166 static BOOL PROPSHEET_AdjustSize(HWND hwndDlg, PropSheetInfo* psInfo);
167 static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, PropSheetInfo* psInfo);
168 static BOOL PROPSHEET_CollectSheetInfoA(LPCPROPSHEETHEADERA lppsh,
169                                        PropSheetInfo * psInfo);
170 static BOOL PROPSHEET_CollectSheetInfoW(LPCPROPSHEETHEADERW lppsh,
171                                        PropSheetInfo * psInfo);
172 static BOOL PROPSHEET_CollectPageInfo(LPCPROPSHEETPAGEW lppsp,
173                                       PropSheetInfo * psInfo,
174                                       int index);
175 static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
176                                        PropSheetInfo * psInfo);
177 static BOOL PROPSHEET_CreatePage(HWND hwndParent, int index,
178                                 const PropSheetInfo * psInfo,
179                                 LPCPROPSHEETPAGEW ppshpage);
180 static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo);
181 static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg);
182 static BOOL PROPSHEET_Back(HWND hwndDlg);
183 static BOOL PROPSHEET_Next(HWND hwndDlg);
184 static BOOL PROPSHEET_Finish(HWND hwndDlg);
185 static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam);
186 static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam);
187 static void PROPSHEET_Help(HWND hwndDlg);
188 static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage);
189 static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage);
190 static void PROPSHEET_PressButton(HWND hwndDlg, int buttonID);
191 static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText);
192 static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText);
193 static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText);
194 static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText);
195 static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg);
196 static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
197                                 int index,
198                                 int skipdir,
199                                 HPROPSHEETPAGE hpage);
200 static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id);
201 static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
202                                        WPARAM wParam, LPARAM lParam);
203 static BOOL PROPSHEET_AddPage(HWND hwndDlg,
204                               HPROPSHEETPAGE hpage);
205
206 static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
207                                  int index,
208                                  HPROPSHEETPAGE hpage);
209 static void PROPSHEET_CleanUp(HWND hwndDlg);
210 static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, PropSheetInfo* psInfo);
211 static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags);
212 static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo* psInfo);
213 static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg);
214 static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID);
215
216 INT_PTR CALLBACK
217 PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
218
219 WINE_DEFAULT_DEBUG_CHANNEL(propsheet);
220
221 #define add_flag(a) if (dwFlags & a) {strcat(string, #a );strcat(string," ");}
222 /******************************************************************************
223  *            PROPSHEET_UnImplementedFlags
224  *
225  * Document use of flags we don't implement yet.
226  */
227 static VOID PROPSHEET_UnImplementedFlags(DWORD dwFlags)
228 {
229     CHAR string[256];
230
231     string[0] = '\0';
232
233   /*
234    * unhandled header flags:
235    *  PSH_WIZARDHASFINISH    0x00000010
236    *  PSH_RTLREADING         0x00000800
237    *  PSH_STRETCHWATERMARK   0x00040000
238    *  PSH_USEPAGELANG        0x00200000
239    */
240
241     add_flag(PSH_WIZARDHASFINISH);
242     add_flag(PSH_RTLREADING);
243     add_flag(PSH_STRETCHWATERMARK);
244     add_flag(PSH_USEPAGELANG);
245     if (string[0] != '\0')
246         FIXME("%s\n", string);
247 }
248 #undef add_flag
249
250 /******************************************************************************
251  *            PROPSHEET_GetPageRect
252  *
253  * Retrieve rect from tab control and map into the dialog for SetWindowPos
254  */
255 static void PROPSHEET_GetPageRect(const PropSheetInfo * psInfo, HWND hwndDlg,
256                                   RECT *rc, LPCPROPSHEETPAGEW ppshpage)
257 {
258     if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) {     
259         HWND hwndChild;
260         RECT r;
261
262         if (((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
263              (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
264              !(ppshpage->dwFlags & PSP_HIDEHEADER)) ||
265             (psInfo->ppshheader.dwFlags & PSH_WIZARD))
266         {
267             rc->left = rc->top = WIZARD_PADDING;
268         }
269         else
270         {
271             rc->left = rc->top = 0;
272         }
273         rc->right = psInfo->width - rc->left;
274         rc->bottom = psInfo->height - rc->top;
275         MapDialogRect(hwndDlg, rc);
276
277         if ((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
278             (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
279             !(ppshpage->dwFlags & PSP_HIDEHEADER))
280         {
281             hwndChild = GetDlgItem(hwndDlg, IDC_SUNKEN_LINEHEADER);
282             GetClientRect(hwndChild, &r);
283             MapWindowPoints(hwndChild, hwndDlg, (LPPOINT) &r, 2);
284             rc->top += r.bottom + 1;
285         }
286     } else {
287         HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
288         GetClientRect(hwndTabCtrl, rc);
289         SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)rc);
290         MapWindowPoints(hwndTabCtrl, hwndDlg, (LPPOINT)rc, 2);
291     }
292 }
293
294 /******************************************************************************
295  *            PROPSHEET_FindPageByResId
296  *
297  * Find page index corresponding to page resource id.
298  */
299 INT PROPSHEET_FindPageByResId(PropSheetInfo * psInfo, LRESULT resId)
300 {
301    INT i;
302
303    for (i = 0; i < psInfo->nPages; i++)
304    {
305       LPCPROPSHEETPAGEA lppsp = (LPCPROPSHEETPAGEA)psInfo->proppage[i].hpage;
306
307       /* Fixme: if resource ID is a string shall we use strcmp ??? */
308       if (lppsp->u.pszTemplate == (LPVOID)resId)
309          break;
310    }
311
312    return i;
313 }
314
315 /******************************************************************************
316  *            PROPSHEET_AtoW
317  *
318  * Convert ASCII to Unicode since all data is saved as Unicode.
319  */
320 static void PROPSHEET_AtoW(LPCWSTR *tostr, LPCSTR frstr)
321 {
322     INT len;
323
324     TRACE("<%s>\n", frstr);
325     len = MultiByteToWideChar(CP_ACP, 0, frstr, -1, 0, 0);
326     *tostr = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
327     MultiByteToWideChar(CP_ACP, 0, frstr, -1, (LPWSTR)*tostr, len);
328 }
329
330 /******************************************************************************
331  *            PROPSHEET_CollectSheetInfoA
332  *
333  * Collect relevant data.
334  */
335 static BOOL PROPSHEET_CollectSheetInfoA(LPCPROPSHEETHEADERA lppsh,
336                                        PropSheetInfo * psInfo)
337 {
338   DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERA));
339   DWORD dwFlags = lppsh->dwFlags;
340
341   psInfo->hasHelp = dwFlags & PSH_HASHELP;
342   psInfo->hasApply = !(dwFlags & PSH_NOAPPLYNOW);
343   psInfo->useCallback = (dwFlags & PSH_USECALLBACK )&& (lppsh->pfnCallback);
344   psInfo->isModeless = dwFlags & PSH_MODELESS;
345
346   memcpy(&psInfo->ppshheader,lppsh,dwSize);
347   TRACE("\n** PROPSHEETHEADER **\ndwSize\t\t%ld\ndwFlags\t\t%08lx\nhwndParent\t%p\nhInstance\t%p\npszCaption\t'%s'\nnPages\t\t%d\npfnCallback\t%p\n",
348         lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance,
349         debugstr_a(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);
350
351   PROPSHEET_UnImplementedFlags(lppsh->dwFlags);
352
353   if (lppsh->dwFlags & INTRNL_ANY_WIZARD)
354      psInfo->ppshheader.pszCaption = NULL;
355   else
356   {
357      if (HIWORD(lppsh->pszCaption))
358      {
359         int len = MultiByteToWideChar(CP_ACP, 0, lppsh->pszCaption, -1, NULL, 0);
360         psInfo->ppshheader.pszCaption = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
361         MultiByteToWideChar(CP_ACP, 0, lppsh->pszCaption, -1, (LPWSTR) psInfo->ppshheader.pszCaption, len);
362      }
363   }
364   psInfo->nPages = lppsh->nPages;
365
366   if (dwFlags & PSH_USEPSTARTPAGE)
367   {
368     TRACE("PSH_USEPSTARTPAGE is on\n");
369     psInfo->active_page = 0;
370   }
371   else
372     psInfo->active_page = lppsh->u2.nStartPage;
373
374   if (psInfo->active_page < 0 || psInfo->active_page >= psInfo->nPages)
375      psInfo->active_page = 0;
376
377   psInfo->restartWindows = FALSE;
378   psInfo->rebootSystem = FALSE;
379   psInfo->hImageList = 0;
380   psInfo->activeValid = FALSE;
381
382   return TRUE;
383 }
384
385 /******************************************************************************
386  *            PROPSHEET_CollectSheetInfoW
387  *
388  * Collect relevant data.
389  */
390 static BOOL PROPSHEET_CollectSheetInfoW(LPCPROPSHEETHEADERW lppsh,
391                                        PropSheetInfo * psInfo)
392 {
393   DWORD dwSize = min(lppsh->dwSize,sizeof(PROPSHEETHEADERW));
394   DWORD dwFlags = lppsh->dwFlags;
395
396   psInfo->hasHelp = dwFlags & PSH_HASHELP;
397   psInfo->hasApply = !(dwFlags & PSH_NOAPPLYNOW);
398   psInfo->useCallback = (dwFlags & PSH_USECALLBACK) && (lppsh->pfnCallback);
399   psInfo->isModeless = dwFlags & PSH_MODELESS;
400
401   memcpy(&psInfo->ppshheader,lppsh,dwSize);
402   TRACE("\n** PROPSHEETHEADER **\ndwSize\t\t%ld\ndwFlags\t\t%08lx\nhwndParent\t%p\nhInstance\t%p\npszCaption\t'%s'\nnPages\t\t%d\npfnCallback\t%p\n",
403       lppsh->dwSize, lppsh->dwFlags, lppsh->hwndParent, lppsh->hInstance, debugstr_w(lppsh->pszCaption), lppsh->nPages, lppsh->pfnCallback);
404
405   PROPSHEET_UnImplementedFlags(lppsh->dwFlags);
406
407   if (lppsh->dwFlags & INTRNL_ANY_WIZARD)
408      psInfo->ppshheader.pszCaption = NULL;
409   else
410   {
411      if (HIWORD(lppsh->pszCaption))
412      {
413         int len = strlenW(lppsh->pszCaption);
414         psInfo->ppshheader.pszCaption = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof(WCHAR) );
415         strcpyW( (WCHAR *)psInfo->ppshheader.pszCaption, lppsh->pszCaption );
416      }
417   }
418   psInfo->nPages = lppsh->nPages;
419
420   if (dwFlags & PSH_USEPSTARTPAGE)
421   {
422     TRACE("PSH_USEPSTARTPAGE is on\n");
423     psInfo->active_page = 0;
424   }
425   else
426     psInfo->active_page = lppsh->u2.nStartPage;
427
428   if (psInfo->active_page < 0 || psInfo->active_page >= psInfo->nPages)
429      psInfo->active_page = 0;
430
431   psInfo->restartWindows = FALSE;
432   psInfo->rebootSystem = FALSE;
433   psInfo->hImageList = 0;
434   psInfo->activeValid = FALSE;
435
436   return TRUE;
437 }
438
439 /******************************************************************************
440  *            PROPSHEET_CollectPageInfo
441  *
442  * Collect property sheet data.
443  * With code taken from DIALOG_ParseTemplate32.
444  */
445 BOOL PROPSHEET_CollectPageInfo(LPCPROPSHEETPAGEW lppsp,
446                                PropSheetInfo * psInfo,
447                                int index)
448 {
449   DLGTEMPLATE* pTemplate;
450   const WORD*  p;
451   DWORD dwFlags;
452   int width, height;
453
454   TRACE("\n");
455   psInfo->proppage[index].hpage = (HPROPSHEETPAGE)lppsp;
456   psInfo->proppage[index].hwndPage = 0;
457   psInfo->proppage[index].isDirty = FALSE;
458
459   /*
460    * Process property page flags.
461    */
462   dwFlags = lppsp->dwFlags;
463   psInfo->proppage[index].useCallback = (dwFlags & PSP_USECALLBACK) && (lppsp->pfnCallback);
464   psInfo->proppage[index].hasHelp = dwFlags & PSP_HASHELP;
465   psInfo->proppage[index].hasIcon = dwFlags & (PSP_USEHICON | PSP_USEICONID);
466
467   /* as soon as we have a page with the help flag, set the sheet flag on */
468   if (psInfo->proppage[index].hasHelp)
469     psInfo->hasHelp = TRUE;
470
471   /*
472    * Process page template.
473    */
474   if (dwFlags & PSP_DLGINDIRECT)
475     pTemplate = (DLGTEMPLATE*)lppsp->u.pResource;
476   else if(dwFlags & PSP_INTERNAL_UNICODE )
477   {
478     HRSRC hResource = FindResourceW(lppsp->hInstance,
479                                     lppsp->u.pszTemplate,
480                                     (LPWSTR)RT_DIALOG);
481     HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
482                                      hResource);
483     pTemplate = (LPDLGTEMPLATEW)LockResource(hTemplate);
484   }
485   else
486   {
487     HRSRC hResource = FindResourceA(lppsp->hInstance,
488                                     (LPSTR)lppsp->u.pszTemplate,
489                                     (LPSTR)RT_DIALOG);
490     HGLOBAL hTemplate = LoadResource(lppsp->hInstance,
491                                      hResource);
492     pTemplate = (LPDLGTEMPLATEA)LockResource(hTemplate);
493   }
494
495   /*
496    * Extract the size of the page and the caption.
497    */
498   if (!pTemplate)
499       return FALSE;
500
501   p = (const WORD *)pTemplate;
502
503   if (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
504   {
505     /* DLGTEMPLATEEX (not defined in any std. header file) */
506
507     p++;       /* dlgVer    */
508     p++;       /* signature */
509     p += 2;    /* help ID   */
510     p += 2;    /* ext style */
511     p += 2;    /* style     */
512   }
513   else
514   {
515     /* DLGTEMPLATE */
516
517     p += 2;    /* style     */
518     p += 2;    /* ext style */
519   }
520
521   p++;    /* nb items */
522   p++;    /*   x      */
523   p++;    /*   y      */
524   width  = (WORD)*p; p++;
525   height = (WORD)*p; p++;
526
527   /* Special calculation for interior wizard pages so the largest page is
528    * calculated correctly. We need to add all the padding and space occupied
529    * by the header so the width and height sums up to the whole wizard client
530    * area. */
531   if ((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
532       (psInfo->ppshheader.dwFlags & PSH_HEADER) &&
533       !(dwFlags & PSP_HIDEHEADER))
534   {
535       height += 2 * WIZARD_PADDING + WIZARD_HEADER_HEIGHT;
536       width += 2 * WIZARD_PADDING;
537   }
538   if (psInfo->ppshheader.dwFlags & PSH_WIZARD)
539   {
540       height += 2 * WIZARD_PADDING;
541       width += 2 * WIZARD_PADDING;
542   }
543
544   /* remember the largest width and height */
545   if (width > psInfo->width)
546     psInfo->width = width;
547
548   if (height > psInfo->height)
549     psInfo->height = height;
550
551   /* menu */
552   switch ((WORD)*p)
553   {
554     case 0x0000:
555       p++;
556       break;
557     case 0xffff:
558       p += 2;
559       break;
560     default:
561       p += lstrlenW( (LPCWSTR)p ) + 1;
562       break;
563   }
564
565   /* class */
566   switch ((WORD)*p)
567   {
568     case 0x0000:
569       p++;
570       break;
571     case 0xffff:
572       p += 2;
573       break;
574     default:
575       p += lstrlenW( (LPCWSTR)p ) + 1;
576       break;
577   }
578
579   /* Extract the caption */
580   psInfo->proppage[index].pszText = (LPCWSTR)p;
581   TRACE("Tab %d %s\n",index,debugstr_w((LPCWSTR)p));
582   p += lstrlenW((LPCWSTR)p) + 1;
583
584   if (dwFlags & PSP_USETITLE)
585   {
586     WCHAR szTitle[256];
587     const WCHAR *pTitle;
588     static const WCHAR pszNull[] = { '(','n','u','l','l',')',0 };
589     int len;
590
591     if ( !HIWORD( lppsp->pszTitle ) )
592     {
593       if (!LoadStringW( lppsp->hInstance, (UINT)lppsp->pszTitle,szTitle,sizeof(szTitle) ))
594       {
595         pTitle = pszNull;
596         FIXME("Could not load resource #%04x?\n",LOWORD(lppsp->pszTitle));
597       }
598       else
599         pTitle = szTitle;
600     }
601     else
602       pTitle = lppsp->pszTitle;
603
604     len = strlenW(pTitle);
605     psInfo->proppage[index].pszText = Alloc( (len+1)*sizeof (WCHAR) );
606     strcpyW( (LPWSTR)psInfo->proppage[index].pszText,pTitle);
607   }
608
609   /*
610    * Build the image list for icons
611    */
612   if ((dwFlags & PSP_USEHICON) || (dwFlags & PSP_USEICONID))
613   {
614     HICON hIcon;
615     int icon_cx = GetSystemMetrics(SM_CXSMICON);
616     int icon_cy = GetSystemMetrics(SM_CYSMICON);
617
618     if (dwFlags & PSP_USEICONID)
619       hIcon = LoadImageW(lppsp->hInstance, lppsp->u2.pszIcon, IMAGE_ICON,
620                          icon_cx, icon_cy, LR_DEFAULTCOLOR);
621     else
622       hIcon = lppsp->u2.hIcon;
623
624     if ( hIcon )
625     {
626       if (psInfo->hImageList == 0 )
627         psInfo->hImageList = ImageList_Create(icon_cx, icon_cy, ILC_COLOR, 1, 1);
628
629       ImageList_AddIcon(psInfo->hImageList, hIcon);
630     }
631
632   }
633
634   return TRUE;
635 }
636
637 /******************************************************************************
638  *            PROPSHEET_CreateDialog
639  *
640  * Creates the actual property sheet.
641  */
642 int PROPSHEET_CreateDialog(PropSheetInfo* psInfo)
643 {
644   LRESULT ret;
645   LPCVOID template;
646   LPVOID temp = 0;
647   HRSRC hRes;
648   DWORD resSize;
649   WORD resID = IDD_PROPSHEET;
650
651   TRACE("\n");
652   if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
653     resID = IDD_WIZARD;
654
655   if( psInfo->unicode )
656   {
657     if(!(hRes = FindResourceW(COMCTL32_hModule,
658                             MAKEINTRESOURCEW(resID),
659                             (LPWSTR)RT_DIALOG)))
660       return -1;
661   }
662   else
663   {
664     if(!(hRes = FindResourceA(COMCTL32_hModule,
665                             MAKEINTRESOURCEA(resID),
666                             (LPSTR)RT_DIALOG)))
667       return -1;
668   }
669
670   if(!(template = (LPVOID)LoadResource(COMCTL32_hModule, hRes)))
671     return -1;
672
673   /*
674    * Make a copy of the dialog template.
675    */
676   resSize = SizeofResource(COMCTL32_hModule, hRes);
677
678   temp = Alloc(resSize);
679
680   if (!temp)
681     return -1;
682
683   memcpy(temp, template, resSize);
684
685   if (psInfo->ppshheader.dwFlags & PSH_NOCONTEXTHELP)
686   {
687     if (((MyDLGTEMPLATEEX*)temp)->signature == 0xFFFF)
688       ((MyDLGTEMPLATEEX*)temp)->style &= ~DS_CONTEXTHELP;
689     else
690       ((DLGTEMPLATE*)temp)->style &= ~DS_CONTEXTHELP;
691   }
692   if ((psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) &&
693       (psInfo->ppshheader.dwFlags & PSH_WIZARDCONTEXTHELP))
694   {
695     if (((MyDLGTEMPLATEEX*)temp)->signature == 0xFFFF)
696       ((MyDLGTEMPLATEEX*)temp)->style |= DS_CONTEXTHELP;
697     else
698       ((DLGTEMPLATE*)temp)->style |= DS_CONTEXTHELP;
699   }
700
701   if (psInfo->useCallback)
702     (*(psInfo->ppshheader.pfnCallback))(0, PSCB_PRECREATE, (LPARAM)temp);
703
704   /* NOTE: MSDN states "Returns a positive value if successful, or -1
705    * otherwise for modal property sheets.", but this is wrong. The
706    * actual return value is either TRUE (success), FALSE (cancel) or
707    * -1 (error). */
708   if( psInfo->unicode )
709   {
710     if (!(psInfo->ppshheader.dwFlags & PSH_MODELESS))
711       ret = DialogBoxIndirectParamW(psInfo->ppshheader.hInstance,
712                                     (LPDLGTEMPLATEW) temp,
713                                     psInfo->ppshheader.hwndParent,
714                                     PROPSHEET_DialogProc,
715                                     (LPARAM)psInfo);
716     else
717     {
718       ret = (int)CreateDialogIndirectParamW(psInfo->ppshheader.hInstance,
719                                             (LPDLGTEMPLATEW) temp,
720                                             psInfo->ppshheader.hwndParent,
721                                             PROPSHEET_DialogProc,
722                                             (LPARAM)psInfo);
723       if ( !ret ) ret = -1;
724     }
725   }
726   else
727   {
728     if (!(psInfo->ppshheader.dwFlags & PSH_MODELESS))
729       ret = DialogBoxIndirectParamA(psInfo->ppshheader.hInstance,
730                                     (LPDLGTEMPLATEA) temp,
731                                     psInfo->ppshheader.hwndParent,
732                                     PROPSHEET_DialogProc,
733                                     (LPARAM)psInfo);
734     else
735     {
736       ret = (int)CreateDialogIndirectParamA(psInfo->ppshheader.hInstance,
737                                             (LPDLGTEMPLATEA) temp,
738                                             psInfo->ppshheader.hwndParent,
739                                             PROPSHEET_DialogProc,
740                                             (LPARAM)psInfo);
741       if ( !ret ) ret = -1;
742     }
743   }
744
745   Free(temp);
746
747   return ret;
748 }
749
750 /******************************************************************************
751  *            PROPSHEET_SizeMismatch
752  *
753  *     Verify that the tab control and the "largest" property sheet page dlg. template
754  *     match in size.
755  */
756 static BOOL PROPSHEET_SizeMismatch(HWND hwndDlg, PropSheetInfo* psInfo)
757 {
758   HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
759   RECT rcOrigTab, rcPage;
760
761   /*
762    * Original tab size.
763    */
764   GetClientRect(hwndTabCtrl, &rcOrigTab);
765   TRACE("orig tab %ld %ld %ld %ld\n", rcOrigTab.left, rcOrigTab.top,
766         rcOrigTab.right, rcOrigTab.bottom);
767
768   /*
769    * Biggest page size.
770    */
771   rcPage.left   = 0;
772   rcPage.top    = 0;
773   rcPage.right  = psInfo->width;
774   rcPage.bottom = psInfo->height;
775
776   MapDialogRect(hwndDlg, &rcPage);
777   TRACE("biggest page %ld %ld %ld %ld\n", rcPage.left, rcPage.top,
778         rcPage.right, rcPage.bottom);
779
780   if ( (rcPage.right - rcPage.left) != (rcOrigTab.right - rcOrigTab.left) )
781     return TRUE;
782   if ( (rcPage.bottom - rcPage.top) != (rcOrigTab.bottom - rcOrigTab.top) )
783     return TRUE;
784
785   return FALSE;
786 }
787
788 /******************************************************************************
789  *            PROPSHEET_AdjustSize
790  *
791  * Resizes the property sheet and the tab control to fit the largest page.
792  */
793 static BOOL PROPSHEET_AdjustSize(HWND hwndDlg, PropSheetInfo* psInfo)
794 {
795   HWND hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
796   HWND hwndButton = GetDlgItem(hwndDlg, IDOK);
797   RECT rc,tabRect;
798   int tabOffsetX, tabOffsetY, buttonHeight;
799   PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndDlg);
800   RECT units;
801
802   /* Get the height of buttons */
803   GetClientRect(hwndButton, &rc);
804   buttonHeight = rc.bottom;
805
806   /*
807    * Biggest page size.
808    */
809   rc.left   = 0;
810   rc.top    = 0;
811   rc.right  = psInfo->width;
812   rc.bottom = psInfo->height;
813
814   MapDialogRect(hwndDlg, &rc);
815
816   /* retrieve the dialog units */
817   units.left = units.right = 4;
818   units.top = units.bottom = 8;
819   MapDialogRect(hwndDlg, &units);
820
821   /*
822    * Resize the tab control.
823    */
824   GetClientRect(hwndTabCtrl,&tabRect);
825
826   SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, FALSE, (LPARAM)&tabRect);
827
828   if ((rc.bottom - rc.top) < (tabRect.bottom - tabRect.top))
829   {
830       rc.bottom = rc.top + tabRect.bottom - tabRect.top;
831       psInfo->height = MulDiv((rc.bottom - rc.top),8,units.top);
832   }
833
834   if ((rc.right - rc.left) < (tabRect.right - tabRect.left))
835   {
836       rc.right = rc.left + tabRect.right - tabRect.left;
837       psInfo->width  = MulDiv((rc.right - rc.left),4,units.left);
838   }
839
840   SendMessageW(hwndTabCtrl, TCM_ADJUSTRECT, TRUE, (LPARAM)&rc);
841
842   tabOffsetX = -(rc.left);
843   tabOffsetY = -(rc.top);
844
845   rc.right -= rc.left;
846   rc.bottom -= rc.top;
847   TRACE("setting tab %08lx, rc (0,0)-(%ld,%ld)\n",
848         (DWORD)hwndTabCtrl, rc.right, rc.bottom);
849   SetWindowPos(hwndTabCtrl, 0, 0, 0, rc.right, rc.bottom,
850                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
851
852   GetClientRect(hwndTabCtrl, &rc);
853
854   TRACE("tab client rc %ld %ld %ld %ld\n",
855         rc.left, rc.top, rc.right, rc.bottom);
856
857   rc.right += ((padding.x * 2) + tabOffsetX);
858   rc.bottom += (buttonHeight + (3 * padding.y) + tabOffsetY);
859
860   /*
861    * Resize the property sheet.
862    */
863   TRACE("setting dialog %08lx, rc (0,0)-(%ld,%ld)\n",
864         (DWORD)hwndDlg, rc.right, rc.bottom);
865   SetWindowPos(hwndDlg, 0, 0, 0, rc.right, rc.bottom,
866                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
867   return TRUE;
868 }
869
870 /******************************************************************************
871  *            PROPSHEET_AdjustSizeWizard
872  *
873  * Resizes the property sheet to fit the largest page.
874  */
875 static BOOL PROPSHEET_AdjustSizeWizard(HWND hwndDlg, PropSheetInfo* psInfo)
876 {
877   HWND hwndLine = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
878   RECT rc, lineRect, dialogRect;
879
880   /* Biggest page size */
881   rc.left   = 0;
882   rc.top    = 0;
883   rc.right  = psInfo->width;
884   rc.bottom = psInfo->height;
885   MapDialogRect(hwndDlg, &rc);
886
887   TRACE("Biggest page %ld %ld %ld %ld\n", rc.left, rc.top, rc.right, rc.bottom);
888
889   /* Add space for the buttons row */
890   GetWindowRect(hwndLine, &lineRect);
891   MapWindowPoints(NULL, hwndDlg, (LPPOINT)&lineRect, 2);
892   GetClientRect(hwndDlg, &dialogRect);
893   rc.bottom += dialogRect.bottom - lineRect.top - 1;
894
895   /* Convert the client coordinates to window coordinates */
896   AdjustWindowRect(&rc, GetWindowLongW(hwndDlg, GWL_STYLE), FALSE);
897
898   /* Resize the property sheet */
899   TRACE("setting dialog %08lx, rc (0,0)-(%ld,%ld)\n",
900         (DWORD)hwndDlg, rc.right, rc.bottom);
901   SetWindowPos(hwndDlg, 0, 0, 0, rc.right - rc.left, rc.bottom - rc.top,
902                SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
903
904   return TRUE;
905 }
906
907 /******************************************************************************
908  *            PROPSHEET_AdjustButtons
909  *
910  * Adjusts the buttons' positions.
911  */
912 static BOOL PROPSHEET_AdjustButtons(HWND hwndParent, PropSheetInfo* psInfo)
913 {
914   HWND hwndButton = GetDlgItem(hwndParent, IDOK);
915   RECT rcSheet;
916   int x, y;
917   int num_buttons = 2;
918   int buttonWidth, buttonHeight;
919   PADDING_INFO padding = PROPSHEET_GetPaddingInfo(hwndParent);
920
921   if (psInfo->hasApply)
922     num_buttons++;
923
924   if (psInfo->hasHelp)
925     num_buttons++;
926
927   /*
928    * Obtain the size of the buttons.
929    */
930   GetClientRect(hwndButton, &rcSheet);
931   buttonWidth = rcSheet.right;
932   buttonHeight = rcSheet.bottom;
933
934   /*
935    * Get the size of the property sheet.
936    */
937   GetClientRect(hwndParent, &rcSheet);
938
939   /*
940    * All buttons will be at this y coordinate.
941    */
942   y = rcSheet.bottom - (padding.y + buttonHeight);
943
944   /*
945    * Position OK button and make it default.
946    */
947   hwndButton = GetDlgItem(hwndParent, IDOK);
948
949   x = rcSheet.right - ((padding.x + buttonWidth) * num_buttons);
950
951   SetWindowPos(hwndButton, 0, x, y, 0, 0,
952                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
953
954   SendMessageA(hwndParent, DM_SETDEFID, IDOK, 0);
955
956
957   /*
958    * Position Cancel button.
959    */
960   hwndButton = GetDlgItem(hwndParent, IDCANCEL);
961
962   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1));
963
964   SetWindowPos(hwndButton, 0, x, y, 0, 0,
965                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
966
967   /*
968    * Position Apply button.
969    */
970   hwndButton = GetDlgItem(hwndParent, IDC_APPLY_BUTTON);
971
972   if (psInfo->hasApply)
973   {
974     if (psInfo->hasHelp)
975       x = rcSheet.right - ((padding.x + buttonWidth) * 2);
976     else
977       x = rcSheet.right - (padding.x + buttonWidth);
978
979     SetWindowPos(hwndButton, 0, x, y, 0, 0,
980                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
981
982     EnableWindow(hwndButton, FALSE);
983   }
984   else
985     ShowWindow(hwndButton, SW_HIDE);
986
987   /*
988    * Position Help button.
989    */
990   hwndButton = GetDlgItem(hwndParent, IDHELP);
991
992   if (psInfo->hasHelp)
993   {
994     x = rcSheet.right - (padding.x + buttonWidth);
995
996     SetWindowPos(hwndButton, 0, x, y, 0, 0,
997                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
998   }
999   else
1000     ShowWindow(hwndButton, SW_HIDE);
1001
1002   return TRUE;
1003 }
1004
1005 /******************************************************************************
1006  *            PROPSHEET_AdjustButtonsWizard
1007  *
1008  * Adjusts the buttons' positions.
1009  */
1010 static BOOL PROPSHEET_AdjustButtonsWizard(HWND hwndParent,
1011                                           PropSheetInfo* psInfo)
1012 {
1013   HWND hwndButton = GetDlgItem(hwndParent, IDCANCEL);
1014   HWND hwndLine = GetDlgItem(hwndParent, IDC_SUNKEN_LINE);
1015   HWND hwndLineHeader = GetDlgItem(hwndParent, IDC_SUNKEN_LINEHEADER);
1016   RECT rcSheet;
1017   int x, y;
1018   int num_buttons = 3;
1019   int buttonWidth, buttonHeight, lineHeight, lineWidth;
1020   PADDING_INFO padding = PROPSHEET_GetPaddingInfoWizard(hwndParent, psInfo);
1021
1022   if (psInfo->hasHelp)
1023     num_buttons++;
1024
1025   /*
1026    * Obtain the size of the buttons.
1027    */
1028   GetClientRect(hwndButton, &rcSheet);
1029   buttonWidth = rcSheet.right;
1030   buttonHeight = rcSheet.bottom;
1031
1032   GetClientRect(hwndLine, &rcSheet);
1033   lineHeight = rcSheet.bottom;
1034
1035   /*
1036    * Get the size of the property sheet.
1037    */
1038   GetClientRect(hwndParent, &rcSheet);
1039
1040   /*
1041    * All buttons will be at this y coordinate.
1042    */
1043   y = rcSheet.bottom - (padding.y + buttonHeight);
1044
1045   /*
1046    * Position the Next and the Finish buttons.
1047    */
1048   hwndButton = GetDlgItem(hwndParent, IDC_NEXT_BUTTON);
1049
1050   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 1));
1051
1052   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1053                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1054
1055   hwndButton = GetDlgItem(hwndParent, IDC_FINISH_BUTTON);
1056
1057   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1058                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1059
1060   ShowWindow(hwndButton, SW_HIDE);
1061
1062   /*
1063    * Position the Back button.
1064    */
1065   hwndButton = GetDlgItem(hwndParent, IDC_BACK_BUTTON);
1066
1067   x -= buttonWidth;
1068
1069   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1070                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1071
1072   /*
1073    * Position the Cancel button.
1074    */
1075   hwndButton = GetDlgItem(hwndParent, IDCANCEL);
1076
1077   x = rcSheet.right - ((padding.x + buttonWidth) * (num_buttons - 2));
1078
1079   SetWindowPos(hwndButton, 0, x, y, 0, 0,
1080                SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1081
1082   /*
1083    * Position Help button.
1084    */
1085   hwndButton = GetDlgItem(hwndParent, IDHELP);
1086
1087   if (psInfo->hasHelp)
1088   {
1089     x = rcSheet.right - (padding.x + buttonWidth);
1090
1091     SetWindowPos(hwndButton, 0, x, y, 0, 0,
1092                  SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
1093   }
1094   else
1095     ShowWindow(hwndButton, SW_HIDE);
1096
1097   if (psInfo->ppshheader.dwFlags &
1098       (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW | PSH_WIZARD_LITE)) 
1099       padding.x = 0;
1100
1101   /*
1102    * Position and resize the sunken line.
1103    */
1104   x = padding.x;
1105   y = rcSheet.bottom - ((padding.y * 2) + buttonHeight + lineHeight);
1106
1107   lineWidth = rcSheet.right - (padding.x * 2);
1108   SetWindowPos(hwndLine, 0, x, y, lineWidth, 2,
1109                SWP_NOZORDER | SWP_NOACTIVATE);
1110
1111   /*
1112    * Position and resize the header sunken line.
1113    */
1114   
1115   SetWindowPos(hwndLineHeader, 0, 0, 0, rcSheet.right, 2,
1116                SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
1117   if (!(psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)))
1118       ShowWindow(hwndLineHeader, SW_HIDE);
1119
1120   return TRUE;
1121 }
1122
1123 /******************************************************************************
1124  *            PROPSHEET_GetPaddingInfo
1125  *
1126  * Returns the layout information.
1127  */
1128 static PADDING_INFO PROPSHEET_GetPaddingInfo(HWND hwndDlg)
1129 {
1130   HWND hwndTab = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1131   RECT rcTab;
1132   POINT tl;
1133   PADDING_INFO padding;
1134
1135   GetWindowRect(hwndTab, &rcTab);
1136
1137   tl.x = rcTab.left;
1138   tl.y = rcTab.top;
1139
1140   ScreenToClient(hwndDlg, &tl);
1141
1142   padding.x = tl.x;
1143   padding.y = tl.y;
1144
1145   return padding;
1146 }
1147
1148 /******************************************************************************
1149  *            PROPSHEET_GetPaddingInfoWizard
1150  *
1151  * Returns the layout information.
1152  * Vertical spacing is the distance between the line and the buttons.
1153  * Do NOT use the Help button to gather padding information when it isn't mapped
1154  * (PSH_HASHELP), as app writers aren't forced to supply correct coordinates
1155  * for it in this case !
1156  * FIXME: I'm not sure about any other coordinate problems with these evil
1157  * buttons. Fix it in case additional problems appear or maybe calculate
1158  * a padding in a completely different way, as this is somewhat messy.
1159  */
1160 static PADDING_INFO PROPSHEET_GetPaddingInfoWizard(HWND hwndDlg, const PropSheetInfo*
1161  psInfo)
1162 {
1163   PADDING_INFO padding;
1164   RECT rc;
1165   HWND hwndControl;
1166   INT idButton;
1167   POINT ptButton, ptLine;
1168
1169   TRACE("\n");
1170   if (psInfo->hasHelp)
1171   {
1172         idButton = IDHELP;
1173   }
1174   else
1175   {
1176     if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
1177     {
1178         idButton = IDC_NEXT_BUTTON;
1179     }
1180     else
1181     {
1182         /* hopefully this is ok */
1183         idButton = IDCANCEL;
1184     }
1185   }
1186
1187   hwndControl = GetDlgItem(hwndDlg, idButton);
1188   GetWindowRect(hwndControl, &rc);
1189
1190   ptButton.x = rc.left;
1191   ptButton.y = rc.top;
1192
1193   ScreenToClient(hwndDlg, &ptButton);
1194
1195   /* Line */
1196   hwndControl = GetDlgItem(hwndDlg, IDC_SUNKEN_LINE);
1197   GetWindowRect(hwndControl, &rc);
1198
1199   ptLine.x = rc.left;
1200   ptLine.y = rc.bottom;
1201
1202   ScreenToClient(hwndDlg, &ptLine);
1203
1204   padding.y = ptButton.y - ptLine.y;
1205
1206   if (padding.y < 0)
1207           ERR("padding negative ! Please report this !\n");
1208
1209   /* this is most probably not correct, but the best we have now */
1210   padding.x = padding.y;
1211   return padding;
1212 }
1213
1214 /******************************************************************************
1215  *            PROPSHEET_CreateTabControl
1216  *
1217  * Insert the tabs in the tab control.
1218  */
1219 static BOOL PROPSHEET_CreateTabControl(HWND hwndParent,
1220                                        PropSheetInfo * psInfo)
1221 {
1222   HWND hwndTabCtrl = GetDlgItem(hwndParent, IDC_TABCONTROL);
1223   TCITEMW item;
1224   int i, nTabs;
1225   int iImage = 0;
1226
1227   TRACE("\n");
1228   item.mask = TCIF_TEXT;
1229   item.cchTextMax = MAX_TABTEXT_LENGTH;
1230
1231   nTabs = psInfo->nPages;
1232
1233   /*
1234    * Set the image list for icons.
1235    */
1236   if (psInfo->hImageList)
1237   {
1238     SendMessageW(hwndTabCtrl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
1239   }
1240
1241   SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 0, 0);
1242   for (i = 0; i < nTabs; i++)
1243   {
1244     if ( psInfo->proppage[i].hasIcon )
1245     {
1246       item.mask |= TCIF_IMAGE;
1247       item.iImage = iImage++;
1248     }
1249     else
1250     {
1251       item.mask &= ~TCIF_IMAGE;
1252     }
1253
1254     item.pszText = (LPWSTR) psInfo->proppage[i].pszText;
1255     SendMessageW(hwndTabCtrl, TCM_INSERTITEMW, (WPARAM)i, (LPARAM)&item);
1256   }
1257   SendMessageW(GetDlgItem(hwndTabCtrl, IDC_TABCONTROL), WM_SETREDRAW, 1, 0);
1258
1259   return TRUE;
1260 }
1261
1262 /******************************************************************************
1263  *            PROPSHEET_WizardSubclassProc
1264  *
1265  * Subclassing window procedure for wizard extrior pages to prevent drawing
1266  * background and so drawing above the watermark.
1267  */
1268 LRESULT CALLBACK
1269 PROPSHEET_WizardSubclassProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uID, DWORD_PTR dwRef)
1270 {
1271   switch (uMsg)
1272   {
1273     case WM_ERASEBKGND:
1274       return TRUE;
1275
1276     case WM_CTLCOLORSTATIC:
1277       SetBkColor((HDC)wParam, GetSysColor(COLOR_WINDOW));
1278       return (INT_PTR)GetSysColorBrush(COLOR_WINDOW);
1279   }
1280
1281   return DefSubclassProc(hwnd, uMsg, wParam, lParam);
1282 }
1283
1284 /*
1285  * Get the size of an in-memory Template
1286  *
1287  *( Based on the code of PROPSHEET_CollectPageInfo)
1288  * See also dialog.c/DIALOG_ParseTemplate32().
1289  */
1290
1291 static UINT GetTemplateSize(DLGTEMPLATE* pTemplate)
1292
1293 {
1294   const WORD*  p = (const WORD *)pTemplate;
1295   BOOL  istemplateex = (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF);
1296   WORD nrofitems;
1297
1298   if (istemplateex)
1299   {
1300     /* DLGTEMPLATEEX (not defined in any std. header file) */
1301
1302     TRACE("is DLGTEMPLATEEX\n");
1303     p++;       /* dlgVer    */
1304     p++;       /* signature */
1305     p += 2;    /* help ID   */
1306     p += 2;    /* ext style */
1307     p += 2;    /* style     */
1308   }
1309   else
1310   {
1311     /* DLGTEMPLATE */
1312
1313     TRACE("is DLGTEMPLATE\n");
1314     p += 2;    /* style     */
1315     p += 2;    /* ext style */
1316   }
1317
1318   nrofitems =   (WORD)*p; p++;    /* nb items */
1319   p++;    /*   x      */
1320   p++;    /*   y      */
1321   p++;    /*   width  */
1322   p++;    /*   height */
1323
1324   /* menu */
1325   switch ((WORD)*p)
1326   {
1327     case 0x0000:
1328       p++;
1329       break;
1330     case 0xffff:
1331       p += 2;
1332       break;
1333     default:
1334       TRACE("menu %s\n",debugstr_w((LPCWSTR)p));
1335       p += lstrlenW( (LPCWSTR)p ) + 1;
1336       break;
1337   }
1338
1339   /* class */
1340   switch ((WORD)*p)
1341   {
1342     case 0x0000:
1343       p++;
1344       break;
1345     case 0xffff:
1346       p += 2; /* 0xffff plus predefined window class ordinal value */
1347       break;
1348     default:
1349       TRACE("class %s\n",debugstr_w((LPCWSTR)p));
1350       p += lstrlenW( (LPCWSTR)p ) + 1;
1351       break;
1352   }
1353
1354   /* title */
1355   TRACE("title %s\n",debugstr_w((LPCWSTR)p));
1356   p += lstrlenW((LPCWSTR)p) + 1;
1357
1358   /* font, if DS_SETFONT set */
1359   if ((DS_SETFONT & ((istemplateex)?  ((MyDLGTEMPLATEEX*)pTemplate)->style :
1360                      pTemplate->style)))
1361     {
1362       p+=(istemplateex)?3:1;
1363       TRACE("font %s\n",debugstr_w((LPCWSTR)p));
1364       p += lstrlenW( (LPCWSTR)p ) + 1; /* the font name */
1365     }
1366
1367   /* now process the DLGITEMTEMPLATE(EX) structs (plus custom data)
1368    * that are following the DLGTEMPLATE(EX) data */
1369   TRACE("%d items\n",nrofitems);
1370   while (nrofitems > 0)
1371     {
1372       p = (WORD*)(((DWORD)p + 3) & ~3); /* DWORD align */
1373       
1374       /* skip header */
1375       p += (istemplateex ? sizeof(MyDLGITEMTEMPLATEEX) : sizeof(DLGITEMTEMPLATE))/sizeof(WORD);
1376       
1377       /* check class */
1378       switch ((WORD)*p)
1379         {
1380         case 0x0000:
1381           p++;
1382           break;
1383         case 0xffff:
1384           TRACE("class ordinal 0x%08lx\n",*(DWORD*)p);
1385           p += 2;
1386           break;
1387         default:
1388           TRACE("class %s\n",debugstr_w((LPCWSTR)p));
1389           p += lstrlenW( (LPCWSTR)p ) + 1;
1390           break;
1391         }
1392
1393       /* check title text */
1394       switch ((WORD)*p)
1395         {
1396         case 0x0000:
1397           p++;
1398           break;
1399         case 0xffff:
1400           TRACE("text ordinal 0x%08lx\n",*(DWORD*)p);
1401           p += 2;
1402           break;
1403         default:
1404           TRACE("text %s\n",debugstr_w((LPCWSTR)p));
1405           p += lstrlenW( (LPCWSTR)p ) + 1;
1406           break;
1407         }
1408       p += *p / sizeof(WORD) + 1;    /* Skip extra data */
1409       --nrofitems;
1410     }
1411   
1412   TRACE("%p %p size 0x%08x\n",p, (WORD*)pTemplate,sizeof(WORD)*(p - (WORD*)pTemplate));
1413   return (p - (WORD*)pTemplate)*sizeof(WORD);
1414   
1415 }
1416
1417 /******************************************************************************
1418  *            PROPSHEET_CreatePage
1419  *
1420  * Creates a page.
1421  */
1422 static BOOL PROPSHEET_CreatePage(HWND hwndParent,
1423                                 int index,
1424                                 const PropSheetInfo * psInfo,
1425                                 LPCPROPSHEETPAGEW ppshpage)
1426 {
1427   DLGTEMPLATE* pTemplate;
1428   HWND hwndPage;
1429   DWORD resSize;
1430   LPVOID temp = NULL;
1431
1432   TRACE("index %d\n", index);
1433
1434   if (ppshpage == NULL)
1435   {
1436     return FALSE;
1437   }
1438
1439   if (ppshpage->dwFlags & PSP_DLGINDIRECT)
1440     {
1441       pTemplate = (DLGTEMPLATE*)ppshpage->u.pResource;
1442       resSize = GetTemplateSize(pTemplate);
1443     }
1444   else if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1445   {
1446     HRSRC hResource;
1447     HANDLE hTemplate;
1448
1449     hResource = FindResourceW(ppshpage->hInstance,
1450                                     ppshpage->u.pszTemplate,
1451                                     (LPWSTR)RT_DIALOG);
1452     if(!hResource)
1453         return FALSE;
1454
1455     resSize = SizeofResource(ppshpage->hInstance, hResource);
1456
1457     hTemplate = LoadResource(ppshpage->hInstance, hResource);
1458     if(!hTemplate)
1459         return FALSE;
1460
1461     pTemplate = (LPDLGTEMPLATEW)LockResource(hTemplate);
1462     /*
1463      * Make a copy of the dialog template to make it writable
1464      */
1465   }
1466   else
1467   {
1468     HRSRC hResource;
1469     HANDLE hTemplate;
1470
1471     hResource = FindResourceA(ppshpage->hInstance,
1472                                     (LPSTR)ppshpage->u.pszTemplate,
1473                                     (LPSTR)RT_DIALOG);
1474     if(!hResource)
1475         return FALSE;
1476
1477     resSize = SizeofResource(ppshpage->hInstance, hResource);
1478
1479     hTemplate = LoadResource(ppshpage->hInstance, hResource);
1480     if(!hTemplate)
1481         return FALSE;
1482
1483     pTemplate = (LPDLGTEMPLATEA)LockResource(hTemplate);
1484     /*
1485      * Make a copy of the dialog template to make it writable
1486      */
1487   }
1488   temp = Alloc(resSize);
1489   if (!temp)
1490     return FALSE;
1491   
1492   TRACE("copying pTemplate %p into temp %p (%ld)\n", pTemplate, temp, resSize);
1493   memcpy(temp, pTemplate, resSize);
1494   pTemplate = temp;
1495
1496   if (((MyDLGTEMPLATEEX*)pTemplate)->signature == 0xFFFF)
1497   {
1498     ((MyDLGTEMPLATEEX*)pTemplate)->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
1499     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~DS_MODALFRAME;
1500     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_CAPTION;
1501     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_SYSMENU;
1502     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_POPUP;
1503     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_DISABLED;
1504     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_VISIBLE;
1505     ((MyDLGTEMPLATEEX*)pTemplate)->style &= ~WS_THICKFRAME;
1506
1507     ((MyDLGTEMPLATEEX*)pTemplate)->exStyle |= WS_EX_CONTROLPARENT;
1508   }
1509   else
1510   {
1511     pTemplate->style |= WS_CHILD | WS_TABSTOP | DS_CONTROL;
1512     pTemplate->style &= ~DS_MODALFRAME;
1513     pTemplate->style &= ~WS_CAPTION;
1514     pTemplate->style &= ~WS_SYSMENU;
1515     pTemplate->style &= ~WS_POPUP;
1516     pTemplate->style &= ~WS_DISABLED;
1517     pTemplate->style &= ~WS_VISIBLE;
1518     pTemplate->style &= ~WS_THICKFRAME;
1519
1520     pTemplate->dwExtendedStyle |= WS_EX_CONTROLPARENT;
1521   }
1522
1523   if (psInfo->proppage[index].useCallback)
1524     (*(ppshpage->pfnCallback))(0, PSPCB_CREATE,
1525                                (LPPROPSHEETPAGEW)ppshpage);
1526
1527   if(ppshpage->dwFlags & PSP_INTERNAL_UNICODE)
1528      hwndPage = CreateDialogIndirectParamW(ppshpage->hInstance,
1529                                         pTemplate,
1530                                         hwndParent,
1531                                         ppshpage->pfnDlgProc,
1532                                         (LPARAM)ppshpage);
1533   else
1534      hwndPage = CreateDialogIndirectParamA(ppshpage->hInstance,
1535                                         pTemplate,
1536                                         hwndParent,
1537                                         ppshpage->pfnDlgProc,
1538                                         (LPARAM)ppshpage);
1539   /* Free a no more needed copy */
1540   if(temp)
1541       Free(temp);
1542
1543   psInfo->proppage[index].hwndPage = hwndPage;
1544
1545   /* Subclass exterior wizard pages */
1546   if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
1547      (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
1548      (ppshpage->dwFlags & PSP_HIDEHEADER))
1549   {
1550       SetWindowSubclass(hwndPage, PROPSHEET_WizardSubclassProc, 1,
1551                         (DWORD_PTR)ppshpage);
1552   }
1553
1554   return TRUE;
1555 }
1556
1557 /******************************************************************************
1558  *            PROPSHEET_LoadWizardBitmaps
1559  *
1560  * Loads the watermark and header bitmaps for a wizard.
1561  */
1562 static VOID PROPSHEET_LoadWizardBitmaps(PropSheetInfo *psInfo)
1563 {
1564   if (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD))
1565   {
1566     /* if PSH_USEHBMWATERMARK is not set, load the resource from pszbmWatermark 
1567        and put the HBITMAP in hbmWatermark. Thus all the rest of the code always 
1568        considers hbmWatermark as valid. */
1569     if ((psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
1570         !(psInfo->ppshheader.dwFlags & PSH_USEHBMWATERMARK))
1571     {
1572       ((PropSheetInfo *)psInfo)->ppshheader.u4.hbmWatermark = 
1573         CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT)psInfo->ppshheader.u4.pszbmWatermark, 0, NULL, 0);
1574     }
1575
1576     /* Same behavior as for watermarks */
1577     if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
1578         !(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER))
1579     {
1580       ((PropSheetInfo *)psInfo)->ppshheader.u5.hbmHeader = 
1581         CreateMappedBitmap(psInfo->ppshheader.hInstance, (INT)psInfo->ppshheader.u5.pszbmHeader, 0, NULL, 0);
1582     }
1583   }
1584 }
1585
1586
1587 /******************************************************************************
1588  *            PROPSHEET_ShowPage
1589  *
1590  * Displays or creates the specified page.
1591  */
1592 static BOOL PROPSHEET_ShowPage(HWND hwndDlg, int index, PropSheetInfo * psInfo)
1593 {
1594   HWND hwndTabCtrl;
1595   HWND hwndLineHeader;
1596   LPCPROPSHEETPAGEW ppshpage;
1597
1598   TRACE("active_page %d, index %d\n", psInfo->active_page, index);
1599   if (index == psInfo->active_page)
1600   {
1601       if (GetTopWindow(hwndDlg) != psInfo->proppage[index].hwndPage)
1602           SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
1603       return TRUE;
1604   }
1605
1606   ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
1607   if (psInfo->proppage[index].hwndPage == 0)
1608   {
1609      PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
1610   }
1611
1612   if ((psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD) &&
1613       (ppshpage->dwFlags & PSP_USETITLE))
1614   {
1615      PROPSHEET_SetTitleW(hwndDlg, psInfo->ppshheader.dwFlags,
1616                          psInfo->proppage[index].pszText);
1617   }
1618
1619   if (psInfo->active_page != -1)
1620      ShowWindow(psInfo->proppage[psInfo->active_page].hwndPage, SW_HIDE);
1621
1622   ShowWindow(psInfo->proppage[index].hwndPage, SW_SHOW);
1623
1624   /* Synchronize current selection with tab control
1625    * It seems to be needed even in case of PSH_WIZARD (no tab controls there) */
1626   hwndTabCtrl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
1627   SendMessageW(hwndTabCtrl, TCM_SETCURSEL, index, 0);
1628
1629   psInfo->active_page = index;
1630   psInfo->activeValid = TRUE;
1631
1632   if (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW) )
1633   {
1634       hwndLineHeader = GetDlgItem(hwndDlg, IDC_SUNKEN_LINEHEADER);
1635       ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
1636       
1637       if ((ppshpage->dwFlags & PSP_HIDEHEADER) || (!(psInfo->ppshheader.dwFlags & PSH_HEADER)) )
1638           ShowWindow(hwndLineHeader, SW_HIDE);
1639       else
1640           ShowWindow(hwndLineHeader, SW_SHOW);
1641   }
1642
1643   InvalidateRgn(hwndDlg, NULL, TRUE);
1644   UpdateWindow(hwndDlg);
1645
1646   return TRUE;
1647 }
1648
1649 /******************************************************************************
1650  *            PROPSHEET_Back
1651  */
1652 static BOOL PROPSHEET_Back(HWND hwndDlg)
1653 {
1654   PSHNOTIFY psn;
1655   HWND hwndPage;
1656   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1657                                                     PropSheetInfoStr);
1658   LRESULT result;
1659   int idx;
1660
1661   TRACE("active_page %d\n", psInfo->active_page);
1662   if (psInfo->active_page < 0)
1663      return FALSE;
1664
1665   psn.hdr.code     = PSN_WIZBACK;
1666   psn.hdr.hwndFrom = hwndDlg;
1667   psn.hdr.idFrom   = 0;
1668   psn.lParam       = 0;
1669
1670   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1671
1672   result = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1673   if (result == -1)
1674     return FALSE;
1675   else if (result == 0)
1676      idx = psInfo->active_page - 1;
1677   else
1678      idx = PROPSHEET_FindPageByResId(psInfo, result);
1679
1680   if (idx >= 0 && idx < psInfo->nPages)
1681   {
1682      if (PROPSHEET_CanSetCurSel(hwndDlg))
1683         PROPSHEET_SetCurSel(hwndDlg, idx, -1, 0);
1684   }
1685   return TRUE;
1686 }
1687
1688 /******************************************************************************
1689  *            PROPSHEET_Next
1690  */
1691 static BOOL PROPSHEET_Next(HWND hwndDlg)
1692 {
1693   PSHNOTIFY psn;
1694   HWND hwndPage;
1695   LRESULT msgResult = 0;
1696   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1697                                                     PropSheetInfoStr);
1698   int idx;
1699
1700   TRACE("active_page %d\n", psInfo->active_page);
1701   if (psInfo->active_page < 0)
1702      return FALSE;
1703
1704   psn.hdr.code     = PSN_WIZNEXT;
1705   psn.hdr.hwndFrom = hwndDlg;
1706   psn.hdr.idFrom   = 0;
1707   psn.lParam       = 0;
1708
1709   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1710
1711   msgResult = SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1712   if (msgResult == -1)
1713     return FALSE;
1714   else if (msgResult == 0)
1715      idx = psInfo->active_page + 1;
1716   else
1717      idx = PROPSHEET_FindPageByResId(psInfo, msgResult);
1718
1719   if (idx < psInfo->nPages )
1720   {
1721      if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
1722         PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
1723   }
1724
1725   return TRUE;
1726 }
1727
1728 /******************************************************************************
1729  *            PROPSHEET_Finish
1730  */
1731 static BOOL PROPSHEET_Finish(HWND hwndDlg)
1732 {
1733   PSHNOTIFY psn;
1734   HWND hwndPage;
1735   LRESULT msgResult = 0;
1736   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1737                                                     PropSheetInfoStr);
1738
1739   TRACE("active_page %d\n", psInfo->active_page);
1740   if (psInfo->active_page < 0)
1741      return FALSE;
1742
1743   psn.hdr.code     = PSN_WIZFINISH;
1744   psn.hdr.hwndFrom = hwndDlg;
1745   psn.hdr.idFrom   = 0;
1746   psn.lParam       = 0;
1747
1748   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1749
1750   msgResult = SendMessageW(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1751
1752   TRACE("msg result %ld\n", msgResult);
1753
1754   if (msgResult != 0)
1755     return FALSE;
1756
1757   if (psInfo->isModeless)
1758     psInfo->activeValid = FALSE;
1759   else
1760     EndDialog(hwndDlg, TRUE);
1761
1762   return TRUE;
1763 }
1764
1765 /******************************************************************************
1766  *            PROPSHEET_Apply
1767  */
1768 static BOOL PROPSHEET_Apply(HWND hwndDlg, LPARAM lParam)
1769 {
1770   int i;
1771   HWND hwndPage;
1772   PSHNOTIFY psn;
1773   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1774                                                     PropSheetInfoStr);
1775
1776   TRACE("active_page %d\n", psInfo->active_page);
1777   if (psInfo->active_page < 0)
1778      return FALSE;
1779
1780   psn.hdr.hwndFrom = hwndDlg;
1781   psn.hdr.idFrom   = 0;
1782   psn.lParam       = 0;
1783
1784
1785   /*
1786    * Send PSN_KILLACTIVE to the current page.
1787    */
1788   psn.hdr.code = PSN_KILLACTIVE;
1789
1790   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1791
1792   if (SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn) != FALSE)
1793     return FALSE;
1794
1795   /*
1796    * Send PSN_APPLY to all pages.
1797    */
1798   psn.hdr.code = PSN_APPLY;
1799   psn.lParam   = lParam;
1800
1801   for (i = 0; i < psInfo->nPages; i++)
1802   {
1803     hwndPage = psInfo->proppage[i].hwndPage;
1804     if (hwndPage)
1805     {
1806        switch (SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1807        {
1808        case PSNRET_INVALID:
1809            PROPSHEET_ShowPage(hwndDlg, i, psInfo);
1810            /* fall through */
1811        case PSNRET_INVALID_NOCHANGEPAGE:
1812            return FALSE;
1813        }
1814     }
1815   }
1816
1817   if(lParam)
1818   {
1819      psInfo->activeValid = FALSE;
1820   }
1821   else if(psInfo->active_page >= 0)
1822   {
1823      psn.hdr.code = PSN_SETACTIVE;
1824      psn.lParam   = 0;
1825      hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1826      SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1827   }
1828
1829   return TRUE;
1830 }
1831
1832 /******************************************************************************
1833  *            PROPSHEET_Cancel
1834  */
1835 static void PROPSHEET_Cancel(HWND hwndDlg, LPARAM lParam)
1836 {
1837   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1838                                                     PropSheetInfoStr);
1839   HWND hwndPage;
1840   PSHNOTIFY psn;
1841   int i;
1842
1843   TRACE("active_page %d\n", psInfo->active_page);
1844   if (psInfo->active_page < 0)
1845      return;
1846
1847   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1848   psn.hdr.code     = PSN_QUERYCANCEL;
1849   psn.hdr.hwndFrom = hwndDlg;
1850   psn.hdr.idFrom   = 0;
1851   psn.lParam       = 0;
1852
1853   if (SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn))
1854     return;
1855
1856   psn.hdr.code = PSN_RESET;
1857   psn.lParam   = lParam;
1858
1859   for (i = 0; i < psInfo->nPages; i++)
1860   {
1861     hwndPage = psInfo->proppage[i].hwndPage;
1862
1863     if (hwndPage)
1864        SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1865   }
1866
1867   if (psInfo->isModeless)
1868   {
1869      /* makes PSM_GETCURRENTPAGEHWND return NULL */
1870      psInfo->activeValid = FALSE;
1871   }
1872   else
1873     EndDialog(hwndDlg, FALSE);
1874 }
1875
1876 /******************************************************************************
1877  *            PROPSHEET_Help
1878  */
1879 static void PROPSHEET_Help(HWND hwndDlg)
1880 {
1881   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1882                                                     PropSheetInfoStr);
1883   HWND hwndPage;
1884   PSHNOTIFY psn;
1885
1886   TRACE("active_page %d\n", psInfo->active_page);
1887   if (psInfo->active_page < 0)
1888      return;
1889
1890   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
1891   psn.hdr.code     = PSN_HELP;
1892   psn.hdr.hwndFrom = hwndDlg;
1893   psn.hdr.idFrom   = 0;
1894   psn.lParam       = 0;
1895
1896   SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
1897 }
1898
1899 /******************************************************************************
1900  *            PROPSHEET_Changed
1901  */
1902 static void PROPSHEET_Changed(HWND hwndDlg, HWND hwndDirtyPage)
1903 {
1904   int i;
1905   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1906                                                     PropSheetInfoStr);
1907
1908   TRACE("\n");
1909   if (!psInfo) return;
1910   /*
1911    * Set the dirty flag of this page.
1912    */
1913   for (i = 0; i < psInfo->nPages; i++)
1914   {
1915     if (psInfo->proppage[i].hwndPage == hwndDirtyPage)
1916       psInfo->proppage[i].isDirty = TRUE;
1917   }
1918
1919   /*
1920    * Enable the Apply button.
1921    */
1922   if (psInfo->hasApply)
1923   {
1924     HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1925
1926     EnableWindow(hwndApplyBtn, TRUE);
1927   }
1928 }
1929
1930 /******************************************************************************
1931  *            PROPSHEET_UnChanged
1932  */
1933 static void PROPSHEET_UnChanged(HWND hwndDlg, HWND hwndCleanPage)
1934 {
1935   int i;
1936   BOOL noPageDirty = TRUE;
1937   HWND hwndApplyBtn = GetDlgItem(hwndDlg, IDC_APPLY_BUTTON);
1938   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
1939                                                     PropSheetInfoStr);
1940
1941   TRACE("\n");
1942   if ( !psInfo ) return;
1943   for (i = 0; i < psInfo->nPages; i++)
1944   {
1945     /* set the specified page as clean */
1946     if (psInfo->proppage[i].hwndPage == hwndCleanPage)
1947       psInfo->proppage[i].isDirty = FALSE;
1948
1949     /* look to see if there's any dirty pages */
1950     if (psInfo->proppage[i].isDirty)
1951       noPageDirty = FALSE;
1952   }
1953
1954   /*
1955    * Disable Apply button.
1956    */
1957   if (noPageDirty)
1958     EnableWindow(hwndApplyBtn, FALSE);
1959 }
1960
1961 /******************************************************************************
1962  *            PROPSHEET_PressButton
1963  */
1964 static void PROPSHEET_PressButton(HWND hwndDlg, int buttonID)
1965 {
1966   TRACE("buttonID %d\n", buttonID);
1967   switch (buttonID)
1968   {
1969     case PSBTN_APPLYNOW:
1970       PROPSHEET_DoCommand(hwndDlg, IDC_APPLY_BUTTON);
1971       break;
1972     case PSBTN_BACK:
1973       PROPSHEET_Back(hwndDlg);
1974       break;
1975     case PSBTN_CANCEL:
1976       PROPSHEET_DoCommand(hwndDlg, IDCANCEL);
1977       break;
1978     case PSBTN_FINISH:
1979       PROPSHEET_Finish(hwndDlg);
1980       break;
1981     case PSBTN_HELP:
1982       PROPSHEET_DoCommand(hwndDlg, IDHELP);
1983       break;
1984     case PSBTN_NEXT:
1985       PROPSHEET_Next(hwndDlg);
1986       break;
1987     case PSBTN_OK:
1988       PROPSHEET_DoCommand(hwndDlg, IDOK);
1989       break;
1990     default:
1991       FIXME("Invalid button index %d\n", buttonID);
1992   }
1993 }
1994
1995
1996 /*************************************************************************
1997  * BOOL PROPSHEET_CanSetCurSel [Internal]
1998  *
1999  * Test whether the current page can be changed by sending a PSN_KILLACTIVE
2000  *
2001  * PARAMS
2002  *     hwndDlg        [I] handle to a Dialog hWnd
2003  *
2004  * RETURNS
2005  *     TRUE if Current Selection can change
2006  *
2007  * NOTES
2008  */
2009 static BOOL PROPSHEET_CanSetCurSel(HWND hwndDlg)
2010 {
2011   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2012                                                     PropSheetInfoStr);
2013   HWND hwndPage;
2014   PSHNOTIFY psn;
2015   BOOL res = FALSE;
2016
2017   TRACE("active_page %d\n", psInfo->active_page);
2018   if (!psInfo)
2019   {
2020      res = FALSE;
2021      goto end;
2022   }
2023
2024   if (psInfo->active_page < 0)
2025   {
2026      res = TRUE;
2027      goto end;
2028   }
2029
2030   /*
2031    * Notify the current page.
2032    */
2033   hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
2034   psn.hdr.code     = PSN_KILLACTIVE;
2035   psn.hdr.hwndFrom = hwndDlg;
2036   psn.hdr.idFrom   = 0;
2037   psn.lParam       = 0;
2038
2039   res = !SendMessageA(hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
2040
2041 end:
2042   TRACE("<-- %d\n", res);
2043   return res;
2044 }
2045
2046 /******************************************************************************
2047  *            PROPSHEET_SetCurSel
2048  */
2049 static BOOL PROPSHEET_SetCurSel(HWND hwndDlg,
2050                                 int index,
2051                                 int skipdir,
2052                                 HPROPSHEETPAGE hpage
2053                                 )
2054 {
2055   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2056   HWND hwndHelp  = GetDlgItem(hwndDlg, IDHELP);
2057   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2058
2059   TRACE("index %d, skipdir %d, hpage %p\n", index, skipdir, hpage);
2060   /* hpage takes precedence over index */
2061   if (hpage != NULL)
2062     index = PROPSHEET_GetPageIndex(hpage, psInfo);
2063
2064   if (index < 0 || index >= psInfo->nPages)
2065   {
2066     TRACE("Could not find page to select!\n");
2067     return FALSE;
2068   }
2069
2070   while (1) {
2071     int result;
2072     PSHNOTIFY psn;
2073     RECT rc;
2074     LPCPROPSHEETPAGEW ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[index].hpage;
2075
2076     if (hwndTabControl)
2077         SendMessageW(hwndTabControl, TCM_SETCURSEL, index, 0);
2078
2079     psn.hdr.code     = PSN_SETACTIVE;
2080     psn.hdr.hwndFrom = hwndDlg;
2081     psn.hdr.idFrom   = 0;
2082     psn.lParam       = 0;
2083
2084     if (!psInfo->proppage[index].hwndPage) {
2085       PROPSHEET_CreatePage(hwndDlg, index, psInfo, ppshpage);
2086     }
2087
2088     /* Resize the property sheet page to the fit in the Tab control
2089      * (for regular property sheets) or to fit in the client area (for
2090      * wizards).
2091      * NOTE: The resizing happens every time the page is selected and
2092      * not only when it's created (some applications depend on it). */
2093     PROPSHEET_GetPageRect(psInfo, hwndDlg, &rc, ppshpage);
2094     TRACE("setting page %p, rc (%ld,%ld)-(%ld,%ld) w=%ld, h=%ld\n",
2095           psInfo->proppage[index].hwndPage, rc.left, rc.top, rc.right, rc.bottom,
2096           rc.right - rc.left, rc.bottom - rc.top);
2097     SetWindowPos(psInfo->proppage[index].hwndPage, HWND_TOP,
2098                  rc.left, rc.top,
2099                  rc.right - rc.left, rc.bottom - rc.top, 0);
2100
2101     result = SendMessageW(psInfo->proppage[index].hwndPage, WM_NOTIFY, 0, (LPARAM) &psn);
2102     if (!result)
2103       break;
2104     if (result == -1) {
2105       index+=skipdir;
2106       if (index < 0) {
2107         index = 0;
2108         WARN("Tried to skip before first property sheet page!\n");
2109         break;
2110       }
2111       if (index >= psInfo->nPages) {
2112         WARN("Tried to skip after last property sheet page!\n");
2113         index = psInfo->nPages-1;
2114         break;
2115       }
2116     }
2117     else if (result != 0)
2118     {
2119       int old_index = index;
2120       index = PROPSHEET_FindPageByResId(psInfo, result);
2121       if(index >= psInfo->nPages) {
2122         index = old_index;
2123         WARN("Tried to skip to nonexistant page by res id\n");
2124         break;
2125       }
2126       continue;
2127     }
2128   }
2129   /*
2130    * Display the new page.
2131    */
2132   PROPSHEET_ShowPage(hwndDlg, index, psInfo);
2133
2134   if (psInfo->proppage[index].hasHelp)
2135     EnableWindow(hwndHelp, TRUE);
2136   else
2137     EnableWindow(hwndHelp, FALSE);
2138
2139   return TRUE;
2140 }
2141
2142 /******************************************************************************
2143  *            PROPSHEET_SetCurSelId
2144  *
2145  * Selects the page, specified by resource id.
2146  */
2147 static void PROPSHEET_SetCurSelId(HWND hwndDlg, int id)
2148 {
2149       int idx;
2150       PropSheetInfo* psInfo =
2151           (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2152
2153       idx = PROPSHEET_FindPageByResId(psInfo, id);
2154       if (idx < psInfo->nPages )
2155       {
2156           if (PROPSHEET_CanSetCurSel(hwndDlg) != FALSE)
2157               PROPSHEET_SetCurSel(hwndDlg, idx, 1, 0);
2158       }
2159 }
2160
2161 /******************************************************************************
2162  *            PROPSHEET_SetTitleA
2163  */
2164 static void PROPSHEET_SetTitleA(HWND hwndDlg, DWORD dwStyle, LPCSTR lpszText)
2165 {
2166   if(HIWORD(lpszText))
2167   {
2168      WCHAR szTitle[256];
2169      MultiByteToWideChar(CP_ACP, 0, lpszText, -1,
2170                              szTitle, sizeof(szTitle));
2171      PROPSHEET_SetTitleW(hwndDlg, dwStyle, szTitle);
2172   }
2173   else
2174   {
2175      PROPSHEET_SetTitleW(hwndDlg, dwStyle, (LPCWSTR)lpszText);
2176   }
2177 }
2178
2179 /******************************************************************************
2180  *            PROPSHEET_SetTitleW
2181  */
2182 static void PROPSHEET_SetTitleW(HWND hwndDlg, DWORD dwStyle, LPCWSTR lpszText)
2183 {
2184   PropSheetInfo*        psInfo = (PropSheetInfo*) GetPropW(hwndDlg, PropSheetInfoStr);
2185   WCHAR                 szTitle[256];
2186
2187   TRACE("'%s' (style %08lx)\n", debugstr_w(lpszText), dwStyle);
2188   if (HIWORD(lpszText) == 0) {
2189     if (!LoadStringW(psInfo->ppshheader.hInstance,
2190                      LOWORD(lpszText), szTitle, sizeof(szTitle)-sizeof(WCHAR)))
2191       return;
2192     lpszText = szTitle;
2193   }
2194   if (dwStyle & PSH_PROPTITLE)
2195   {
2196     WCHAR* dest;
2197     int lentitle = strlenW(lpszText);
2198     int lenprop  = strlenW(psInfo->strPropertiesFor);
2199
2200     dest = Alloc( (lentitle + lenprop + 1)*sizeof (WCHAR));
2201     strcpyW(dest, psInfo->strPropertiesFor);
2202     strcatW(dest, lpszText);
2203
2204     SetWindowTextW(hwndDlg, dest);
2205     Free(dest);
2206   }
2207   else
2208     SetWindowTextW(hwndDlg, lpszText);
2209 }
2210
2211 /******************************************************************************
2212  *            PROPSHEET_SetFinishTextA
2213  */
2214 static void PROPSHEET_SetFinishTextA(HWND hwndDlg, LPCSTR lpszText)
2215 {
2216   HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2217
2218   TRACE("'%s'\n", lpszText);
2219   /* Set text, show and enable the Finish button */
2220   SetWindowTextA(hwndButton, lpszText);
2221   ShowWindow(hwndButton, SW_SHOW);
2222   EnableWindow(hwndButton, TRUE);
2223
2224   /* Make it default pushbutton */
2225   SendMessageA(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2226
2227   /* Hide Back button */
2228   hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2229   ShowWindow(hwndButton, SW_HIDE);
2230
2231   /* Hide Next button */
2232   hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2233   ShowWindow(hwndButton, SW_HIDE);
2234 }
2235
2236 /******************************************************************************
2237  *            PROPSHEET_SetFinishTextW
2238  */
2239 static void PROPSHEET_SetFinishTextW(HWND hwndDlg, LPCWSTR lpszText)
2240 {
2241   HWND hwndButton = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2242
2243   TRACE("'%s'\n", debugstr_w(lpszText));
2244   /* Set text, show and enable the Finish button */
2245   SetWindowTextW(hwndButton, lpszText);
2246   ShowWindow(hwndButton, SW_SHOW);
2247   EnableWindow(hwndButton, TRUE);
2248
2249   /* Make it default pushbutton */
2250   SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2251
2252   /* Hide Back button */
2253   hwndButton = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2254   ShowWindow(hwndButton, SW_HIDE);
2255
2256   /* Hide Next button */
2257   hwndButton = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2258   ShowWindow(hwndButton, SW_HIDE);
2259 }
2260
2261 /******************************************************************************
2262  *            PROPSHEET_QuerySiblings
2263  */
2264 static LRESULT PROPSHEET_QuerySiblings(HWND hwndDlg,
2265                                        WPARAM wParam, LPARAM lParam)
2266 {
2267   int i = 0;
2268   HWND hwndPage;
2269   LRESULT msgResult = 0;
2270   PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2271                                                     PropSheetInfoStr);
2272
2273   while ((i < psInfo->nPages) && (msgResult == 0))
2274   {
2275     hwndPage = psInfo->proppage[i].hwndPage;
2276     msgResult = SendMessageA(hwndPage, PSM_QUERYSIBLINGS, wParam, lParam);
2277     i++;
2278   }
2279
2280   return msgResult;
2281 }
2282
2283
2284 /******************************************************************************
2285  *            PROPSHEET_AddPage
2286  */
2287 static BOOL PROPSHEET_AddPage(HWND hwndDlg,
2288                               HPROPSHEETPAGE hpage)
2289 {
2290   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2291                                                      PropSheetInfoStr);
2292   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2293   TCITEMW item;
2294   LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)hpage;
2295
2296   TRACE("hpage %p\n", hpage);
2297   /*
2298    * Allocate and fill in a new PropPageInfo entry.
2299    */
2300   psInfo->proppage = (PropPageInfo*) ReAlloc(psInfo->proppage,
2301                                                       sizeof(PropPageInfo) *
2302                                                       (psInfo->nPages + 1));
2303   if (!PROPSHEET_CollectPageInfo(ppsp, psInfo, psInfo->nPages))
2304       return FALSE;
2305
2306   psInfo->proppage[psInfo->nPages].hpage = hpage;
2307
2308   if (ppsp->dwFlags & PSP_PREMATURE)
2309   {
2310      /* Create the page but don't show it */
2311      PROPSHEET_CreatePage(hwndDlg, psInfo->nPages, psInfo, ppsp);
2312   }
2313
2314   /*
2315    * Add a new tab to the tab control.
2316    */
2317   item.mask = TCIF_TEXT;
2318   item.pszText = (LPWSTR) psInfo->proppage[psInfo->nPages].pszText;
2319   item.cchTextMax = MAX_TABTEXT_LENGTH;
2320
2321   if (psInfo->hImageList)
2322   {
2323     SendMessageW(hwndTabControl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
2324   }
2325
2326   if ( psInfo->proppage[psInfo->nPages].hasIcon )
2327   {
2328     item.mask |= TCIF_IMAGE;
2329     item.iImage = psInfo->nPages;
2330   }
2331
2332   SendMessageW(hwndTabControl, TCM_INSERTITEMW, psInfo->nPages + 1,
2333                (LPARAM)&item);
2334
2335   psInfo->nPages++;
2336
2337   /* If it is the only page - show it */
2338   if(psInfo->nPages == 1)
2339      PROPSHEET_SetCurSel(hwndDlg, 0, 1, 0);
2340   return TRUE;
2341 }
2342
2343 /******************************************************************************
2344  *            PROPSHEET_RemovePage
2345  */
2346 static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
2347                                  int index,
2348                                  HPROPSHEETPAGE hpage)
2349 {
2350   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2351                                                      PropSheetInfoStr);
2352   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2353   PropPageInfo* oldPages;
2354
2355   TRACE("index %d, hpage %p\n", index, hpage);
2356   if (!psInfo) {
2357     return FALSE;
2358   }
2359   /*
2360    * hpage takes precedence over index.
2361    */
2362   if (hpage != 0)
2363   {
2364     index = PROPSHEET_GetPageIndex(hpage, psInfo);
2365   }
2366
2367   /* Make sure that index is within range */
2368   if (index < 0 || index >= psInfo->nPages)
2369   {
2370       TRACE("Could not find page to remove!\n");
2371       return FALSE;
2372   }
2373
2374   TRACE("total pages %d removing page %d active page %d\n",
2375         psInfo->nPages, index, psInfo->active_page);
2376   /*
2377    * Check if we're removing the active page.
2378    */
2379   if (index == psInfo->active_page)
2380   {
2381     if (psInfo->nPages > 1)
2382     {
2383       if (index > 0)
2384       {
2385         /* activate previous page  */
2386         PROPSHEET_SetCurSel(hwndDlg, index - 1, -1, 0);
2387       }
2388       else
2389       {
2390         /* activate the next page */
2391         PROPSHEET_SetCurSel(hwndDlg, index + 1, 1, 0);
2392         psInfo->active_page = index;
2393       }
2394     }
2395     else
2396     {
2397       psInfo->active_page = -1;
2398       if (!psInfo->isModeless)
2399       {
2400          EndDialog(hwndDlg, FALSE);
2401          return TRUE;
2402       }
2403     }
2404   }
2405   else if (index < psInfo->active_page)
2406     psInfo->active_page--;
2407
2408   /* Unsubclass the page dialog window */
2409   if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD) &&
2410      (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2411      ((PROPSHEETPAGEW*)psInfo->proppage[index].hpage)->dwFlags & PSP_HIDEHEADER))
2412   {
2413      RemoveWindowSubclass(psInfo->proppage[index].hwndPage,
2414                           PROPSHEET_WizardSubclassProc, 1);
2415   }
2416
2417   /* Destroy page dialog window */
2418   DestroyWindow(psInfo->proppage[index].hwndPage);
2419
2420   /* Free page resources */
2421   if(psInfo->proppage[index].hpage)
2422   {
2423      PROPSHEETPAGEW* psp = (PROPSHEETPAGEW*)psInfo->proppage[index].hpage;
2424
2425      if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[index].pszText)
2426         HeapFree(GetProcessHeap(), 0, (LPVOID)psInfo->proppage[index].pszText);
2427
2428      DestroyPropertySheetPage(psInfo->proppage[index].hpage);
2429   }
2430
2431   /* Remove the tab */
2432   SendMessageW(hwndTabControl, TCM_DELETEITEM, index, 0);
2433
2434   oldPages = psInfo->proppage;
2435   psInfo->nPages--;
2436   psInfo->proppage = Alloc(sizeof(PropPageInfo) * psInfo->nPages);
2437
2438   if (index > 0)
2439     memcpy(&psInfo->proppage[0], &oldPages[0], index * sizeof(PropPageInfo));
2440
2441   if (index < psInfo->nPages)
2442     memcpy(&psInfo->proppage[index], &oldPages[index + 1],
2443            (psInfo->nPages - index) * sizeof(PropPageInfo));
2444
2445   Free(oldPages);
2446
2447   return FALSE;
2448 }
2449
2450 /******************************************************************************
2451  *            PROPSHEET_SetWizButtons
2452  *
2453  * This code will work if (and assumes that) the Next button is on top of the
2454  * Finish button. ie. Finish comes after Next in the Z order.
2455  * This means make sure the dialog template reflects this.
2456  *
2457  */
2458 static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags)
2459 {
2460   HWND hwndBack   = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2461   HWND hwndNext   = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2462   HWND hwndFinish = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2463
2464   TRACE("%ld\n", dwFlags);
2465
2466   EnableWindow(hwndBack, FALSE);
2467   EnableWindow(hwndNext, FALSE);
2468   EnableWindow(hwndFinish, FALSE);
2469
2470   if (dwFlags & PSWIZB_BACK)
2471     EnableWindow(hwndBack, TRUE);
2472
2473   if (dwFlags & PSWIZB_NEXT)
2474   {
2475     /* Hide the Finish button */
2476     ShowWindow(hwndFinish, SW_HIDE);
2477
2478     /* Show and enable the Next button */
2479     ShowWindow(hwndNext, SW_SHOW);
2480     EnableWindow(hwndNext, TRUE);
2481
2482     /* Set the Next button as the default pushbutton  */
2483     SendMessageA(hwndDlg, DM_SETDEFID, IDC_NEXT_BUTTON, 0);
2484   }
2485
2486   if ((dwFlags & PSWIZB_FINISH) || (dwFlags & PSWIZB_DISABLEDFINISH))
2487   {
2488     /* Hide the Next button */
2489     ShowWindow(hwndNext, SW_HIDE);
2490
2491     /* Show the Finish button */
2492     ShowWindow(hwndFinish, SW_SHOW);
2493
2494     if (dwFlags & PSWIZB_FINISH)
2495       EnableWindow(hwndFinish, TRUE);
2496
2497     /* Set the Finish button as the default pushbutton  */
2498     SendMessageA(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2499   }
2500 }
2501
2502 /******************************************************************************
2503  *            PROPSHEET_InsertPage
2504  */
2505 static BOOL PROPSHEET_InsertPage(HWND hwndDlg, HPROPSHEETPAGE hpageInsertAfter, HPROPSHEETPAGE hpage)
2506 {
2507     if (!HIWORD(hpageInsertAfter))
2508         FIXME("(%p, %d, %p): stub\n", hwndDlg, LOWORD(hpageInsertAfter), hpage);
2509     else
2510         FIXME("(%p, %p, %p): stub\n", hwndDlg, hpageInsertAfter, hpage);
2511     return FALSE;
2512 }
2513
2514 /******************************************************************************
2515  *            PROPSHEET_SetHeaderTitleW
2516  */
2517 static void PROPSHEET_SetHeaderTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderTitle)
2518 {
2519     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderTitle));
2520 }
2521
2522 /******************************************************************************
2523  *            PROPSHEET_SetHeaderTitleA
2524  */
2525 static void PROPSHEET_SetHeaderTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderTitle)
2526 {
2527     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderTitle));
2528 }
2529
2530 /******************************************************************************
2531  *            PROPSHEET_SetHeaderSubTitleW
2532  */
2533 static void PROPSHEET_SetHeaderSubTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderSubTitle)
2534 {
2535     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderSubTitle));
2536 }
2537
2538 /******************************************************************************
2539  *            PROPSHEET_SetHeaderSubTitleA
2540  */
2541 static void PROPSHEET_SetHeaderSubTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderSubTitle)
2542 {
2543     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderSubTitle));
2544 }
2545
2546 /******************************************************************************
2547  *            PROPSHEET_HwndToIndex
2548  */
2549 static LRESULT PROPSHEET_HwndToIndex(HWND hwndDlg, HWND hPageDlg)
2550 {
2551     int index;
2552     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2553                                                        PropSheetInfoStr);
2554
2555     TRACE("(%p, %p)\n", hwndDlg, hPageDlg);
2556
2557     for (index = 0; index < psInfo->nPages; index++)
2558         if (psInfo->proppage[index].hwndPage == hPageDlg)
2559             return index;
2560     WARN("%p not found\n", hPageDlg);
2561     return -1;
2562 }
2563
2564 /******************************************************************************
2565  *            PROPSHEET_IndexToHwnd
2566  */
2567 static LRESULT PROPSHEET_IndexToHwnd(HWND hwndDlg, int iPageIndex)
2568 {
2569     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2570                                                        PropSheetInfoStr);
2571     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2572     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2573         WARN("%d out of range.\n", iPageIndex);
2574         return 0;
2575     }
2576     return (LRESULT)psInfo->proppage[iPageIndex].hwndPage;
2577 }
2578
2579 /******************************************************************************
2580  *            PROPSHEET_PageToIndex
2581  */
2582 static LRESULT PROPSHEET_PageToIndex(HWND hwndDlg, HPROPSHEETPAGE hPage)
2583 {
2584     int index;
2585     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2586                                                        PropSheetInfoStr);
2587
2588     TRACE("(%p, %p)\n", hwndDlg, hPage);
2589
2590     for (index = 0; index < psInfo->nPages; index++)
2591         if (psInfo->proppage[index].hpage == hPage)
2592             return index;
2593     WARN("%p not found\n", hPage);
2594     return -1;
2595 }
2596
2597 /******************************************************************************
2598  *            PROPSHEET_IndexToPage
2599  */
2600 static LRESULT PROPSHEET_IndexToPage(HWND hwndDlg, int iPageIndex)
2601 {
2602     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2603                                                        PropSheetInfoStr);
2604     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2605     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2606         WARN("%d out of range.\n", iPageIndex);
2607         return 0;
2608     }
2609     return (LRESULT)psInfo->proppage[iPageIndex].hpage;
2610 }
2611
2612 /******************************************************************************
2613  *            PROPSHEET_IdToIndex
2614  */
2615 static LRESULT PROPSHEET_IdToIndex(HWND hwndDlg, int iPageId)
2616 {
2617     FIXME("(%p, %d): stub\n", hwndDlg, iPageId);
2618     return -1;
2619 }
2620
2621 /******************************************************************************
2622  *            PROPSHEET_IndexToId
2623  */
2624 static LRESULT PROPSHEET_IndexToId(HWND hwndDlg, int iPageIndex)
2625 {
2626     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2627                                                        PropSheetInfoStr);
2628     LPCPROPSHEETPAGEW psp;
2629     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2630     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2631         WARN("%d out of range.\n", iPageIndex);
2632         return 0;
2633     }
2634     psp = (LPCPROPSHEETPAGEW)psInfo->proppage[iPageIndex].hpage;
2635     if (psp->dwFlags & PSP_DLGINDIRECT || HIWORD(psp->u.pszTemplate)) {
2636         return 0;
2637     }
2638     return (LRESULT)psp->u.pszTemplate;
2639 }
2640
2641 /******************************************************************************
2642  *            PROPSHEET_GetResult
2643  */
2644 static LRESULT PROPSHEET_GetResult(HWND hwndDlg)
2645 {
2646     FIXME("(%p): stub\n", hwndDlg);
2647     return -1;
2648 }
2649
2650 /******************************************************************************
2651  *            PROPSHEET_RecalcPageSizes
2652  */
2653 static BOOL PROPSHEET_RecalcPageSizes(HWND hwndDlg)
2654 {
2655     FIXME("(%p): stub\n", hwndDlg);
2656     return FALSE;
2657 }
2658
2659 /******************************************************************************
2660  *            PROPSHEET_GetPageIndex
2661  *
2662  * Given a HPROPSHEETPAGE, returns the index of the corresponding page from
2663  * the array of PropPageInfo.
2664  */
2665 static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, PropSheetInfo* psInfo)
2666 {
2667   BOOL found = FALSE;
2668   int index = 0;
2669
2670   TRACE("hpage %p\n", hpage);
2671   while ((index < psInfo->nPages) && (found == FALSE))
2672   {
2673     if (psInfo->proppage[index].hpage == hpage)
2674       found = TRUE;
2675     else
2676       index++;
2677   }
2678
2679   if (found == FALSE)
2680     index = -1;
2681
2682   return index;
2683 }
2684
2685 /******************************************************************************
2686  *            PROPSHEET_CleanUp
2687  */
2688 static void PROPSHEET_CleanUp(HWND hwndDlg)
2689 {
2690   int i;
2691   PropSheetInfo* psInfo = (PropSheetInfo*) RemovePropW(hwndDlg,
2692                                                        PropSheetInfoStr);
2693
2694   TRACE("\n");
2695   if (!psInfo) return;
2696   if (HIWORD(psInfo->ppshheader.pszCaption))
2697       HeapFree(GetProcessHeap(), 0, (LPVOID)psInfo->ppshheader.pszCaption);
2698
2699   for (i = 0; i < psInfo->nPages; i++)
2700   {
2701      PROPSHEETPAGEA* psp = (PROPSHEETPAGEA*)psInfo->proppage[i].hpage;
2702
2703      /* Unsubclass the page dialog window */
2704      if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
2705         (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2706         (psp->dwFlags & PSP_HIDEHEADER))
2707      {
2708         RemoveWindowSubclass(psInfo->proppage[i].hwndPage,
2709                              PROPSHEET_WizardSubclassProc, 1);
2710      }
2711
2712      if(psInfo->proppage[i].hwndPage)
2713         DestroyWindow(psInfo->proppage[i].hwndPage);
2714
2715      if(psp)
2716      {
2717         if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[i].pszText)
2718            HeapFree(GetProcessHeap(), 0, (LPVOID)psInfo->proppage[i].pszText);
2719
2720         DestroyPropertySheetPage(psInfo->proppage[i].hpage);
2721      }
2722   }
2723
2724   DeleteObject(psInfo->hFont);
2725   DeleteObject(psInfo->hFontBold);
2726   /* If we created the bitmaps, destroy them */
2727   if ((psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2728       (!(psInfo->ppshheader.dwFlags & PSH_USEHBMWATERMARK)) )
2729       DeleteObject(psInfo->ppshheader.u4.hbmWatermark);
2730   if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
2731       (!(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER)) )
2732       DeleteObject(psInfo->ppshheader.u5.hbmHeader);
2733
2734   Free(psInfo->proppage);
2735   Free(psInfo->strPropertiesFor);
2736   ImageList_Destroy(psInfo->hImageList);
2737
2738   GlobalFree((HGLOBAL)psInfo);
2739 }
2740
2741 /******************************************************************************
2742  *            PropertySheet    (COMCTL32.@)
2743  *            PropertySheetA   (COMCTL32.@)
2744  *
2745  * Creates a property sheet in the specified property sheet header.
2746  *
2747  * RETURNS
2748  *     Modal property sheets: Positive if successful or -1 otherwise.
2749  *     Modeless property sheets: Property sheet handle.
2750  *     Or:
2751  *| ID_PSREBOOTSYSTEM - The user must reboot the computer for the changes to take effect.
2752  *| ID_PSRESTARTWINDOWS - The user must restart Windows for the changes to take effect.
2753  */
2754 INT WINAPI PropertySheetA(LPCPROPSHEETHEADERA lppsh)
2755 {
2756   int bRet = 0;
2757   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2758                                                        sizeof(PropSheetInfo));
2759   UINT i, n;
2760   BYTE* pByte;
2761
2762   TRACE("(%p)\n", lppsh);
2763
2764   PROPSHEET_CollectSheetInfoA(lppsh, psInfo);
2765
2766   psInfo->proppage = (PropPageInfo*) Alloc(sizeof(PropPageInfo) *
2767                                                     lppsh->nPages);
2768   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2769
2770   for (n = i = 0; i < lppsh->nPages; i++, n++)
2771   {
2772     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2773       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2774     else
2775     {
2776        psInfo->proppage[n].hpage = CreatePropertySheetPageA((LPCPROPSHEETPAGEA)pByte);
2777        pByte += ((LPPROPSHEETPAGEA)pByte)->dwSize;
2778     }
2779
2780     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2781                                psInfo, n))
2782     {
2783         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2784             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2785         n--;
2786         psInfo->nPages--;
2787     }
2788   }
2789
2790   psInfo->unicode = FALSE;
2791   bRet = PROPSHEET_CreateDialog(psInfo);
2792
2793   return bRet;
2794 }
2795
2796 /******************************************************************************
2797  *            PropertySheetW   (COMCTL32.@)
2798  *
2799  * See PropertySheetA.
2800  */
2801 INT WINAPI PropertySheetW(LPCPROPSHEETHEADERW lppsh)
2802 {
2803   int bRet = 0;
2804   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2805                                                        sizeof(PropSheetInfo));
2806   UINT i, n;
2807   BYTE* pByte;
2808
2809   TRACE("(%p)\n", lppsh);
2810
2811   PROPSHEET_CollectSheetInfoW(lppsh, psInfo);
2812
2813   psInfo->proppage = (PropPageInfo*) Alloc(sizeof(PropPageInfo) *
2814                                                     lppsh->nPages);
2815   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2816
2817   for (n = i = 0; i < lppsh->nPages; i++, n++)
2818   {
2819     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2820       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2821     else
2822     {
2823        psInfo->proppage[n].hpage = CreatePropertySheetPageW((LPCPROPSHEETPAGEW)pByte);
2824        pByte += ((LPPROPSHEETPAGEW)pByte)->dwSize;
2825     }
2826
2827     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2828                                psInfo, n))
2829     {
2830         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2831             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2832         n--;
2833         psInfo->nPages--;
2834     }
2835   }
2836
2837   psInfo->unicode = TRUE;
2838   bRet = PROPSHEET_CreateDialog(psInfo);
2839
2840   return bRet;
2841 }
2842
2843 /******************************************************************************
2844  *            CreatePropertySheetPage    (COMCTL32.@)
2845  *            CreatePropertySheetPageA   (COMCTL32.@)
2846  *
2847  * Creates a new property sheet page.
2848  *
2849  * RETURNS
2850  *     Success: Handle to new property sheet page.
2851  *     Failure: NULL.
2852  *
2853  * NOTES
2854  *     An application must use the PSM_ADDPAGE message to add the new page to
2855  *     an existing property sheet.
2856  */
2857 HPROPSHEETPAGE WINAPI CreatePropertySheetPageA(
2858                           LPCPROPSHEETPAGEA lpPropSheetPage)
2859 {
2860   PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2861
2862   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEA)));
2863
2864   ppsp->dwFlags &= ~ PSP_INTERNAL_UNICODE;
2865   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2866   {
2867      int len = strlen(lpPropSheetPage->u.pszTemplate);
2868
2869      ppsp->u.pszTemplate = HeapAlloc( GetProcessHeap(),0,len+1 );
2870      strcpy( (LPSTR)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2871   }
2872   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2873   {
2874       PROPSHEET_AtoW(&ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon);
2875   }
2876
2877   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2878   {
2879       PROPSHEET_AtoW(&ppsp->pszTitle, lpPropSheetPage->pszTitle);
2880   }
2881   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2882       ppsp->pszTitle = NULL;
2883
2884   return (HPROPSHEETPAGE)ppsp;
2885 }
2886
2887 /******************************************************************************
2888  *            CreatePropertySheetPageW   (COMCTL32.@)
2889  *
2890  * See CreatePropertySheetA.
2891  */
2892 HPROPSHEETPAGE WINAPI CreatePropertySheetPageW(LPCPROPSHEETPAGEW lpPropSheetPage)
2893 {
2894   PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2895
2896   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEW)));
2897
2898   ppsp->dwFlags |= PSP_INTERNAL_UNICODE;
2899
2900   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2901   {
2902     int len = strlenW(lpPropSheetPage->u.pszTemplate);
2903
2904     ppsp->u.pszTemplate = HeapAlloc( GetProcessHeap(),0,(len+1)*sizeof (WCHAR) );
2905     strcpyW( (WCHAR *)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2906   }
2907   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2908   {
2909       int len = strlenW(lpPropSheetPage->u2.pszIcon);
2910       ppsp->u2.pszIcon = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof (WCHAR) );
2911       strcpyW( (WCHAR *)ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon );
2912   }
2913
2914   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2915   {
2916       int len = strlenW(lpPropSheetPage->pszTitle);
2917       ppsp->pszTitle = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof (WCHAR) );
2918       strcpyW( (WCHAR *)ppsp->pszTitle, lpPropSheetPage->pszTitle );
2919   }
2920   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2921       ppsp->pszTitle = NULL;
2922
2923   return (HPROPSHEETPAGE)ppsp;
2924 }
2925
2926 /******************************************************************************
2927  *            DestroyPropertySheetPage   (COMCTL32.@)
2928  *
2929  * Destroys a property sheet page previously created with
2930  * CreatePropertySheetA() or CreatePropertySheetW() and frees the associated
2931  * memory.
2932  *
2933  * RETURNS
2934  *     Success: TRUE
2935  *     Failure: FALSE
2936  */
2937 BOOL WINAPI DestroyPropertySheetPage(HPROPSHEETPAGE hPropPage)
2938 {
2939   PROPSHEETPAGEW *psp = (PROPSHEETPAGEW *)hPropPage;
2940
2941   if (!psp)
2942      return FALSE;
2943
2944   if ( !(psp->dwFlags & PSP_DLGINDIRECT) && HIWORD( psp->u.pszTemplate ) )
2945      HeapFree(GetProcessHeap(), 0, (LPVOID)psp->u.pszTemplate);
2946
2947   if ( (psp->dwFlags & PSP_USEICONID) && HIWORD( psp->u2.pszIcon ) )
2948      HeapFree(GetProcessHeap(), 0, (LPVOID)psp->u2.pszIcon);
2949
2950   if ((psp->dwFlags & PSP_USETITLE) && HIWORD( psp->pszTitle ))
2951      HeapFree(GetProcessHeap(), 0, (LPVOID)psp->pszTitle);
2952
2953   Free(hPropPage);
2954
2955   return TRUE;
2956 }
2957
2958 /******************************************************************************
2959  *            PROPSHEET_IsDialogMessage
2960  */
2961 static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg)
2962 {
2963    PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
2964
2965    TRACE("\n");
2966    if (!psInfo || (hwnd != lpMsg->hwnd && !IsChild(hwnd, lpMsg->hwnd)))
2967       return FALSE;
2968
2969    if (lpMsg->message == WM_KEYDOWN && (GetKeyState(VK_CONTROL) & 0x8000))
2970    {
2971       int new_page = 0;
2972       INT dlgCode = SendMessageA(lpMsg->hwnd, WM_GETDLGCODE, 0, (LPARAM)lpMsg);
2973
2974       if (!(dlgCode & DLGC_WANTMESSAGE))
2975       {
2976          switch (lpMsg->wParam)
2977          {
2978             case VK_TAB:
2979                if (GetKeyState(VK_SHIFT) & 0x8000)
2980                    new_page = -1;
2981                 else
2982                    new_page = 1;
2983                break;
2984
2985             case VK_NEXT:   new_page = 1;  break;
2986             case VK_PRIOR:  new_page = -1; break;
2987          }
2988       }
2989
2990       if (new_page)
2991       {
2992          if (PROPSHEET_CanSetCurSel(hwnd) != FALSE)
2993          {
2994             new_page += psInfo->active_page;
2995
2996             if (new_page < 0)
2997                new_page = psInfo->nPages - 1;
2998             else if (new_page >= psInfo->nPages)
2999                new_page = 0;
3000
3001             PROPSHEET_SetCurSel(hwnd, new_page, 1, 0);
3002          }
3003
3004          return TRUE;
3005       }
3006    }
3007
3008    return IsDialogMessageA(hwnd, lpMsg);
3009 }
3010
3011 /******************************************************************************
3012  *            PROPSHEET_DoCommand
3013  */
3014 static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID)
3015 {
3016
3017     switch (wID) {
3018
3019     case IDOK:
3020     case IDC_APPLY_BUTTON:
3021         {
3022             HWND hwndApplyBtn = GetDlgItem(hwnd, IDC_APPLY_BUTTON);
3023
3024             if (PROPSHEET_Apply(hwnd, wID == IDOK ? 1: 0) == FALSE)
3025                 break;
3026
3027             if (wID == IDOK)
3028                 {
3029                     PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3030                                                                       PropSheetInfoStr);
3031                     int result = TRUE;
3032
3033                     if (psInfo->restartWindows)
3034                         result = ID_PSRESTARTWINDOWS;
3035
3036                     /* reboot system takes precedence over restart windows */
3037                     if (psInfo->rebootSystem)
3038                         result = ID_PSREBOOTSYSTEM;
3039
3040                     if (psInfo->isModeless)
3041                         psInfo->activeValid = FALSE;
3042                     else
3043                         EndDialog(hwnd, result);
3044                 }
3045             else
3046                 EnableWindow(hwndApplyBtn, FALSE);
3047
3048             break;
3049         }
3050
3051     case IDC_BACK_BUTTON:
3052         PROPSHEET_Back(hwnd);
3053         break;
3054
3055     case IDC_NEXT_BUTTON:
3056         PROPSHEET_Next(hwnd);
3057         break;
3058
3059     case IDC_FINISH_BUTTON:
3060         PROPSHEET_Finish(hwnd);
3061         break;
3062
3063     case IDCANCEL:
3064         PROPSHEET_Cancel(hwnd, 0);
3065         break;
3066
3067     case IDHELP:
3068         PROPSHEET_Help(hwnd);
3069         break;
3070
3071     default:
3072         return FALSE;
3073     }
3074
3075     return TRUE;
3076 }
3077
3078 /******************************************************************************
3079  *            PROPSHEET_Paint
3080  */
3081 static LRESULT PROPSHEET_Paint(HWND hwnd)
3082 {
3083     PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
3084     PAINTSTRUCT ps;
3085     HDC hdc, hdcSrc;
3086     BITMAP bm;
3087     HBITMAP hbmp;
3088     HPALETTE hOldPal = 0;
3089     int offsety = 0;
3090     HBRUSH hbr;
3091     RECT r, rzone;
3092     LPCPROPSHEETPAGEW ppshpage;
3093     WCHAR szBuffer[256];
3094     int nLength;
3095
3096     hdc = BeginPaint(hwnd, &ps);
3097     if (!hdc) return 1;
3098
3099     hdcSrc = CreateCompatibleDC(0);
3100     ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[psInfo->active_page].hpage;
3101
3102     if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
3103         hOldPal = SelectPalette(hdc, psInfo->ppshheader.hplWatermark, FALSE);
3104
3105     if ( (!(ppshpage->dwFlags & PSP_HIDEHEADER)) &&
3106          (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
3107          (psInfo->ppshheader.dwFlags & PSH_HEADER) ) 
3108     {
3109         HWND hwndLineHeader = GetDlgItem(hwnd, IDC_SUNKEN_LINEHEADER);
3110         HFONT hOldFont;
3111         COLORREF clrOld = 0;
3112         int oldBkMode = 0;
3113
3114         hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u5.hbmHeader);
3115         hOldFont = SelectObject(hdc, psInfo->hFontBold);
3116
3117         GetClientRect(hwndLineHeader, &r);
3118         MapWindowPoints(hwndLineHeader, hwnd, (LPPOINT) &r, 2);
3119         SetRect(&rzone, 0, 0, r.right + 1, r.top - 1);
3120
3121         GetObjectA(psInfo->ppshheader.u5.hbmHeader, sizeof(BITMAP), (LPVOID)&bm);               
3122
3123         if (psInfo->ppshheader.dwFlags & PSH_WIZARD97_OLD)
3124         {
3125             /* Fill the unoccupied part of the header with color of the
3126              * left-top pixel, but do it only when needed.
3127              */
3128             if (bm.bmWidth < r.right || bm.bmHeight < r.bottom)
3129             {
3130                 hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
3131                 CopyRect(&r, &rzone);
3132                 if (bm.bmWidth < r.right)
3133                 {
3134                     r.left = bm.bmWidth;
3135                     FillRect(hdc, &r, hbr);
3136                 }
3137                 if (bm.bmHeight < r.bottom)
3138                 {
3139                     r.left = 0;
3140                     r.top = bm.bmHeight;
3141                     FillRect(hdc, &r, hbr);
3142                 }
3143                 DeleteObject(hbr);
3144             }
3145
3146             /* Draw the header itself. */
3147             BitBlt(hdc, 0, 0,
3148                    bm.bmWidth, min(bm.bmHeight, rzone.bottom),
3149                    hdcSrc, 0, 0, SRCCOPY);
3150         }
3151         else
3152         {
3153             hbr = GetSysColorBrush(COLOR_WINDOW);
3154             FillRect(hdc, &rzone, hbr);
3155
3156             /* Draw the header bitmap. It's always centered like a
3157              * common 49 x 49 bitmap. */
3158             BitBlt(hdc, rzone.right - 49 - ((rzone.bottom - 49) / 2),
3159                    (rzone.bottom - 49) / 2,
3160                    bm.bmWidth, bm.bmHeight,
3161                    hdcSrc, 0, 0, SRCCOPY);
3162
3163             /* NOTE: Native COMCTL32 draws a white stripe over the bitmap
3164              * if its height is smaller than 49 pixels. Because the reason
3165              * for this bug is unknown the current code doesn't try to
3166              * replicate it. */
3167         }
3168
3169         clrOld = SetTextColor (hdc, 0x00000000);
3170         oldBkMode = SetBkMode (hdc, TRANSPARENT); 
3171
3172         if (ppshpage->dwFlags & PSP_USEHEADERTITLE) {
3173             SetRect(&r, 20, 10, 0, 0);
3174             if (HIWORD(ppshpage->pszHeaderTitle))
3175             {
3176                 if (psInfo->unicode)
3177                     DrawTextW(hdc, (LPWSTR)ppshpage->pszHeaderTitle,
3178                               -1, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3179                 else
3180                     DrawTextA(hdc, (LPCSTR)ppshpage->pszHeaderTitle,
3181                               -1, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3182             }
3183             else
3184             {
3185                 nLength = LoadStringW(ppshpage->hInstance, (UINT)ppshpage->pszHeaderTitle,
3186                                       szBuffer, 256);
3187                 if (nLength != 0)
3188                 {
3189                     DrawTextW(hdc, szBuffer, nLength,
3190                               &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3191                 }
3192             }
3193         }
3194
3195         if (ppshpage->dwFlags & PSP_USEHEADERSUBTITLE) {
3196             SelectObject(hdc, psInfo->hFont);
3197             SetRect(&r, 40, 25, rzone.right - 69, rzone.bottom);
3198             if (HIWORD(ppshpage->pszHeaderTitle))
3199             {
3200                 if (psInfo->unicode)
3201                     DrawTextW(hdc, (LPWSTR)ppshpage->pszHeaderSubTitle,
3202                               -1, &r, DT_LEFT | DT_SINGLELINE);
3203                 else
3204                     DrawTextA(hdc, (LPCSTR)ppshpage->pszHeaderSubTitle,
3205                               -1, &r, DT_LEFT | DT_SINGLELINE);
3206             }
3207             else
3208             {
3209                 nLength = LoadStringW(ppshpage->hInstance, (UINT)ppshpage->pszHeaderSubTitle,
3210                                       szBuffer, 256);
3211                 if (nLength != 0)
3212                 {
3213                     DrawTextW(hdc, szBuffer, nLength,
3214                               &r, DT_LEFT | DT_SINGLELINE);
3215                 }
3216             }
3217         }
3218
3219         offsety = rzone.bottom + 2;
3220
3221         SetTextColor(hdc, clrOld);
3222         SetBkMode(hdc, oldBkMode);
3223         SelectObject(hdc, hOldFont);
3224         SelectObject(hdcSrc, hbmp);
3225     }
3226
3227     if ( (ppshpage->dwFlags & PSP_HIDEHEADER) &&
3228          (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
3229          (psInfo->ppshheader.dwFlags & PSH_WATERMARK) ) 
3230     {
3231         HWND hwndLine = GetDlgItem(hwnd, IDC_SUNKEN_LINE);          
3232
3233         GetClientRect(hwndLine, &r);
3234         MapWindowPoints(hwndLine, hwnd, (LPPOINT) &r, 2);
3235
3236         rzone.left = 0;
3237         rzone.top = 0;
3238         rzone.right = r.right;
3239         rzone.bottom = r.top - 1;
3240
3241         hbr = GetSysColorBrush(COLOR_WINDOW);
3242         FillRect(hdc, &rzone, hbr);
3243
3244         GetObjectA(psInfo->ppshheader.u4.hbmWatermark, sizeof(BITMAP), (LPVOID)&bm);
3245         hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u4.hbmWatermark);
3246
3247         BitBlt(hdc, 0, offsety, min(bm.bmWidth, r.right),
3248                min(bm.bmHeight, r.bottom), hdcSrc, 0, 0, SRCCOPY);
3249
3250         /* If the bitmap is not big enough, fill the remaining area
3251            with the color of pixel (0,0) of bitmap - see MSDN */
3252         if (r.top > bm.bmHeight) {
3253             r.bottom = r.top - 1;
3254             r.top = bm.bmHeight;
3255             r.left = 0;
3256             r.right = bm.bmWidth;
3257             hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
3258             FillRect(hdc, &r, hbr);
3259             DeleteObject(hbr);
3260         }
3261
3262         SelectObject(hdcSrc, hbmp);         
3263     }
3264
3265     if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
3266         SelectPalette(hdc, hOldPal, FALSE);
3267
3268     DeleteDC(hdcSrc);
3269
3270     EndPaint(hwnd, &ps);
3271
3272     return 0;
3273 }
3274
3275 /******************************************************************************
3276  *            PROPSHEET_DialogProc
3277  */
3278 INT_PTR CALLBACK
3279 PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3280 {
3281   TRACE("hwnd=%p msg=0x%04x wparam=%x lparam=%lx\n",
3282         hwnd, uMsg, wParam, lParam);
3283
3284   switch (uMsg)
3285   {
3286     case WM_INITDIALOG:
3287     {
3288       PropSheetInfo* psInfo = (PropSheetInfo*) lParam;
3289       WCHAR* strCaption = (WCHAR*)Alloc(MAX_CAPTION_LENGTH*sizeof(WCHAR));
3290       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
3291       LPCPROPSHEETPAGEW ppshpage;
3292       int idx;
3293       LOGFONTA logFont;
3294
3295       /* Using PropSheetInfoStr to store extra data doesn't match the native
3296        * common control: native uses TCM_[GS]ETITEM
3297        */
3298       SetPropW(hwnd, PropSheetInfoStr, (HANDLE)psInfo);
3299
3300       /*
3301        * psInfo->hwnd is not being used by WINE code - it exists
3302        * for compatibility with "real" Windoze. The same about
3303        * SetWindowLongPtr - WINE is only using the PropSheetInfoStr
3304        * property.
3305        */
3306       psInfo->hwnd = hwnd;
3307       SetWindowLongPtrW(hwnd, DWLP_USER, (DWORD_PTR)psInfo);
3308
3309       /* set up the Next and Back buttons by default */
3310       PROPSHEET_SetWizButtons(hwnd, PSWIZB_BACK|PSWIZB_NEXT);
3311
3312       /* Set up fonts */
3313       SystemParametersInfoA (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
3314       psInfo->hFont = CreateFontIndirectA (&logFont);
3315       logFont.lfWeight = FW_BOLD;
3316       psInfo->hFontBold = CreateFontIndirectA (&logFont);
3317       
3318       /*
3319        * Small icon in the title bar.
3320        */
3321       if ((psInfo->ppshheader.dwFlags & PSH_USEICONID) ||
3322           (psInfo->ppshheader.dwFlags & PSH_USEHICON))
3323       {
3324         HICON hIcon;
3325         int icon_cx = GetSystemMetrics(SM_CXSMICON);
3326         int icon_cy = GetSystemMetrics(SM_CYSMICON);
3327
3328         if (psInfo->ppshheader.dwFlags & PSH_USEICONID)
3329           hIcon = LoadImageW(psInfo->ppshheader.hInstance,
3330                              psInfo->ppshheader.u.pszIcon,
3331                              IMAGE_ICON,
3332                              icon_cx, icon_cy,
3333                              LR_DEFAULTCOLOR);
3334         else
3335           hIcon = psInfo->ppshheader.u.hIcon;
3336
3337         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)hIcon);
3338       }
3339
3340       if (psInfo->ppshheader.dwFlags & PSH_USEHICON)
3341         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)psInfo->ppshheader.u.hIcon);
3342
3343       psInfo->strPropertiesFor = strCaption;
3344
3345       GetWindowTextW(hwnd, psInfo->strPropertiesFor, MAX_CAPTION_LENGTH);
3346
3347       PROPSHEET_CreateTabControl(hwnd, psInfo);
3348
3349       PROPSHEET_LoadWizardBitmaps(psInfo);
3350
3351       if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
3352       {
3353         ShowWindow(hwndTabCtrl, SW_HIDE);
3354         PROPSHEET_AdjustSizeWizard(hwnd, psInfo);
3355         PROPSHEET_AdjustButtonsWizard(hwnd, psInfo);
3356       }
3357       else
3358       {
3359         if (PROPSHEET_SizeMismatch(hwnd, psInfo))
3360         {
3361           PROPSHEET_AdjustSize(hwnd, psInfo);
3362           PROPSHEET_AdjustButtons(hwnd, psInfo);
3363         }
3364       }
3365
3366       if (psInfo->useCallback)
3367              (*(psInfo->ppshheader.pfnCallback))(hwnd,
3368                                               PSCB_INITIALIZED, (LPARAM)0);
3369
3370       idx = psInfo->active_page;
3371       ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[idx].hpage;
3372       psInfo->active_page = -1;
3373
3374       PROPSHEET_SetCurSel(hwnd, idx, 1, psInfo->proppage[idx].hpage);
3375
3376       /* doing TCM_SETCURSEL seems to be needed even in case of PSH_WIZARD,
3377        * as some programs call TCM_GETCURSEL to get the current selection
3378        * from which to switch to the next page */
3379       SendMessageW(hwndTabCtrl, TCM_SETCURSEL, psInfo->active_page, 0);
3380
3381       if (!HIWORD(psInfo->ppshheader.pszCaption) &&
3382               psInfo->ppshheader.hInstance)
3383       {
3384          WCHAR szText[256];
3385
3386          if (LoadStringW(psInfo->ppshheader.hInstance,
3387                  (UINT)psInfo->ppshheader.pszCaption, szText, 255))
3388             PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags, szText);
3389       }
3390       else
3391       {
3392          PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags,
3393                          psInfo->ppshheader.pszCaption);
3394       }
3395
3396       return TRUE;
3397     }
3398
3399     case WM_PAINT:
3400       PROPSHEET_Paint(hwnd);
3401       return TRUE;
3402
3403     case WM_DESTROY:
3404       PROPSHEET_CleanUp(hwnd);
3405       return TRUE;
3406
3407     case WM_CLOSE:
3408       PROPSHEET_Cancel(hwnd, 1);
3409       return TRUE;
3410
3411     case WM_COMMAND:
3412       if (!PROPSHEET_DoCommand(hwnd, LOWORD(wParam)))
3413       {
3414           PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
3415
3416           if (!psInfo)
3417               return FALSE;
3418
3419           /* No default handler, forward notification to active page */
3420           if (psInfo->activeValid && psInfo->active_page != -1)
3421           {
3422              HWND hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
3423              SendMessageW(hwndPage, WM_COMMAND, wParam, lParam);
3424           }
3425       }
3426       return TRUE;
3427
3428     case WM_NOTIFY:
3429     {
3430       NMHDR* pnmh = (LPNMHDR) lParam;
3431
3432       if (pnmh->code == TCN_SELCHANGE)
3433       {
3434         int index = SendMessageW(pnmh->hwndFrom, TCM_GETCURSEL, 0, 0);
3435         PROPSHEET_SetCurSel(hwnd, index, 1, 0);
3436       }
3437
3438       if(pnmh->code == TCN_SELCHANGING)
3439       {
3440         BOOL bRet = PROPSHEET_CanSetCurSel(hwnd);
3441         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, !bRet);
3442         return TRUE;
3443       }
3444
3445       return FALSE;
3446     }
3447
3448     case PSM_GETCURRENTPAGEHWND:
3449     {
3450       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3451                                                         PropSheetInfoStr);
3452       HWND hwndPage = 0;
3453
3454       if (!psInfo)
3455         return FALSE;
3456
3457       if (psInfo->activeValid && psInfo->active_page != -1)
3458         hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
3459
3460       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndPage);
3461
3462       return TRUE;
3463     }
3464
3465     case PSM_CHANGED:
3466       PROPSHEET_Changed(hwnd, (HWND)wParam);
3467       return TRUE;
3468
3469     case PSM_UNCHANGED:
3470       PROPSHEET_UnChanged(hwnd, (HWND)wParam);
3471       return TRUE;
3472
3473     case PSM_GETTABCONTROL:
3474     {
3475       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
3476
3477       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndTabCtrl);
3478
3479       return TRUE;
3480     }
3481
3482     case PSM_SETCURSEL:
3483     {
3484       BOOL msgResult;
3485
3486       msgResult = PROPSHEET_CanSetCurSel(hwnd);
3487       if(msgResult != FALSE)
3488       {
3489         msgResult = PROPSHEET_SetCurSel(hwnd,
3490                                        (int)wParam,
3491                                        1,
3492                                        (HPROPSHEETPAGE)lParam);
3493       }
3494
3495       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3496
3497       return TRUE;
3498     }
3499
3500     case PSM_CANCELTOCLOSE:
3501     {
3502       WCHAR buf[MAX_BUTTONTEXT_LENGTH];
3503       HWND hwndOK = GetDlgItem(hwnd, IDOK);
3504       HWND hwndCancel = GetDlgItem(hwnd, IDCANCEL);
3505
3506       EnableWindow(hwndCancel, FALSE);
3507       if (LoadStringW(COMCTL32_hModule, IDS_CLOSE, buf, sizeof(buf)))
3508          SetWindowTextW(hwndOK, buf);
3509
3510       return FALSE;
3511     }
3512
3513     case PSM_RESTARTWINDOWS:
3514     {
3515       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3516                                                         PropSheetInfoStr);
3517
3518       if (!psInfo)
3519         return FALSE;
3520
3521       psInfo->restartWindows = TRUE;
3522       return TRUE;
3523     }
3524
3525     case PSM_REBOOTSYSTEM:
3526     {
3527       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3528                                                         PropSheetInfoStr);
3529
3530       if (!psInfo)
3531         return FALSE;
3532
3533       psInfo->rebootSystem = TRUE;
3534       return TRUE;
3535     }
3536
3537     case PSM_SETTITLEA:
3538       PROPSHEET_SetTitleA(hwnd, (DWORD) wParam, (LPCSTR) lParam);
3539       return TRUE;
3540
3541     case PSM_SETTITLEW:
3542       PROPSHEET_SetTitleW(hwnd, (DWORD) wParam, (LPCWSTR) lParam);
3543       return TRUE;
3544
3545     case PSM_APPLY:
3546     {
3547       BOOL msgResult = PROPSHEET_Apply(hwnd, 0);
3548
3549       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3550
3551       return TRUE;
3552     }
3553
3554     case PSM_QUERYSIBLINGS:
3555     {
3556       LRESULT msgResult = PROPSHEET_QuerySiblings(hwnd, wParam, lParam);
3557
3558       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3559
3560       return TRUE;
3561     }
3562
3563     case PSM_ADDPAGE:
3564     {
3565       /*
3566        * Note: MSVC++ 6.0 documentation says that PSM_ADDPAGE does not have
3567        *       a return value. This is not true. PSM_ADDPAGE returns TRUE
3568        *       on success or FALSE otherwise, as specified on MSDN Online.
3569        *       Also see the MFC code for
3570        *       CPropertySheet::AddPage(CPropertyPage* pPage).
3571        */
3572
3573       BOOL msgResult = PROPSHEET_AddPage(hwnd, (HPROPSHEETPAGE)lParam);
3574
3575       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3576
3577       return TRUE;
3578     }
3579
3580     case PSM_REMOVEPAGE:
3581       PROPSHEET_RemovePage(hwnd, (int)wParam, (HPROPSHEETPAGE)lParam);
3582       return TRUE;
3583
3584     case PSM_ISDIALOGMESSAGE:
3585     {
3586        BOOL msgResult = PROPSHEET_IsDialogMessage(hwnd, (LPMSG)lParam);
3587        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3588        return TRUE;
3589     }
3590
3591     case PSM_PRESSBUTTON:
3592       PROPSHEET_PressButton(hwnd, (int)wParam);
3593       return TRUE;
3594
3595     case PSM_SETFINISHTEXTA:
3596       PROPSHEET_SetFinishTextA(hwnd, (LPCSTR) lParam);
3597       return TRUE;
3598
3599     case PSM_SETWIZBUTTONS:
3600       PROPSHEET_SetWizButtons(hwnd, (DWORD)lParam);
3601       return TRUE;
3602
3603     case PSM_SETCURSELID:
3604         PROPSHEET_SetCurSelId(hwnd, (int)lParam);
3605         return TRUE;
3606
3607     case PSM_SETFINISHTEXTW:
3608         PROPSHEET_SetFinishTextW(hwnd, (LPCWSTR) lParam);
3609         return FALSE;
3610
3611     case PSM_INSERTPAGE:
3612     {
3613         BOOL msgResult = PROPSHEET_InsertPage(hwnd, (HPROPSHEETPAGE)wParam, (HPROPSHEETPAGE)lParam);
3614         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3615         return TRUE;
3616     }
3617
3618     case PSM_SETHEADERTITLEW:
3619         PROPSHEET_SetHeaderTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
3620         return TRUE;
3621
3622     case PSM_SETHEADERTITLEA:
3623         PROPSHEET_SetHeaderTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
3624         return TRUE;
3625
3626     case PSM_SETHEADERSUBTITLEW:
3627         PROPSHEET_SetHeaderSubTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
3628         return TRUE;
3629
3630     case PSM_SETHEADERSUBTITLEA:
3631         PROPSHEET_SetHeaderSubTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
3632         return TRUE;
3633
3634     case PSM_HWNDTOINDEX:
3635     {
3636         LRESULT msgResult = PROPSHEET_HwndToIndex(hwnd, (HWND)wParam);
3637         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3638         return TRUE;
3639     }
3640
3641     case PSM_INDEXTOHWND:
3642     {
3643         LRESULT msgResult = PROPSHEET_IndexToHwnd(hwnd, (int)wParam);
3644         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3645         return TRUE;
3646     }
3647
3648     case PSM_PAGETOINDEX:
3649     {
3650         LRESULT msgResult = PROPSHEET_PageToIndex(hwnd, (HPROPSHEETPAGE)wParam);
3651         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3652         return TRUE;
3653     }
3654
3655     case PSM_INDEXTOPAGE:
3656     {
3657         LRESULT msgResult = PROPSHEET_IndexToPage(hwnd, (int)wParam);
3658         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3659         return TRUE;
3660     }
3661
3662     case PSM_IDTOINDEX:
3663     {
3664         LRESULT msgResult = PROPSHEET_IdToIndex(hwnd, (int)lParam);
3665         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3666         return TRUE;
3667     }
3668
3669     case PSM_INDEXTOID:
3670     {
3671         LRESULT msgResult = PROPSHEET_IndexToId(hwnd, (int)wParam);
3672         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3673         return TRUE;
3674     }
3675
3676     case PSM_GETRESULT:
3677     {
3678         LRESULT msgResult = PROPSHEET_GetResult(hwnd);
3679         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3680         return TRUE;
3681     }
3682
3683     case PSM_RECALCPAGESIZES:
3684     {
3685         LRESULT msgResult = PROPSHEET_RecalcPageSizes(hwnd);
3686         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3687         return TRUE;
3688     }
3689
3690     default:
3691       return FALSE;
3692   }
3693
3694   return FALSE;
3695 }