- Implement IPersistFolder2.
[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 = Alloc(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 = Alloc( 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 = Alloc( (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   SendMessageW(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 = SendMessageW(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 (SendMessageW(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 (SendMessageW(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      SendMessageW(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 (SendMessageW(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        SendMessageW(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   SendMessageW(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 = !SendMessageW(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   SendMessageW(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, PropSheetInfoStr);
2271
2272   while ((i < psInfo->nPages) && (msgResult == 0))
2273   {
2274     hwndPage = psInfo->proppage[i].hwndPage;
2275     msgResult = SendMessageW(hwndPage, PSM_QUERYSIBLINGS, wParam, lParam);
2276     i++;
2277   }
2278
2279   return msgResult;
2280 }
2281
2282
2283 /******************************************************************************
2284  *            PROPSHEET_AddPage
2285  */
2286 static BOOL PROPSHEET_AddPage(HWND hwndDlg,
2287                               HPROPSHEETPAGE hpage)
2288 {
2289   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2290                                                      PropSheetInfoStr);
2291   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2292   TCITEMW item;
2293   LPCPROPSHEETPAGEW ppsp = (LPCPROPSHEETPAGEW)hpage;
2294
2295   TRACE("hpage %p\n", hpage);
2296   /*
2297    * Allocate and fill in a new PropPageInfo entry.
2298    */
2299   psInfo->proppage = (PropPageInfo*) ReAlloc(psInfo->proppage,
2300                                                       sizeof(PropPageInfo) *
2301                                                       (psInfo->nPages + 1));
2302   if (!PROPSHEET_CollectPageInfo(ppsp, psInfo, psInfo->nPages))
2303       return FALSE;
2304
2305   psInfo->proppage[psInfo->nPages].hpage = hpage;
2306
2307   if (ppsp->dwFlags & PSP_PREMATURE)
2308   {
2309      /* Create the page but don't show it */
2310      PROPSHEET_CreatePage(hwndDlg, psInfo->nPages, psInfo, ppsp);
2311   }
2312
2313   /*
2314    * Add a new tab to the tab control.
2315    */
2316   item.mask = TCIF_TEXT;
2317   item.pszText = (LPWSTR) psInfo->proppage[psInfo->nPages].pszText;
2318   item.cchTextMax = MAX_TABTEXT_LENGTH;
2319
2320   if (psInfo->hImageList)
2321   {
2322     SendMessageW(hwndTabControl, TCM_SETIMAGELIST, 0, (LPARAM)psInfo->hImageList);
2323   }
2324
2325   if ( psInfo->proppage[psInfo->nPages].hasIcon )
2326   {
2327     item.mask |= TCIF_IMAGE;
2328     item.iImage = psInfo->nPages;
2329   }
2330
2331   SendMessageW(hwndTabControl, TCM_INSERTITEMW, psInfo->nPages + 1,
2332                (LPARAM)&item);
2333
2334   psInfo->nPages++;
2335
2336   /* If it is the only page - show it */
2337   if(psInfo->nPages == 1)
2338      PROPSHEET_SetCurSel(hwndDlg, 0, 1, 0);
2339   return TRUE;
2340 }
2341
2342 /******************************************************************************
2343  *            PROPSHEET_RemovePage
2344  */
2345 static BOOL PROPSHEET_RemovePage(HWND hwndDlg,
2346                                  int index,
2347                                  HPROPSHEETPAGE hpage)
2348 {
2349   PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2350                                                      PropSheetInfoStr);
2351   HWND hwndTabControl = GetDlgItem(hwndDlg, IDC_TABCONTROL);
2352   PropPageInfo* oldPages;
2353
2354   TRACE("index %d, hpage %p\n", index, hpage);
2355   if (!psInfo) {
2356     return FALSE;
2357   }
2358   /*
2359    * hpage takes precedence over index.
2360    */
2361   if (hpage != 0)
2362   {
2363     index = PROPSHEET_GetPageIndex(hpage, psInfo);
2364   }
2365
2366   /* Make sure that index is within range */
2367   if (index < 0 || index >= psInfo->nPages)
2368   {
2369       TRACE("Could not find page to remove!\n");
2370       return FALSE;
2371   }
2372
2373   TRACE("total pages %d removing page %d active page %d\n",
2374         psInfo->nPages, index, psInfo->active_page);
2375   /*
2376    * Check if we're removing the active page.
2377    */
2378   if (index == psInfo->active_page)
2379   {
2380     if (psInfo->nPages > 1)
2381     {
2382       if (index > 0)
2383       {
2384         /* activate previous page  */
2385         PROPSHEET_SetCurSel(hwndDlg, index - 1, -1, 0);
2386       }
2387       else
2388       {
2389         /* activate the next page */
2390         PROPSHEET_SetCurSel(hwndDlg, index + 1, 1, 0);
2391         psInfo->active_page = index;
2392       }
2393     }
2394     else
2395     {
2396       psInfo->active_page = -1;
2397       if (!psInfo->isModeless)
2398       {
2399          EndDialog(hwndDlg, FALSE);
2400          return TRUE;
2401       }
2402     }
2403   }
2404   else if (index < psInfo->active_page)
2405     psInfo->active_page--;
2406
2407   /* Unsubclass the page dialog window */
2408   if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD) &&
2409      (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2410      ((PROPSHEETPAGEW*)psInfo->proppage[index].hpage)->dwFlags & PSP_HIDEHEADER))
2411   {
2412      RemoveWindowSubclass(psInfo->proppage[index].hwndPage,
2413                           PROPSHEET_WizardSubclassProc, 1);
2414   }
2415
2416   /* Destroy page dialog window */
2417   DestroyWindow(psInfo->proppage[index].hwndPage);
2418
2419   /* Free page resources */
2420   if(psInfo->proppage[index].hpage)
2421   {
2422      PROPSHEETPAGEW* psp = (PROPSHEETPAGEW*)psInfo->proppage[index].hpage;
2423
2424      if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[index].pszText)
2425         Free ((LPVOID)psInfo->proppage[index].pszText);
2426
2427      DestroyPropertySheetPage(psInfo->proppage[index].hpage);
2428   }
2429
2430   /* Remove the tab */
2431   SendMessageW(hwndTabControl, TCM_DELETEITEM, index, 0);
2432
2433   oldPages = psInfo->proppage;
2434   psInfo->nPages--;
2435   psInfo->proppage = Alloc(sizeof(PropPageInfo) * psInfo->nPages);
2436
2437   if (index > 0)
2438     memcpy(&psInfo->proppage[0], &oldPages[0], index * sizeof(PropPageInfo));
2439
2440   if (index < psInfo->nPages)
2441     memcpy(&psInfo->proppage[index], &oldPages[index + 1],
2442            (psInfo->nPages - index) * sizeof(PropPageInfo));
2443
2444   Free(oldPages);
2445
2446   return FALSE;
2447 }
2448
2449 /******************************************************************************
2450  *            PROPSHEET_SetWizButtons
2451  *
2452  * This code will work if (and assumes that) the Next button is on top of the
2453  * Finish button. ie. Finish comes after Next in the Z order.
2454  * This means make sure the dialog template reflects this.
2455  *
2456  */
2457 static void PROPSHEET_SetWizButtons(HWND hwndDlg, DWORD dwFlags)
2458 {
2459   HWND hwndBack   = GetDlgItem(hwndDlg, IDC_BACK_BUTTON);
2460   HWND hwndNext   = GetDlgItem(hwndDlg, IDC_NEXT_BUTTON);
2461   HWND hwndFinish = GetDlgItem(hwndDlg, IDC_FINISH_BUTTON);
2462
2463   TRACE("%ld\n", dwFlags);
2464
2465   EnableWindow(hwndBack, FALSE);
2466   EnableWindow(hwndNext, FALSE);
2467   EnableWindow(hwndFinish, FALSE);
2468
2469   if (dwFlags & PSWIZB_BACK)
2470     EnableWindow(hwndBack, TRUE);
2471
2472   if (dwFlags & PSWIZB_NEXT)
2473   {
2474     /* Hide the Finish button */
2475     ShowWindow(hwndFinish, SW_HIDE);
2476
2477     /* Show and enable the Next button */
2478     ShowWindow(hwndNext, SW_SHOW);
2479     EnableWindow(hwndNext, TRUE);
2480
2481     /* Set the Next button as the default pushbutton  */
2482     SendMessageW(hwndDlg, DM_SETDEFID, IDC_NEXT_BUTTON, 0);
2483   }
2484
2485   if ((dwFlags & PSWIZB_FINISH) || (dwFlags & PSWIZB_DISABLEDFINISH))
2486   {
2487     /* Hide the Next button */
2488     ShowWindow(hwndNext, SW_HIDE);
2489
2490     /* Show the Finish button */
2491     ShowWindow(hwndFinish, SW_SHOW);
2492
2493     if (dwFlags & PSWIZB_FINISH)
2494       EnableWindow(hwndFinish, TRUE);
2495
2496     /* Set the Finish button as the default pushbutton  */
2497     SendMessageW(hwndDlg, DM_SETDEFID, IDC_FINISH_BUTTON, 0);
2498   }
2499 }
2500
2501 /******************************************************************************
2502  *            PROPSHEET_InsertPage
2503  */
2504 static BOOL PROPSHEET_InsertPage(HWND hwndDlg, HPROPSHEETPAGE hpageInsertAfter, HPROPSHEETPAGE hpage)
2505 {
2506     if (!HIWORD(hpageInsertAfter))
2507         FIXME("(%p, %d, %p): stub\n", hwndDlg, LOWORD(hpageInsertAfter), hpage);
2508     else
2509         FIXME("(%p, %p, %p): stub\n", hwndDlg, hpageInsertAfter, hpage);
2510     return FALSE;
2511 }
2512
2513 /******************************************************************************
2514  *            PROPSHEET_SetHeaderTitleW
2515  */
2516 static void PROPSHEET_SetHeaderTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderTitle)
2517 {
2518     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderTitle));
2519 }
2520
2521 /******************************************************************************
2522  *            PROPSHEET_SetHeaderTitleA
2523  */
2524 static void PROPSHEET_SetHeaderTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderTitle)
2525 {
2526     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderTitle));
2527 }
2528
2529 /******************************************************************************
2530  *            PROPSHEET_SetHeaderSubTitleW
2531  */
2532 static void PROPSHEET_SetHeaderSubTitleW(HWND hwndDlg, int iPageIndex, LPCWSTR pszHeaderSubTitle)
2533 {
2534     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_w(pszHeaderSubTitle));
2535 }
2536
2537 /******************************************************************************
2538  *            PROPSHEET_SetHeaderSubTitleA
2539  */
2540 static void PROPSHEET_SetHeaderSubTitleA(HWND hwndDlg, int iPageIndex, LPCSTR pszHeaderSubTitle)
2541 {
2542     FIXME("(%p, %d, %s): stub\n", hwndDlg, iPageIndex, debugstr_a(pszHeaderSubTitle));
2543 }
2544
2545 /******************************************************************************
2546  *            PROPSHEET_HwndToIndex
2547  */
2548 static LRESULT PROPSHEET_HwndToIndex(HWND hwndDlg, HWND hPageDlg)
2549 {
2550     int index;
2551     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2552                                                        PropSheetInfoStr);
2553
2554     TRACE("(%p, %p)\n", hwndDlg, hPageDlg);
2555
2556     for (index = 0; index < psInfo->nPages; index++)
2557         if (psInfo->proppage[index].hwndPage == hPageDlg)
2558             return index;
2559     WARN("%p not found\n", hPageDlg);
2560     return -1;
2561 }
2562
2563 /******************************************************************************
2564  *            PROPSHEET_IndexToHwnd
2565  */
2566 static LRESULT PROPSHEET_IndexToHwnd(HWND hwndDlg, int iPageIndex)
2567 {
2568     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2569                                                        PropSheetInfoStr);
2570     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2571     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2572         WARN("%d out of range.\n", iPageIndex);
2573         return 0;
2574     }
2575     return (LRESULT)psInfo->proppage[iPageIndex].hwndPage;
2576 }
2577
2578 /******************************************************************************
2579  *            PROPSHEET_PageToIndex
2580  */
2581 static LRESULT PROPSHEET_PageToIndex(HWND hwndDlg, HPROPSHEETPAGE hPage)
2582 {
2583     int index;
2584     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2585                                                        PropSheetInfoStr);
2586
2587     TRACE("(%p, %p)\n", hwndDlg, hPage);
2588
2589     for (index = 0; index < psInfo->nPages; index++)
2590         if (psInfo->proppage[index].hpage == hPage)
2591             return index;
2592     WARN("%p not found\n", hPage);
2593     return -1;
2594 }
2595
2596 /******************************************************************************
2597  *            PROPSHEET_IndexToPage
2598  */
2599 static LRESULT PROPSHEET_IndexToPage(HWND hwndDlg, int iPageIndex)
2600 {
2601     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2602                                                        PropSheetInfoStr);
2603     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2604     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2605         WARN("%d out of range.\n", iPageIndex);
2606         return 0;
2607     }
2608     return (LRESULT)psInfo->proppage[iPageIndex].hpage;
2609 }
2610
2611 /******************************************************************************
2612  *            PROPSHEET_IdToIndex
2613  */
2614 static LRESULT PROPSHEET_IdToIndex(HWND hwndDlg, int iPageId)
2615 {
2616     FIXME("(%p, %d): stub\n", hwndDlg, iPageId);
2617     return -1;
2618 }
2619
2620 /******************************************************************************
2621  *            PROPSHEET_IndexToId
2622  */
2623 static LRESULT PROPSHEET_IndexToId(HWND hwndDlg, int iPageIndex)
2624 {
2625     PropSheetInfo * psInfo = (PropSheetInfo*) GetPropW(hwndDlg,
2626                                                        PropSheetInfoStr);
2627     LPCPROPSHEETPAGEW psp;
2628     TRACE("(%p, %d)\n", hwndDlg, iPageIndex);
2629     if (iPageIndex<0 || iPageIndex>=psInfo->nPages) {
2630         WARN("%d out of range.\n", iPageIndex);
2631         return 0;
2632     }
2633     psp = (LPCPROPSHEETPAGEW)psInfo->proppage[iPageIndex].hpage;
2634     if (psp->dwFlags & PSP_DLGINDIRECT || HIWORD(psp->u.pszTemplate)) {
2635         return 0;
2636     }
2637     return (LRESULT)psp->u.pszTemplate;
2638 }
2639
2640 /******************************************************************************
2641  *            PROPSHEET_GetResult
2642  */
2643 static LRESULT PROPSHEET_GetResult(HWND hwndDlg)
2644 {
2645     FIXME("(%p): stub\n", hwndDlg);
2646     return -1;
2647 }
2648
2649 /******************************************************************************
2650  *            PROPSHEET_RecalcPageSizes
2651  */
2652 static BOOL PROPSHEET_RecalcPageSizes(HWND hwndDlg)
2653 {
2654     FIXME("(%p): stub\n", hwndDlg);
2655     return FALSE;
2656 }
2657
2658 /******************************************************************************
2659  *            PROPSHEET_GetPageIndex
2660  *
2661  * Given a HPROPSHEETPAGE, returns the index of the corresponding page from
2662  * the array of PropPageInfo.
2663  */
2664 static int PROPSHEET_GetPageIndex(HPROPSHEETPAGE hpage, PropSheetInfo* psInfo)
2665 {
2666   BOOL found = FALSE;
2667   int index = 0;
2668
2669   TRACE("hpage %p\n", hpage);
2670   while ((index < psInfo->nPages) && (found == FALSE))
2671   {
2672     if (psInfo->proppage[index].hpage == hpage)
2673       found = TRUE;
2674     else
2675       index++;
2676   }
2677
2678   if (found == FALSE)
2679     index = -1;
2680
2681   return index;
2682 }
2683
2684 /******************************************************************************
2685  *            PROPSHEET_CleanUp
2686  */
2687 static void PROPSHEET_CleanUp(HWND hwndDlg)
2688 {
2689   int i;
2690   PropSheetInfo* psInfo = (PropSheetInfo*) RemovePropW(hwndDlg,
2691                                                        PropSheetInfoStr);
2692
2693   TRACE("\n");
2694   if (!psInfo) return;
2695   if (HIWORD(psInfo->ppshheader.pszCaption))
2696       Free ((LPVOID)psInfo->ppshheader.pszCaption);
2697
2698   for (i = 0; i < psInfo->nPages; i++)
2699   {
2700      PROPSHEETPAGEA* psp = (PROPSHEETPAGEA*)psInfo->proppage[i].hpage;
2701
2702      /* Unsubclass the page dialog window */
2703      if((psInfo->ppshheader.dwFlags & (PSH_WIZARD97_NEW | PSH_WIZARD97_OLD)) &&
2704         (psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2705         (psp->dwFlags & PSP_HIDEHEADER))
2706      {
2707         RemoveWindowSubclass(psInfo->proppage[i].hwndPage,
2708                              PROPSHEET_WizardSubclassProc, 1);
2709      }
2710
2711      if(psInfo->proppage[i].hwndPage)
2712         DestroyWindow(psInfo->proppage[i].hwndPage);
2713
2714      if(psp)
2715      {
2716         if ((psp->dwFlags & PSP_USETITLE) && psInfo->proppage[i].pszText)
2717            Free ((LPVOID)psInfo->proppage[i].pszText);
2718
2719         DestroyPropertySheetPage(psInfo->proppage[i].hpage);
2720      }
2721   }
2722
2723   DeleteObject(psInfo->hFont);
2724   DeleteObject(psInfo->hFontBold);
2725   /* If we created the bitmaps, destroy them */
2726   if ((psInfo->ppshheader.dwFlags & PSH_WATERMARK) &&
2727       (!(psInfo->ppshheader.dwFlags & PSH_USEHBMWATERMARK)) )
2728       DeleteObject(psInfo->ppshheader.u4.hbmWatermark);
2729   if ((psInfo->ppshheader.dwFlags & PSH_HEADER) &&
2730       (!(psInfo->ppshheader.dwFlags & PSH_USEHBMHEADER)) )
2731       DeleteObject(psInfo->ppshheader.u5.hbmHeader);
2732
2733   Free(psInfo->proppage);
2734   Free(psInfo->strPropertiesFor);
2735   ImageList_Destroy(psInfo->hImageList);
2736
2737   GlobalFree((HGLOBAL)psInfo);
2738 }
2739
2740 /******************************************************************************
2741  *            PropertySheet    (COMCTL32.@)
2742  *            PropertySheetA   (COMCTL32.@)
2743  *
2744  * Creates a property sheet in the specified property sheet header.
2745  *
2746  * RETURNS
2747  *     Modal property sheets: Positive if successful or -1 otherwise.
2748  *     Modeless property sheets: Property sheet handle.
2749  *     Or:
2750  *| ID_PSREBOOTSYSTEM - The user must reboot the computer for the changes to take effect.
2751  *| ID_PSRESTARTWINDOWS - The user must restart Windows for the changes to take effect.
2752  */
2753 INT WINAPI PropertySheetA(LPCPROPSHEETHEADERA lppsh)
2754 {
2755   int bRet = 0;
2756   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2757                                                        sizeof(PropSheetInfo));
2758   UINT i, n;
2759   BYTE* pByte;
2760
2761   TRACE("(%p)\n", lppsh);
2762
2763   PROPSHEET_CollectSheetInfoA(lppsh, psInfo);
2764
2765   psInfo->proppage = (PropPageInfo*) Alloc(sizeof(PropPageInfo) *
2766                                                     lppsh->nPages);
2767   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2768
2769   for (n = i = 0; i < lppsh->nPages; i++, n++)
2770   {
2771     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2772       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2773     else
2774     {
2775        psInfo->proppage[n].hpage = CreatePropertySheetPageA((LPCPROPSHEETPAGEA)pByte);
2776        pByte += ((LPPROPSHEETPAGEA)pByte)->dwSize;
2777     }
2778
2779     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2780                                psInfo, n))
2781     {
2782         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2783             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2784         n--;
2785         psInfo->nPages--;
2786     }
2787   }
2788
2789   psInfo->unicode = FALSE;
2790   bRet = PROPSHEET_CreateDialog(psInfo);
2791
2792   return bRet;
2793 }
2794
2795 /******************************************************************************
2796  *            PropertySheetW   (COMCTL32.@)
2797  *
2798  * See PropertySheetA.
2799  */
2800 INT WINAPI PropertySheetW(LPCPROPSHEETHEADERW lppsh)
2801 {
2802   int bRet = 0;
2803   PropSheetInfo* psInfo = (PropSheetInfo*) GlobalAlloc(GPTR,
2804                                                        sizeof(PropSheetInfo));
2805   UINT i, n;
2806   BYTE* pByte;
2807
2808   TRACE("(%p)\n", lppsh);
2809
2810   PROPSHEET_CollectSheetInfoW(lppsh, psInfo);
2811
2812   psInfo->proppage = (PropPageInfo*) Alloc(sizeof(PropPageInfo) *
2813                                                     lppsh->nPages);
2814   pByte = (BYTE*) psInfo->ppshheader.u3.ppsp;
2815
2816   for (n = i = 0; i < lppsh->nPages; i++, n++)
2817   {
2818     if (!(lppsh->dwFlags & PSH_PROPSHEETPAGE))
2819       psInfo->proppage[n].hpage = psInfo->ppshheader.u3.phpage[i];
2820     else
2821     {
2822        psInfo->proppage[n].hpage = CreatePropertySheetPageW((LPCPROPSHEETPAGEW)pByte);
2823        pByte += ((LPPROPSHEETPAGEW)pByte)->dwSize;
2824     }
2825
2826     if (!PROPSHEET_CollectPageInfo((LPCPROPSHEETPAGEW)psInfo->proppage[n].hpage,
2827                                psInfo, n))
2828     {
2829         if (lppsh->dwFlags & PSH_PROPSHEETPAGE)
2830             DestroyPropertySheetPage(psInfo->proppage[n].hpage);
2831         n--;
2832         psInfo->nPages--;
2833     }
2834   }
2835
2836   psInfo->unicode = TRUE;
2837   bRet = PROPSHEET_CreateDialog(psInfo);
2838
2839   return bRet;
2840 }
2841
2842 /******************************************************************************
2843  *            CreatePropertySheetPage    (COMCTL32.@)
2844  *            CreatePropertySheetPageA   (COMCTL32.@)
2845  *
2846  * Creates a new property sheet page.
2847  *
2848  * RETURNS
2849  *     Success: Handle to new property sheet page.
2850  *     Failure: NULL.
2851  *
2852  * NOTES
2853  *     An application must use the PSM_ADDPAGE message to add the new page to
2854  *     an existing property sheet.
2855  */
2856 HPROPSHEETPAGE WINAPI CreatePropertySheetPageA(
2857                           LPCPROPSHEETPAGEA lpPropSheetPage)
2858 {
2859   PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2860
2861   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEA)));
2862
2863   ppsp->dwFlags &= ~ PSP_INTERNAL_UNICODE;
2864   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2865   {
2866      int len = strlen(lpPropSheetPage->u.pszTemplate);
2867
2868      ppsp->u.pszTemplate = Alloc( len+1 );
2869      strcpy( (LPSTR)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2870   }
2871   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2872   {
2873       PROPSHEET_AtoW(&ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon);
2874   }
2875
2876   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2877   {
2878       PROPSHEET_AtoW(&ppsp->pszTitle, lpPropSheetPage->pszTitle);
2879   }
2880   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2881       ppsp->pszTitle = NULL;
2882
2883   return (HPROPSHEETPAGE)ppsp;
2884 }
2885
2886 /******************************************************************************
2887  *            CreatePropertySheetPageW   (COMCTL32.@)
2888  *
2889  * See CreatePropertySheetA.
2890  */
2891 HPROPSHEETPAGE WINAPI CreatePropertySheetPageW(LPCPROPSHEETPAGEW lpPropSheetPage)
2892 {
2893   PROPSHEETPAGEW* ppsp = Alloc(sizeof(PROPSHEETPAGEW));
2894
2895   memcpy(ppsp,lpPropSheetPage,min(lpPropSheetPage->dwSize,sizeof(PROPSHEETPAGEW)));
2896
2897   ppsp->dwFlags |= PSP_INTERNAL_UNICODE;
2898
2899   if ( !(ppsp->dwFlags & PSP_DLGINDIRECT) && HIWORD( ppsp->u.pszTemplate ) )
2900   {
2901     int len = strlenW(lpPropSheetPage->u.pszTemplate);
2902
2903     ppsp->u.pszTemplate = Alloc( (len+1)*sizeof (WCHAR) );
2904     strcpyW( (WCHAR *)ppsp->u.pszTemplate, lpPropSheetPage->u.pszTemplate );
2905   }
2906   if ( (ppsp->dwFlags & PSP_USEICONID) && HIWORD( ppsp->u2.pszIcon ) )
2907   {
2908       int len = strlenW(lpPropSheetPage->u2.pszIcon);
2909       ppsp->u2.pszIcon = Alloc( (len+1)*sizeof (WCHAR) );
2910       strcpyW( (WCHAR *)ppsp->u2.pszIcon, lpPropSheetPage->u2.pszIcon );
2911   }
2912
2913   if ((ppsp->dwFlags & PSP_USETITLE) && HIWORD( ppsp->pszTitle ))
2914   {
2915       int len = strlenW(lpPropSheetPage->pszTitle);
2916       ppsp->pszTitle = Alloc( (len+1)*sizeof (WCHAR) );
2917       strcpyW( (WCHAR *)ppsp->pszTitle, lpPropSheetPage->pszTitle );
2918   }
2919   else if ( !(ppsp->dwFlags & PSP_USETITLE) )
2920       ppsp->pszTitle = NULL;
2921
2922   return (HPROPSHEETPAGE)ppsp;
2923 }
2924
2925 /******************************************************************************
2926  *            DestroyPropertySheetPage   (COMCTL32.@)
2927  *
2928  * Destroys a property sheet page previously created with
2929  * CreatePropertySheetA() or CreatePropertySheetW() and frees the associated
2930  * memory.
2931  *
2932  * RETURNS
2933  *     Success: TRUE
2934  *     Failure: FALSE
2935  */
2936 BOOL WINAPI DestroyPropertySheetPage(HPROPSHEETPAGE hPropPage)
2937 {
2938   PROPSHEETPAGEW *psp = (PROPSHEETPAGEW *)hPropPage;
2939
2940   if (!psp)
2941      return FALSE;
2942
2943   if ( !(psp->dwFlags & PSP_DLGINDIRECT) && HIWORD( psp->u.pszTemplate ) )
2944      Free ((LPVOID)psp->u.pszTemplate);
2945
2946   if ( (psp->dwFlags & PSP_USEICONID) && HIWORD( psp->u2.pszIcon ) )
2947      Free ((LPVOID)psp->u2.pszIcon);
2948
2949   if ((psp->dwFlags & PSP_USETITLE) && HIWORD( psp->pszTitle ))
2950      Free ((LPVOID)psp->pszTitle);
2951
2952   Free(hPropPage);
2953
2954   return TRUE;
2955 }
2956
2957 /******************************************************************************
2958  *            PROPSHEET_IsDialogMessage
2959  */
2960 static BOOL PROPSHEET_IsDialogMessage(HWND hwnd, LPMSG lpMsg)
2961 {
2962    PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
2963
2964    TRACE("\n");
2965    if (!psInfo || (hwnd != lpMsg->hwnd && !IsChild(hwnd, lpMsg->hwnd)))
2966       return FALSE;
2967
2968    if (lpMsg->message == WM_KEYDOWN && (GetKeyState(VK_CONTROL) & 0x8000))
2969    {
2970       int new_page = 0;
2971       INT dlgCode = SendMessageW(lpMsg->hwnd, WM_GETDLGCODE, 0, (LPARAM)lpMsg);
2972
2973       if (!(dlgCode & DLGC_WANTMESSAGE))
2974       {
2975          switch (lpMsg->wParam)
2976          {
2977             case VK_TAB:
2978                if (GetKeyState(VK_SHIFT) & 0x8000)
2979                    new_page = -1;
2980                 else
2981                    new_page = 1;
2982                break;
2983
2984             case VK_NEXT:   new_page = 1;  break;
2985             case VK_PRIOR:  new_page = -1; break;
2986          }
2987       }
2988
2989       if (new_page)
2990       {
2991          if (PROPSHEET_CanSetCurSel(hwnd) != FALSE)
2992          {
2993             new_page += psInfo->active_page;
2994
2995             if (new_page < 0)
2996                new_page = psInfo->nPages - 1;
2997             else if (new_page >= psInfo->nPages)
2998                new_page = 0;
2999
3000             PROPSHEET_SetCurSel(hwnd, new_page, 1, 0);
3001          }
3002
3003          return TRUE;
3004       }
3005    }
3006
3007    return IsDialogMessageW(hwnd, lpMsg);
3008 }
3009
3010 /******************************************************************************
3011  *            PROPSHEET_DoCommand
3012  */
3013 static BOOL PROPSHEET_DoCommand(HWND hwnd, WORD wID)
3014 {
3015
3016     switch (wID) {
3017
3018     case IDOK:
3019     case IDC_APPLY_BUTTON:
3020         {
3021             HWND hwndApplyBtn = GetDlgItem(hwnd, IDC_APPLY_BUTTON);
3022
3023             if (PROPSHEET_Apply(hwnd, wID == IDOK ? 1: 0) == FALSE)
3024                 break;
3025
3026             if (wID == IDOK)
3027                 {
3028                     PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3029                                                                       PropSheetInfoStr);
3030                     int result = TRUE;
3031
3032                     if (psInfo->restartWindows)
3033                         result = ID_PSRESTARTWINDOWS;
3034
3035                     /* reboot system takes precedence over restart windows */
3036                     if (psInfo->rebootSystem)
3037                         result = ID_PSREBOOTSYSTEM;
3038
3039                     if (psInfo->isModeless)
3040                         psInfo->activeValid = FALSE;
3041                     else
3042                         EndDialog(hwnd, result);
3043                 }
3044             else
3045                 EnableWindow(hwndApplyBtn, FALSE);
3046
3047             break;
3048         }
3049
3050     case IDC_BACK_BUTTON:
3051         PROPSHEET_Back(hwnd);
3052         break;
3053
3054     case IDC_NEXT_BUTTON:
3055         PROPSHEET_Next(hwnd);
3056         break;
3057
3058     case IDC_FINISH_BUTTON:
3059         PROPSHEET_Finish(hwnd);
3060         break;
3061
3062     case IDCANCEL:
3063         PROPSHEET_Cancel(hwnd, 0);
3064         break;
3065
3066     case IDHELP:
3067         PROPSHEET_Help(hwnd);
3068         break;
3069
3070     default:
3071         return FALSE;
3072     }
3073
3074     return TRUE;
3075 }
3076
3077 /******************************************************************************
3078  *            PROPSHEET_Paint
3079  */
3080 static LRESULT PROPSHEET_Paint(HWND hwnd, HDC hdcParam)
3081 {
3082     PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
3083     PAINTSTRUCT ps;
3084     HDC hdc, hdcSrc;
3085     BITMAP bm;
3086     HBITMAP hbmp;
3087     HPALETTE hOldPal = 0;
3088     int offsety = 0;
3089     HBRUSH hbr;
3090     RECT r, rzone;
3091     LPCPROPSHEETPAGEW ppshpage;
3092     WCHAR szBuffer[256];
3093     int nLength;
3094
3095     hdc = hdcParam ? hdcParam : BeginPaint(hwnd, &ps);
3096     if (!hdc) return 1;
3097
3098     hdcSrc = CreateCompatibleDC(0);
3099     ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[psInfo->active_page].hpage;
3100
3101     if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
3102         hOldPal = SelectPalette(hdc, psInfo->ppshheader.hplWatermark, FALSE);
3103
3104     if ( (!(ppshpage->dwFlags & PSP_HIDEHEADER)) &&
3105          (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
3106          (psInfo->ppshheader.dwFlags & PSH_HEADER) ) 
3107     {
3108         HWND hwndLineHeader = GetDlgItem(hwnd, IDC_SUNKEN_LINEHEADER);
3109         HFONT hOldFont;
3110         COLORREF clrOld = 0;
3111         int oldBkMode = 0;
3112
3113         hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u5.hbmHeader);
3114         hOldFont = SelectObject(hdc, psInfo->hFontBold);
3115
3116         GetClientRect(hwndLineHeader, &r);
3117         MapWindowPoints(hwndLineHeader, hwnd, (LPPOINT) &r, 2);
3118         SetRect(&rzone, 0, 0, r.right + 1, r.top - 1);
3119
3120         GetObjectW(psInfo->ppshheader.u5.hbmHeader, sizeof(BITMAP), (LPVOID)&bm);               
3121
3122         if (psInfo->ppshheader.dwFlags & PSH_WIZARD97_OLD)
3123         {
3124             /* Fill the unoccupied part of the header with color of the
3125              * left-top pixel, but do it only when needed.
3126              */
3127             if (bm.bmWidth < r.right || bm.bmHeight < r.bottom)
3128             {
3129                 hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
3130                 CopyRect(&r, &rzone);
3131                 if (bm.bmWidth < r.right)
3132                 {
3133                     r.left = bm.bmWidth;
3134                     FillRect(hdc, &r, hbr);
3135                 }
3136                 if (bm.bmHeight < r.bottom)
3137                 {
3138                     r.left = 0;
3139                     r.top = bm.bmHeight;
3140                     FillRect(hdc, &r, hbr);
3141                 }
3142                 DeleteObject(hbr);
3143             }
3144
3145             /* Draw the header itself. */
3146             BitBlt(hdc, 0, 0,
3147                    bm.bmWidth, min(bm.bmHeight, rzone.bottom),
3148                    hdcSrc, 0, 0, SRCCOPY);
3149         }
3150         else
3151         {
3152             hbr = GetSysColorBrush(COLOR_WINDOW);
3153             FillRect(hdc, &rzone, hbr);
3154
3155             /* Draw the header bitmap. It's always centered like a
3156              * common 49 x 49 bitmap. */
3157             BitBlt(hdc, rzone.right - 49 - ((rzone.bottom - 49) / 2),
3158                    (rzone.bottom - 49) / 2,
3159                    bm.bmWidth, bm.bmHeight,
3160                    hdcSrc, 0, 0, SRCCOPY);
3161
3162             /* NOTE: Native COMCTL32 draws a white stripe over the bitmap
3163              * if its height is smaller than 49 pixels. Because the reason
3164              * for this bug is unknown the current code doesn't try to
3165              * replicate it. */
3166         }
3167
3168         clrOld = SetTextColor (hdc, 0x00000000);
3169         oldBkMode = SetBkMode (hdc, TRANSPARENT); 
3170
3171         if (ppshpage->dwFlags & PSP_USEHEADERTITLE) {
3172             SetRect(&r, 20, 10, 0, 0);
3173             if (HIWORD(ppshpage->pszHeaderTitle))
3174             {
3175                 if (psInfo->unicode)
3176                     DrawTextW(hdc, (LPWSTR)ppshpage->pszHeaderTitle,
3177                               -1, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3178                 else
3179                     DrawTextA(hdc, (LPCSTR)ppshpage->pszHeaderTitle,
3180                               -1, &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3181             }
3182             else
3183             {
3184                 nLength = LoadStringW(ppshpage->hInstance, (UINT)ppshpage->pszHeaderTitle,
3185                                       szBuffer, 256);
3186                 if (nLength != 0)
3187                 {
3188                     DrawTextW(hdc, szBuffer, nLength,
3189                               &r, DT_LEFT | DT_SINGLELINE | DT_NOCLIP);
3190                 }
3191             }
3192         }
3193
3194         if (ppshpage->dwFlags & PSP_USEHEADERSUBTITLE) {
3195             SelectObject(hdc, psInfo->hFont);
3196             SetRect(&r, 40, 25, rzone.right - 69, rzone.bottom);
3197             if (HIWORD(ppshpage->pszHeaderTitle))
3198             {
3199                 if (psInfo->unicode)
3200                     DrawTextW(hdc, (LPWSTR)ppshpage->pszHeaderSubTitle,
3201                               -1, &r, DT_LEFT | DT_SINGLELINE);
3202                 else
3203                     DrawTextA(hdc, (LPCSTR)ppshpage->pszHeaderSubTitle,
3204                               -1, &r, DT_LEFT | DT_SINGLELINE);
3205             }
3206             else
3207             {
3208                 nLength = LoadStringW(ppshpage->hInstance, (UINT)ppshpage->pszHeaderSubTitle,
3209                                       szBuffer, 256);
3210                 if (nLength != 0)
3211                 {
3212                     DrawTextW(hdc, szBuffer, nLength,
3213                               &r, DT_LEFT | DT_SINGLELINE);
3214                 }
3215             }
3216         }
3217
3218         offsety = rzone.bottom + 2;
3219
3220         SetTextColor(hdc, clrOld);
3221         SetBkMode(hdc, oldBkMode);
3222         SelectObject(hdc, hOldFont);
3223         SelectObject(hdcSrc, hbmp);
3224     }
3225
3226     if ( (ppshpage->dwFlags & PSP_HIDEHEADER) &&
3227          (psInfo->ppshheader.dwFlags & (PSH_WIZARD97_OLD | PSH_WIZARD97_NEW)) &&
3228          (psInfo->ppshheader.dwFlags & PSH_WATERMARK) ) 
3229     {
3230         HWND hwndLine = GetDlgItem(hwnd, IDC_SUNKEN_LINE);          
3231
3232         GetClientRect(hwndLine, &r);
3233         MapWindowPoints(hwndLine, hwnd, (LPPOINT) &r, 2);
3234
3235         rzone.left = 0;
3236         rzone.top = 0;
3237         rzone.right = r.right;
3238         rzone.bottom = r.top - 1;
3239
3240         hbr = GetSysColorBrush(COLOR_WINDOW);
3241         FillRect(hdc, &rzone, hbr);
3242
3243         GetObjectW(psInfo->ppshheader.u4.hbmWatermark, sizeof(BITMAP), (LPVOID)&bm);
3244         hbmp = SelectObject(hdcSrc, psInfo->ppshheader.u4.hbmWatermark);
3245
3246         BitBlt(hdc, 0, offsety, min(bm.bmWidth, r.right),
3247                min(bm.bmHeight, r.bottom), hdcSrc, 0, 0, SRCCOPY);
3248
3249         /* If the bitmap is not big enough, fill the remaining area
3250            with the color of pixel (0,0) of bitmap - see MSDN */
3251         if (r.top > bm.bmHeight) {
3252             r.bottom = r.top - 1;
3253             r.top = bm.bmHeight;
3254             r.left = 0;
3255             r.right = bm.bmWidth;
3256             hbr = CreateSolidBrush(GetPixel(hdcSrc, 0, 0));
3257             FillRect(hdc, &r, hbr);
3258             DeleteObject(hbr);
3259         }
3260
3261         SelectObject(hdcSrc, hbmp);         
3262     }
3263
3264     if (psInfo->ppshheader.dwFlags & PSH_USEHPLWATERMARK) 
3265         SelectPalette(hdc, hOldPal, FALSE);
3266
3267     DeleteDC(hdcSrc);
3268
3269     if (!hdcParam) EndPaint(hwnd, &ps);
3270
3271     return 0;
3272 }
3273
3274 /******************************************************************************
3275  *            PROPSHEET_DialogProc
3276  */
3277 INT_PTR CALLBACK
3278 PROPSHEET_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
3279 {
3280   TRACE("hwnd=%p msg=0x%04x wparam=%x lparam=%lx\n",
3281         hwnd, uMsg, wParam, lParam);
3282
3283   switch (uMsg)
3284   {
3285     case WM_INITDIALOG:
3286     {
3287       PropSheetInfo* psInfo = (PropSheetInfo*) lParam;
3288       WCHAR* strCaption = (WCHAR*)Alloc(MAX_CAPTION_LENGTH*sizeof(WCHAR));
3289       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
3290       LPCPROPSHEETPAGEW ppshpage;
3291       int idx;
3292       LOGFONTW logFont;
3293
3294       /* Using PropSheetInfoStr to store extra data doesn't match the native
3295        * common control: native uses TCM_[GS]ETITEM
3296        */
3297       SetPropW(hwnd, PropSheetInfoStr, (HANDLE)psInfo);
3298
3299       /*
3300        * psInfo->hwnd is not being used by WINE code - it exists
3301        * for compatibility with "real" Windoze. The same about
3302        * SetWindowLongPtr - WINE is only using the PropSheetInfoStr
3303        * property.
3304        */
3305       psInfo->hwnd = hwnd;
3306       SetWindowLongPtrW(hwnd, DWLP_USER, (DWORD_PTR)psInfo);
3307
3308       /* set up the Next and Back buttons by default */
3309       PROPSHEET_SetWizButtons(hwnd, PSWIZB_BACK|PSWIZB_NEXT);
3310
3311       /* Set up fonts */
3312       SystemParametersInfoW (SPI_GETICONTITLELOGFONT, 0, &logFont, 0);
3313       psInfo->hFont = CreateFontIndirectW (&logFont);
3314       logFont.lfWeight = FW_BOLD;
3315       psInfo->hFontBold = CreateFontIndirectW (&logFont);
3316       
3317       /*
3318        * Small icon in the title bar.
3319        */
3320       if ((psInfo->ppshheader.dwFlags & PSH_USEICONID) ||
3321           (psInfo->ppshheader.dwFlags & PSH_USEHICON))
3322       {
3323         HICON hIcon;
3324         int icon_cx = GetSystemMetrics(SM_CXSMICON);
3325         int icon_cy = GetSystemMetrics(SM_CYSMICON);
3326
3327         if (psInfo->ppshheader.dwFlags & PSH_USEICONID)
3328           hIcon = LoadImageW(psInfo->ppshheader.hInstance,
3329                              psInfo->ppshheader.u.pszIcon,
3330                              IMAGE_ICON,
3331                              icon_cx, icon_cy,
3332                              LR_DEFAULTCOLOR);
3333         else
3334           hIcon = psInfo->ppshheader.u.hIcon;
3335
3336         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)hIcon);
3337       }
3338
3339       if (psInfo->ppshheader.dwFlags & PSH_USEHICON)
3340         SendMessageW(hwnd, WM_SETICON, 0, (LPARAM)psInfo->ppshheader.u.hIcon);
3341
3342       psInfo->strPropertiesFor = strCaption;
3343
3344       GetWindowTextW(hwnd, psInfo->strPropertiesFor, MAX_CAPTION_LENGTH);
3345
3346       PROPSHEET_CreateTabControl(hwnd, psInfo);
3347
3348       PROPSHEET_LoadWizardBitmaps(psInfo);
3349
3350       if (psInfo->ppshheader.dwFlags & INTRNL_ANY_WIZARD)
3351       {
3352         ShowWindow(hwndTabCtrl, SW_HIDE);
3353         PROPSHEET_AdjustSizeWizard(hwnd, psInfo);
3354         PROPSHEET_AdjustButtonsWizard(hwnd, psInfo);
3355       }
3356       else
3357       {
3358         if (PROPSHEET_SizeMismatch(hwnd, psInfo))
3359         {
3360           PROPSHEET_AdjustSize(hwnd, psInfo);
3361           PROPSHEET_AdjustButtons(hwnd, psInfo);
3362         }
3363       }
3364
3365       if (psInfo->useCallback)
3366              (*(psInfo->ppshheader.pfnCallback))(hwnd,
3367                                               PSCB_INITIALIZED, (LPARAM)0);
3368
3369       idx = psInfo->active_page;
3370       ppshpage = (LPCPROPSHEETPAGEW)psInfo->proppage[idx].hpage;
3371       psInfo->active_page = -1;
3372
3373       PROPSHEET_SetCurSel(hwnd, idx, 1, psInfo->proppage[idx].hpage);
3374
3375       /* doing TCM_SETCURSEL seems to be needed even in case of PSH_WIZARD,
3376        * as some programs call TCM_GETCURSEL to get the current selection
3377        * from which to switch to the next page */
3378       SendMessageW(hwndTabCtrl, TCM_SETCURSEL, psInfo->active_page, 0);
3379
3380       if (!HIWORD(psInfo->ppshheader.pszCaption) &&
3381               psInfo->ppshheader.hInstance)
3382       {
3383          WCHAR szText[256];
3384
3385          if (LoadStringW(psInfo->ppshheader.hInstance,
3386                  (UINT)psInfo->ppshheader.pszCaption, szText, 255))
3387             PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags, szText);
3388       }
3389       else
3390       {
3391          PROPSHEET_SetTitleW(hwnd, psInfo->ppshheader.dwFlags,
3392                          psInfo->ppshheader.pszCaption);
3393       }
3394
3395       return TRUE;
3396     }
3397
3398     case WM_PAINT:
3399       PROPSHEET_Paint(hwnd, (HDC)wParam);
3400       return TRUE;
3401
3402     case WM_DESTROY:
3403       PROPSHEET_CleanUp(hwnd);
3404       return TRUE;
3405
3406     case WM_CLOSE:
3407       PROPSHEET_Cancel(hwnd, 1);
3408       return TRUE;
3409
3410     case WM_COMMAND:
3411       if (!PROPSHEET_DoCommand(hwnd, LOWORD(wParam)))
3412       {
3413           PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd, PropSheetInfoStr);
3414
3415           if (!psInfo)
3416               return FALSE;
3417
3418           /* No default handler, forward notification to active page */
3419           if (psInfo->activeValid && psInfo->active_page != -1)
3420           {
3421              HWND hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
3422              SendMessageW(hwndPage, WM_COMMAND, wParam, lParam);
3423           }
3424       }
3425       return TRUE;
3426
3427     case WM_NOTIFY:
3428     {
3429       NMHDR* pnmh = (LPNMHDR) lParam;
3430
3431       if (pnmh->code == TCN_SELCHANGE)
3432       {
3433         int index = SendMessageW(pnmh->hwndFrom, TCM_GETCURSEL, 0, 0);
3434         PROPSHEET_SetCurSel(hwnd, index, 1, 0);
3435       }
3436
3437       if(pnmh->code == TCN_SELCHANGING)
3438       {
3439         BOOL bRet = PROPSHEET_CanSetCurSel(hwnd);
3440         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, !bRet);
3441         return TRUE;
3442       }
3443
3444       return FALSE;
3445     }
3446
3447     case PSM_GETCURRENTPAGEHWND:
3448     {
3449       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3450                                                         PropSheetInfoStr);
3451       HWND hwndPage = 0;
3452
3453       if (!psInfo)
3454         return FALSE;
3455
3456       if (psInfo->activeValid && psInfo->active_page != -1)
3457         hwndPage = psInfo->proppage[psInfo->active_page].hwndPage;
3458
3459       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndPage);
3460
3461       return TRUE;
3462     }
3463
3464     case PSM_CHANGED:
3465       PROPSHEET_Changed(hwnd, (HWND)wParam);
3466       return TRUE;
3467
3468     case PSM_UNCHANGED:
3469       PROPSHEET_UnChanged(hwnd, (HWND)wParam);
3470       return TRUE;
3471
3472     case PSM_GETTABCONTROL:
3473     {
3474       HWND hwndTabCtrl = GetDlgItem(hwnd, IDC_TABCONTROL);
3475
3476       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, (DWORD_PTR)hwndTabCtrl);
3477
3478       return TRUE;
3479     }
3480
3481     case PSM_SETCURSEL:
3482     {
3483       BOOL msgResult;
3484
3485       msgResult = PROPSHEET_CanSetCurSel(hwnd);
3486       if(msgResult != FALSE)
3487       {
3488         msgResult = PROPSHEET_SetCurSel(hwnd,
3489                                        (int)wParam,
3490                                        1,
3491                                        (HPROPSHEETPAGE)lParam);
3492       }
3493
3494       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3495
3496       return TRUE;
3497     }
3498
3499     case PSM_CANCELTOCLOSE:
3500     {
3501       WCHAR buf[MAX_BUTTONTEXT_LENGTH];
3502       HWND hwndOK = GetDlgItem(hwnd, IDOK);
3503       HWND hwndCancel = GetDlgItem(hwnd, IDCANCEL);
3504
3505       EnableWindow(hwndCancel, FALSE);
3506       if (LoadStringW(COMCTL32_hModule, IDS_CLOSE, buf, sizeof(buf)))
3507          SetWindowTextW(hwndOK, buf);
3508
3509       return FALSE;
3510     }
3511
3512     case PSM_RESTARTWINDOWS:
3513     {
3514       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3515                                                         PropSheetInfoStr);
3516
3517       if (!psInfo)
3518         return FALSE;
3519
3520       psInfo->restartWindows = TRUE;
3521       return TRUE;
3522     }
3523
3524     case PSM_REBOOTSYSTEM:
3525     {
3526       PropSheetInfo* psInfo = (PropSheetInfo*) GetPropW(hwnd,
3527                                                         PropSheetInfoStr);
3528
3529       if (!psInfo)
3530         return FALSE;
3531
3532       psInfo->rebootSystem = TRUE;
3533       return TRUE;
3534     }
3535
3536     case PSM_SETTITLEA:
3537       PROPSHEET_SetTitleA(hwnd, (DWORD) wParam, (LPCSTR) lParam);
3538       return TRUE;
3539
3540     case PSM_SETTITLEW:
3541       PROPSHEET_SetTitleW(hwnd, (DWORD) wParam, (LPCWSTR) lParam);
3542       return TRUE;
3543
3544     case PSM_APPLY:
3545     {
3546       BOOL msgResult = PROPSHEET_Apply(hwnd, 0);
3547
3548       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3549
3550       return TRUE;
3551     }
3552
3553     case PSM_QUERYSIBLINGS:
3554     {
3555       LRESULT msgResult = PROPSHEET_QuerySiblings(hwnd, wParam, lParam);
3556
3557       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3558
3559       return TRUE;
3560     }
3561
3562     case PSM_ADDPAGE:
3563     {
3564       /*
3565        * Note: MSVC++ 6.0 documentation says that PSM_ADDPAGE does not have
3566        *       a return value. This is not true. PSM_ADDPAGE returns TRUE
3567        *       on success or FALSE otherwise, as specified on MSDN Online.
3568        *       Also see the MFC code for
3569        *       CPropertySheet::AddPage(CPropertyPage* pPage).
3570        */
3571
3572       BOOL msgResult = PROPSHEET_AddPage(hwnd, (HPROPSHEETPAGE)lParam);
3573
3574       SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3575
3576       return TRUE;
3577     }
3578
3579     case PSM_REMOVEPAGE:
3580       PROPSHEET_RemovePage(hwnd, (int)wParam, (HPROPSHEETPAGE)lParam);
3581       return TRUE;
3582
3583     case PSM_ISDIALOGMESSAGE:
3584     {
3585        BOOL msgResult = PROPSHEET_IsDialogMessage(hwnd, (LPMSG)lParam);
3586        SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3587        return TRUE;
3588     }
3589
3590     case PSM_PRESSBUTTON:
3591       PROPSHEET_PressButton(hwnd, (int)wParam);
3592       return TRUE;
3593
3594     case PSM_SETFINISHTEXTA:
3595       PROPSHEET_SetFinishTextA(hwnd, (LPCSTR) lParam);
3596       return TRUE;
3597
3598     case PSM_SETWIZBUTTONS:
3599       PROPSHEET_SetWizButtons(hwnd, (DWORD)lParam);
3600       return TRUE;
3601
3602     case PSM_SETCURSELID:
3603         PROPSHEET_SetCurSelId(hwnd, (int)lParam);
3604         return TRUE;
3605
3606     case PSM_SETFINISHTEXTW:
3607         PROPSHEET_SetFinishTextW(hwnd, (LPCWSTR) lParam);
3608         return FALSE;
3609
3610     case PSM_INSERTPAGE:
3611     {
3612         BOOL msgResult = PROPSHEET_InsertPage(hwnd, (HPROPSHEETPAGE)wParam, (HPROPSHEETPAGE)lParam);
3613         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3614         return TRUE;
3615     }
3616
3617     case PSM_SETHEADERTITLEW:
3618         PROPSHEET_SetHeaderTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
3619         return TRUE;
3620
3621     case PSM_SETHEADERTITLEA:
3622         PROPSHEET_SetHeaderTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
3623         return TRUE;
3624
3625     case PSM_SETHEADERSUBTITLEW:
3626         PROPSHEET_SetHeaderSubTitleW(hwnd, (int)wParam, (LPCWSTR)lParam);
3627         return TRUE;
3628
3629     case PSM_SETHEADERSUBTITLEA:
3630         PROPSHEET_SetHeaderSubTitleA(hwnd, (int)wParam, (LPCSTR)lParam);
3631         return TRUE;
3632
3633     case PSM_HWNDTOINDEX:
3634     {
3635         LRESULT msgResult = PROPSHEET_HwndToIndex(hwnd, (HWND)wParam);
3636         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3637         return TRUE;
3638     }
3639
3640     case PSM_INDEXTOHWND:
3641     {
3642         LRESULT msgResult = PROPSHEET_IndexToHwnd(hwnd, (int)wParam);
3643         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3644         return TRUE;
3645     }
3646
3647     case PSM_PAGETOINDEX:
3648     {
3649         LRESULT msgResult = PROPSHEET_PageToIndex(hwnd, (HPROPSHEETPAGE)wParam);
3650         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3651         return TRUE;
3652     }
3653
3654     case PSM_INDEXTOPAGE:
3655     {
3656         LRESULT msgResult = PROPSHEET_IndexToPage(hwnd, (int)wParam);
3657         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3658         return TRUE;
3659     }
3660
3661     case PSM_IDTOINDEX:
3662     {
3663         LRESULT msgResult = PROPSHEET_IdToIndex(hwnd, (int)lParam);
3664         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3665         return TRUE;
3666     }
3667
3668     case PSM_INDEXTOID:
3669     {
3670         LRESULT msgResult = PROPSHEET_IndexToId(hwnd, (int)wParam);
3671         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3672         return TRUE;
3673     }
3674
3675     case PSM_GETRESULT:
3676     {
3677         LRESULT msgResult = PROPSHEET_GetResult(hwnd);
3678         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3679         return TRUE;
3680     }
3681
3682     case PSM_RECALCPAGESIZES:
3683     {
3684         LRESULT msgResult = PROPSHEET_RecalcPageSizes(hwnd);
3685         SetWindowLongPtrW(hwnd, DWLP_MSGRESULT, msgResult);
3686         return TRUE;
3687     }
3688
3689     default:
3690       return FALSE;
3691   }
3692 }